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
25 #include <urcu/arch.h>
28 #include <lttng/ringbuffer-config.h>
36 * lib_ring_buffer_backend_allocate - allocate a channel buffer
37 * @config: ring buffer instance configuration
38 * @buf: the buffer struct
39 * @size: total size of the buffer
40 * @num_subbuf: number of subbuffers
41 * @extra_reader_sb: need extra subbuffer for reader
44 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config
*config
,
45 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
46 size_t size
, size_t num_subbuf
,
48 struct lttng_ust_shm_handle
*handle
,
49 struct shm_object
*shmobj
)
51 struct channel_backend
*chanb
;
52 unsigned long subbuf_size
, mmap_offset
= 0;
53 unsigned long num_subbuf_alloc
;
57 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
61 subbuf_size
= chanb
->subbuf_size
;
62 num_subbuf_alloc
= num_subbuf
;
67 page_size
= sysconf(_SC_PAGE_SIZE
);
72 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
73 set_shmp(bufb
->array
, zalloc_shm(shmobj
,
74 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
));
75 if (caa_unlikely(!shmp(handle
, bufb
->array
)))
79 * This is the largest element (the buffer pages) which needs to
80 * be aligned on page size.
82 align_shm(shmobj
, page_size
);
83 set_shmp(bufb
->memory_map
, zalloc_shm(shmobj
,
84 subbuf_size
* num_subbuf_alloc
));
85 if (caa_unlikely(!shmp(handle
, bufb
->memory_map
)))
86 goto memory_map_error
;
88 /* Allocate backend pages array elements */
89 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
90 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
91 set_shmp(shmp_index(handle
, bufb
->array
, i
)->shmp
,
93 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
)));
94 if (!shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
))
98 /* Allocate write-side subbuffer table */
99 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
100 set_shmp(bufb
->buf_wsb
, zalloc_shm(shmobj
,
101 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
)
103 if (caa_unlikely(!shmp(handle
, bufb
->buf_wsb
)))
106 for (i
= 0; i
< num_subbuf
; i
++) {
107 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
109 sb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
112 sb
->id
= subbuffer_id(config
, 0, 1, i
);
115 /* Assign read-side subbuffer table */
117 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
118 num_subbuf_alloc
- 1);
120 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
122 /* Allocate subbuffer packet counter table */
123 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_counts
));
124 set_shmp(bufb
->buf_cnt
, zalloc_shm(shmobj
,
125 sizeof(struct lttng_ust_lib_ring_buffer_backend_counts
)
127 if (caa_unlikely(!shmp(handle
, bufb
->buf_cnt
)))
130 /* Assign pages to page index */
131 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
132 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*sbp
;
133 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
136 ref
.index
= bufb
->memory_map
._ref
.index
;
137 ref
.offset
= bufb
->memory_map
._ref
.offset
;
138 ref
.offset
+= i
* subbuf_size
;
140 sbp
= shmp_index(handle
, bufb
->array
, i
);
143 pages
= shmp(handle
, sbp
->shmp
);
146 set_shmp(pages
->p
, ref
);
147 if (config
->output
== RING_BUFFER_MMAP
) {
148 pages
->mmap_offset
= mmap_offset
;
149 mmap_offset
+= subbuf_size
;
155 /* bufb->buf_wsb will be freed by shm teardown */
157 /* bufb->array[i] will be freed by shm teardown */
159 /* bufb->array will be freed by shm teardown */
165 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
166 struct channel_backend
*chanb
, int cpu
,
167 struct lttng_ust_shm_handle
*handle
,
168 struct shm_object
*shmobj
)
170 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
172 set_shmp(bufb
->chan
, handle
->chan
._ref
);
175 return lib_ring_buffer_backend_allocate(config
, bufb
, chanb
->buf_size
,
177 chanb
->extra_reader_sb
,
181 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
182 struct lttng_ust_shm_handle
*handle
)
184 struct channel_backend
*chanb
;
185 const struct lttng_ust_lib_ring_buffer_config
*config
;
186 unsigned long num_subbuf_alloc
;
189 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
192 config
= &chanb
->config
;
194 num_subbuf_alloc
= chanb
->num_subbuf
;
195 if (chanb
->extra_reader_sb
)
198 for (i
= 0; i
< chanb
->num_subbuf
; i
++) {
199 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
201 sb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
204 sb
->id
= subbuffer_id(config
, 0, 1, i
);
206 if (chanb
->extra_reader_sb
)
207 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
208 num_subbuf_alloc
- 1);
210 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
212 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
213 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*sbp
;
214 struct lttng_ust_lib_ring_buffer_backend_pages
*pages
;
216 sbp
= shmp_index(handle
, bufb
->array
, i
);
219 pages
= shmp(handle
, sbp
->shmp
);
222 /* Don't reset mmap_offset */
223 v_set(config
, &pages
->records_commit
, 0);
224 v_set(config
, &pages
->records_unread
, 0);
225 pages
->data_size
= 0;
226 /* Don't reset backend page and virt addresses */
228 /* Don't reset num_pages_per_subbuf, cpu, allocated */
229 v_set(config
, &bufb
->records_read
, 0);
233 * The frontend is responsible for also calling ring_buffer_backend_reset for
234 * each buffer when calling channel_backend_reset.
236 void channel_backend_reset(struct channel_backend
*chanb
)
238 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
239 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
242 * Don't reset buf_size, subbuf_size, subbuf_size_order,
243 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
244 * priv, notifiers, config, cpumask and name.
246 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
250 * channel_backend_init - initialize a channel backend
251 * @chanb: channel backend
252 * @name: channel name
253 * @config: client ring buffer configuration
254 * @parent: dentry of parent directory, %NULL for root directory
255 * @subbuf_size: size of sub-buffers (> page size, power of 2)
256 * @num_subbuf: number of sub-buffers (power of 2)
257 * @lttng_ust_shm_handle: shared memory handle
258 * @stream_fds: stream file descriptors.
260 * Returns channel pointer if successful, %NULL otherwise.
262 * Creates per-cpu channel buffers using the sizes and attributes
263 * specified. The created channel buffer files will be named
264 * name_0...name_N-1. File permissions will be %S_IRUSR.
266 * Called with CPU hotplug disabled.
268 int channel_backend_init(struct channel_backend
*chanb
,
270 const struct lttng_ust_lib_ring_buffer_config
*config
,
271 size_t subbuf_size
, size_t num_subbuf
,
272 struct lttng_ust_shm_handle
*handle
,
273 const int *stream_fds
)
275 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
278 size_t shmsize
= 0, num_subbuf_alloc
;
284 page_size
= sysconf(_SC_PAGE_SIZE
);
285 if (page_size
<= 0) {
288 /* Check that the subbuffer size is larger than a page. */
289 if (subbuf_size
< page_size
)
293 * Make sure the number of subbuffers and subbuffer size are
294 * power of 2, and nonzero.
296 if (!subbuf_size
|| (subbuf_size
& (subbuf_size
- 1)))
298 if (!num_subbuf
|| (num_subbuf
& (num_subbuf
- 1)))
301 * Overwrite mode buffers require at least 2 subbuffers per
304 if (config
->mode
== RING_BUFFER_OVERWRITE
&& num_subbuf
< 2)
307 ret
= subbuffer_id_check_index(config
, num_subbuf
);
311 chanb
->buf_size
= num_subbuf
* subbuf_size
;
312 chanb
->subbuf_size
= subbuf_size
;
313 chanb
->buf_size_order
= get_count_order(chanb
->buf_size
);
314 chanb
->subbuf_size_order
= get_count_order(subbuf_size
);
315 chanb
->num_subbuf_order
= get_count_order(num_subbuf
);
316 chanb
->extra_reader_sb
=
317 (config
->mode
== RING_BUFFER_OVERWRITE
) ? 1 : 0;
318 chanb
->num_subbuf
= num_subbuf
;
319 strncpy(chanb
->name
, name
, NAME_MAX
);
320 chanb
->name
[NAME_MAX
- 1] = '\0';
321 memcpy(&chanb
->config
, config
, sizeof(*config
));
323 /* Per-cpu buffer size: control (prior to backend) */
324 shmsize
= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer
));
325 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer
);
326 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_hot
));
327 shmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
328 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_cold
));
329 shmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
330 /* Sampled timestamp end */
331 shmsize
+= offset_align(shmsize
, __alignof__(uint64_t));
332 shmsize
+= sizeof(uint64_t) * num_subbuf
;
334 /* Per-cpu buffer size: backend */
335 /* num_subbuf + 1 is the worse case */
336 num_subbuf_alloc
= num_subbuf
+ 1;
337 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
338 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
;
339 shmsize
+= offset_align(shmsize
, page_size
);
340 shmsize
+= subbuf_size
* num_subbuf_alloc
;
341 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
342 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
343 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
344 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
345 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_counts
));
346 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_counts
) * num_subbuf
;
348 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
349 struct lttng_ust_lib_ring_buffer
*buf
;
351 * We need to allocate for all possible cpus.
353 for_each_possible_cpu(i
) {
354 struct shm_object
*shmobj
;
356 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
357 SHM_OBJECT_SHM
, stream_fds
[i
], i
);
360 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
361 set_shmp(chanb
->buf
[i
].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
362 buf
= shmp(handle
, chanb
->buf
[i
].shmp
);
365 set_shmp(buf
->self
, chanb
->buf
[i
].shmp
._ref
);
366 ret
= lib_ring_buffer_create(buf
, chanb
, i
,
369 goto free_bufs
; /* cpu hotplug locked */
372 struct shm_object
*shmobj
;
373 struct lttng_ust_lib_ring_buffer
*buf
;
375 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
376 SHM_OBJECT_SHM
, stream_fds
[0], -1);
379 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
380 set_shmp(chanb
->buf
[0].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
381 buf
= shmp(handle
, chanb
->buf
[0].shmp
);
384 set_shmp(buf
->self
, chanb
->buf
[0].shmp
._ref
);
385 ret
= lib_ring_buffer_create(buf
, chanb
, -1,
390 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
395 /* We only free the buffer data upon shm teardown */
401 * channel_backend_free - destroy the channel
404 * Destroy all channel buffers and frees the channel.
406 void channel_backend_free(struct channel_backend
*chanb
,
407 struct lttng_ust_shm_handle
*handle
)
409 /* SHM teardown takes care of everything */
413 * lib_ring_buffer_read - read data from ring_buffer_buffer.
414 * @bufb : buffer backend
415 * @offset : offset within the buffer
416 * @dest : destination address
417 * @len : length to copy to destination
419 * Should be protected by get_subbuf/put_subbuf.
420 * Returns the length copied.
422 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
423 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
425 struct channel_backend
*chanb
;
426 const struct lttng_ust_lib_ring_buffer_config
*config
;
428 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
429 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
430 unsigned long sb_bindex
, id
;
433 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
436 config
= &chanb
->config
;
438 offset
&= chanb
->buf_size
- 1;
440 if (caa_unlikely(!len
))
442 id
= bufb
->buf_rsb
.id
;
443 sb_bindex
= subbuffer_id_get_index(config
, id
);
444 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
448 * Underlying layer should never ask for reads across
451 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
452 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
453 && subbuffer_id_is_noref(config
, id
));
454 backend_pages
= shmp(handle
, rpages
->shmp
);
457 src
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
458 if (caa_unlikely(!src
))
460 memcpy(dest
, src
, len
);
465 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
466 * @bufb : buffer backend
467 * @offset : offset within the buffer
468 * @dest : destination address
469 * @len : destination's length
471 * Return string's length, or -EINVAL on error.
472 * Should be protected by get_subbuf/put_subbuf.
473 * Destination length should be at least 1 to hold '\0'.
475 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
476 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
478 struct channel_backend
*chanb
;
479 const struct lttng_ust_lib_ring_buffer_config
*config
;
480 ssize_t string_len
, orig_offset
;
482 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
483 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
484 unsigned long sb_bindex
, id
;
486 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
489 config
= &chanb
->config
;
490 if (caa_unlikely(!len
))
492 offset
&= chanb
->buf_size
- 1;
493 orig_offset
= offset
;
494 id
= bufb
->buf_rsb
.id
;
495 sb_bindex
= subbuffer_id_get_index(config
, id
);
496 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
500 * Underlying layer should never ask for reads across
503 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
504 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
505 && subbuffer_id_is_noref(config
, id
));
506 backend_pages
= shmp(handle
, rpages
->shmp
);
509 str
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
510 if (caa_unlikely(!str
))
512 string_len
= strnlen(str
, len
);
514 memcpy(dest
, str
, string_len
);
515 ((char *)dest
)[0] = 0;
517 return offset
- orig_offset
;
521 * lib_ring_buffer_read_offset_address - get address of a buffer location
522 * @bufb : buffer backend
523 * @offset : offset within the buffer.
525 * Return the address where a given offset is located (for read).
526 * Should be used to get the current subbuffer header pointer. Given we know
527 * it's never on a page boundary, it's safe to read/write directly
528 * from/to this address, as long as the read/write is never bigger than
531 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
533 struct lttng_ust_shm_handle
*handle
)
535 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
536 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
537 struct channel_backend
*chanb
;
538 const struct lttng_ust_lib_ring_buffer_config
*config
;
539 unsigned long sb_bindex
, id
;
541 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
544 config
= &chanb
->config
;
545 offset
&= chanb
->buf_size
- 1;
546 id
= bufb
->buf_rsb
.id
;
547 sb_bindex
= subbuffer_id_get_index(config
, id
);
548 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
551 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
552 && subbuffer_id_is_noref(config
, id
));
553 backend_pages
= shmp(handle
, rpages
->shmp
);
556 return shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
560 * lib_ring_buffer_offset_address - get address of a location within the buffer
561 * @bufb : buffer backend
562 * @offset : offset within the buffer.
564 * Return the address where a given offset is located.
565 * Should be used to get the current subbuffer header pointer. Given we know
566 * it's always at the beginning of a page, it's safe to write directly to this
567 * address, as long as the write is never bigger than a page size.
569 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
571 struct lttng_ust_shm_handle
*handle
)
574 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
575 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
576 struct channel_backend
*chanb
;
577 const struct lttng_ust_lib_ring_buffer_config
*config
;
578 unsigned long sb_bindex
, id
;
579 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*sb
;
581 chanb
= &shmp(handle
, bufb
->chan
)->backend
;
584 config
= &chanb
->config
;
585 offset
&= chanb
->buf_size
- 1;
586 sbidx
= offset
>> chanb
->subbuf_size_order
;
587 sb
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
);
591 sb_bindex
= subbuffer_id_get_index(config
, id
);
592 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
595 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
596 && subbuffer_id_is_noref(config
, id
));
597 backend_pages
= shmp(handle
, rpages
->shmp
);
600 return shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));