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
22 #include <urcu/arch.h>
25 #include <lttng/ringbuffer-config.h>
33 * lib_ring_buffer_backend_allocate - allocate a channel buffer
34 * @config: ring buffer instance configuration
35 * @buf: the buffer struct
36 * @size: total size of the buffer
37 * @num_subbuf: number of subbuffers
38 * @extra_reader_sb: need extra subbuffer for reader
41 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config
*config
,
42 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
43 size_t size
, size_t num_subbuf
,
45 struct lttng_ust_shm_handle
*handle
,
46 struct shm_object
*shmobj
)
48 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
49 unsigned long subbuf_size
, mmap_offset
= 0;
50 unsigned long num_subbuf_alloc
;
53 subbuf_size
= chanb
->subbuf_size
;
54 num_subbuf_alloc
= num_subbuf
;
59 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
60 set_shmp(bufb
->array
, zalloc_shm(shmobj
,
61 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
));
62 if (caa_unlikely(!shmp(handle
, bufb
->array
)))
66 * This is the largest element (the buffer pages) which needs to
67 * be aligned on PAGE_SIZE.
69 align_shm(shmobj
, PAGE_SIZE
);
70 set_shmp(bufb
->memory_map
, zalloc_shm(shmobj
,
71 subbuf_size
* num_subbuf_alloc
));
72 if (caa_unlikely(!shmp(handle
, bufb
->memory_map
)))
73 goto memory_map_error
;
75 /* Allocate backend pages array elements */
76 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
77 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
78 set_shmp(shmp_index(handle
, bufb
->array
, i
)->shmp
,
80 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
)));
81 if (!shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
))
85 /* Allocate write-side subbuffer table */
86 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
87 set_shmp(bufb
->buf_wsb
, zalloc_shm(shmobj
,
88 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
)
90 if (caa_unlikely(!shmp(handle
, bufb
->buf_wsb
)))
93 for (i
= 0; i
< num_subbuf
; i
++)
94 shmp_index(handle
, bufb
->buf_wsb
, i
)->id
= subbuffer_id(config
, 0, 1, i
);
96 /* Assign read-side subbuffer table */
98 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
99 num_subbuf_alloc
- 1);
101 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
103 /* Assign pages to page index */
104 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
107 ref
.index
= bufb
->memory_map
._ref
.index
;
108 ref
.offset
= bufb
->memory_map
._ref
.offset
;
109 ref
.offset
+= i
* subbuf_size
;
111 set_shmp(shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->p
,
113 if (config
->output
== RING_BUFFER_MMAP
) {
114 shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->mmap_offset
= mmap_offset
;
115 mmap_offset
+= subbuf_size
;
121 /* bufb->array[i] will be freed by shm teardown */
123 /* bufb->array will be freed by shm teardown */
128 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
129 struct channel_backend
*chanb
, int cpu
,
130 struct lttng_ust_shm_handle
*handle
,
131 struct shm_object
*shmobj
)
133 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
135 set_shmp(bufb
->chan
, handle
->chan
._ref
);
138 return lib_ring_buffer_backend_allocate(config
, bufb
, chanb
->buf_size
,
140 chanb
->extra_reader_sb
,
144 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
145 struct lttng_ust_shm_handle
*handle
)
147 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
148 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
149 unsigned long num_subbuf_alloc
;
152 num_subbuf_alloc
= chanb
->num_subbuf
;
153 if (chanb
->extra_reader_sb
)
156 for (i
= 0; i
< chanb
->num_subbuf
; i
++)
157 shmp_index(handle
, bufb
->buf_wsb
, i
)->id
= subbuffer_id(config
, 0, 1, i
);
158 if (chanb
->extra_reader_sb
)
159 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
160 num_subbuf_alloc
- 1);
162 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
164 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
165 /* Don't reset mmap_offset */
166 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_commit
, 0);
167 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_unread
, 0);
168 shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->data_size
= 0;
169 /* Don't reset backend page and virt addresses */
171 /* Don't reset num_pages_per_subbuf, cpu, allocated */
172 v_set(config
, &bufb
->records_read
, 0);
176 * The frontend is responsible for also calling ring_buffer_backend_reset for
177 * each buffer when calling channel_backend_reset.
179 void channel_backend_reset(struct channel_backend
*chanb
)
181 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
182 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
185 * Don't reset buf_size, subbuf_size, subbuf_size_order,
186 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
187 * priv, notifiers, config, cpumask and name.
189 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
193 * channel_backend_init - initialize a channel backend
194 * @chanb: channel backend
195 * @name: channel name
196 * @config: client ring buffer configuration
197 * @parent: dentry of parent directory, %NULL for root directory
198 * @subbuf_size: size of sub-buffers (> PAGE_SIZE, power of 2)
199 * @num_subbuf: number of sub-buffers (power of 2)
200 * @lttng_ust_shm_handle: shared memory handle
201 * @stream_fds: stream file descriptors.
203 * Returns channel pointer if successful, %NULL otherwise.
205 * Creates per-cpu channel buffers using the sizes and attributes
206 * specified. The created channel buffer files will be named
207 * name_0...name_N-1. File permissions will be %S_IRUSR.
209 * Called with CPU hotplug disabled.
211 int channel_backend_init(struct channel_backend
*chanb
,
213 const struct lttng_ust_lib_ring_buffer_config
*config
,
214 size_t subbuf_size
, size_t num_subbuf
,
215 struct lttng_ust_shm_handle
*handle
,
216 const int *stream_fds
)
218 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
221 size_t shmsize
= 0, num_subbuf_alloc
;
226 /* Check that the subbuffer size is larger than a page. */
227 if (subbuf_size
< PAGE_SIZE
)
231 * Make sure the number of subbuffers and subbuffer size are
232 * power of 2, and nonzero.
234 if (!subbuf_size
|| (subbuf_size
& (subbuf_size
- 1)))
236 if (!num_subbuf
|| (num_subbuf
& (num_subbuf
- 1)))
239 * Overwrite mode buffers require at least 2 subbuffers per
242 if (config
->mode
== RING_BUFFER_OVERWRITE
&& num_subbuf
< 2)
245 ret
= subbuffer_id_check_index(config
, num_subbuf
);
249 chanb
->buf_size
= num_subbuf
* subbuf_size
;
250 chanb
->subbuf_size
= subbuf_size
;
251 chanb
->buf_size_order
= get_count_order(chanb
->buf_size
);
252 chanb
->subbuf_size_order
= get_count_order(subbuf_size
);
253 chanb
->num_subbuf_order
= get_count_order(num_subbuf
);
254 chanb
->extra_reader_sb
=
255 (config
->mode
== RING_BUFFER_OVERWRITE
) ? 1 : 0;
256 chanb
->num_subbuf
= num_subbuf
;
257 strncpy(chanb
->name
, name
, NAME_MAX
);
258 chanb
->name
[NAME_MAX
- 1] = '\0';
259 memcpy(&chanb
->config
, config
, sizeof(*config
));
261 /* Per-cpu buffer size: control (prior to backend) */
262 shmsize
= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer
));
263 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer
);
265 /* Per-cpu buffer size: backend */
266 /* num_subbuf + 1 is the worse case */
267 num_subbuf_alloc
= num_subbuf
+ 1;
268 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
269 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
;
270 shmsize
+= offset_align(shmsize
, PAGE_SIZE
);
271 shmsize
+= subbuf_size
* num_subbuf_alloc
;
272 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
273 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
274 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
275 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
276 /* Per-cpu buffer size: control (after backend) */
277 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_hot
));
278 shmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
279 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_cold
));
280 shmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
282 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
283 struct lttng_ust_lib_ring_buffer
*buf
;
285 * We need to allocate for all possible cpus.
287 for_each_possible_cpu(i
) {
288 struct shm_object
*shmobj
;
290 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
291 SHM_OBJECT_SHM
, stream_fds
[i
]);
294 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
295 set_shmp(chanb
->buf
[i
].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
296 buf
= shmp(handle
, chanb
->buf
[i
].shmp
);
299 set_shmp(buf
->self
, chanb
->buf
[i
].shmp
._ref
);
300 ret
= lib_ring_buffer_create(buf
, chanb
, i
,
303 goto free_bufs
; /* cpu hotplug locked */
306 struct shm_object
*shmobj
;
307 struct lttng_ust_lib_ring_buffer
*buf
;
309 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
310 SHM_OBJECT_SHM
, stream_fds
[0]);
313 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
314 set_shmp(chanb
->buf
[0].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
315 buf
= shmp(handle
, chanb
->buf
[0].shmp
);
318 set_shmp(buf
->self
, chanb
->buf
[0].shmp
._ref
);
319 ret
= lib_ring_buffer_create(buf
, chanb
, -1,
324 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
329 /* We only free the buffer data upon shm teardown */
335 * channel_backend_free - destroy the channel
338 * Destroy all channel buffers and frees the channel.
340 void channel_backend_free(struct channel_backend
*chanb
,
341 struct lttng_ust_shm_handle
*handle
)
343 /* SHM teardown takes care of everything */
347 * lib_ring_buffer_read - read data from ring_buffer_buffer.
348 * @bufb : buffer backend
349 * @offset : offset within the buffer
350 * @dest : destination address
351 * @len : length to copy to destination
353 * Should be protected by get_subbuf/put_subbuf.
354 * Returns the length copied.
356 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
357 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
359 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
360 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
362 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
363 unsigned long sb_bindex
, id
;
366 offset
&= chanb
->buf_size
- 1;
368 if (caa_unlikely(!len
))
370 id
= bufb
->buf_rsb
.id
;
371 sb_bindex
= subbuffer_id_get_index(config
, id
);
372 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
374 * Underlying layer should never ask for reads across
377 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
378 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
379 && subbuffer_id_is_noref(config
, id
));
380 memcpy(dest
, shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1)), len
);
385 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
386 * @bufb : buffer backend
387 * @offset : offset within the buffer
388 * @dest : destination address
389 * @len : destination's length
391 * Return string's length, or -EINVAL on error.
392 * Should be protected by get_subbuf/put_subbuf.
393 * Destination length should be at least 1 to hold '\0'.
395 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
396 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
398 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
399 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
400 ssize_t string_len
, orig_offset
;
402 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
403 unsigned long sb_bindex
, id
;
405 if (caa_unlikely(!len
))
407 offset
&= chanb
->buf_size
- 1;
408 orig_offset
= offset
;
409 id
= bufb
->buf_rsb
.id
;
410 sb_bindex
= subbuffer_id_get_index(config
, id
);
411 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
413 * Underlying layer should never ask for reads across
416 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
417 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
418 && subbuffer_id_is_noref(config
, id
));
419 str
= shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
420 string_len
= strnlen(str
, len
);
422 memcpy(dest
, str
, string_len
);
423 ((char *)dest
)[0] = 0;
425 return offset
- orig_offset
;
429 * lib_ring_buffer_read_offset_address - get address of a buffer location
430 * @bufb : buffer backend
431 * @offset : offset within the buffer.
433 * Return the address where a given offset is located (for read).
434 * Should be used to get the current subbuffer header pointer. Given we know
435 * it's never on a page boundary, it's safe to read/write directly
436 * from/to this address, as long as the read/write is never bigger than
439 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
441 struct lttng_ust_shm_handle
*handle
)
443 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
444 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
445 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
446 unsigned long sb_bindex
, id
;
448 offset
&= chanb
->buf_size
- 1;
449 id
= bufb
->buf_rsb
.id
;
450 sb_bindex
= subbuffer_id_get_index(config
, id
);
451 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
452 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
453 && subbuffer_id_is_noref(config
, id
));
454 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
458 * lib_ring_buffer_offset_address - get address of a location within the buffer
459 * @bufb : buffer backend
460 * @offset : offset within the buffer.
462 * Return the address where a given offset is located.
463 * Should be used to get the current subbuffer header pointer. Given we know
464 * it's always at the beginning of a page, it's safe to write directly to this
465 * address, as long as the write is never bigger than a page size.
467 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
469 struct lttng_ust_shm_handle
*handle
)
472 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
473 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
474 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
475 unsigned long sb_bindex
, id
;
477 offset
&= chanb
->buf_size
- 1;
478 sbidx
= offset
>> chanb
->subbuf_size_order
;
479 id
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
)->id
;
480 sb_bindex
= subbuffer_id_get_index(config
, id
);
481 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
482 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
483 && subbuffer_id_is_noref(config
, id
));
484 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));