Move compat macros in 'lttng/align.h' to a private header
[lttng-ust.git] / libringbuffer / ring_buffer_backend.c
CommitLineData
852c2936 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
852c2936 3 *
e92f3e28 4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
852c2936
MD
5 */
6
3fbec7dc 7#define _LGPL_SOURCE
b4051ad8 8#include <stddef.h>
fb31eb73 9#include <stdint.h>
2657d1ba 10#include <unistd.h>
14641deb 11#include <urcu/arch.h>
96e80018 12#include <limits.h>
14641deb 13
b72687b8 14#include <lttng/align.h>
4318ae1b 15#include <lttng/ringbuffer-config.h>
2fed87ae 16#include "vatomic.h"
4931a13e
MD
17#include "backend.h"
18#include "frontend.h"
a6352fd4 19#include "smp.h"
431d5cf0 20#include "shm.h"
cd61d9bf 21#include "ust-compat.h"
852c2936
MD
22
23/**
24 * lib_ring_buffer_backend_allocate - allocate a channel buffer
25 * @config: ring buffer instance configuration
26 * @buf: the buffer struct
27 * @size: total size of the buffer
28 * @num_subbuf: number of subbuffers
29 * @extra_reader_sb: need extra subbuffer for reader
30 */
31static
4cfec15c
MD
32int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config *config,
33 struct lttng_ust_lib_ring_buffer_backend *bufb,
852c2936 34 size_t size, size_t num_subbuf,
a6352fd4 35 int extra_reader_sb,
38fae1d3 36 struct lttng_ust_shm_handle *handle,
1d498196 37 struct shm_object *shmobj)
852c2936 38{
34daae3e 39 struct channel_backend *chanb;
852c2936
MD
40 unsigned long subbuf_size, mmap_offset = 0;
41 unsigned long num_subbuf_alloc;
852c2936 42 unsigned long i;
2657d1ba 43 long page_size;
852c2936 44
34daae3e
MD
45 chanb = &shmp(handle, bufb->chan)->backend;
46 if (!chanb)
47 return -EINVAL;
48
852c2936
MD
49 subbuf_size = chanb->subbuf_size;
50 num_subbuf_alloc = num_subbuf;
51
a6352fd4 52 if (extra_reader_sb)
852c2936 53 num_subbuf_alloc++;
852c2936 54
b72687b8 55 page_size = LTTNG_UST_PAGE_SIZE;
2657d1ba
MD
56 if (page_size <= 0) {
57 goto page_size_error;
58 }
59
4cfec15c 60 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp));
1d498196 61 set_shmp(bufb->array, zalloc_shm(shmobj,
4cfec15c 62 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc));
b5a3dfa5 63 if (caa_unlikely(!shmp(handle, bufb->array)))
852c2936
MD
64 goto array_error;
65
431d5cf0
MD
66 /*
67 * This is the largest element (the buffer pages) which needs to
2657d1ba 68 * be aligned on page size.
431d5cf0 69 */
2657d1ba 70 align_shm(shmobj, page_size);
1d498196 71 set_shmp(bufb->memory_map, zalloc_shm(shmobj,
a6352fd4 72 subbuf_size * num_subbuf_alloc));
b5a3dfa5 73 if (caa_unlikely(!shmp(handle, bufb->memory_map)))
a6352fd4 74 goto memory_map_error;
852c2936
MD
75
76 /* Allocate backend pages array elements */
77 for (i = 0; i < num_subbuf_alloc; i++) {
4cfec15c 78 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages));
aead2025 79 set_shmp(shmp_index(handle, bufb->array, i)->shmp,
1d498196 80 zalloc_shm(shmobj,
4cfec15c 81 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages)));
4746ae29 82 if (!shmp(handle, shmp_index(handle, bufb->array, i)->shmp))
852c2936
MD
83 goto free_array;
84 }
85
86 /* Allocate write-side subbuffer table */
4cfec15c 87 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
1d498196 88 set_shmp(bufb->buf_wsb, zalloc_shm(shmobj,
4cfec15c 89 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer)
1d498196 90 * num_subbuf));
b5a3dfa5 91 if (caa_unlikely(!shmp(handle, bufb->buf_wsb)))
852c2936
MD
92 goto free_array;
93
34daae3e
MD
94 for (i = 0; i < num_subbuf; i++) {
95 struct lttng_ust_lib_ring_buffer_backend_subbuffer *sb;
96
97 sb = shmp_index(handle, bufb->buf_wsb, i);
98 if (!sb)
99 goto free_array;
100 sb->id = subbuffer_id(config, 0, 1, i);
101 }
852c2936
MD
102
103 /* Assign read-side subbuffer table */
104 if (extra_reader_sb)
105 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
106 num_subbuf_alloc - 1);
107 else
108 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
109
1ff31389 110 /* Allocate subbuffer packet counter table */
bf8f2ef2 111 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_counts));
1ff31389
JD
112 set_shmp(bufb->buf_cnt, zalloc_shm(shmobj,
113 sizeof(struct lttng_ust_lib_ring_buffer_backend_counts)
114 * num_subbuf));
115 if (caa_unlikely(!shmp(handle, bufb->buf_cnt)))
116 goto free_wsb;
117
852c2936
MD
118 /* Assign pages to page index */
119 for (i = 0; i < num_subbuf_alloc; i++) {
34daae3e
MD
120 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *sbp;
121 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
1d498196
MD
122 struct shm_ref ref;
123
124 ref.index = bufb->memory_map._ref.index;
125 ref.offset = bufb->memory_map._ref.offset;
126 ref.offset += i * subbuf_size;
127
34daae3e
MD
128 sbp = shmp_index(handle, bufb->array, i);
129 if (!sbp)
130 goto free_array;
131 pages = shmp(handle, sbp->shmp);
132 if (!pages)
133 goto free_array;
134 set_shmp(pages->p, ref);
852c2936 135 if (config->output == RING_BUFFER_MMAP) {
34daae3e 136 pages->mmap_offset = mmap_offset;
852c2936
MD
137 mmap_offset += subbuf_size;
138 }
139 }
852c2936
MD
140 return 0;
141
1ff31389
JD
142free_wsb:
143 /* bufb->buf_wsb will be freed by shm teardown */
852c2936 144free_array:
a6352fd4
MD
145 /* bufb->array[i] will be freed by shm teardown */
146memory_map_error:
147 /* bufb->array will be freed by shm teardown */
852c2936 148array_error:
2657d1ba 149page_size_error:
852c2936
MD
150 return -ENOMEM;
151}
152
4cfec15c 153int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb,
a6352fd4 154 struct channel_backend *chanb, int cpu,
38fae1d3 155 struct lttng_ust_shm_handle *handle,
1d498196 156 struct shm_object *shmobj)
852c2936 157{
4cfec15c 158 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
852c2936 159
1d498196 160 set_shmp(bufb->chan, handle->chan._ref);
852c2936
MD
161 bufb->cpu = cpu;
162
163 return lib_ring_buffer_backend_allocate(config, bufb, chanb->buf_size,
164 chanb->num_subbuf,
a6352fd4 165 chanb->extra_reader_sb,
1d498196 166 handle, shmobj);
852c2936
MD
167}
168
4cfec15c 169void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb,
38fae1d3 170 struct lttng_ust_shm_handle *handle)
852c2936 171{
34daae3e
MD
172 struct channel_backend *chanb;
173 const struct lttng_ust_lib_ring_buffer_config *config;
852c2936
MD
174 unsigned long num_subbuf_alloc;
175 unsigned int i;
176
34daae3e
MD
177 chanb = &shmp(handle, bufb->chan)->backend;
178 if (!chanb)
15500a1b 179 return;
34daae3e
MD
180 config = &chanb->config;
181
852c2936
MD
182 num_subbuf_alloc = chanb->num_subbuf;
183 if (chanb->extra_reader_sb)
184 num_subbuf_alloc++;
185
34daae3e
MD
186 for (i = 0; i < chanb->num_subbuf; i++) {
187 struct lttng_ust_lib_ring_buffer_backend_subbuffer *sb;
188
189 sb = shmp_index(handle, bufb->buf_wsb, i);
190 if (!sb)
15500a1b 191 return;
34daae3e
MD
192 sb->id = subbuffer_id(config, 0, 1, i);
193 }
852c2936
MD
194 if (chanb->extra_reader_sb)
195 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
196 num_subbuf_alloc - 1);
197 else
198 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
199
200 for (i = 0; i < num_subbuf_alloc; i++) {
34daae3e
MD
201 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *sbp;
202 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
203
204 sbp = shmp_index(handle, bufb->array, i);
205 if (!sbp)
15500a1b 206 return;
34daae3e
MD
207 pages = shmp(handle, sbp->shmp);
208 if (!pages)
15500a1b 209 return;
852c2936 210 /* Don't reset mmap_offset */
34daae3e
MD
211 v_set(config, &pages->records_commit, 0);
212 v_set(config, &pages->records_unread, 0);
213 pages->data_size = 0;
852c2936
MD
214 /* Don't reset backend page and virt addresses */
215 }
216 /* Don't reset num_pages_per_subbuf, cpu, allocated */
217 v_set(config, &bufb->records_read, 0);
218}
219
220/*
221 * The frontend is responsible for also calling ring_buffer_backend_reset for
222 * each buffer when calling channel_backend_reset.
223 */
224void channel_backend_reset(struct channel_backend *chanb)
225{
14641deb 226 struct channel *chan = caa_container_of(chanb, struct channel, backend);
4cfec15c 227 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
852c2936
MD
228
229 /*
230 * Don't reset buf_size, subbuf_size, subbuf_size_order,
231 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
232 * priv, notifiers, config, cpumask and name.
233 */
234 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
235}
236
852c2936
MD
237/**
238 * channel_backend_init - initialize a channel backend
239 * @chanb: channel backend
240 * @name: channel name
241 * @config: client ring buffer configuration
852c2936 242 * @parent: dentry of parent directory, %NULL for root directory
2657d1ba 243 * @subbuf_size: size of sub-buffers (> page size, power of 2)
852c2936 244 * @num_subbuf: number of sub-buffers (power of 2)
38fae1d3 245 * @lttng_ust_shm_handle: shared memory handle
5ea386c3 246 * @stream_fds: stream file descriptors.
852c2936
MD
247 *
248 * Returns channel pointer if successful, %NULL otherwise.
249 *
250 * Creates per-cpu channel buffers using the sizes and attributes
251 * specified. The created channel buffer files will be named
252 * name_0...name_N-1. File permissions will be %S_IRUSR.
253 *
254 * Called with CPU hotplug disabled.
255 */
256int channel_backend_init(struct channel_backend *chanb,
257 const char *name,
4cfec15c 258 const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f 259 size_t subbuf_size, size_t num_subbuf,
a9ff648c 260 struct lttng_ust_shm_handle *handle,
5ea386c3 261 const int *stream_fds)
852c2936 262{
14641deb 263 struct channel *chan = caa_container_of(chanb, struct channel, backend);
852c2936
MD
264 unsigned int i;
265 int ret;
6c1f7d8b 266 size_t shmsize = 0, num_subbuf_alloc;
2657d1ba 267 long page_size;
852c2936
MD
268
269 if (!name)
270 return -EPERM;
271
b72687b8 272 page_size = LTTNG_UST_PAGE_SIZE;
2657d1ba
MD
273 if (page_size <= 0) {
274 return -ENOMEM;
275 }
852c2936 276 /* Check that the subbuffer size is larger than a page. */
2657d1ba 277 if (subbuf_size < page_size)
852c2936
MD
278 return -EINVAL;
279
280 /*
12bcbbdb
MD
281 * Make sure the number of subbuffers and subbuffer size are
282 * power of 2, and nonzero.
852c2936 283 */
12bcbbdb 284 if (!subbuf_size || (subbuf_size & (subbuf_size - 1)))
e52b0723 285 return -EINVAL;
12bcbbdb 286 if (!num_subbuf || (num_subbuf & (num_subbuf - 1)))
e52b0723 287 return -EINVAL;
3d8e9399
MD
288 /*
289 * Overwrite mode buffers require at least 2 subbuffers per
290 * buffer.
291 */
292 if (config->mode == RING_BUFFER_OVERWRITE && num_subbuf < 2)
293 return -EINVAL;
852c2936
MD
294
295 ret = subbuffer_id_check_index(config, num_subbuf);
296 if (ret)
297 return ret;
298
852c2936
MD
299 chanb->buf_size = num_subbuf * subbuf_size;
300 chanb->subbuf_size = subbuf_size;
301 chanb->buf_size_order = get_count_order(chanb->buf_size);
302 chanb->subbuf_size_order = get_count_order(subbuf_size);
303 chanb->num_subbuf_order = get_count_order(num_subbuf);
304 chanb->extra_reader_sb =
305 (config->mode == RING_BUFFER_OVERWRITE) ? 1 : 0;
306 chanb->num_subbuf = num_subbuf;
a6352fd4
MD
307 strncpy(chanb->name, name, NAME_MAX);
308 chanb->name[NAME_MAX - 1] = '\0';
193183fb 309 memcpy(&chanb->config, config, sizeof(*config));
852c2936 310
1d498196 311 /* Per-cpu buffer size: control (prior to backend) */
b72687b8 312 shmsize = lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer));
4cfec15c 313 shmsize += sizeof(struct lttng_ust_lib_ring_buffer);
b72687b8 314 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct commit_counters_hot));
d2f09c1c 315 shmsize += sizeof(struct commit_counters_hot) * num_subbuf;
b72687b8 316 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct commit_counters_cold));
d2f09c1c
MD
317 shmsize += sizeof(struct commit_counters_cold) * num_subbuf;
318 /* Sampled timestamp end */
b72687b8 319 shmsize += lttng_ust_offset_align(shmsize, __alignof__(uint64_t));
d2f09c1c 320 shmsize += sizeof(uint64_t) * num_subbuf;
1d498196
MD
321
322 /* Per-cpu buffer size: backend */
323 /* num_subbuf + 1 is the worse case */
324 num_subbuf_alloc = num_subbuf + 1;
b72687b8 325 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp));
4cfec15c 326 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc;
b72687b8 327 shmsize += lttng_ust_offset_align(shmsize, page_size);
1d498196 328 shmsize += subbuf_size * num_subbuf_alloc;
b72687b8 329 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages));
4cfec15c 330 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages) * num_subbuf_alloc;
b72687b8 331 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
4cfec15c 332 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer) * num_subbuf;
b72687b8 333 shmsize += lttng_ust_offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_counts));
bf8f2ef2 334 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_counts) * num_subbuf;
1d498196 335
852c2936 336 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
4cfec15c 337 struct lttng_ust_lib_ring_buffer *buf;
852c2936 338 /*
a6352fd4 339 * We need to allocate for all possible cpus.
852c2936 340 */
852c2936 341 for_each_possible_cpu(i) {
1d498196 342 struct shm_object *shmobj;
5ea386c3 343
74d81a6c 344 shmobj = shm_object_table_alloc(handle->table, shmsize,
4b68c31f 345 SHM_OBJECT_SHM, stream_fds[i], i);
afdf9825
MD
346 if (!shmobj)
347 goto end;
4cfec15c
MD
348 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer));
349 set_shmp(chanb->buf[i].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer)));
1d498196
MD
350 buf = shmp(handle, chanb->buf[i].shmp);
351 if (!buf)
352 goto end;
5d61a504 353 set_shmp(buf->self, chanb->buf[i].shmp._ref);
1d498196
MD
354 ret = lib_ring_buffer_create(buf, chanb, i,
355 handle, shmobj);
852c2936
MD
356 if (ret)
357 goto free_bufs; /* cpu hotplug locked */
358 }
852c2936 359 } else {
1d498196 360 struct shm_object *shmobj;
4cfec15c 361 struct lttng_ust_lib_ring_buffer *buf;
a6352fd4 362
74d81a6c 363 shmobj = shm_object_table_alloc(handle->table, shmsize,
4b68c31f 364 SHM_OBJECT_SHM, stream_fds[0], -1);
afdf9825
MD
365 if (!shmobj)
366 goto end;
4cfec15c
MD
367 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer));
368 set_shmp(chanb->buf[0].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer)));
1d498196 369 buf = shmp(handle, chanb->buf[0].shmp);
a6352fd4
MD
370 if (!buf)
371 goto end;
cb14bae9 372 set_shmp(buf->self, chanb->buf[0].shmp._ref);
1d498196
MD
373 ret = lib_ring_buffer_create(buf, chanb, -1,
374 handle, shmobj);
852c2936
MD
375 if (ret)
376 goto free_bufs;
377 }
378 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
379
380 return 0;
381
382free_bufs:
a6352fd4
MD
383 /* We only free the buffer data upon shm teardown */
384end:
852c2936
MD
385 return -ENOMEM;
386}
387
852c2936
MD
388/**
389 * channel_backend_free - destroy the channel
390 * @chan: the channel
391 *
392 * Destroy all channel buffers and frees the channel.
393 */
1d498196 394void channel_backend_free(struct channel_backend *chanb,
38fae1d3 395 struct lttng_ust_shm_handle *handle)
852c2936 396{
45e9e699 397 /* SHM teardown takes care of everything */
852c2936
MD
398}
399
852c2936
MD
400/**
401 * lib_ring_buffer_read - read data from ring_buffer_buffer.
402 * @bufb : buffer backend
403 * @offset : offset within the buffer
404 * @dest : destination address
405 * @len : length to copy to destination
406 *
407 * Should be protected by get_subbuf/put_subbuf.
408 * Returns the length copied.
409 */
4cfec15c 410size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
38fae1d3 411 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
852c2936 412{
34daae3e
MD
413 struct channel_backend *chanb;
414 const struct lttng_ust_lib_ring_buffer_config *config;
a6352fd4 415 ssize_t orig_len;
4cfec15c 416 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
15500a1b 417 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
852c2936 418 unsigned long sb_bindex, id;
99b99b47 419 void *src;
852c2936 420
34daae3e
MD
421 chanb = &shmp(handle, bufb->chan)->backend;
422 if (!chanb)
423 return 0;
424 config = &chanb->config;
852c2936
MD
425 orig_len = len;
426 offset &= chanb->buf_size - 1;
a6352fd4 427
b5a3dfa5 428 if (caa_unlikely(!len))
852c2936 429 return 0;
a6352fd4
MD
430 id = bufb->buf_rsb.id;
431 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 432 rpages = shmp_index(handle, bufb->array, sb_bindex);
15500a1b
MD
433 if (!rpages)
434 return 0;
a6352fd4
MD
435 /*
436 * Underlying layer should never ask for reads across
437 * subbuffers.
438 */
439 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
440 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
441 && subbuffer_id_is_noref(config, id));
15500a1b
MD
442 backend_pages = shmp(handle, rpages->shmp);
443 if (!backend_pages)
444 return 0;
445 src = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
99b99b47
MD
446 if (caa_unlikely(!src))
447 return 0;
448 memcpy(dest, src, len);
852c2936
MD
449 return orig_len;
450}
852c2936 451
852c2936
MD
452/**
453 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
454 * @bufb : buffer backend
455 * @offset : offset within the buffer
456 * @dest : destination address
457 * @len : destination's length
458 *
0bf3c920 459 * Return string's length, or -EINVAL on error.
852c2936 460 * Should be protected by get_subbuf/put_subbuf.
0bf3c920 461 * Destination length should be at least 1 to hold '\0'.
852c2936 462 */
4cfec15c 463int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
38fae1d3 464 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
852c2936 465{
34daae3e
MD
466 struct channel_backend *chanb;
467 const struct lttng_ust_lib_ring_buffer_config *config;
a6352fd4 468 ssize_t string_len, orig_offset;
852c2936 469 char *str;
4cfec15c 470 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
15500a1b 471 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
852c2936
MD
472 unsigned long sb_bindex, id;
473
34daae3e
MD
474 chanb = &shmp(handle, bufb->chan)->backend;
475 if (!chanb)
476 return -EINVAL;
477 config = &chanb->config;
0bf3c920
MD
478 if (caa_unlikely(!len))
479 return -EINVAL;
852c2936 480 offset &= chanb->buf_size - 1;
852c2936 481 orig_offset = offset;
852c2936
MD
482 id = bufb->buf_rsb.id;
483 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 484 rpages = shmp_index(handle, bufb->array, sb_bindex);
15500a1b
MD
485 if (!rpages)
486 return -EINVAL;
a6352fd4
MD
487 /*
488 * Underlying layer should never ask for reads across
489 * subbuffers.
490 */
491 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
852c2936
MD
492 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
493 && subbuffer_id_is_noref(config, id));
15500a1b
MD
494 backend_pages = shmp(handle, rpages->shmp);
495 if (!backend_pages)
496 return -EINVAL;
497 str = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
99b99b47
MD
498 if (caa_unlikely(!str))
499 return -EINVAL;
a6352fd4
MD
500 string_len = strnlen(str, len);
501 if (dest && len) {
502 memcpy(dest, str, string_len);
503 ((char *)dest)[0] = 0;
504 }
505 return offset - orig_offset;
852c2936 506}
852c2936
MD
507
508/**
509 * lib_ring_buffer_read_offset_address - get address of a buffer location
510 * @bufb : buffer backend
511 * @offset : offset within the buffer.
512 *
513 * Return the address where a given offset is located (for read).
514 * Should be used to get the current subbuffer header pointer. Given we know
22b22ce9
MD
515 * it's never on a page boundary, it's safe to read/write directly
516 * from/to this address, as long as the read/write is never bigger than
517 * a page size.
852c2936 518 */
4cfec15c 519void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 520 size_t offset,
38fae1d3 521 struct lttng_ust_shm_handle *handle)
852c2936 522{
4cfec15c 523 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
15500a1b 524 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
34daae3e
MD
525 struct channel_backend *chanb;
526 const struct lttng_ust_lib_ring_buffer_config *config;
852c2936
MD
527 unsigned long sb_bindex, id;
528
34daae3e
MD
529 chanb = &shmp(handle, bufb->chan)->backend;
530 if (!chanb)
531 return NULL;
532 config = &chanb->config;
852c2936 533 offset &= chanb->buf_size - 1;
852c2936
MD
534 id = bufb->buf_rsb.id;
535 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 536 rpages = shmp_index(handle, bufb->array, sb_bindex);
15500a1b
MD
537 if (!rpages)
538 return NULL;
852c2936
MD
539 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
540 && subbuffer_id_is_noref(config, id));
15500a1b
MD
541 backend_pages = shmp(handle, rpages->shmp);
542 if (!backend_pages)
543 return NULL;
544 return shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
852c2936 545}
852c2936
MD
546
547/**
548 * lib_ring_buffer_offset_address - get address of a location within the buffer
549 * @bufb : buffer backend
550 * @offset : offset within the buffer.
551 *
552 * Return the address where a given offset is located.
553 * Should be used to get the current subbuffer header pointer. Given we know
554 * it's always at the beginning of a page, it's safe to write directly to this
555 * address, as long as the write is never bigger than a page size.
556 */
4cfec15c 557void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 558 size_t offset,
38fae1d3 559 struct lttng_ust_shm_handle *handle)
852c2936 560{
a6352fd4 561 size_t sbidx;
4cfec15c 562 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
15500a1b 563 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
34daae3e
MD
564 struct channel_backend *chanb;
565 const struct lttng_ust_lib_ring_buffer_config *config;
852c2936 566 unsigned long sb_bindex, id;
34daae3e 567 struct lttng_ust_lib_ring_buffer_backend_subbuffer *sb;
852c2936 568
34daae3e
MD
569 chanb = &shmp(handle, bufb->chan)->backend;
570 if (!chanb)
571 return NULL;
572 config = &chanb->config;
852c2936
MD
573 offset &= chanb->buf_size - 1;
574 sbidx = offset >> chanb->subbuf_size_order;
34daae3e
MD
575 sb = shmp_index(handle, bufb->buf_wsb, sbidx);
576 if (!sb)
577 return NULL;
578 id = sb->id;
852c2936 579 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 580 rpages = shmp_index(handle, bufb->array, sb_bindex);
15500a1b
MD
581 if (!rpages)
582 return NULL;
852c2936
MD
583 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
584 && subbuffer_id_is_noref(config, id));
15500a1b
MD
585 backend_pages = shmp(handle, rpages->shmp);
586 if (!backend_pages)
587 return NULL;
588 return shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
852c2936 589}
This page took 0.061409 seconds and 4 git commands to generate.