2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 #include <urcu/arch.h>
14 #include <lttng/ust-utils.h>
15 #include <lttng/ringbuffer-context.h>
17 #include "ringbuffer-config.h"
23 #include "ust-compat.h"
26 * lib_ring_buffer_backend_allocate - allocate a channel buffer
27 * @config: ring buffer instance configuration
28 * @buf: the buffer struct
29 * @size: total size of the buffer
30 * @num_subbuf: number of subbuffers
31 * @extra_reader_sb: need extra subbuffer for reader
34 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config
*config
,
35 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
36 size_t size
__attribute__((unused
)), size_t num_subbuf
,
38 struct lttng_ust_shm_handle
*handle
,
39 struct shm_object
*shmobj
)
41 struct channel_backend
*chanb
;
42 unsigned long subbuf_size
, mmap_offset
= 0;
43 unsigned long num_subbuf_alloc
;
47 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
51 subbuf_size
= chanb
->subbuf_size
;
52 num_subbuf_alloc
= num_subbuf
;
57 page_size
= LTTNG_UST_PAGE_SIZE
;
62 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
63 set_shmp(bufb
->array
, zalloc_shm(shmobj
,
64 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
));
65 if (caa_unlikely(!shmp(handle
, bufb
->array
)))
69 * This is the largest element (the buffer pages) which needs to
70 * be aligned on page size.
72 align_shm(shmobj
, page_size
);
73 set_shmp(bufb
->memory_map
, zalloc_shm(shmobj
,
74 subbuf_size
* num_subbuf_alloc
));
75 if (caa_unlikely(!shmp(handle
, bufb
->memory_map
)))
76 goto memory_map_error
;
78 /* Allocate backend pages array elements */
79 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
80 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
81 set_shmp(shmp_index(handle
, bufb
->array
, i
)->shmp
,
83 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
)));
84 if (!shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
))
88 /* Allocate write-side subbuffer table */
89 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
90 set_shmp(bufb
->buf_wsb
, zalloc_shm(shmobj
,
91 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
)
93 if (caa_unlikely(!shmp(handle
, bufb
->buf_wsb
)))
96 for (i
= 0; i
< num_subbuf
; i
++) {
97 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
99 sb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
102 sb
->id
= subbuffer_id(config
, 0, 1, i
);
105 /* Assign read-side subbuffer table */
107 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
108 num_subbuf_alloc
- 1);
110 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
112 /* Allocate subbuffer packet counter table */
113 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_counts
));
114 set_shmp(bufb
->buf_cnt
, zalloc_shm(shmobj
,
115 sizeof(struct lttng_ust_lib_ring_buffer_backend_counts
)
117 if (caa_unlikely(!shmp(handle
, bufb
->buf_cnt
)))
120 /* Assign pages to page index */
121 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
122 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*sbp
;
123 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
126 ref
.index
= bufb
->memory_map
._ref
.index
;
127 ref
.offset
= bufb
->memory_map
._ref
.offset
;
128 ref
.offset
+= i
* subbuf_size
;
130 sbp
= shmp_index(handle
, bufb
->array
, i
);
133 pages
= shmp(handle
, sbp
->shmp
);
136 set_shmp(pages
->p
, ref
);
137 if (config
->output
== RING_BUFFER_MMAP
) {
138 pages
->mmap_offset
= mmap_offset
;
139 mmap_offset
+= subbuf_size
;
145 /* bufb->buf_wsb will be freed by shm teardown */
147 /* bufb->array[i] will be freed by shm teardown */
149 /* bufb->array will be freed by shm teardown */
155 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
156 struct channel_backend
*chanb
, int cpu
,
157 struct lttng_ust_shm_handle
*handle
,
158 struct shm_object
*shmobj
)
160 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
162 set_shmp(bufb
->chan
, handle
->chan
._ref
);
165 return lib_ring_buffer_backend_allocate(config
, bufb
, chanb
->buf_size
,
167 chanb
->extra_reader_sb
,
171 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
172 struct lttng_ust_shm_handle
*handle
)
174 struct channel_backend
*chanb
;
175 const struct lttng_ust_lib_ring_buffer_config
*config
;
176 unsigned long num_subbuf_alloc
;
179 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
182 config
= &chanb
->config
;
184 num_subbuf_alloc
= chanb
->num_subbuf
;
185 if (chanb
->extra_reader_sb
)
188 for (i
= 0; i
< chanb
->num_subbuf
; i
++) {
189 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
191 sb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
194 sb
->id
= subbuffer_id(config
, 0, 1, i
);
196 if (chanb
->extra_reader_sb
)
197 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
198 num_subbuf_alloc
- 1);
200 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
202 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
203 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*sbp
;
204 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
206 sbp
= shmp_index(handle
, bufb
->array
, i
);
209 pages
= shmp(handle
, sbp
->shmp
);
212 /* Don't reset mmap_offset */
213 v_set(config
, &pages
->records_commit
, 0);
214 v_set(config
, &pages
->records_unread
, 0);
215 pages
->data_size
= 0;
216 /* Don't reset backend page and virt addresses */
218 /* Don't reset num_pages_per_subbuf, cpu, allocated */
219 v_set(config
, &bufb
->records_read
, 0);
223 * The frontend is responsible for also calling ring_buffer_backend_reset for
224 * each buffer when calling channel_backend_reset.
226 void channel_backend_reset(struct channel_backend
*chanb
)
228 struct lttng_ust_lib_ring_buffer_channel
*chan
= caa_container_of(chanb
,
229 struct lttng_ust_lib_ring_buffer_channel
, backend
);
230 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
233 * Don't reset buf_size, subbuf_size, subbuf_size_order,
234 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
235 * priv, notifiers, config, cpumask and name.
237 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
241 * channel_backend_init - initialize a channel backend
242 * @chanb: channel backend
243 * @name: channel name
244 * @config: client ring buffer configuration
245 * @parent: dentry of parent directory, %NULL for root directory
246 * @subbuf_size: size of sub-buffers (> page size, power of 2)
247 * @num_subbuf: number of sub-buffers (power of 2)
248 * @lttng_ust_shm_handle: shared memory handle
249 * @stream_fds: stream file descriptors.
251 * Returns channel pointer if successful, %NULL otherwise.
253 * Creates per-cpu channel buffers using the sizes and attributes
254 * specified. The created channel buffer files will be named
255 * name_0...name_N-1. File permissions will be %S_IRUSR.
257 * Called with CPU hotplug disabled.
259 int channel_backend_init(struct channel_backend
*chanb
,
261 const struct lttng_ust_lib_ring_buffer_config
*config
,
262 size_t subbuf_size
, size_t num_subbuf
,
263 struct lttng_ust_shm_handle
*handle
,
264 const int *stream_fds
)
266 struct lttng_ust_lib_ring_buffer_channel
*chan
= caa_container_of(chanb
,
267 struct lttng_ust_lib_ring_buffer_channel
, backend
);
270 size_t shmsize
= 0, num_subbuf_alloc
;
276 page_size
= LTTNG_UST_PAGE_SIZE
;
277 if (page_size
<= 0) {
280 /* Check that the subbuffer size is larger than a page. */
281 if (subbuf_size
< page_size
)
285 * Make sure the number of subbuffers and subbuffer size are
286 * power of 2, and nonzero.
288 if (!subbuf_size
|| (subbuf_size
& (subbuf_size
- 1)))
290 if (!num_subbuf
|| (num_subbuf
& (num_subbuf
- 1)))
293 * Overwrite mode buffers require at least 2 subbuffers per
296 if (config
->mode
== RING_BUFFER_OVERWRITE
&& num_subbuf
< 2)
299 ret
= subbuffer_id_check_index(config
, num_subbuf
);
303 chanb
->buf_size
= num_subbuf
* subbuf_size
;
304 chanb
->subbuf_size
= subbuf_size
;
305 chanb
->buf_size_order
= get_count_order(chanb
->buf_size
);
306 chanb
->subbuf_size_order
= get_count_order(subbuf_size
);
307 chanb
->num_subbuf_order
= get_count_order(num_subbuf
);
308 chanb
->extra_reader_sb
=
309 (config
->mode
== RING_BUFFER_OVERWRITE
) ? 1 : 0;
310 chanb
->num_subbuf
= num_subbuf
;
311 strncpy(chanb
->name
, name
, NAME_MAX
);
312 chanb
->name
[NAME_MAX
- 1] = '\0';
313 memcpy(&chanb
->config
, config
, sizeof(*config
));
315 /* Per-cpu buffer size: control (prior to backend) */
316 shmsize
= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer
));
317 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer
);
318 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct commit_counters_hot
));
319 shmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
320 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct commit_counters_cold
));
321 shmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
322 /* Sampled timestamp end */
323 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(uint64_t));
324 shmsize
+= sizeof(uint64_t) * num_subbuf
;
326 /* Per-cpu buffer size: backend */
327 /* num_subbuf + 1 is the worse case */
328 num_subbuf_alloc
= num_subbuf
+ 1;
329 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
330 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
;
331 shmsize
+= lttng_ust_offset_align(shmsize
, page_size
);
332 shmsize
+= subbuf_size
* num_subbuf_alloc
;
333 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
334 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
335 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
336 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
337 shmsize
+= lttng_ust_offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_counts
));
338 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_counts
) * num_subbuf
;
340 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
341 struct lttng_ust_lib_ring_buffer
*buf
;
343 * We need to allocate for all possible cpus.
345 for_each_possible_cpu(i
) {
346 struct shm_object
*shmobj
;
348 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
349 SHM_OBJECT_SHM
, stream_fds
[i
], i
);
352 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
353 set_shmp(chanb
->buf
[i
].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
354 buf
= shmp(handle
, chanb
->buf
[i
].shmp
);
357 set_shmp(buf
->self
, chanb
->buf
[i
].shmp
._ref
);
358 ret
= lib_ring_buffer_create(buf
, chanb
, i
,
361 goto free_bufs
; /* cpu hotplug locked */
364 struct shm_object
*shmobj
;
365 struct lttng_ust_lib_ring_buffer
*buf
;
367 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
368 SHM_OBJECT_SHM
, stream_fds
[0], -1);
371 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
372 set_shmp(chanb
->buf
[0].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
373 buf
= shmp(handle
, chanb
->buf
[0].shmp
);
376 set_shmp(buf
->self
, chanb
->buf
[0].shmp
._ref
);
377 ret
= lib_ring_buffer_create(buf
, chanb
, -1,
382 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
387 /* We only free the buffer data upon shm teardown */
393 * channel_backend_free - destroy the channel
396 * Destroy all channel buffers and frees the channel.
398 void channel_backend_free(struct channel_backend
*chanb
__attribute__((unused
)),
399 struct lttng_ust_shm_handle
*handle
__attribute__((unused
)))
401 /* SHM teardown takes care of everything */
405 * lib_ring_buffer_read - read data from ring_buffer_buffer.
406 * @bufb : buffer backend
407 * @offset : offset within the buffer
408 * @dest : destination address
409 * @len : length to copy to destination
411 * Should be protected by get_subbuf/put_subbuf.
412 * Returns the length copied.
414 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
415 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
417 struct channel_backend
*chanb
;
418 const struct lttng_ust_lib_ring_buffer_config
*config
;
420 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
421 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
422 unsigned long sb_bindex
, id
;
425 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
428 config
= &chanb
->config
;
430 offset
&= chanb
->buf_size
- 1;
432 if (caa_unlikely(!len
))
434 id
= bufb
->buf_rsb
.id
;
435 sb_bindex
= subbuffer_id_get_index(config
, id
);
436 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
440 * Underlying layer should never ask for reads across
443 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
444 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
445 && subbuffer_id_is_noref(config
, id
));
446 backend_pages
= shmp(handle
, rpages
->shmp
);
449 src
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
450 if (caa_unlikely(!src
))
452 memcpy(dest
, src
, len
);
457 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
458 * @bufb : buffer backend
459 * @offset : offset within the buffer
460 * @dest : destination address
461 * @len : destination's length
463 * Return string's length, or -EINVAL on error.
464 * Should be protected by get_subbuf/put_subbuf.
465 * Destination length should be at least 1 to hold '\0'.
467 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
468 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
470 struct channel_backend
*chanb
;
471 const struct lttng_ust_lib_ring_buffer_config
*config
;
472 ssize_t string_len
, orig_offset
;
474 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
475 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
476 unsigned long sb_bindex
, id
;
478 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
481 config
= &chanb
->config
;
482 if (caa_unlikely(!len
))
484 offset
&= chanb
->buf_size
- 1;
485 orig_offset
= offset
;
486 id
= bufb
->buf_rsb
.id
;
487 sb_bindex
= subbuffer_id_get_index(config
, id
);
488 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
492 * Underlying layer should never ask for reads across
495 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
496 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
497 && subbuffer_id_is_noref(config
, id
));
498 backend_pages
= shmp(handle
, rpages
->shmp
);
501 str
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
502 if (caa_unlikely(!str
))
504 string_len
= strnlen(str
, len
);
506 memcpy(dest
, str
, string_len
);
507 ((char *)dest
)[0] = 0;
509 return offset
- orig_offset
;
513 * lib_ring_buffer_read_offset_address - get address of a buffer location
514 * @bufb : buffer backend
515 * @offset : offset within the buffer.
517 * Return the address where a given offset is located (for read).
518 * Should be used to get the current subbuffer header pointer. Given we know
519 * it's never on a page boundary, it's safe to read/write directly
520 * from/to this address, as long as the read/write is never bigger than
523 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
525 struct lttng_ust_shm_handle
*handle
)
527 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
528 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
529 struct channel_backend
*chanb
;
530 const struct lttng_ust_lib_ring_buffer_config
*config
;
531 unsigned long sb_bindex
, id
;
533 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
536 config
= &chanb
->config
;
537 offset
&= chanb
->buf_size
- 1;
538 id
= bufb
->buf_rsb
.id
;
539 sb_bindex
= subbuffer_id_get_index(config
, id
);
540 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
543 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
544 && subbuffer_id_is_noref(config
, id
));
545 backend_pages
= shmp(handle
, rpages
->shmp
);
548 return shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
552 * lib_ring_buffer_offset_address - get address of a location within the buffer
553 * @bufb : buffer backend
554 * @offset : offset within the buffer.
556 * Return the address where a given offset is located.
557 * Should be used to get the current subbuffer header pointer. Given we know
558 * it's always at the beginning of a page, it's safe to write directly to this
559 * address, as long as the write is never bigger than a page size.
561 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
563 struct lttng_ust_shm_handle
*handle
)
566 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
567 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
568 struct channel_backend
*chanb
;
569 const struct lttng_ust_lib_ring_buffer_config
*config
;
570 unsigned long sb_bindex
, id
;
571 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
573 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
576 config
= &chanb
->config
;
577 offset
&= chanb
->buf_size
- 1;
578 sbidx
= offset
>> chanb
->subbuf_size_order
;
579 sb
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
);
583 sb_bindex
= subbuffer_id_get_index(config
, id
);
584 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
587 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
588 && subbuffer_id_is_noref(config
, id
));
589 backend_pages
= shmp(handle
, rpages
->shmp
);
592 return shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));