2 * ring_buffer_backend.c
4 * Copyright (C) 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Dual LGPL v2.1/GPL v2 license.
10 #include <urcu/arch.h>
13 #include <lttng/ringbuffer-config.h>
21 * lib_ring_buffer_backend_allocate - allocate a channel buffer
22 * @config: ring buffer instance configuration
23 * @buf: the buffer struct
24 * @size: total size of the buffer
25 * @num_subbuf: number of subbuffers
26 * @extra_reader_sb: need extra subbuffer for reader
29 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config
*config
,
30 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
31 size_t size
, size_t num_subbuf
,
33 struct lttng_ust_shm_handle
*handle
,
34 struct shm_object
*shmobj
)
36 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
37 unsigned long subbuf_size
, mmap_offset
= 0;
38 unsigned long num_subbuf_alloc
;
41 subbuf_size
= chanb
->subbuf_size
;
42 num_subbuf_alloc
= num_subbuf
;
47 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
48 set_shmp(bufb
->array
, zalloc_shm(shmobj
,
49 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
));
50 if (caa_unlikely(!shmp(handle
, bufb
->array
)))
54 * This is the largest element (the buffer pages) which needs to
55 * be aligned on PAGE_SIZE.
57 align_shm(shmobj
, PAGE_SIZE
);
58 set_shmp(bufb
->memory_map
, zalloc_shm(shmobj
,
59 subbuf_size
* num_subbuf_alloc
));
60 if (caa_unlikely(!shmp(handle
, bufb
->memory_map
)))
61 goto memory_map_error
;
63 /* Allocate backend pages array elements */
64 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
65 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
66 set_shmp(shmp_index(handle
, bufb
->array
, i
)->shmp
,
68 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
)));
69 if (!shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
))
73 /* Allocate write-side subbuffer table */
74 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
75 set_shmp(bufb
->buf_wsb
, zalloc_shm(shmobj
,
76 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
)
78 if (caa_unlikely(!shmp(handle
, bufb
->buf_wsb
)))
81 for (i
= 0; i
< num_subbuf
; i
++)
82 shmp_index(handle
, bufb
->buf_wsb
, i
)->id
= subbuffer_id(config
, 0, 1, i
);
84 /* Assign read-side subbuffer table */
86 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
87 num_subbuf_alloc
- 1);
89 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
91 /* Assign pages to page index */
92 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
95 ref
.index
= bufb
->memory_map
._ref
.index
;
96 ref
.offset
= bufb
->memory_map
._ref
.offset
;
97 ref
.offset
+= i
* subbuf_size
;
99 set_shmp(shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->p
,
101 if (config
->output
== RING_BUFFER_MMAP
) {
102 shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->mmap_offset
= mmap_offset
;
103 mmap_offset
+= subbuf_size
;
110 /* bufb->array[i] will be freed by shm teardown */
112 /* bufb->array will be freed by shm teardown */
117 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
118 struct channel_backend
*chanb
, int cpu
,
119 struct lttng_ust_shm_handle
*handle
,
120 struct shm_object
*shmobj
)
122 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
124 set_shmp(bufb
->chan
, handle
->chan
._ref
);
127 return lib_ring_buffer_backend_allocate(config
, bufb
, chanb
->buf_size
,
129 chanb
->extra_reader_sb
,
133 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
134 struct lttng_ust_shm_handle
*handle
)
136 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
137 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
138 unsigned long num_subbuf_alloc
;
141 num_subbuf_alloc
= chanb
->num_subbuf
;
142 if (chanb
->extra_reader_sb
)
145 for (i
= 0; i
< chanb
->num_subbuf
; i
++)
146 shmp_index(handle
, bufb
->buf_wsb
, i
)->id
= subbuffer_id(config
, 0, 1, i
);
147 if (chanb
->extra_reader_sb
)
148 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
149 num_subbuf_alloc
- 1);
151 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
153 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
154 /* Don't reset mmap_offset */
155 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_commit
, 0);
156 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_unread
, 0);
157 shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->data_size
= 0;
158 /* Don't reset backend page and virt addresses */
160 /* Don't reset num_pages_per_subbuf, cpu, allocated */
161 v_set(config
, &bufb
->records_read
, 0);
165 * The frontend is responsible for also calling ring_buffer_backend_reset for
166 * each buffer when calling channel_backend_reset.
168 void channel_backend_reset(struct channel_backend
*chanb
)
170 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
171 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
174 * Don't reset buf_size, subbuf_size, subbuf_size_order,
175 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
176 * priv, notifiers, config, cpumask and name.
178 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
182 * channel_backend_init - initialize a channel backend
183 * @chanb: channel backend
184 * @name: channel name
185 * @config: client ring buffer configuration
186 * @parent: dentry of parent directory, %NULL for root directory
187 * @subbuf_size: size of sub-buffers (> PAGE_SIZE, power of 2)
188 * @num_subbuf: number of sub-buffers (power of 2)
189 * @lttng_ust_shm_handle: shared memory handle
191 * Returns channel pointer if successful, %NULL otherwise.
193 * Creates per-cpu channel buffers using the sizes and attributes
194 * specified. The created channel buffer files will be named
195 * name_0...name_N-1. File permissions will be %S_IRUSR.
197 * Called with CPU hotplug disabled.
199 int channel_backend_init(struct channel_backend
*chanb
,
201 const struct lttng_ust_lib_ring_buffer_config
*config
,
202 size_t subbuf_size
, size_t num_subbuf
,
203 struct lttng_ust_shm_handle
*handle
)
205 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
208 size_t shmsize
= 0, num_subbuf_alloc
;
213 if (!(subbuf_size
&& num_subbuf
))
216 /* Check that the subbuffer size is larger than a page. */
217 if (subbuf_size
< PAGE_SIZE
)
221 * Make sure the number of subbuffers and subbuffer size are power of 2.
223 CHAN_WARN_ON(chanb
, hweight32(subbuf_size
) != 1);
224 CHAN_WARN_ON(chanb
, hweight32(num_subbuf
) != 1);
226 ret
= subbuffer_id_check_index(config
, num_subbuf
);
230 chanb
->buf_size
= num_subbuf
* subbuf_size
;
231 chanb
->subbuf_size
= subbuf_size
;
232 chanb
->buf_size_order
= get_count_order(chanb
->buf_size
);
233 chanb
->subbuf_size_order
= get_count_order(subbuf_size
);
234 chanb
->num_subbuf_order
= get_count_order(num_subbuf
);
235 chanb
->extra_reader_sb
=
236 (config
->mode
== RING_BUFFER_OVERWRITE
) ? 1 : 0;
237 chanb
->num_subbuf
= num_subbuf
;
238 strncpy(chanb
->name
, name
, NAME_MAX
);
239 chanb
->name
[NAME_MAX
- 1] = '\0';
240 memcpy(&chanb
->config
, config
, sizeof(*config
));
242 /* Per-cpu buffer size: control (prior to backend) */
243 shmsize
= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer
));
244 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer
);
246 /* Per-cpu buffer size: backend */
247 /* num_subbuf + 1 is the worse case */
248 num_subbuf_alloc
= num_subbuf
+ 1;
249 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
250 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
;
251 shmsize
+= offset_align(shmsize
, PAGE_SIZE
);
252 shmsize
+= subbuf_size
* num_subbuf_alloc
;
253 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
254 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
255 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
256 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
257 /* Per-cpu buffer size: control (after backend) */
258 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_hot
));
259 shmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
260 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_cold
));
261 shmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
263 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
264 struct lttng_ust_lib_ring_buffer
*buf
;
266 * We need to allocate for all possible cpus.
268 for_each_possible_cpu(i
) {
269 struct shm_object
*shmobj
;
271 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
274 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
275 set_shmp(chanb
->buf
[i
].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
276 buf
= shmp(handle
, chanb
->buf
[i
].shmp
);
279 set_shmp(buf
->self
, chanb
->buf
[i
].shmp
._ref
);
280 ret
= lib_ring_buffer_create(buf
, chanb
, i
,
283 goto free_bufs
; /* cpu hotplug locked */
286 struct shm_object
*shmobj
;
287 struct lttng_ust_lib_ring_buffer
*buf
;
289 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
292 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
293 set_shmp(chanb
->buf
[0].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
294 buf
= shmp(handle
, chanb
->buf
[0].shmp
);
297 set_shmp(buf
->self
, chanb
->buf
[0].shmp
._ref
);
298 ret
= lib_ring_buffer_create(buf
, chanb
, -1,
303 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
308 /* We only free the buffer data upon shm teardown */
314 * channel_backend_free - destroy the channel
317 * Destroy all channel buffers and frees the channel.
319 void channel_backend_free(struct channel_backend
*chanb
,
320 struct lttng_ust_shm_handle
*handle
)
322 /* SHM teardown takes care of everything */
326 * lib_ring_buffer_read - read data from ring_buffer_buffer.
327 * @bufb : buffer backend
328 * @offset : offset within the buffer
329 * @dest : destination address
330 * @len : length to copy to destination
332 * Should be protected by get_subbuf/put_subbuf.
333 * Returns the length copied.
335 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
336 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
338 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
339 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
341 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
342 unsigned long sb_bindex
, id
;
345 offset
&= chanb
->buf_size
- 1;
347 if (caa_unlikely(!len
))
349 id
= bufb
->buf_rsb
.id
;
350 sb_bindex
= subbuffer_id_get_index(config
, id
);
351 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
353 * Underlying layer should never ask for reads across
356 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
357 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
358 && subbuffer_id_is_noref(config
, id
));
359 memcpy(dest
, shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1)), len
);
364 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
365 * @bufb : buffer backend
366 * @offset : offset within the buffer
367 * @dest : destination address
368 * @len : destination's length
370 * return string's length
371 * Should be protected by get_subbuf/put_subbuf.
373 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
374 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
376 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
377 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
378 ssize_t string_len
, orig_offset
;
380 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
381 unsigned long sb_bindex
, id
;
383 offset
&= chanb
->buf_size
- 1;
384 orig_offset
= offset
;
385 id
= bufb
->buf_rsb
.id
;
386 sb_bindex
= subbuffer_id_get_index(config
, id
);
387 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
389 * Underlying layer should never ask for reads across
392 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
393 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
394 && subbuffer_id_is_noref(config
, id
));
395 str
= shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
396 string_len
= strnlen(str
, len
);
398 memcpy(dest
, str
, string_len
);
399 ((char *)dest
)[0] = 0;
401 return offset
- orig_offset
;
405 * lib_ring_buffer_read_offset_address - get address of a buffer location
406 * @bufb : buffer backend
407 * @offset : offset within the buffer.
409 * Return the address where a given offset is located (for read).
410 * Should be used to get the current subbuffer header pointer. Given we know
411 * it's never on a page boundary, it's safe to write directly to this address,
412 * as long as the write is never bigger than a page size.
414 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
416 struct lttng_ust_shm_handle
*handle
)
418 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
419 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
420 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
421 unsigned long sb_bindex
, id
;
423 offset
&= chanb
->buf_size
- 1;
424 id
= bufb
->buf_rsb
.id
;
425 sb_bindex
= subbuffer_id_get_index(config
, id
);
426 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
427 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
428 && subbuffer_id_is_noref(config
, id
));
429 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
433 * lib_ring_buffer_offset_address - get address of a location within the buffer
434 * @bufb : buffer backend
435 * @offset : offset within the buffer.
437 * Return the address where a given offset is located.
438 * Should be used to get the current subbuffer header pointer. Given we know
439 * it's always at the beginning of a page, it's safe to write directly to this
440 * address, as long as the write is never bigger than a page size.
442 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
444 struct lttng_ust_shm_handle
*handle
)
447 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
448 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
449 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
450 unsigned long sb_bindex
, id
;
452 offset
&= chanb
->buf_size
- 1;
453 sbidx
= offset
>> chanb
->subbuf_size_order
;
454 id
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
)->id
;
455 sb_bindex
= subbuffer_id_get_index(config
, id
);
456 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
457 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
458 && subbuffer_id_is_noref(config
, id
));
459 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));