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"
27 using std::lock_guard;
29 using std::recursive_mutex;
34 const uint64_t Segment::MaxChunkSize = 10 * 1024 * 1024; /* 10MiB */
36 Segment::Segment(uint32_t segment_id, uint64_t samplerate, unsigned int unit_size) :
37 segment_id_(segment_id),
40 samplerate_(samplerate),
41 unit_size_(unit_size),
43 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_ = min(MaxChunkSize, (MaxChunkSize / unit_size_) * unit_size_);
53 // Create the initial chunk
54 current_chunk_ = new uint8_t[chunk_size_];
55 data_chunks_.push_back(current_chunk_);
57 unused_samples_ = chunk_size_ / unit_size_;
62 lock_guard<recursive_mutex> lock(mutex_);
64 for (uint8_t* chunk : data_chunks_)
68 uint64_t Segment::get_sample_count() const
70 lock_guard<recursive_mutex> lock(mutex_);
74 const pv::util::Timestamp& Segment::start_time() const
79 double Segment::samplerate() const
84 void Segment::set_samplerate(double samplerate)
86 samplerate_ = samplerate;
89 unsigned int Segment::unit_size() const
94 uint32_t Segment::segment_id() const
99 void Segment::set_complete()
104 bool Segment::is_complete() const
109 void Segment::free_unused_memory()
111 lock_guard<recursive_mutex> lock(mutex_);
113 // Do not mess with the data chunks if we have iterators pointing at them
114 if (iterator_count_ > 0) {
115 mem_optimization_requested_ = true;
119 // No more data will come in, so re-create the last chunk accordingly
120 uint8_t* resized_chunk = new uint8_t[used_samples_ * unit_size_];
121 memcpy(resized_chunk, current_chunk_, used_samples_ * unit_size_);
123 delete[] current_chunk_;
124 current_chunk_ = resized_chunk;
126 data_chunks_.pop_back();
127 data_chunks_.push_back(resized_chunk);
130 void Segment::append_single_sample(void *data)
132 lock_guard<recursive_mutex> lock(mutex_);
134 // There will always be space for at least one sample in
135 // the current chunk, so we do not need to test for space
137 memcpy(current_chunk_ + (used_samples_ * unit_size_), data, unit_size_);
141 if (unused_samples_ == 0) {
142 current_chunk_ = new uint8_t[chunk_size_];
143 data_chunks_.push_back(current_chunk_);
145 unused_samples_ = chunk_size_ / unit_size_;
151 void Segment::append_samples(void* data, uint64_t samples)
153 lock_guard<recursive_mutex> lock(mutex_);
155 const uint8_t* data_byte_ptr = (uint8_t*)data;
156 uint64_t remaining_samples = samples;
157 uint64_t data_offset = 0;
160 uint64_t copy_count = 0;
162 if (remaining_samples <= unused_samples_) {
163 // All samples fit into the current chunk
164 copy_count = remaining_samples;
166 // Only a part of the samples fit, fill up current chunk
167 copy_count = unused_samples_;
170 const uint8_t* dest = &(current_chunk_[used_samples_ * unit_size_]);
171 const uint8_t* src = &(data_byte_ptr[data_offset]);
172 memcpy((void*)dest, (void*)src, (copy_count * unit_size_));
174 used_samples_ += copy_count;
175 unused_samples_ -= copy_count;
176 remaining_samples -= copy_count;
177 data_offset += (copy_count * unit_size_);
179 if (unused_samples_ == 0) {
180 // If we're out of memory, this will throw std::bad_alloc
181 current_chunk_ = new uint8_t[chunk_size_];
182 data_chunks_.push_back(current_chunk_);
184 unused_samples_ = chunk_size_ / unit_size_;
186 } while (remaining_samples > 0);
188 sample_count_ += samples;
191 void Segment::get_raw_samples(uint64_t start, uint64_t count,
194 assert(start < sample_count_);
195 assert(start + count <= sample_count_);
197 assert(dest != nullptr);
199 lock_guard<recursive_mutex> lock(mutex_);
201 uint8_t* dest_ptr = dest;
203 uint64_t chunk_num = (start * unit_size_) / chunk_size_;
204 uint64_t chunk_offs = (start * unit_size_) % chunk_size_;
207 const uint8_t* chunk = data_chunks_[chunk_num];
209 uint64_t copy_size = min(count * unit_size_,
210 chunk_size_ - chunk_offs);
212 memcpy(dest_ptr, chunk + chunk_offs, copy_size);
214 dest_ptr += copy_size;
215 count -= (copy_size / unit_size_);
222 SegmentRawDataIterator* Segment::begin_raw_sample_iteration(uint64_t start)
224 SegmentRawDataIterator* it = new SegmentRawDataIterator;
226 assert(start < sample_count_);
230 it->sample_index = start;
231 it->chunk_num = (start * unit_size_) / chunk_size_;
232 it->chunk_offs = (start * unit_size_) % chunk_size_;
233 it->chunk = data_chunks_[it->chunk_num];
234 it->value = it->chunk + it->chunk_offs;
239 void Segment::continue_raw_sample_iteration(SegmentRawDataIterator* it, uint64_t increase)
241 // Fail gracefully if we are asked to deliver data we don't have
242 if (it->sample_index > sample_count_)
245 it->sample_index += increase;
246 it->chunk_offs += (increase * unit_size_);
248 if (it->chunk_offs > (chunk_size_ - 1)) {
250 it->chunk_offs -= chunk_size_;
251 it->chunk = data_chunks_[it->chunk_num];
254 it->value = it->chunk + it->chunk_offs;
257 void Segment::end_raw_sample_iteration(SegmentRawDataIterator* it)
263 if ((iterator_count_ == 0) && mem_optimization_requested_) {
264 mem_optimization_requested_ = false;
265 free_unused_memory();