]>
Commit | Line | Data |
---|---|---|
28a4c9c5 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
28a4c9c5 | 3 | * |
26a883ed | 4 | * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net> |
28a4c9c5 JH |
5 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
efdec55a | 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
28a4c9c5 JH |
19 | */ |
20 | ||
f3d66e52 | 21 | #include "segment.hpp" |
28a4c9c5 | 22 | |
eb8269e3 UH |
23 | #include <cassert> |
24 | #include <cstdlib> | |
25 | #include <cstring> | |
26a883ed | 26 | |
3782d860 UH |
27 | #include <QDebug> |
28 | ||
29 | using std::bad_alloc; | |
3b68d03d | 30 | using std::lock_guard; |
6f925ba9 | 31 | using std::min; |
3b68d03d | 32 | using std::recursive_mutex; |
7d29656f | 33 | |
51e77110 | 34 | namespace pv { |
1b1ec774 | 35 | namespace data { |
51e77110 | 36 | |
51d3950f | 37 | const uint64_t Segment::MaxChunkSize = 10 * 1024 * 1024; /* 10MiB */ |
dd3f9a41 | 38 | |
3782d860 UH |
39 | Segment::Segment(uint32_t segment_id, uint64_t samplerate, unsigned int unit_size) : |
40 | segment_id_(segment_id), | |
8dbbc7f0 | 41 | sample_count_(0), |
7f4038d6 | 42 | start_time_(0), |
ff008de6 | 43 | samplerate_(samplerate), |
c70e3464 SA |
44 | unit_size_(unit_size), |
45 | iterator_count_(0), | |
3782d860 UH |
46 | mem_optimization_requested_(false), |
47 | is_complete_(false) | |
f556bc6a | 48 | { |
8dbbc7f0 JH |
49 | lock_guard<recursive_mutex> lock(mutex_); |
50 | assert(unit_size_ > 0); | |
26a883ed SA |
51 | |
52 | // Determine the number of samples we can fit in one chunk | |
53 | // without exceeding MaxChunkSize | |
6f925ba9 | 54 | chunk_size_ = min(MaxChunkSize, (MaxChunkSize / unit_size_) * unit_size_); |
26a883ed SA |
55 | |
56 | // Create the initial chunk | |
3782d860 | 57 | current_chunk_ = new uint8_t[chunk_size_ + 7]; /* FIXME +7 is workaround for #1284 */ |
26a883ed SA |
58 | data_chunks_.push_back(current_chunk_); |
59 | used_samples_ = 0; | |
60 | unused_samples_ = chunk_size_ / unit_size_; | |
f556bc6a JH |
61 | } |
62 | ||
f3d66e52 | 63 | Segment::~Segment() |
28a4c9c5 | 64 | { |
8dbbc7f0 | 65 | lock_guard<recursive_mutex> lock(mutex_); |
26a883ed SA |
66 | |
67 | for (uint8_t* chunk : data_chunks_) | |
68 | delete[] chunk; | |
28a4c9c5 JH |
69 | } |
70 | ||
f3d66e52 | 71 | uint64_t Segment::get_sample_count() const |
28a4c9c5 | 72 | { |
8dbbc7f0 JH |
73 | lock_guard<recursive_mutex> lock(mutex_); |
74 | return sample_count_; | |
f556bc6a JH |
75 | } |
76 | ||
60d9b99a | 77 | const pv::util::Timestamp& Segment::start_time() const |
7f4038d6 JH |
78 | { |
79 | return start_time_; | |
80 | } | |
81 | ||
f3d66e52 | 82 | double Segment::samplerate() const |
ff008de6 JH |
83 | { |
84 | return samplerate_; | |
85 | } | |
86 | ||
f3d66e52 | 87 | void Segment::set_samplerate(double samplerate) |
ff008de6 JH |
88 | { |
89 | samplerate_ = samplerate; | |
90 | } | |
91 | ||
f3d66e52 | 92 | unsigned int Segment::unit_size() const |
6fd14a32 | 93 | { |
8dbbc7f0 | 94 | return unit_size_; |
6fd14a32 JH |
95 | } |
96 | ||
3782d860 UH |
97 | uint32_t Segment::segment_id() const |
98 | { | |
99 | return segment_id_; | |
100 | } | |
101 | ||
102 | void Segment::set_complete() | |
103 | { | |
104 | is_complete_ = true; | |
105 | } | |
106 | ||
107 | bool Segment::is_complete() const | |
108 | { | |
109 | return is_complete_; | |
110 | } | |
111 | ||
5e6967cb SA |
112 | void Segment::free_unused_memory() |
113 | { | |
114 | lock_guard<recursive_mutex> lock(mutex_); | |
115 | ||
c70e3464 SA |
116 | // Do not mess with the data chunks if we have iterators pointing at them |
117 | if (iterator_count_ > 0) { | |
118 | mem_optimization_requested_ = true; | |
119 | return; | |
120 | } | |
121 | ||
3782d860 UH |
122 | if (current_chunk_) { |
123 | // No more data will come in, so re-create the last chunk accordingly | |
124 | uint8_t* resized_chunk = new uint8_t[used_samples_ * unit_size_ + 7]; /* FIXME +7 is workaround for #1284 */ | |
125 | memcpy(resized_chunk, current_chunk_, used_samples_ * unit_size_); | |
5e6967cb | 126 | |
3782d860 UH |
127 | delete[] current_chunk_; |
128 | current_chunk_ = resized_chunk; | |
5e6967cb | 129 | |
3782d860 UH |
130 | data_chunks_.pop_back(); |
131 | data_chunks_.push_back(resized_chunk); | |
132 | } | |
5e6967cb SA |
133 | } |
134 | ||
26a883ed SA |
135 | void Segment::append_single_sample(void *data) |
136 | { | |
137 | lock_guard<recursive_mutex> lock(mutex_); | |
138 | ||
139 | // There will always be space for at least one sample in | |
140 | // the current chunk, so we do not need to test for space | |
141 | ||
c063290a | 142 | memcpy(current_chunk_ + (used_samples_ * unit_size_), data, unit_size_); |
26a883ed SA |
143 | used_samples_++; |
144 | unused_samples_--; | |
145 | ||
146 | if (unused_samples_ == 0) { | |
3782d860 | 147 | current_chunk_ = new uint8_t[chunk_size_ + 7]; /* FIXME +7 is workaround for #1284 */ |
26a883ed SA |
148 | data_chunks_.push_back(current_chunk_); |
149 | used_samples_ = 0; | |
150 | unused_samples_ = chunk_size_ / unit_size_; | |
151 | } | |
152 | ||
153 | sample_count_++; | |
154 | } | |
155 | ||
156 | void Segment::append_samples(void* data, uint64_t samples) | |
27d7c96b | 157 | { |
8dbbc7f0 | 158 | lock_guard<recursive_mutex> lock(mutex_); |
27d7c96b | 159 | |
257211b8 SA |
160 | const uint8_t* data_byte_ptr = (uint8_t*)data; |
161 | uint64_t remaining_samples = samples; | |
162 | uint64_t data_offset = 0; | |
163 | ||
164 | do { | |
165 | uint64_t copy_count = 0; | |
166 | ||
167 | if (remaining_samples <= unused_samples_) { | |
168 | // All samples fit into the current chunk | |
169 | copy_count = remaining_samples; | |
170 | } else { | |
171 | // Only a part of the samples fit, fill up current chunk | |
172 | copy_count = unused_samples_; | |
173 | } | |
174 | ||
175 | const uint8_t* dest = &(current_chunk_[used_samples_ * unit_size_]); | |
176 | const uint8_t* src = &(data_byte_ptr[data_offset]); | |
177 | memcpy((void*)dest, (void*)src, (copy_count * unit_size_)); | |
178 | ||
179 | used_samples_ += copy_count; | |
180 | unused_samples_ -= copy_count; | |
181 | remaining_samples -= copy_count; | |
182 | data_offset += (copy_count * unit_size_); | |
183 | ||
184 | if (unused_samples_ == 0) { | |
3782d860 UH |
185 | try { |
186 | // If we're out of memory, allocating a chunk will throw | |
187 | // std::bad_alloc. To give the application some usable memory | |
188 | // to work with in case chunk allocation fails, we allocate | |
189 | // extra memory and throw it away if it all succeeded. | |
190 | // This way, memory allocation will fail early enough to let | |
191 | // PV remain alive. Otherwise, PV will crash in a random | |
192 | // memory-allocating part of the application. | |
193 | current_chunk_ = new uint8_t[chunk_size_ + 7]; /* FIXME +7 is workaround for #1284 */ | |
194 | ||
195 | const int dummy_size = 2 * chunk_size_; | |
196 | auto dummy_chunk = new uint8_t[dummy_size]; | |
197 | memset(dummy_chunk, 0xFF, dummy_size); | |
198 | delete[] dummy_chunk; | |
199 | } catch (bad_alloc&) { | |
200 | delete[] current_chunk_; // The new may have succeeded | |
201 | current_chunk_ = nullptr; | |
202 | throw; | |
203 | } | |
204 | ||
257211b8 SA |
205 | data_chunks_.push_back(current_chunk_); |
206 | used_samples_ = 0; | |
207 | unused_samples_ = chunk_size_ / unit_size_; | |
208 | } | |
209 | } while (remaining_samples > 0); | |
26a883ed SA |
210 | |
211 | sample_count_ += samples; | |
27d7c96b DK |
212 | } |
213 | ||
3782d860 UH |
214 | void Segment::get_raw_samples(uint64_t start, uint64_t count, |
215 | uint8_t* dest) const | |
27d7c96b | 216 | { |
26a883ed SA |
217 | assert(start < sample_count_); |
218 | assert(start + count <= sample_count_); | |
219 | assert(count > 0); | |
3782d860 | 220 | assert(dest != nullptr); |
26a883ed | 221 | |
8dbbc7f0 | 222 | lock_guard<recursive_mutex> lock(mutex_); |
26a883ed | 223 | |
26a883ed SA |
224 | uint8_t* dest_ptr = dest; |
225 | ||
226 | uint64_t chunk_num = (start * unit_size_) / chunk_size_; | |
227 | uint64_t chunk_offs = (start * unit_size_) % chunk_size_; | |
228 | ||
229 | while (count > 0) { | |
230 | const uint8_t* chunk = data_chunks_[chunk_num]; | |
231 | ||
6f925ba9 | 232 | uint64_t copy_size = min(count * unit_size_, |
26a883ed SA |
233 | chunk_size_ - chunk_offs); |
234 | ||
235 | memcpy(dest_ptr, chunk + chunk_offs, copy_size); | |
236 | ||
237 | dest_ptr += copy_size; | |
238 | count -= (copy_size / unit_size_); | |
239 | ||
240 | chunk_num++; | |
241 | chunk_offs = 0; | |
242 | } | |
27d7c96b DK |
243 | } |
244 | ||
3782d860 | 245 | SegmentDataIterator* Segment::begin_sample_iteration(uint64_t start) |
26a883ed | 246 | { |
3782d860 | 247 | SegmentDataIterator* it = new SegmentDataIterator; |
26a883ed SA |
248 | |
249 | assert(start < sample_count_); | |
250 | ||
c70e3464 SA |
251 | iterator_count_++; |
252 | ||
26a883ed SA |
253 | it->sample_index = start; |
254 | it->chunk_num = (start * unit_size_) / chunk_size_; | |
255 | it->chunk_offs = (start * unit_size_) % chunk_size_; | |
256 | it->chunk = data_chunks_[it->chunk_num]; | |
26a883ed SA |
257 | |
258 | return it; | |
259 | } | |
260 | ||
3782d860 | 261 | void Segment::continue_sample_iteration(SegmentDataIterator* it, uint64_t increase) |
f556bc6a | 262 | { |
c063290a UH |
263 | it->sample_index += increase; |
264 | it->chunk_offs += (increase * unit_size_); | |
26a883ed SA |
265 | |
266 | if (it->chunk_offs > (chunk_size_ - 1)) { | |
267 | it->chunk_num++; | |
268 | it->chunk_offs -= chunk_size_; | |
269 | it->chunk = data_chunks_[it->chunk_num]; | |
270 | } | |
26a883ed | 271 | } |
27d7c96b | 272 | |
3782d860 | 273 | void Segment::end_sample_iteration(SegmentDataIterator* it) |
26a883ed SA |
274 | { |
275 | delete it; | |
c70e3464 SA |
276 | |
277 | iterator_count_--; | |
278 | ||
279 | if ((iterator_count_ == 0) && mem_optimization_requested_) { | |
280 | mem_optimization_requested_ = false; | |
281 | free_unused_memory(); | |
282 | } | |
28a4c9c5 | 283 | } |
51e77110 | 284 | |
3782d860 UH |
285 | uint8_t* Segment::get_iterator_value(SegmentDataIterator* it) |
286 | { | |
287 | assert(it->sample_index <= (sample_count_ - 1)); | |
288 | ||
289 | return (it->chunk + it->chunk_offs); | |
290 | } | |
291 | ||
292 | uint64_t Segment::get_iterator_valid_length(SegmentDataIterator* it) | |
293 | { | |
294 | assert(it->sample_index <= (sample_count_ - 1)); | |
295 | ||
296 | return ((chunk_size_ - it->chunk_offs) / unit_size_); | |
297 | } | |
298 | ||
1b1ec774 | 299 | } // namespace data |
51e77110 | 300 | } // namespace pv |