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