2 * ring_buffer_backend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
24 #include <urcu/arch.h>
27 #include <lttng/ringbuffer-config.h>
35 * lib_ring_buffer_backend_allocate - allocate a channel buffer
36 * @config: ring buffer instance configuration
37 * @buf: the buffer struct
38 * @size: total size of the buffer
39 * @num_subbuf: number of subbuffers
40 * @extra_reader_sb: need extra subbuffer for reader
43 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config
*config
,
44 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
45 size_t size
, size_t num_subbuf
,
47 struct lttng_ust_shm_handle
*handle
,
48 struct shm_object
*shmobj
)
50 struct channel_backend
*chanb
;
51 unsigned long subbuf_size
, mmap_offset
= 0;
52 unsigned long num_subbuf_alloc
;
56 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
60 subbuf_size
= chanb
->subbuf_size
;
61 num_subbuf_alloc
= num_subbuf
;
66 page_size
= sysconf(_SC_PAGE_SIZE
);
71 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
72 set_shmp(bufb
->array
, zalloc_shm(shmobj
,
73 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
));
74 if (caa_unlikely(!shmp(handle
, bufb
->array
)))
78 * This is the largest element (the buffer pages) which needs to
79 * be aligned on page size.
81 align_shm(shmobj
, page_size
);
82 set_shmp(bufb
->memory_map
, zalloc_shm(shmobj
,
83 subbuf_size
* num_subbuf_alloc
));
84 if (caa_unlikely(!shmp(handle
, bufb
->memory_map
)))
85 goto memory_map_error
;
87 /* Allocate backend pages array elements */
88 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
89 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
90 set_shmp(shmp_index(handle
, bufb
->array
, i
)->shmp
,
92 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
)));
93 if (!shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
))
97 /* Allocate write-side subbuffer table */
98 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
99 set_shmp(bufb
->buf_wsb
, zalloc_shm(shmobj
,
100 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
)
102 if (caa_unlikely(!shmp(handle
, bufb
->buf_wsb
)))
105 for (i
= 0; i
< num_subbuf
; i
++) {
106 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
108 sb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
111 sb
->id
= subbuffer_id(config
, 0, 1, i
);
114 /* Assign read-side subbuffer table */
116 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
117 num_subbuf_alloc
- 1);
119 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
121 /* Allocate subbuffer packet counter table */
122 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_counts
));
123 set_shmp(bufb
->buf_cnt
, zalloc_shm(shmobj
,
124 sizeof(struct lttng_ust_lib_ring_buffer_backend_counts
)
126 if (caa_unlikely(!shmp(handle
, bufb
->buf_cnt
)))
129 /* Assign pages to page index */
130 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
131 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*sbp
;
132 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
135 ref
.index
= bufb
->memory_map
._ref
.index
;
136 ref
.offset
= bufb
->memory_map
._ref
.offset
;
137 ref
.offset
+= i
* subbuf_size
;
139 sbp
= shmp_index(handle
, bufb
->array
, i
);
142 pages
= shmp(handle
, sbp
->shmp
);
145 set_shmp(pages
->p
, ref
);
146 if (config
->output
== RING_BUFFER_MMAP
) {
147 pages
->mmap_offset
= mmap_offset
;
148 mmap_offset
+= subbuf_size
;
154 /* bufb->buf_wsb will be freed by shm teardown */
156 /* bufb->array[i] will be freed by shm teardown */
158 /* bufb->array will be freed by shm teardown */
164 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
165 struct channel_backend
*chanb
, int cpu
,
166 struct lttng_ust_shm_handle
*handle
,
167 struct shm_object
*shmobj
)
169 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
171 set_shmp(bufb
->chan
, handle
->chan
._ref
);
174 return lib_ring_buffer_backend_allocate(config
, bufb
, chanb
->buf_size
,
176 chanb
->extra_reader_sb
,
180 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
181 struct lttng_ust_shm_handle
*handle
)
183 struct channel_backend
*chanb
;
184 const struct lttng_ust_lib_ring_buffer_config
*config
;
185 unsigned long num_subbuf_alloc
;
188 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
191 config
= &chanb
->config
;
193 num_subbuf_alloc
= chanb
->num_subbuf
;
194 if (chanb
->extra_reader_sb
)
197 for (i
= 0; i
< chanb
->num_subbuf
; i
++) {
198 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
200 sb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
203 sb
->id
= subbuffer_id(config
, 0, 1, i
);
205 if (chanb
->extra_reader_sb
)
206 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
207 num_subbuf_alloc
- 1);
209 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
211 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
212 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*sbp
;
213 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
215 sbp
= shmp_index(handle
, bufb
->array
, i
);
218 pages
= shmp(handle
, sbp
->shmp
);
221 /* Don't reset mmap_offset */
222 v_set(config
, &pages
->records_commit
, 0);
223 v_set(config
, &pages
->records_unread
, 0);
224 pages
->data_size
= 0;
225 /* Don't reset backend page and virt addresses */
227 /* Don't reset num_pages_per_subbuf, cpu, allocated */
228 v_set(config
, &bufb
->records_read
, 0);
232 * The frontend is responsible for also calling ring_buffer_backend_reset for
233 * each buffer when calling channel_backend_reset.
235 void channel_backend_reset(struct channel_backend
*chanb
)
237 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
238 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
241 * Don't reset buf_size, subbuf_size, subbuf_size_order,
242 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
243 * priv, notifiers, config, cpumask and name.
245 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
249 * channel_backend_init - initialize a channel backend
250 * @chanb: channel backend
251 * @name: channel name
252 * @config: client ring buffer configuration
253 * @parent: dentry of parent directory, %NULL for root directory
254 * @subbuf_size: size of sub-buffers (> page size, power of 2)
255 * @num_subbuf: number of sub-buffers (power of 2)
256 * @lttng_ust_shm_handle: shared memory handle
257 * @stream_fds: stream file descriptors.
259 * Returns channel pointer if successful, %NULL otherwise.
261 * Creates per-cpu channel buffers using the sizes and attributes
262 * specified. The created channel buffer files will be named
263 * name_0...name_N-1. File permissions will be %S_IRUSR.
265 * Called with CPU hotplug disabled.
267 int channel_backend_init(struct channel_backend
*chanb
,
269 const struct lttng_ust_lib_ring_buffer_config
*config
,
270 size_t subbuf_size
, size_t num_subbuf
,
271 struct lttng_ust_shm_handle
*handle
,
272 const int *stream_fds
)
274 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
277 size_t shmsize
= 0, num_subbuf_alloc
;
283 page_size
= sysconf(_SC_PAGE_SIZE
);
284 if (page_size
<= 0) {
287 /* Check that the subbuffer size is larger than a page. */
288 if (subbuf_size
< page_size
)
292 * Make sure the number of subbuffers and subbuffer size are
293 * power of 2, and nonzero.
295 if (!subbuf_size
|| (subbuf_size
& (subbuf_size
- 1)))
297 if (!num_subbuf
|| (num_subbuf
& (num_subbuf
- 1)))
300 * Overwrite mode buffers require at least 2 subbuffers per
303 if (config
->mode
== RING_BUFFER_OVERWRITE
&& num_subbuf
< 2)
306 ret
= subbuffer_id_check_index(config
, num_subbuf
);
310 chanb
->buf_size
= num_subbuf
* subbuf_size
;
311 chanb
->subbuf_size
= subbuf_size
;
312 chanb
->buf_size_order
= get_count_order(chanb
->buf_size
);
313 chanb
->subbuf_size_order
= get_count_order(subbuf_size
);
314 chanb
->num_subbuf_order
= get_count_order(num_subbuf
);
315 chanb
->extra_reader_sb
=
316 (config
->mode
== RING_BUFFER_OVERWRITE
) ? 1 : 0;
317 chanb
->num_subbuf
= num_subbuf
;
318 strncpy(chanb
->name
, name
, NAME_MAX
);
319 chanb
->name
[NAME_MAX
- 1] = '\0';
320 memcpy(&chanb
->config
, config
, sizeof(*config
));
322 /* Per-cpu buffer size: control (prior to backend) */
323 shmsize
= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer
));
324 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer
);
326 /* Per-cpu buffer size: backend */
327 /* num_subbuf + 1 is the worse case */
328 num_subbuf_alloc
= num_subbuf
+ 1;
329 shmsize
+= 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
+= offset_align(shmsize
, page_size
);
332 shmsize
+= subbuf_size
* num_subbuf_alloc
;
333 shmsize
+= 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
+= 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
+= 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
;
339 /* Per-cpu buffer size: control (after backend) */
340 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_hot
));
341 shmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
342 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_cold
));
343 shmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
345 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
346 struct lttng_ust_lib_ring_buffer
*buf
;
348 * We need to allocate for all possible cpus.
350 for_each_possible_cpu(i
) {
351 struct shm_object
*shmobj
;
353 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
354 SHM_OBJECT_SHM
, stream_fds
[i
]);
357 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
358 set_shmp(chanb
->buf
[i
].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
359 buf
= shmp(handle
, chanb
->buf
[i
].shmp
);
362 set_shmp(buf
->self
, chanb
->buf
[i
].shmp
._ref
);
363 ret
= lib_ring_buffer_create(buf
, chanb
, i
,
366 goto free_bufs
; /* cpu hotplug locked */
369 struct shm_object
*shmobj
;
370 struct lttng_ust_lib_ring_buffer
*buf
;
372 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
373 SHM_OBJECT_SHM
, stream_fds
[0]);
376 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
377 set_shmp(chanb
->buf
[0].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
378 buf
= shmp(handle
, chanb
->buf
[0].shmp
);
381 set_shmp(buf
->self
, chanb
->buf
[0].shmp
._ref
);
382 ret
= lib_ring_buffer_create(buf
, chanb
, -1,
387 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
392 /* We only free the buffer data upon shm teardown */
398 * channel_backend_free - destroy the channel
401 * Destroy all channel buffers and frees the channel.
403 void channel_backend_free(struct channel_backend
*chanb
,
404 struct lttng_ust_shm_handle
*handle
)
406 /* SHM teardown takes care of everything */
410 * lib_ring_buffer_read - read data from ring_buffer_buffer.
411 * @bufb : buffer backend
412 * @offset : offset within the buffer
413 * @dest : destination address
414 * @len : length to copy to destination
416 * Should be protected by get_subbuf/put_subbuf.
417 * Returns the length copied.
419 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
420 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
422 struct channel_backend
*chanb
;
423 const struct lttng_ust_lib_ring_buffer_config
*config
;
425 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
426 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
427 unsigned long sb_bindex
, id
;
430 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
433 config
= &chanb
->config
;
435 offset
&= chanb
->buf_size
- 1;
437 if (caa_unlikely(!len
))
439 id
= bufb
->buf_rsb
.id
;
440 sb_bindex
= subbuffer_id_get_index(config
, id
);
441 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
445 * Underlying layer should never ask for reads across
448 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
449 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
450 && subbuffer_id_is_noref(config
, id
));
451 backend_pages
= shmp(handle
, rpages
->shmp
);
454 src
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
455 if (caa_unlikely(!src
))
457 memcpy(dest
, src
, len
);
462 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
463 * @bufb : buffer backend
464 * @offset : offset within the buffer
465 * @dest : destination address
466 * @len : destination's length
468 * Return string's length, or -EINVAL on error.
469 * Should be protected by get_subbuf/put_subbuf.
470 * Destination length should be at least 1 to hold '\0'.
472 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
473 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
475 struct channel_backend
*chanb
;
476 const struct lttng_ust_lib_ring_buffer_config
*config
;
477 ssize_t string_len
, orig_offset
;
479 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
480 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
481 unsigned long sb_bindex
, id
;
483 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
486 config
= &chanb
->config
;
487 if (caa_unlikely(!len
))
489 offset
&= chanb
->buf_size
- 1;
490 orig_offset
= offset
;
491 id
= bufb
->buf_rsb
.id
;
492 sb_bindex
= subbuffer_id_get_index(config
, id
);
493 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
497 * Underlying layer should never ask for reads across
500 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
501 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
502 && subbuffer_id_is_noref(config
, id
));
503 backend_pages
= shmp(handle
, rpages
->shmp
);
506 str
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
507 if (caa_unlikely(!str
))
509 string_len
= strnlen(str
, len
);
511 memcpy(dest
, str
, string_len
);
512 ((char *)dest
)[0] = 0;
514 return offset
- orig_offset
;
518 * lib_ring_buffer_read_offset_address - get address of a buffer location
519 * @bufb : buffer backend
520 * @offset : offset within the buffer.
522 * Return the address where a given offset is located (for read).
523 * Should be used to get the current subbuffer header pointer. Given we know
524 * it's never on a page boundary, it's safe to read/write directly
525 * from/to this address, as long as the read/write is never bigger than
528 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
530 struct lttng_ust_shm_handle
*handle
)
532 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
533 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
534 struct channel_backend
*chanb
;
535 const struct lttng_ust_lib_ring_buffer_config
*config
;
536 unsigned long sb_bindex
, id
;
538 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
541 config
= &chanb
->config
;
542 offset
&= chanb
->buf_size
- 1;
543 id
= bufb
->buf_rsb
.id
;
544 sb_bindex
= subbuffer_id_get_index(config
, id
);
545 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
548 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
549 && subbuffer_id_is_noref(config
, id
));
550 backend_pages
= shmp(handle
, rpages
->shmp
);
553 return shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
557 * lib_ring_buffer_offset_address - get address of a location within the buffer
558 * @bufb : buffer backend
559 * @offset : offset within the buffer.
561 * Return the address where a given offset is located.
562 * Should be used to get the current subbuffer header pointer. Given we know
563 * it's always at the beginning of a page, it's safe to write directly to this
564 * address, as long as the write is never bigger than a page size.
566 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
568 struct lttng_ust_shm_handle
*handle
)
571 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
572 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
573 struct channel_backend
*chanb
;
574 const struct lttng_ust_lib_ring_buffer_config
*config
;
575 unsigned long sb_bindex
, id
;
576 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
578 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
581 config
= &chanb
->config
;
582 offset
&= chanb
->buf_size
- 1;
583 sbidx
= offset
>> chanb
->subbuf_size_order
;
584 sb
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
);
588 sb_bindex
= subbuffer_id_get_index(config
, id
);
589 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
592 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
593 && subbuffer_id_is_noref(config
, id
));
594 backend_pages
= shmp(handle
, rpages
->shmp
);
597 return shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));