2 * This file is part of the PulseView project.
4 * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "segment.hpp"
29 using std::lock_guard;
30 using std::recursive_mutex;
36 const uint64_t Segment::MaxChunkSize = 10*1024*1024; /* 10MiB */
38 Segment::Segment(uint64_t samplerate, unsigned int unit_size) :
41 samplerate_(samplerate),
42 unit_size_(unit_size),
44 mem_optimization_requested_(false)
46 lock_guard<recursive_mutex> lock(mutex_);
47 assert(unit_size_ > 0);
49 // Determine the number of samples we can fit in one chunk
50 // without exceeding MaxChunkSize
51 chunk_size_ = std::min(MaxChunkSize,
52 (MaxChunkSize / unit_size_) * unit_size_);
54 // Create the initial chunk
55 current_chunk_ = new uint8_t[chunk_size_];
56 data_chunks_.push_back(current_chunk_);
58 unused_samples_ = chunk_size_ / unit_size_;
63 lock_guard<recursive_mutex> lock(mutex_);
65 for (uint8_t* chunk : data_chunks_)
69 uint64_t Segment::get_sample_count() const
71 lock_guard<recursive_mutex> lock(mutex_);
75 const pv::util::Timestamp& Segment::start_time() const
80 double Segment::samplerate() const
85 void Segment::set_samplerate(double samplerate)
87 samplerate_ = samplerate;
90 unsigned int Segment::unit_size() const
95 void Segment::free_unused_memory()
97 lock_guard<recursive_mutex> lock(mutex_);
99 // Do not mess with the data chunks if we have iterators pointing at them
100 if (iterator_count_ > 0) {
101 mem_optimization_requested_ = true;
105 // No more data will come in, so re-create the last chunk accordingly
106 uint8_t* resized_chunk = new uint8_t[used_samples_ * unit_size_];
107 memcpy(resized_chunk, current_chunk_, used_samples_ * unit_size_);
109 delete[] current_chunk_;
110 current_chunk_ = resized_chunk;
112 data_chunks_.pop_back();
113 data_chunks_.push_back(resized_chunk);
116 void Segment::append_single_sample(void *data)
118 lock_guard<recursive_mutex> lock(mutex_);
120 // There will always be space for at least one sample in
121 // the current chunk, so we do not need to test for space
123 memcpy(current_chunk_ + (used_samples_ * unit_size_),
128 if (unused_samples_ == 0) {
129 current_chunk_ = new uint8_t[chunk_size_];
130 data_chunks_.push_back(current_chunk_);
132 unused_samples_ = chunk_size_ / unit_size_;
138 void Segment::append_samples(void* data, uint64_t samples)
140 lock_guard<recursive_mutex> lock(mutex_);
142 if (unused_samples_ >= samples) {
143 // All samples fit into the current chunk
144 memcpy(current_chunk_ + (used_samples_ * unit_size_),
145 data, (samples * unit_size_));
146 used_samples_ += samples;
147 unused_samples_ -= samples;
149 // Only a part of the samples fit, split data up between chunks
150 memcpy(current_chunk_ + (used_samples_ * unit_size_),
151 data, (unused_samples_ * unit_size_));
152 const uint64_t remaining_samples = samples - unused_samples_;
154 // If we're out of memory, this will throw std::bad_alloc
155 current_chunk_ = new uint8_t[chunk_size_];
156 data_chunks_.push_back(current_chunk_);
157 memcpy(current_chunk_, (uint8_t*)data + (unused_samples_ * unit_size_),
158 (remaining_samples * unit_size_));
160 used_samples_ = remaining_samples;
161 unused_samples_ = (chunk_size_ / unit_size_) - remaining_samples;
164 if (unused_samples_ == 0) {
165 // If we're out of memory, this will throw std::bad_alloc
166 current_chunk_ = new uint8_t[chunk_size_];
167 data_chunks_.push_back(current_chunk_);
169 unused_samples_ = chunk_size_ / unit_size_;
172 sample_count_ += samples;
175 uint8_t* Segment::get_raw_samples(uint64_t start, uint64_t count) const
177 assert(start < sample_count_);
178 assert(start + count <= sample_count_);
181 lock_guard<recursive_mutex> lock(mutex_);
183 uint8_t* dest = new uint8_t[count * unit_size_];
184 uint8_t* dest_ptr = dest;
186 uint64_t chunk_num = (start * unit_size_) / chunk_size_;
187 uint64_t chunk_offs = (start * unit_size_) % chunk_size_;
190 const uint8_t* chunk = data_chunks_[chunk_num];
192 uint64_t copy_size = std::min(count * unit_size_,
193 chunk_size_ - chunk_offs);
195 memcpy(dest_ptr, chunk + chunk_offs, copy_size);
197 dest_ptr += copy_size;
198 count -= (copy_size / unit_size_);
207 SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start)
209 SegmentRawDataIterator* it = new SegmentRawDataIterator;
211 assert(start < sample_count_);
215 it->sample_index = start;
216 it->chunk_num = (start * unit_size_) / chunk_size_;
217 it->chunk_offs = (start * unit_size_) % chunk_size_;
218 it->chunk = data_chunks_[it->chunk_num];
219 it->value = it->chunk + it->chunk_offs;
224 void Segment::continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase)
226 lock_guard<recursive_mutex> lock(mutex_);
228 if (it->sample_index > sample_count_)
230 // Fail gracefully if we are asked to deliver data we don't have
233 it->sample_index += increase;
234 it->chunk_offs += (increase * unit_size_);
237 if (it->chunk_offs > (chunk_size_ - 1)) {
239 it->chunk_offs -= chunk_size_;
240 it->chunk = data_chunks_[it->chunk_num];
243 it->value = it->chunk + it->chunk_offs;
246 void Segment::end_raw_sample_iteration(SegmentRawDataIterator* it)
252 if ((iterator_count_ == 0) && mem_optimization_requested_) {
253 mem_optimization_requested_ = false;
254 free_unused_memory();