Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | /* |
2 | * ring_buffer_frontend.c | |
3 | * | |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; only | |
9 | * version 2.1 of the License. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | * | |
852c2936 MD |
20 | * |
21 | * Ring buffer wait-free buffer synchronization. Producer-consumer and flight | |
22 | * recorder (overwrite) modes. See thesis: | |
23 | * | |
24 | * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D. | |
25 | * dissertation, Ecole Polytechnique de Montreal. | |
26 | * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf | |
27 | * | |
28 | * - Algorithm presentation in Chapter 5: | |
29 | * "Lockless Multi-Core High-Throughput Buffering". | |
30 | * - Algorithm formal verification in Section 8.6: | |
31 | * "Formal verification of LTTng" | |
32 | * | |
33 | * Author: | |
34 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
35 | * | |
36 | * Inspired from LTT and RelayFS: | |
37 | * Karim Yaghmour <karim@opersys.com> | |
38 | * Tom Zanussi <zanussi@us.ibm.com> | |
39 | * Bob Wisniewski <bob@watson.ibm.com> | |
40 | * And from K42 : | |
41 | * Bob Wisniewski <bob@watson.ibm.com> | |
42 | * | |
43 | * Buffer reader semantic : | |
44 | * | |
45 | * - get_subbuf_size | |
46 | * while buffer is not finalized and empty | |
47 | * - get_subbuf | |
48 | * - if return value != 0, continue | |
49 | * - splice one subbuffer worth of data to a pipe | |
50 | * - splice the data from pipe to disk/network | |
51 | * - put_subbuf | |
852c2936 MD |
52 | */ |
53 | ||
5ad63a16 | 54 | #define _GNU_SOURCE |
a6352fd4 | 55 | #include <sys/types.h> |
431d5cf0 MD |
56 | #include <sys/mman.h> |
57 | #include <sys/stat.h> | |
58 | #include <fcntl.h> | |
14641deb | 59 | #include <urcu/compiler.h> |
a6352fd4 | 60 | #include <urcu/ref.h> |
35897f8b | 61 | #include <helper.h> |
14641deb | 62 | |
a6352fd4 | 63 | #include "smp.h" |
4318ae1b | 64 | #include <lttng/ringbuffer-config.h> |
2fed87ae | 65 | #include "vatomic.h" |
4931a13e MD |
66 | #include "backend.h" |
67 | #include "frontend.h" | |
a6352fd4 | 68 | #include "shm.h" |
f645cfa7 | 69 | #include "tlsfixup.h" |
bdcf8d82 | 70 | #include "../liblttng-ust/compat.h" /* For ENODATA */ |
852c2936 | 71 | |
431d5cf0 MD |
72 | #ifndef max |
73 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
74 | #endif | |
75 | ||
64493e4f MD |
76 | /* Print DBG() messages about events lost only every 1048576 hits */ |
77 | #define DBG_PRINT_NR_LOST (1UL << 20) | |
78 | ||
2432c3c9 MD |
79 | /* |
80 | * Use POSIX SHM: shm_open(3) and shm_unlink(3). | |
81 | * close(2) to close the fd returned by shm_open. | |
82 | * shm_unlink releases the shared memory object name. | |
83 | * ftruncate(2) sets the size of the memory object. | |
84 | * mmap/munmap maps the shared memory obj to a virtual address in the | |
85 | * calling proceess (should be done both in libust and consumer). | |
86 | * See shm_overview(7) for details. | |
87 | * Pass file descriptor returned by shm_open(3) to ltt-sessiond through | |
88 | * a UNIX socket. | |
89 | * | |
90 | * Since we don't need to access the object using its name, we can | |
91 | * immediately shm_unlink(3) it, and only keep the handle with its file | |
92 | * descriptor. | |
93 | */ | |
94 | ||
852c2936 MD |
95 | /* |
96 | * Internal structure representing offsets to use at a sub-buffer switch. | |
97 | */ | |
98 | struct switch_offsets { | |
99 | unsigned long begin, end, old; | |
100 | size_t pre_header_padding, size; | |
101 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, | |
102 | switch_old_end:1; | |
103 | }; | |
104 | ||
a6352fd4 | 105 | __thread unsigned int lib_ring_buffer_nesting; |
852c2936 | 106 | |
45e9e699 MD |
107 | /* |
108 | * TODO: this is unused. Errors are saved within the ring buffer. | |
109 | * Eventually, allow consumerd to print these errors. | |
110 | */ | |
852c2936 MD |
111 | static |
112 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 113 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
b68d3dc0 MD |
114 | struct lttng_ust_shm_handle *handle) |
115 | __attribute__((unused)); | |
852c2936 | 116 | |
852c2936 MD |
117 | /** |
118 | * lib_ring_buffer_reset - Reset ring buffer to initial values. | |
119 | * @buf: Ring buffer. | |
120 | * | |
121 | * Effectively empty the ring buffer. Should be called when the buffer is not | |
122 | * used for writing. The ring buffer can be opened for reading, but the reader | |
123 | * should not be using the iterator concurrently with reset. The previous | |
124 | * current iterator record is reset. | |
125 | */ | |
4cfec15c | 126 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 127 | struct lttng_ust_shm_handle *handle) |
852c2936 | 128 | { |
1d498196 | 129 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 130 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
131 | unsigned int i; |
132 | ||
133 | /* | |
134 | * Reset iterator first. It will put the subbuffer if it currently holds | |
135 | * it. | |
136 | */ | |
852c2936 MD |
137 | v_set(config, &buf->offset, 0); |
138 | for (i = 0; i < chan->backend.num_subbuf; i++) { | |
4746ae29 MD |
139 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->cc, 0); |
140 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->seq, 0); | |
141 | v_set(config, &shmp_index(handle, buf->commit_cold, i)->cc_sb, 0); | |
852c2936 | 142 | } |
a6352fd4 MD |
143 | uatomic_set(&buf->consumed, 0); |
144 | uatomic_set(&buf->record_disabled, 0); | |
852c2936 | 145 | v_set(config, &buf->last_tsc, 0); |
1d498196 | 146 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
852c2936 MD |
147 | /* Don't reset number of active readers */ |
148 | v_set(config, &buf->records_lost_full, 0); | |
149 | v_set(config, &buf->records_lost_wrap, 0); | |
150 | v_set(config, &buf->records_lost_big, 0); | |
151 | v_set(config, &buf->records_count, 0); | |
152 | v_set(config, &buf->records_overrun, 0); | |
153 | buf->finalized = 0; | |
154 | } | |
852c2936 MD |
155 | |
156 | /** | |
157 | * channel_reset - Reset channel to initial values. | |
158 | * @chan: Channel. | |
159 | * | |
160 | * Effectively empty the channel. Should be called when the channel is not used | |
161 | * for writing. The channel can be opened for reading, but the reader should not | |
162 | * be using the iterator concurrently with reset. The previous current iterator | |
163 | * record is reset. | |
164 | */ | |
165 | void channel_reset(struct channel *chan) | |
166 | { | |
167 | /* | |
168 | * Reset iterators first. Will put the subbuffer if held for reading. | |
169 | */ | |
a6352fd4 | 170 | uatomic_set(&chan->record_disabled, 0); |
852c2936 MD |
171 | /* Don't reset commit_count_mask, still valid */ |
172 | channel_backend_reset(&chan->backend); | |
173 | /* Don't reset switch/read timer interval */ | |
174 | /* Don't reset notifiers and notifier enable bits */ | |
175 | /* Don't reset reader reference count */ | |
176 | } | |
852c2936 MD |
177 | |
178 | /* | |
179 | * Must be called under cpu hotplug protection. | |
180 | */ | |
4cfec15c | 181 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
a6352fd4 | 182 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 183 | struct lttng_ust_shm_handle *handle, |
1d498196 | 184 | struct shm_object *shmobj) |
852c2936 | 185 | { |
4cfec15c | 186 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
14641deb | 187 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
a3f61e7f | 188 | void *priv = channel_get_private(chan); |
852c2936 | 189 | size_t subbuf_header_size; |
2fed87ae | 190 | uint64_t tsc; |
852c2936 MD |
191 | int ret; |
192 | ||
193 | /* Test for cpu hotplug */ | |
194 | if (buf->backend.allocated) | |
195 | return 0; | |
196 | ||
a6352fd4 | 197 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
1d498196 | 198 | cpu, handle, shmobj); |
852c2936 MD |
199 | if (ret) |
200 | return ret; | |
201 | ||
1d498196 MD |
202 | align_shm(shmobj, __alignof__(struct commit_counters_hot)); |
203 | set_shmp(buf->commit_hot, | |
204 | zalloc_shm(shmobj, | |
205 | sizeof(struct commit_counters_hot) * chan->backend.num_subbuf)); | |
206 | if (!shmp(handle, buf->commit_hot)) { | |
852c2936 MD |
207 | ret = -ENOMEM; |
208 | goto free_chanbuf; | |
209 | } | |
210 | ||
1d498196 MD |
211 | align_shm(shmobj, __alignof__(struct commit_counters_cold)); |
212 | set_shmp(buf->commit_cold, | |
213 | zalloc_shm(shmobj, | |
214 | sizeof(struct commit_counters_cold) * chan->backend.num_subbuf)); | |
215 | if (!shmp(handle, buf->commit_cold)) { | |
852c2936 MD |
216 | ret = -ENOMEM; |
217 | goto free_commit; | |
218 | } | |
219 | ||
852c2936 MD |
220 | /* |
221 | * Write the subbuffer header for first subbuffer so we know the total | |
222 | * duration of data gathering. | |
223 | */ | |
224 | subbuf_header_size = config->cb.subbuffer_header_size(); | |
225 | v_set(config, &buf->offset, subbuf_header_size); | |
4746ae29 | 226 | subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id); |
1d498196 MD |
227 | tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan)); |
228 | config->cb.buffer_begin(buf, tsc, 0, handle); | |
4746ae29 | 229 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc); |
852c2936 MD |
230 | |
231 | if (config->cb.buffer_create) { | |
1d498196 | 232 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
852c2936 MD |
233 | if (ret) |
234 | goto free_init; | |
235 | } | |
852c2936 | 236 | buf->backend.allocated = 1; |
852c2936 MD |
237 | return 0; |
238 | ||
239 | /* Error handling */ | |
240 | free_init: | |
a6352fd4 | 241 | /* commit_cold will be freed by shm teardown */ |
852c2936 | 242 | free_commit: |
a6352fd4 | 243 | /* commit_hot will be freed by shm teardown */ |
852c2936 | 244 | free_chanbuf: |
852c2936 MD |
245 | return ret; |
246 | } | |
247 | ||
1d498196 | 248 | #if 0 |
852c2936 MD |
249 | static void switch_buffer_timer(unsigned long data) |
250 | { | |
4cfec15c | 251 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 252 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 253 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
254 | |
255 | /* | |
256 | * Only flush buffers periodically if readers are active. | |
257 | */ | |
824f40b8 | 258 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
1d498196 | 259 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle); |
852c2936 | 260 | |
a6352fd4 MD |
261 | //TODO timers |
262 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
263 | // mod_timer_pinned(&buf->switch_timer, | |
264 | // jiffies + chan->switch_timer_interval); | |
265 | //else | |
266 | // mod_timer(&buf->switch_timer, | |
267 | // jiffies + chan->switch_timer_interval); | |
852c2936 | 268 | } |
1d498196 | 269 | #endif //0 |
852c2936 | 270 | |
4cfec15c | 271 | static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 272 | struct lttng_ust_shm_handle *handle) |
852c2936 | 273 | { |
1d498196 | 274 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 275 | //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
276 | |
277 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) | |
278 | return; | |
a6352fd4 MD |
279 | //TODO |
280 | //init_timer(&buf->switch_timer); | |
281 | //buf->switch_timer.function = switch_buffer_timer; | |
282 | //buf->switch_timer.expires = jiffies + chan->switch_timer_interval; | |
283 | //buf->switch_timer.data = (unsigned long)buf; | |
284 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
285 | // add_timer_on(&buf->switch_timer, buf->backend.cpu); | |
286 | //else | |
287 | // add_timer(&buf->switch_timer); | |
852c2936 MD |
288 | buf->switch_timer_enabled = 1; |
289 | } | |
290 | ||
4cfec15c | 291 | static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 292 | struct lttng_ust_shm_handle *handle) |
852c2936 | 293 | { |
1d498196 | 294 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 MD |
295 | |
296 | if (!chan->switch_timer_interval || !buf->switch_timer_enabled) | |
297 | return; | |
298 | ||
a6352fd4 MD |
299 | //TODO |
300 | //del_timer_sync(&buf->switch_timer); | |
852c2936 MD |
301 | buf->switch_timer_enabled = 0; |
302 | } | |
303 | ||
1d498196 | 304 | #if 0 |
852c2936 MD |
305 | /* |
306 | * Polling timer to check the channels for data. | |
307 | */ | |
308 | static void read_buffer_timer(unsigned long data) | |
309 | { | |
4cfec15c | 310 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 311 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 312 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
313 | |
314 | CHAN_WARN_ON(chan, !buf->backend.allocated); | |
315 | ||
824f40b8 | 316 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
852c2936 | 317 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
a6352fd4 MD |
318 | //TODO |
319 | //wake_up_interruptible(&buf->read_wait); | |
320 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
321 | } |
322 | ||
a6352fd4 MD |
323 | //TODO |
324 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
325 | // mod_timer_pinned(&buf->read_timer, | |
326 | // jiffies + chan->read_timer_interval); | |
327 | //else | |
328 | // mod_timer(&buf->read_timer, | |
329 | // jiffies + chan->read_timer_interval); | |
852c2936 | 330 | } |
1d498196 | 331 | #endif //0 |
852c2936 | 332 | |
4cfec15c | 333 | static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 334 | struct lttng_ust_shm_handle *handle) |
852c2936 | 335 | { |
1d498196 | 336 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 337 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
338 | |
339 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
340 | || !chan->read_timer_interval | |
341 | || buf->read_timer_enabled) | |
342 | return; | |
343 | ||
a6352fd4 MD |
344 | //TODO |
345 | //init_timer(&buf->read_timer); | |
346 | //buf->read_timer.function = read_buffer_timer; | |
347 | //buf->read_timer.expires = jiffies + chan->read_timer_interval; | |
348 | //buf->read_timer.data = (unsigned long)buf; | |
852c2936 | 349 | |
a6352fd4 MD |
350 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
351 | // add_timer_on(&buf->read_timer, buf->backend.cpu); | |
352 | //else | |
353 | // add_timer(&buf->read_timer); | |
852c2936 MD |
354 | buf->read_timer_enabled = 1; |
355 | } | |
356 | ||
4cfec15c | 357 | static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 358 | struct lttng_ust_shm_handle *handle) |
852c2936 | 359 | { |
1d498196 | 360 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 361 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
362 | |
363 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
364 | || !chan->read_timer_interval | |
365 | || !buf->read_timer_enabled) | |
366 | return; | |
367 | ||
a6352fd4 MD |
368 | //TODO |
369 | //del_timer_sync(&buf->read_timer); | |
852c2936 MD |
370 | /* |
371 | * do one more check to catch data that has been written in the last | |
372 | * timer period. | |
373 | */ | |
1d498196 | 374 | if (lib_ring_buffer_poll_deliver(config, buf, chan, handle)) { |
a6352fd4 MD |
375 | //TODO |
376 | //wake_up_interruptible(&buf->read_wait); | |
377 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
378 | } |
379 | buf->read_timer_enabled = 0; | |
380 | } | |
381 | ||
1d498196 | 382 | static void channel_unregister_notifiers(struct channel *chan, |
38fae1d3 | 383 | struct lttng_ust_shm_handle *handle) |
852c2936 | 384 | { |
4cfec15c | 385 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
386 | int cpu; |
387 | ||
852c2936 | 388 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
852c2936 | 389 | for_each_possible_cpu(cpu) { |
4cfec15c | 390 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
a6352fd4 | 391 | |
1d498196 MD |
392 | lib_ring_buffer_stop_switch_timer(buf, handle); |
393 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 394 | } |
852c2936 | 395 | } else { |
4cfec15c | 396 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 397 | |
1d498196 MD |
398 | lib_ring_buffer_stop_switch_timer(buf, handle); |
399 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 400 | } |
8d8a24c8 | 401 | //channel_backend_unregister_notifiers(&chan->backend); |
852c2936 MD |
402 | } |
403 | ||
38fae1d3 | 404 | static void channel_free(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 405 | int shadow) |
852c2936 | 406 | { |
824f40b8 MD |
407 | if (!shadow) |
408 | channel_backend_free(&chan->backend, handle); | |
431d5cf0 | 409 | /* chan is freed by shm teardown */ |
1d498196 MD |
410 | shm_object_table_destroy(handle->table); |
411 | free(handle); | |
852c2936 MD |
412 | } |
413 | ||
414 | /** | |
415 | * channel_create - Create channel. | |
416 | * @config: ring buffer instance configuration | |
417 | * @name: name of the channel | |
a3f61e7f MD |
418 | * @priv_data: ring buffer client private data area pointer (output) |
419 | * @priv_data_size: length, in bytes, of the private data area. | |
d028eddb | 420 | * @priv_data_init: initialization data for private data. |
852c2936 MD |
421 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
422 | * address mapping. It is used only by RING_BUFFER_STATIC | |
423 | * configuration. It can be set to NULL for other backends. | |
424 | * @subbuf_size: subbuffer size | |
425 | * @num_subbuf: number of subbuffers | |
426 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
427 | * padding to let readers get those sub-buffers. | |
428 | * Used for live streaming. | |
429 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
430 | * | |
431 | * Holds cpu hotplug. | |
432 | * Returns NULL on failure. | |
433 | */ | |
4cfec15c | 434 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
435 | const char *name, |
436 | void **priv_data, | |
437 | size_t priv_data_align, | |
438 | size_t priv_data_size, | |
d028eddb | 439 | void *priv_data_init, |
a3f61e7f | 440 | void *buf_addr, size_t subbuf_size, |
852c2936 | 441 | size_t num_subbuf, unsigned int switch_timer_interval, |
193183fb | 442 | unsigned int read_timer_interval, |
ef9ff354 | 443 | int **shm_fd, int **wait_fd, uint64_t **memory_map_size) |
852c2936 | 444 | { |
1d498196 | 445 | int ret, cpu; |
a3f61e7f | 446 | size_t shmsize, chansize; |
852c2936 | 447 | struct channel *chan; |
38fae1d3 | 448 | struct lttng_ust_shm_handle *handle; |
1d498196 | 449 | struct shm_object *shmobj; |
193183fb | 450 | struct shm_ref *ref; |
852c2936 MD |
451 | |
452 | if (lib_ring_buffer_check_config(config, switch_timer_interval, | |
453 | read_timer_interval)) | |
454 | return NULL; | |
455 | ||
38fae1d3 | 456 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
431d5cf0 MD |
457 | if (!handle) |
458 | return NULL; | |
459 | ||
1d498196 MD |
460 | /* Allocate table for channel + per-cpu buffers */ |
461 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
462 | if (!handle->table) | |
463 | goto error_table_alloc; | |
852c2936 | 464 | |
1d498196 MD |
465 | /* Calculate the shm allocation layout */ |
466 | shmsize = sizeof(struct channel); | |
c1fca457 | 467 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
1d498196 | 468 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
4cfec15c | 469 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * num_possible_cpus(); |
1d498196 | 470 | else |
4cfec15c | 471 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp); |
a3f61e7f MD |
472 | chansize = shmsize; |
473 | shmsize += offset_align(shmsize, priv_data_align); | |
474 | shmsize += priv_data_size; | |
a6352fd4 | 475 | |
1d498196 | 476 | shmobj = shm_object_table_append(handle->table, shmsize); |
b5a14697 MD |
477 | if (!shmobj) |
478 | goto error_append; | |
57773204 | 479 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
a3f61e7f | 480 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
57773204 MD |
481 | assert(handle->chan._ref.index == 0); |
482 | assert(handle->chan._ref.offset == 0); | |
1d498196 | 483 | chan = shmp(handle, handle->chan); |
a6352fd4 | 484 | if (!chan) |
1d498196 | 485 | goto error_append; |
a6352fd4 | 486 | |
a3f61e7f MD |
487 | /* space for private data */ |
488 | if (priv_data_size) { | |
489 | DECLARE_SHMP(void, priv_data_alloc); | |
490 | ||
491 | align_shm(shmobj, priv_data_align); | |
492 | chan->priv_data_offset = shmobj->allocated_len; | |
493 | set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size)); | |
494 | if (!shmp(handle, priv_data_alloc)) | |
495 | goto error_append; | |
496 | *priv_data = channel_get_private(chan); | |
d028eddb | 497 | memcpy(*priv_data, priv_data_init, priv_data_size); |
a3f61e7f MD |
498 | } else { |
499 | chan->priv_data_offset = -1; | |
500 | *priv_data = NULL; | |
501 | } | |
502 | ||
503 | ret = channel_backend_init(&chan->backend, name, config, | |
1d498196 | 504 | subbuf_size, num_subbuf, handle); |
852c2936 | 505 | if (ret) |
1d498196 | 506 | goto error_backend_init; |
852c2936 MD |
507 | |
508 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); | |
a6352fd4 MD |
509 | //TODO |
510 | //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); | |
511 | //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); | |
a6352fd4 MD |
512 | //TODO |
513 | //init_waitqueue_head(&chan->read_wait); | |
514 | //init_waitqueue_head(&chan->hp_wait); | |
852c2936 MD |
515 | |
516 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
852c2936 MD |
517 | /* |
518 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
519 | * in early initcall, it will not be notified of secondary cpus. | |
520 | * In that off case, we need to allocate for all possible cpus. | |
521 | */ | |
852c2936 | 522 | for_each_possible_cpu(cpu) { |
4cfec15c | 523 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
1d498196 MD |
524 | lib_ring_buffer_start_switch_timer(buf, handle); |
525 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 526 | } |
852c2936 | 527 | } else { |
4cfec15c | 528 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 529 | |
1d498196 MD |
530 | lib_ring_buffer_start_switch_timer(buf, handle); |
531 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 532 | } |
193183fb MD |
533 | ref = &handle->chan._ref; |
534 | shm_get_object_data(handle, ref, shm_fd, wait_fd, memory_map_size); | |
431d5cf0 | 535 | return handle; |
852c2936 | 536 | |
1d498196 MD |
537 | error_backend_init: |
538 | error_append: | |
539 | shm_object_table_destroy(handle->table); | |
540 | error_table_alloc: | |
431d5cf0 | 541 | free(handle); |
852c2936 MD |
542 | return NULL; |
543 | } | |
852c2936 | 544 | |
38fae1d3 | 545 | struct lttng_ust_shm_handle *channel_handle_create(int shm_fd, int wait_fd, |
193183fb MD |
546 | uint64_t memory_map_size) |
547 | { | |
38fae1d3 | 548 | struct lttng_ust_shm_handle *handle; |
193183fb MD |
549 | struct shm_object *object; |
550 | ||
38fae1d3 | 551 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
193183fb MD |
552 | if (!handle) |
553 | return NULL; | |
554 | ||
555 | /* Allocate table for channel + per-cpu buffers */ | |
556 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
557 | if (!handle->table) | |
558 | goto error_table_alloc; | |
559 | /* Add channel object */ | |
560 | object = shm_object_table_append_shadow(handle->table, | |
561 | shm_fd, wait_fd, memory_map_size); | |
562 | if (!object) | |
563 | goto error_table_object; | |
57773204 MD |
564 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
565 | handle->chan._ref.index = 0; | |
566 | handle->chan._ref.offset = 0; | |
193183fb MD |
567 | return handle; |
568 | ||
569 | error_table_object: | |
570 | shm_object_table_destroy(handle->table); | |
571 | error_table_alloc: | |
572 | free(handle); | |
573 | return NULL; | |
574 | } | |
575 | ||
38fae1d3 | 576 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
193183fb MD |
577 | int shm_fd, int wait_fd, uint64_t memory_map_size) |
578 | { | |
579 | struct shm_object *object; | |
580 | ||
581 | /* Add stream object */ | |
582 | object = shm_object_table_append_shadow(handle->table, | |
583 | shm_fd, wait_fd, memory_map_size); | |
584 | if (!object) | |
585 | return -1; | |
586 | return 0; | |
587 | } | |
588 | ||
852c2936 | 589 | static |
38fae1d3 | 590 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 591 | int shadow) |
852c2936 | 592 | { |
824f40b8 | 593 | channel_free(chan, handle, shadow); |
852c2936 MD |
594 | } |
595 | ||
596 | /** | |
597 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
598 | * @chan: channel to destroy | |
599 | * | |
600 | * Holds cpu hotplug. | |
431d5cf0 MD |
601 | * Call "destroy" callback, finalize channels, decrement the channel |
602 | * reference count. Note that when readers have completed data | |
603 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
a3f61e7f | 604 | * They should release their handle at that point. |
852c2936 | 605 | */ |
a3f61e7f | 606 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 607 | int shadow) |
852c2936 | 608 | { |
824f40b8 MD |
609 | if (shadow) { |
610 | channel_release(chan, handle, shadow); | |
a3f61e7f | 611 | return; |
824f40b8 MD |
612 | } |
613 | ||
1d498196 | 614 | channel_unregister_notifiers(chan, handle); |
852c2936 | 615 | |
45e9e699 MD |
616 | /* |
617 | * Note: the consumer takes care of finalizing and switching the | |
618 | * buffers. | |
619 | */ | |
852c2936 | 620 | |
431d5cf0 MD |
621 | /* |
622 | * sessiond/consumer are keeping a reference on the shm file | |
623 | * descriptor directly. No need to refcount. | |
624 | */ | |
824f40b8 | 625 | channel_release(chan, handle, shadow); |
a3f61e7f | 626 | return; |
852c2936 | 627 | } |
852c2936 | 628 | |
4cfec15c MD |
629 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
630 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 631 | struct channel *chan, int cpu, |
38fae1d3 | 632 | struct lttng_ust_shm_handle *handle, |
ef9ff354 MD |
633 | int **shm_fd, int **wait_fd, |
634 | uint64_t **memory_map_size) | |
852c2936 | 635 | { |
381c0f1e MD |
636 | struct shm_ref *ref; |
637 | ||
638 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
639 | ref = &chan->backend.buf[0].shmp._ref; | |
640 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
641 | memory_map_size); | |
1d498196 | 642 | return shmp(handle, chan->backend.buf[0].shmp); |
381c0f1e | 643 | } else { |
e095d803 MD |
644 | if (cpu >= num_possible_cpus()) |
645 | return NULL; | |
381c0f1e MD |
646 | ref = &chan->backend.buf[cpu].shmp._ref; |
647 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
648 | memory_map_size); | |
1d498196 | 649 | return shmp(handle, chan->backend.buf[cpu].shmp); |
381c0f1e | 650 | } |
852c2936 | 651 | } |
852c2936 | 652 | |
4cfec15c | 653 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 654 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 655 | int shadow) |
852c2936 | 656 | { |
824f40b8 MD |
657 | if (shadow) { |
658 | if (uatomic_cmpxchg(&buf->active_shadow_readers, 0, 1) != 0) | |
659 | return -EBUSY; | |
660 | cmm_smp_mb(); | |
661 | return 0; | |
662 | } | |
a6352fd4 | 663 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
852c2936 | 664 | return -EBUSY; |
a6352fd4 | 665 | cmm_smp_mb(); |
852c2936 MD |
666 | return 0; |
667 | } | |
852c2936 | 668 | |
4cfec15c | 669 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 670 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 671 | int shadow) |
852c2936 | 672 | { |
1d498196 | 673 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 | 674 | |
824f40b8 MD |
675 | if (shadow) { |
676 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_shadow_readers) != 1); | |
677 | cmm_smp_mb(); | |
678 | uatomic_dec(&buf->active_shadow_readers); | |
679 | return; | |
680 | } | |
a6352fd4 MD |
681 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
682 | cmm_smp_mb(); | |
683 | uatomic_dec(&buf->active_readers); | |
852c2936 MD |
684 | } |
685 | ||
686 | /** | |
687 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
688 | * @buf: ring buffer | |
689 | * @consumed: consumed count indicating the position where to read | |
690 | * @produced: produced count, indicates position when to stop reading | |
691 | * | |
692 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
693 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
694 | */ |
695 | ||
4cfec15c | 696 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 697 | unsigned long *consumed, unsigned long *produced, |
38fae1d3 | 698 | struct lttng_ust_shm_handle *handle) |
852c2936 | 699 | { |
1d498196 | 700 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 701 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
702 | unsigned long consumed_cur, write_offset; |
703 | int finalized; | |
704 | ||
14641deb | 705 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
706 | /* |
707 | * Read finalized before counters. | |
708 | */ | |
a6352fd4 MD |
709 | cmm_smp_rmb(); |
710 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 MD |
711 | /* |
712 | * No need to issue a memory barrier between consumed count read and | |
713 | * write offset read, because consumed count can only change | |
714 | * concurrently in overwrite mode, and we keep a sequence counter | |
715 | * identifier derived from the write offset to check we are getting | |
716 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
717 | * "tagged" upon writes, tags are checked upon read). | |
718 | */ | |
719 | write_offset = v_read(config, &buf->offset); | |
720 | ||
721 | /* | |
722 | * Check that we are not about to read the same subbuffer in | |
723 | * which the writer head is. | |
724 | */ | |
725 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
726 | == 0) | |
727 | goto nodata; | |
728 | ||
729 | *consumed = consumed_cur; | |
730 | *produced = subbuf_trunc(write_offset, chan); | |
731 | ||
732 | return 0; | |
733 | ||
734 | nodata: | |
735 | /* | |
736 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
737 | * of "raw_spin_is_locked" memory ordering. | |
738 | */ | |
739 | if (finalized) | |
740 | return -ENODATA; | |
852c2936 MD |
741 | else |
742 | return -EAGAIN; | |
743 | } | |
852c2936 MD |
744 | |
745 | /** | |
746 | * lib_ring_buffer_put_snapshot - move consumed counter forward | |
747 | * @buf: ring buffer | |
748 | * @consumed_new: new consumed count value | |
749 | */ | |
4cfec15c | 750 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 751 | unsigned long consumed_new, |
38fae1d3 | 752 | struct lttng_ust_shm_handle *handle) |
852c2936 | 753 | { |
4cfec15c | 754 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 755 | struct channel *chan = shmp(handle, bufb->chan); |
852c2936 MD |
756 | unsigned long consumed; |
757 | ||
824f40b8 MD |
758 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
759 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
760 | |
761 | /* | |
762 | * Only push the consumed value forward. | |
763 | * If the consumed cmpxchg fails, this is because we have been pushed by | |
764 | * the writer in flight recorder mode. | |
765 | */ | |
a6352fd4 | 766 | consumed = uatomic_read(&buf->consumed); |
852c2936 | 767 | while ((long) consumed - (long) consumed_new < 0) |
a6352fd4 MD |
768 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
769 | consumed_new); | |
852c2936 | 770 | } |
852c2936 MD |
771 | |
772 | /** | |
773 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading | |
774 | * @buf: ring buffer | |
775 | * @consumed: consumed count indicating the position where to read | |
776 | * | |
777 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
778 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 | 779 | */ |
4cfec15c | 780 | int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 781 | unsigned long consumed, |
38fae1d3 | 782 | struct lttng_ust_shm_handle *handle) |
852c2936 | 783 | { |
1d498196 | 784 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 785 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
786 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
787 | int ret; | |
788 | int finalized; | |
789 | ||
790 | retry: | |
14641deb | 791 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
792 | /* |
793 | * Read finalized before counters. | |
794 | */ | |
a6352fd4 MD |
795 | cmm_smp_rmb(); |
796 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 | 797 | consumed_idx = subbuf_index(consumed, chan); |
4746ae29 | 798 | commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb); |
852c2936 MD |
799 | /* |
800 | * Make sure we read the commit count before reading the buffer | |
801 | * data and the write offset. Correct consumed offset ordering | |
802 | * wrt commit count is insured by the use of cmpxchg to update | |
803 | * the consumed offset. | |
852c2936 | 804 | */ |
a6352fd4 MD |
805 | /* |
806 | * Local rmb to match the remote wmb to read the commit count | |
807 | * before the buffer data and the write offset. | |
808 | */ | |
809 | cmm_smp_rmb(); | |
852c2936 MD |
810 | |
811 | write_offset = v_read(config, &buf->offset); | |
812 | ||
813 | /* | |
814 | * Check that the buffer we are getting is after or at consumed_cur | |
815 | * position. | |
816 | */ | |
817 | if ((long) subbuf_trunc(consumed, chan) | |
818 | - (long) subbuf_trunc(consumed_cur, chan) < 0) | |
819 | goto nodata; | |
820 | ||
821 | /* | |
822 | * Check that the subbuffer we are trying to consume has been | |
823 | * already fully committed. | |
824 | */ | |
825 | if (((commit_count - chan->backend.subbuf_size) | |
826 | & chan->commit_count_mask) | |
827 | - (buf_trunc(consumed_cur, chan) | |
828 | >> chan->backend.num_subbuf_order) | |
829 | != 0) | |
830 | goto nodata; | |
831 | ||
832 | /* | |
833 | * Check that we are not about to read the same subbuffer in | |
834 | * which the writer head is. | |
835 | */ | |
836 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
837 | == 0) | |
838 | goto nodata; | |
839 | ||
840 | /* | |
841 | * Failure to get the subbuffer causes a busy-loop retry without going | |
842 | * to a wait queue. These are caused by short-lived race windows where | |
843 | * the writer is getting access to a subbuffer we were trying to get | |
844 | * access to. Also checks that the "consumed" buffer count we are | |
845 | * looking for matches the one contained in the subbuffer id. | |
846 | */ | |
847 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
848 | consumed_idx, buf_trunc_val(consumed, chan), |
849 | handle); | |
852c2936 MD |
850 | if (ret) |
851 | goto retry; | |
852 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); | |
853 | ||
854 | buf->get_subbuf_consumed = consumed; | |
855 | buf->get_subbuf = 1; | |
856 | ||
857 | return 0; | |
858 | ||
859 | nodata: | |
860 | /* | |
861 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
862 | * of "raw_spin_is_locked" memory ordering. | |
863 | */ | |
864 | if (finalized) | |
865 | return -ENODATA; | |
852c2936 MD |
866 | else |
867 | return -EAGAIN; | |
868 | } | |
852c2936 MD |
869 | |
870 | /** | |
871 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access | |
872 | * @buf: ring buffer | |
873 | */ | |
4cfec15c | 874 | void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 875 | struct lttng_ust_shm_handle *handle) |
852c2936 | 876 | { |
4cfec15c | 877 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 878 | struct channel *chan = shmp(handle, bufb->chan); |
4cfec15c | 879 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
880 | unsigned long read_sb_bindex, consumed_idx, consumed; |
881 | ||
824f40b8 MD |
882 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
883 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
884 | |
885 | if (!buf->get_subbuf) { | |
886 | /* | |
887 | * Reader puts a subbuffer it did not get. | |
888 | */ | |
889 | CHAN_WARN_ON(chan, 1); | |
890 | return; | |
891 | } | |
892 | consumed = buf->get_subbuf_consumed; | |
893 | buf->get_subbuf = 0; | |
894 | ||
895 | /* | |
896 | * Clear the records_unread counter. (overruns counter) | |
897 | * Can still be non-zero if a file reader simply grabbed the data | |
898 | * without using iterators. | |
899 | * Can be below zero if an iterator is used on a snapshot more than | |
900 | * once. | |
901 | */ | |
902 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
903 | v_add(config, v_read(config, | |
4746ae29 | 904 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
852c2936 | 905 | &bufb->records_read); |
4746ae29 | 906 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
852c2936 MD |
907 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
908 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
909 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
910 | ||
911 | /* | |
912 | * Exchange the reader subbuffer with the one we put in its place in the | |
913 | * writer subbuffer table. Expect the original consumed count. If | |
914 | * update_read_sb_index fails, this is because the writer updated the | |
915 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
916 | * currently have: it has become invalid to try reading this sub-buffer | |
917 | * consumed count value anyway. | |
918 | */ | |
919 | consumed_idx = subbuf_index(consumed, chan); | |
920 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
921 | consumed_idx, buf_trunc_val(consumed, chan), |
922 | handle); | |
852c2936 MD |
923 | /* |
924 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
925 | * if the writer concurrently updated it. | |
926 | */ | |
927 | } | |
852c2936 MD |
928 | |
929 | /* | |
930 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
931 | * position and the writer position. (inclusive) | |
932 | */ | |
933 | static | |
4cfec15c | 934 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
935 | struct channel *chan, |
936 | unsigned long cons_offset, | |
1d498196 | 937 | int cpu, |
38fae1d3 | 938 | struct lttng_ust_shm_handle *handle) |
852c2936 | 939 | { |
4cfec15c | 940 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
941 | unsigned long cons_idx, commit_count, commit_count_sb; |
942 | ||
943 | cons_idx = subbuf_index(cons_offset, chan); | |
4746ae29 MD |
944 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc); |
945 | commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb); | |
852c2936 MD |
946 | |
947 | if (subbuf_offset(commit_count, chan) != 0) | |
4d3c9523 | 948 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
949 | "commit count in subbuffer %lu,\n" |
950 | "expecting multiples of %lu bytes\n" | |
951 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
952 | chan->backend.name, cpu, cons_idx, | |
953 | chan->backend.subbuf_size, | |
954 | commit_count, commit_count_sb); | |
955 | ||
4d3c9523 | 956 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
852c2936 MD |
957 | chan->backend.name, cpu, commit_count); |
958 | } | |
959 | ||
960 | static | |
4cfec15c | 961 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 962 | struct channel *chan, |
1d498196 | 963 | void *priv, int cpu, |
38fae1d3 | 964 | struct lttng_ust_shm_handle *handle) |
852c2936 | 965 | { |
4cfec15c | 966 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
967 | unsigned long write_offset, cons_offset; |
968 | ||
852c2936 MD |
969 | /* |
970 | * No need to order commit_count, write_offset and cons_offset reads | |
971 | * because we execute at teardown when no more writer nor reader | |
972 | * references are left. | |
973 | */ | |
974 | write_offset = v_read(config, &buf->offset); | |
a6352fd4 | 975 | cons_offset = uatomic_read(&buf->consumed); |
852c2936 | 976 | if (write_offset != cons_offset) |
4d3c9523 | 977 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
978 | "non-consumed data\n" |
979 | " [ %lu bytes written, %lu bytes read ]\n", | |
980 | chan->backend.name, cpu, write_offset, cons_offset); | |
981 | ||
a6352fd4 | 982 | for (cons_offset = uatomic_read(&buf->consumed); |
852c2936 MD |
983 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
984 | chan) | |
985 | - cons_offset) > 0; | |
986 | cons_offset = subbuf_align(cons_offset, chan)) | |
987 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1d498196 | 988 | cpu, handle); |
852c2936 MD |
989 | } |
990 | ||
991 | static | |
992 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 993 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
38fae1d3 | 994 | struct lttng_ust_shm_handle *handle) |
852c2936 | 995 | { |
4cfec15c | 996 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
a3f61e7f | 997 | void *priv = channel_get_private(chan); |
852c2936 | 998 | |
a1360615 MD |
999 | if (!strcmp(chan->backend.name, "relay-metadata-mmap")) { |
1000 | DBG("ring buffer %s: %lu records written, " | |
1001 | "%lu records overrun\n", | |
1002 | chan->backend.name, | |
1003 | v_read(config, &buf->records_count), | |
1004 | v_read(config, &buf->records_overrun)); | |
1005 | } else { | |
1006 | DBG("ring buffer %s, cpu %d: %lu records written, " | |
1007 | "%lu records overrun\n", | |
1008 | chan->backend.name, cpu, | |
1009 | v_read(config, &buf->records_count), | |
1010 | v_read(config, &buf->records_overrun)); | |
1011 | ||
1012 | if (v_read(config, &buf->records_lost_full) | |
1013 | || v_read(config, &buf->records_lost_wrap) | |
1014 | || v_read(config, &buf->records_lost_big)) | |
1015 | DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n" | |
1016 | " [ %lu buffer full, %lu nest buffer wrap-around, " | |
1017 | "%lu event too big ]\n", | |
1018 | chan->backend.name, cpu, | |
1019 | v_read(config, &buf->records_lost_full), | |
1020 | v_read(config, &buf->records_lost_wrap), | |
1021 | v_read(config, &buf->records_lost_big)); | |
1022 | } | |
1d498196 | 1023 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
852c2936 MD |
1024 | } |
1025 | ||
1026 | /* | |
1027 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. | |
1028 | * | |
1029 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. | |
1030 | */ | |
1031 | static | |
4cfec15c | 1032 | void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1033 | struct channel *chan, |
1034 | struct switch_offsets *offsets, | |
2fed87ae | 1035 | uint64_t tsc, |
38fae1d3 | 1036 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1037 | { |
4cfec15c | 1038 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1039 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
1040 | unsigned long commit_count; | |
1041 | ||
1d498196 | 1042 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
852c2936 MD |
1043 | |
1044 | /* | |
1045 | * Order all writes to buffer before the commit count update that will | |
1046 | * determine that the subbuffer is full. | |
1047 | */ | |
a6352fd4 | 1048 | cmm_smp_wmb(); |
852c2936 | 1049 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1050 | &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1051 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 MD |
1052 | /* Check if the written buffer has to be delivered */ |
1053 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, | |
1d498196 | 1054 | commit_count, oldidx, handle); |
852c2936 MD |
1055 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1056 | offsets->old, commit_count, | |
1d498196 MD |
1057 | config->cb.subbuffer_header_size(), |
1058 | handle); | |
852c2936 MD |
1059 | } |
1060 | ||
1061 | /* | |
1062 | * lib_ring_buffer_switch_old_end: switch old subbuffer | |
1063 | * | |
1064 | * Note : offset_old should never be 0 here. It is ok, because we never perform | |
1065 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller | |
1066 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty | |
1067 | * subbuffer. | |
1068 | */ | |
1069 | static | |
4cfec15c | 1070 | void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1071 | struct channel *chan, |
1072 | struct switch_offsets *offsets, | |
2fed87ae | 1073 | uint64_t tsc, |
38fae1d3 | 1074 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1075 | { |
4cfec15c | 1076 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1077 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
1078 | unsigned long commit_count, padding_size, data_size; | |
1079 | ||
1080 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; | |
1081 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1082 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
1083 | handle); | |
852c2936 MD |
1084 | |
1085 | /* | |
1086 | * Order all writes to buffer before the commit count update that will | |
1087 | * determine that the subbuffer is full. | |
1088 | */ | |
a6352fd4 | 1089 | cmm_smp_wmb(); |
4746ae29 MD |
1090 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1091 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 | 1092 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
1d498196 | 1093 | commit_count, oldidx, handle); |
852c2936 MD |
1094 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1095 | offsets->old, commit_count, | |
1d498196 | 1096 | padding_size, handle); |
852c2936 MD |
1097 | } |
1098 | ||
1099 | /* | |
1100 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. | |
1101 | * | |
1102 | * This code can be executed unordered : writers may already have written to the | |
1103 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1104 | * that this code is executed before the deliver of this sub-buffer. | |
1105 | */ | |
1106 | static | |
4cfec15c | 1107 | void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1108 | struct channel *chan, |
1109 | struct switch_offsets *offsets, | |
2fed87ae | 1110 | uint64_t tsc, |
38fae1d3 | 1111 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1112 | { |
4cfec15c | 1113 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1114 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
1115 | unsigned long commit_count; | |
1116 | ||
1d498196 | 1117 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
852c2936 MD |
1118 | |
1119 | /* | |
1120 | * Order all writes to buffer before the commit count update that will | |
1121 | * determine that the subbuffer is full. | |
1122 | */ | |
a6352fd4 | 1123 | cmm_smp_wmb(); |
852c2936 | 1124 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1125 | &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
1126 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc); | |
852c2936 MD |
1127 | /* Check if the written buffer has to be delivered */ |
1128 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, | |
1d498196 | 1129 | commit_count, beginidx, handle); |
852c2936 MD |
1130 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
1131 | offsets->begin, commit_count, | |
1d498196 MD |
1132 | config->cb.subbuffer_header_size(), |
1133 | handle); | |
852c2936 MD |
1134 | } |
1135 | ||
1136 | /* | |
1137 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer | |
1138 | * | |
1139 | * The only remaining threads could be the ones with pending commits. They will | |
1140 | * have to do the deliver themselves. | |
1141 | */ | |
1142 | static | |
4cfec15c | 1143 | void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 MD |
1144 | struct channel *chan, |
1145 | struct switch_offsets *offsets, | |
2fed87ae | 1146 | uint64_t tsc, |
38fae1d3 | 1147 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1148 | { |
4cfec15c | 1149 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1150 | unsigned long endidx = subbuf_index(offsets->end - 1, chan); |
1151 | unsigned long commit_count, padding_size, data_size; | |
1152 | ||
1153 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; | |
1154 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1155 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
1156 | handle); | |
852c2936 MD |
1157 | |
1158 | /* | |
1159 | * Order all writes to buffer before the commit count update that will | |
1160 | * determine that the subbuffer is full. | |
1161 | */ | |
a6352fd4 | 1162 | cmm_smp_wmb(); |
4746ae29 MD |
1163 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
1164 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc); | |
852c2936 | 1165 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1, |
1d498196 | 1166 | commit_count, endidx, handle); |
852c2936 MD |
1167 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, |
1168 | offsets->end, commit_count, | |
1d498196 | 1169 | padding_size, handle); |
852c2936 MD |
1170 | } |
1171 | ||
1172 | /* | |
1173 | * Returns : | |
1174 | * 0 if ok | |
1175 | * !0 if execution must be aborted. | |
1176 | */ | |
1177 | static | |
1178 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, | |
4cfec15c | 1179 | struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1180 | struct channel *chan, |
1181 | struct switch_offsets *offsets, | |
2fed87ae | 1182 | uint64_t *tsc) |
852c2936 | 1183 | { |
4cfec15c | 1184 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1185 | unsigned long off; |
1186 | ||
1187 | offsets->begin = v_read(config, &buf->offset); | |
1188 | offsets->old = offsets->begin; | |
1189 | offsets->switch_old_start = 0; | |
1190 | off = subbuf_offset(offsets->begin, chan); | |
1191 | ||
1192 | *tsc = config->cb.ring_buffer_clock_read(chan); | |
1193 | ||
1194 | /* | |
1195 | * Ensure we flush the header of an empty subbuffer when doing the | |
1196 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the | |
1197 | * total data gathering duration even if there were no records saved | |
1198 | * after the last buffer switch. | |
1199 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. | |
1200 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of | |
1201 | * subbuffer header as appropriate. | |
1202 | * The next record that reserves space will be responsible for | |
1203 | * populating the following subbuffer header. We choose not to populate | |
1204 | * the next subbuffer header here because we want to be able to use | |
a6352fd4 MD |
1205 | * SWITCH_ACTIVE for periodical buffer flush, which must |
1206 | * guarantee that all the buffer content (records and header | |
1207 | * timestamps) are visible to the reader. This is required for | |
1208 | * quiescence guarantees for the fusion merge. | |
852c2936 MD |
1209 | */ |
1210 | if (mode == SWITCH_FLUSH || off > 0) { | |
b5a3dfa5 | 1211 | if (caa_unlikely(off == 0)) { |
852c2936 MD |
1212 | /* |
1213 | * The client does not save any header information. | |
1214 | * Don't switch empty subbuffer on finalize, because it | |
1215 | * is invalid to deliver a completely empty subbuffer. | |
1216 | */ | |
1217 | if (!config->cb.subbuffer_header_size()) | |
1218 | return -1; | |
1219 | /* | |
1220 | * Need to write the subbuffer start header on finalize. | |
1221 | */ | |
1222 | offsets->switch_old_start = 1; | |
1223 | } | |
1224 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1225 | } else | |
1226 | return -1; /* we do not have to switch : buffer is empty */ | |
1227 | /* Note: old points to the next subbuf at offset 0 */ | |
1228 | offsets->end = offsets->begin; | |
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | /* | |
1233 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1234 | * called while tracing is active with absolutely no lock held. | |
1235 | * | |
1236 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1237 | * operations, this function must be called from the CPU which owns the buffer | |
1238 | * for a ACTIVE flush. | |
1239 | */ | |
4cfec15c | 1240 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
38fae1d3 | 1241 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1242 | { |
1d498196 | 1243 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 1244 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1245 | struct switch_offsets offsets; |
1246 | unsigned long oldidx; | |
2fed87ae | 1247 | uint64_t tsc; |
852c2936 MD |
1248 | |
1249 | offsets.size = 0; | |
1250 | ||
1251 | /* | |
1252 | * Perform retryable operations. | |
1253 | */ | |
1254 | do { | |
1255 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
1256 | &tsc)) | |
1257 | return; /* Switch not needed */ | |
1258 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1259 | != offsets.old); | |
1260 | ||
1261 | /* | |
1262 | * Atomically update last_tsc. This update races against concurrent | |
1263 | * atomic updates, but the race will always cause supplementary full TSC | |
1264 | * records, never the opposite (missing a full TSC record when it would | |
1265 | * be needed). | |
1266 | */ | |
1267 | save_last_tsc(config, buf, tsc); | |
1268 | ||
1269 | /* | |
1270 | * Push the reader if necessary | |
1271 | */ | |
1272 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1273 | ||
1274 | oldidx = subbuf_index(offsets.old, chan); | |
1d498196 | 1275 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
852c2936 MD |
1276 | |
1277 | /* | |
1278 | * May need to populate header start on SWITCH_FLUSH. | |
1279 | */ | |
1280 | if (offsets.switch_old_start) { | |
1d498196 | 1281 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
852c2936 MD |
1282 | offsets.old += config->cb.subbuffer_header_size(); |
1283 | } | |
1284 | ||
1285 | /* | |
1286 | * Switch old subbuffer. | |
1287 | */ | |
1d498196 | 1288 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
852c2936 | 1289 | } |
852c2936 MD |
1290 | |
1291 | /* | |
1292 | * Returns : | |
1293 | * 0 if ok | |
1294 | * -ENOSPC if event size is too large for packet. | |
1295 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1296 | * -EIO if data cannot be written into the buffer for any other reason. | |
1297 | */ | |
1298 | static | |
4cfec15c | 1299 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1300 | struct channel *chan, |
1301 | struct switch_offsets *offsets, | |
4cfec15c | 1302 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 | 1303 | { |
4cfec15c | 1304 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
38fae1d3 | 1305 | struct lttng_ust_shm_handle *handle = ctx->handle; |
852c2936 MD |
1306 | unsigned long reserve_commit_diff; |
1307 | ||
1308 | offsets->begin = v_read(config, &buf->offset); | |
1309 | offsets->old = offsets->begin; | |
1310 | offsets->switch_new_start = 0; | |
1311 | offsets->switch_new_end = 0; | |
1312 | offsets->switch_old_end = 0; | |
1313 | offsets->pre_header_padding = 0; | |
1314 | ||
1315 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
1316 | if ((int64_t) ctx->tsc == -EIO) | |
1317 | return -EIO; | |
1318 | ||
1319 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
1320 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
1321 | ||
b5a3dfa5 | 1322 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
852c2936 MD |
1323 | offsets->switch_new_start = 1; /* For offsets->begin */ |
1324 | } else { | |
1325 | offsets->size = config->cb.record_header_size(config, chan, | |
1326 | offsets->begin, | |
1327 | &offsets->pre_header_padding, | |
1328 | ctx); | |
1329 | offsets->size += | |
1330 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1331 | ctx->largest_align) | |
1332 | + ctx->data_size; | |
b5a3dfa5 | 1333 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
852c2936 MD |
1334 | offsets->size > chan->backend.subbuf_size)) { |
1335 | offsets->switch_old_end = 1; /* For offsets->old */ | |
1336 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1337 | } | |
1338 | } | |
b5a3dfa5 | 1339 | if (caa_unlikely(offsets->switch_new_start)) { |
852c2936 MD |
1340 | unsigned long sb_index; |
1341 | ||
1342 | /* | |
1343 | * We are typically not filling the previous buffer completely. | |
1344 | */ | |
b5a3dfa5 | 1345 | if (caa_likely(offsets->switch_old_end)) |
852c2936 MD |
1346 | offsets->begin = subbuf_align(offsets->begin, chan); |
1347 | offsets->begin = offsets->begin | |
1348 | + config->cb.subbuffer_header_size(); | |
1349 | /* Test new buffer integrity */ | |
1350 | sb_index = subbuf_index(offsets->begin, chan); | |
1351 | reserve_commit_diff = | |
1352 | (buf_trunc(offsets->begin, chan) | |
1353 | >> chan->backend.num_subbuf_order) | |
1354 | - ((unsigned long) v_read(config, | |
4746ae29 | 1355 | &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb) |
852c2936 | 1356 | & chan->commit_count_mask); |
b5a3dfa5 | 1357 | if (caa_likely(reserve_commit_diff == 0)) { |
852c2936 | 1358 | /* Next subbuffer not being written to. */ |
b5a3dfa5 | 1359 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
852c2936 MD |
1360 | subbuf_trunc(offsets->begin, chan) |
1361 | - subbuf_trunc((unsigned long) | |
a6352fd4 | 1362 | uatomic_read(&buf->consumed), chan) |
852c2936 | 1363 | >= chan->backend.buf_size)) { |
64493e4f MD |
1364 | unsigned long nr_lost; |
1365 | ||
852c2936 MD |
1366 | /* |
1367 | * We do not overwrite non consumed buffers | |
1368 | * and we are full : record is lost. | |
1369 | */ | |
64493e4f | 1370 | nr_lost = v_read(config, &buf->records_lost_full); |
852c2936 | 1371 | v_inc(config, &buf->records_lost_full); |
64493e4f MD |
1372 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1373 | DBG("%lu or more records lost in (%s:%d) (buffer full)\n", | |
1374 | nr_lost + 1, chan->backend.name, | |
1375 | buf->backend.cpu); | |
1376 | } | |
852c2936 MD |
1377 | return -ENOBUFS; |
1378 | } else { | |
1379 | /* | |
1380 | * Next subbuffer not being written to, and we | |
1381 | * are either in overwrite mode or the buffer is | |
1382 | * not full. It's safe to write in this new | |
1383 | * subbuffer. | |
1384 | */ | |
1385 | } | |
1386 | } else { | |
64493e4f MD |
1387 | unsigned long nr_lost; |
1388 | ||
852c2936 MD |
1389 | /* |
1390 | * Next subbuffer reserve offset does not match the | |
1391 | * commit offset. Drop record in producer-consumer and | |
1392 | * overwrite mode. Caused by either a writer OOPS or too | |
1393 | * many nested writes over a reserve/commit pair. | |
1394 | */ | |
64493e4f | 1395 | nr_lost = v_read(config, &buf->records_lost_wrap); |
852c2936 | 1396 | v_inc(config, &buf->records_lost_wrap); |
64493e4f MD |
1397 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1398 | DBG("%lu or more records lost in (%s:%d) (wrap-around)\n", | |
1399 | nr_lost + 1, chan->backend.name, | |
1400 | buf->backend.cpu); | |
1401 | } | |
852c2936 MD |
1402 | return -EIO; |
1403 | } | |
1404 | offsets->size = | |
1405 | config->cb.record_header_size(config, chan, | |
1406 | offsets->begin, | |
1407 | &offsets->pre_header_padding, | |
1408 | ctx); | |
1409 | offsets->size += | |
1410 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1411 | ctx->largest_align) | |
1412 | + ctx->data_size; | |
b5a3dfa5 | 1413 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
852c2936 | 1414 | + offsets->size > chan->backend.subbuf_size)) { |
64493e4f MD |
1415 | unsigned long nr_lost; |
1416 | ||
852c2936 MD |
1417 | /* |
1418 | * Record too big for subbuffers, report error, don't | |
1419 | * complete the sub-buffer switch. | |
1420 | */ | |
64493e4f | 1421 | nr_lost = v_read(config, &buf->records_lost_big); |
852c2936 | 1422 | v_inc(config, &buf->records_lost_big); |
64493e4f MD |
1423 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1424 | DBG("%lu or more records lost in (%s:%d) record size " | |
1425 | " of %zu bytes is too large for buffer\n", | |
1426 | nr_lost + 1, chan->backend.name, | |
1427 | buf->backend.cpu, offsets->size); | |
1428 | } | |
852c2936 MD |
1429 | return -ENOSPC; |
1430 | } else { | |
1431 | /* | |
1432 | * We just made a successful buffer switch and the | |
1433 | * record fits in the new subbuffer. Let's write. | |
1434 | */ | |
1435 | } | |
1436 | } else { | |
1437 | /* | |
1438 | * Record fits in the current buffer and we are not on a switch | |
1439 | * boundary. It's safe to write. | |
1440 | */ | |
1441 | } | |
1442 | offsets->end = offsets->begin + offsets->size; | |
1443 | ||
b5a3dfa5 | 1444 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
852c2936 MD |
1445 | /* |
1446 | * The offset_end will fall at the very beginning of the next | |
1447 | * subbuffer. | |
1448 | */ | |
1449 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
1450 | } | |
1451 | return 0; | |
1452 | } | |
1453 | ||
1454 | /** | |
1455 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
1456 | * @ctx: ring buffer context. | |
1457 | * | |
1458 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, | |
1459 | * -EIO for other errors, else returns 0. | |
1460 | * It will take care of sub-buffer switching. | |
1461 | */ | |
4cfec15c | 1462 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 MD |
1463 | { |
1464 | struct channel *chan = ctx->chan; | |
38fae1d3 | 1465 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c MD |
1466 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
1467 | struct lttng_ust_lib_ring_buffer *buf; | |
852c2936 MD |
1468 | struct switch_offsets offsets; |
1469 | int ret; | |
1470 | ||
1471 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 1472 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 1473 | else |
1d498196 | 1474 | buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 MD |
1475 | ctx->buf = buf; |
1476 | ||
1477 | offsets.size = 0; | |
1478 | ||
1479 | do { | |
1480 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, | |
1481 | ctx); | |
b5a3dfa5 | 1482 | if (caa_unlikely(ret)) |
852c2936 | 1483 | return ret; |
b5a3dfa5 | 1484 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
852c2936 MD |
1485 | offsets.end) |
1486 | != offsets.old)); | |
1487 | ||
1488 | /* | |
1489 | * Atomically update last_tsc. This update races against concurrent | |
1490 | * atomic updates, but the race will always cause supplementary full TSC | |
1491 | * records, never the opposite (missing a full TSC record when it would | |
1492 | * be needed). | |
1493 | */ | |
1494 | save_last_tsc(config, buf, ctx->tsc); | |
1495 | ||
1496 | /* | |
1497 | * Push the reader if necessary | |
1498 | */ | |
1499 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
1500 | ||
1501 | /* | |
1502 | * Clear noref flag for this subbuffer. | |
1503 | */ | |
1504 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
1505 | subbuf_index(offsets.end - 1, chan), |
1506 | handle); | |
852c2936 MD |
1507 | |
1508 | /* | |
1509 | * Switch old subbuffer if needed. | |
1510 | */ | |
b5a3dfa5 | 1511 | if (caa_unlikely(offsets.switch_old_end)) { |
852c2936 | 1512 | lib_ring_buffer_clear_noref(config, &buf->backend, |
1d498196 MD |
1513 | subbuf_index(offsets.old - 1, chan), |
1514 | handle); | |
1515 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); | |
852c2936 MD |
1516 | } |
1517 | ||
1518 | /* | |
1519 | * Populate new subbuffer. | |
1520 | */ | |
b5a3dfa5 | 1521 | if (caa_unlikely(offsets.switch_new_start)) |
1d498196 | 1522 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 | 1523 | |
b5a3dfa5 | 1524 | if (caa_unlikely(offsets.switch_new_end)) |
1d498196 | 1525 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 MD |
1526 | |
1527 | ctx->slot_size = offsets.size; | |
1528 | ctx->pre_offset = offsets.begin; | |
1529 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
1530 | return 0; | |
1531 | } | |
f645cfa7 MD |
1532 | |
1533 | /* | |
1534 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
1535 | */ | |
1536 | void lttng_fixup_ringbuffer_tls(void) | |
1537 | { | |
1538 | asm volatile ("" : : "m" (lib_ring_buffer_nesting)); | |
1539 | } |