Fix type range comparison always false for 64-bit arch
[lttng-ust.git] / libringbuffer / ring_buffer_backend.c
CommitLineData
852c2936
MD
1/*
2 * ring_buffer_backend.c
3 *
4 * Copyright (C) 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Dual LGPL v2.1/GPL v2 license.
7 */
8
5ad63a16 9#define _GNU_SOURCE
14641deb 10#include <urcu/arch.h>
96e80018 11#include <limits.h>
14641deb 12
4318ae1b 13#include <lttng/ringbuffer-config.h>
2fed87ae 14#include "vatomic.h"
4931a13e
MD
15#include "backend.h"
16#include "frontend.h"
a6352fd4 17#include "smp.h"
431d5cf0 18#include "shm.h"
852c2936
MD
19
20/**
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
27 */
28static
4cfec15c
MD
29int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config *config,
30 struct lttng_ust_lib_ring_buffer_backend *bufb,
852c2936 31 size_t size, size_t num_subbuf,
a6352fd4 32 int extra_reader_sb,
38fae1d3 33 struct lttng_ust_shm_handle *handle,
1d498196 34 struct shm_object *shmobj)
852c2936 35{
1d498196 36 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
852c2936
MD
37 unsigned long subbuf_size, mmap_offset = 0;
38 unsigned long num_subbuf_alloc;
852c2936
MD
39 unsigned long i;
40
852c2936
MD
41 subbuf_size = chanb->subbuf_size;
42 num_subbuf_alloc = num_subbuf;
43
a6352fd4 44 if (extra_reader_sb)
852c2936 45 num_subbuf_alloc++;
852c2936 46
4cfec15c 47 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp));
1d498196 48 set_shmp(bufb->array, zalloc_shm(shmobj,
4cfec15c 49 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc));
b5a3dfa5 50 if (caa_unlikely(!shmp(handle, bufb->array)))
852c2936
MD
51 goto array_error;
52
431d5cf0
MD
53 /*
54 * This is the largest element (the buffer pages) which needs to
55 * be aligned on PAGE_SIZE.
56 */
1d498196
MD
57 align_shm(shmobj, PAGE_SIZE);
58 set_shmp(bufb->memory_map, zalloc_shm(shmobj,
a6352fd4 59 subbuf_size * num_subbuf_alloc));
b5a3dfa5 60 if (caa_unlikely(!shmp(handle, bufb->memory_map)))
a6352fd4 61 goto memory_map_error;
852c2936
MD
62
63 /* Allocate backend pages array elements */
64 for (i = 0; i < num_subbuf_alloc; i++) {
4cfec15c 65 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages));
aead2025 66 set_shmp(shmp_index(handle, bufb->array, i)->shmp,
1d498196 67 zalloc_shm(shmobj,
4cfec15c 68 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages)));
4746ae29 69 if (!shmp(handle, shmp_index(handle, bufb->array, i)->shmp))
852c2936
MD
70 goto free_array;
71 }
72
73 /* Allocate write-side subbuffer table */
4cfec15c 74 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
1d498196 75 set_shmp(bufb->buf_wsb, zalloc_shm(shmobj,
4cfec15c 76 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer)
1d498196 77 * num_subbuf));
b5a3dfa5 78 if (caa_unlikely(!shmp(handle, bufb->buf_wsb)))
852c2936
MD
79 goto free_array;
80
81 for (i = 0; i < num_subbuf; i++)
4746ae29 82 shmp_index(handle, bufb->buf_wsb, i)->id = subbuffer_id(config, 0, 1, i);
852c2936
MD
83
84 /* Assign read-side subbuffer table */
85 if (extra_reader_sb)
86 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
87 num_subbuf_alloc - 1);
88 else
89 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
90
91 /* Assign pages to page index */
92 for (i = 0; i < num_subbuf_alloc; i++) {
1d498196
MD
93 struct shm_ref ref;
94
95 ref.index = bufb->memory_map._ref.index;
96 ref.offset = bufb->memory_map._ref.offset;
97 ref.offset += i * subbuf_size;
98
4746ae29 99 set_shmp(shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->p,
1d498196 100 ref);
852c2936 101 if (config->output == RING_BUFFER_MMAP) {
4746ae29 102 shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->mmap_offset = mmap_offset;
852c2936
MD
103 mmap_offset += subbuf_size;
104 }
105 }
106
852c2936
MD
107 return 0;
108
109free_array:
a6352fd4
MD
110 /* bufb->array[i] will be freed by shm teardown */
111memory_map_error:
112 /* bufb->array will be freed by shm teardown */
852c2936 113array_error:
852c2936
MD
114 return -ENOMEM;
115}
116
4cfec15c 117int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb,
a6352fd4 118 struct channel_backend *chanb, int cpu,
38fae1d3 119 struct lttng_ust_shm_handle *handle,
1d498196 120 struct shm_object *shmobj)
852c2936 121{
4cfec15c 122 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
852c2936 123
1d498196 124 set_shmp(bufb->chan, handle->chan._ref);
852c2936
MD
125 bufb->cpu = cpu;
126
127 return lib_ring_buffer_backend_allocate(config, bufb, chanb->buf_size,
128 chanb->num_subbuf,
a6352fd4 129 chanb->extra_reader_sb,
1d498196 130 handle, shmobj);
852c2936
MD
131}
132
4cfec15c 133void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb,
38fae1d3 134 struct lttng_ust_shm_handle *handle)
852c2936 135{
1d498196 136 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
4cfec15c 137 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
852c2936
MD
138 unsigned long num_subbuf_alloc;
139 unsigned int i;
140
141 num_subbuf_alloc = chanb->num_subbuf;
142 if (chanb->extra_reader_sb)
143 num_subbuf_alloc++;
144
145 for (i = 0; i < chanb->num_subbuf; i++)
4746ae29 146 shmp_index(handle, bufb->buf_wsb, i)->id = subbuffer_id(config, 0, 1, i);
852c2936
MD
147 if (chanb->extra_reader_sb)
148 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
149 num_subbuf_alloc - 1);
150 else
151 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
152
153 for (i = 0; i < num_subbuf_alloc; i++) {
154 /* Don't reset mmap_offset */
4746ae29
MD
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;
852c2936
MD
158 /* Don't reset backend page and virt addresses */
159 }
160 /* Don't reset num_pages_per_subbuf, cpu, allocated */
161 v_set(config, &bufb->records_read, 0);
162}
163
164/*
165 * The frontend is responsible for also calling ring_buffer_backend_reset for
166 * each buffer when calling channel_backend_reset.
167 */
168void channel_backend_reset(struct channel_backend *chanb)
169{
14641deb 170 struct channel *chan = caa_container_of(chanb, struct channel, backend);
4cfec15c 171 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
852c2936
MD
172
173 /*
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.
177 */
178 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
179}
180
852c2936
MD
181/**
182 * channel_backend_init - initialize a channel backend
183 * @chanb: channel backend
184 * @name: channel name
185 * @config: client ring buffer configuration
852c2936
MD
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)
38fae1d3 189 * @lttng_ust_shm_handle: shared memory handle
852c2936
MD
190 *
191 * Returns channel pointer if successful, %NULL otherwise.
192 *
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.
196 *
197 * Called with CPU hotplug disabled.
198 */
199int channel_backend_init(struct channel_backend *chanb,
200 const char *name,
4cfec15c 201 const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f 202 size_t subbuf_size, size_t num_subbuf,
38fae1d3 203 struct lttng_ust_shm_handle *handle)
852c2936 204{
14641deb 205 struct channel *chan = caa_container_of(chanb, struct channel, backend);
852c2936
MD
206 unsigned int i;
207 int ret;
6c1f7d8b 208 size_t shmsize = 0, num_subbuf_alloc;
852c2936
MD
209
210 if (!name)
211 return -EPERM;
212
213 if (!(subbuf_size && num_subbuf))
214 return -EPERM;
215
216 /* Check that the subbuffer size is larger than a page. */
217 if (subbuf_size < PAGE_SIZE)
218 return -EINVAL;
219
220 /*
221 * Make sure the number of subbuffers and subbuffer size are power of 2.
222 */
223 CHAN_WARN_ON(chanb, hweight32(subbuf_size) != 1);
224 CHAN_WARN_ON(chanb, hweight32(num_subbuf) != 1);
225
226 ret = subbuffer_id_check_index(config, num_subbuf);
227 if (ret)
228 return ret;
229
852c2936
MD
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;
a6352fd4
MD
238 strncpy(chanb->name, name, NAME_MAX);
239 chanb->name[NAME_MAX - 1] = '\0';
193183fb 240 memcpy(&chanb->config, config, sizeof(*config));
852c2936 241
1d498196 242 /* Per-cpu buffer size: control (prior to backend) */
4cfec15c
MD
243 shmsize = offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer));
244 shmsize += sizeof(struct lttng_ust_lib_ring_buffer);
1d498196
MD
245
246 /* Per-cpu buffer size: backend */
247 /* num_subbuf + 1 is the worse case */
248 num_subbuf_alloc = num_subbuf + 1;
4cfec15c
MD
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;
6c1f7d8b 251 shmsize += offset_align(shmsize, PAGE_SIZE);
1d498196 252 shmsize += subbuf_size * num_subbuf_alloc;
4cfec15c
MD
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;
1d498196
MD
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;
262
852c2936 263 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
4cfec15c 264 struct lttng_ust_lib_ring_buffer *buf;
852c2936 265 /*
a6352fd4 266 * We need to allocate for all possible cpus.
852c2936 267 */
852c2936 268 for_each_possible_cpu(i) {
1d498196
MD
269 struct shm_object *shmobj;
270
271 shmobj = shm_object_table_append(handle->table, shmsize);
afdf9825
MD
272 if (!shmobj)
273 goto end;
4cfec15c
MD
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)));
1d498196
MD
276 buf = shmp(handle, chanb->buf[i].shmp);
277 if (!buf)
278 goto end;
5d61a504 279 set_shmp(buf->self, chanb->buf[i].shmp._ref);
1d498196
MD
280 ret = lib_ring_buffer_create(buf, chanb, i,
281 handle, shmobj);
852c2936
MD
282 if (ret)
283 goto free_bufs; /* cpu hotplug locked */
284 }
852c2936 285 } else {
1d498196 286 struct shm_object *shmobj;
4cfec15c 287 struct lttng_ust_lib_ring_buffer *buf;
a6352fd4 288
1d498196 289 shmobj = shm_object_table_append(handle->table, shmsize);
afdf9825
MD
290 if (!shmobj)
291 goto end;
4cfec15c
MD
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)));
1d498196 294 buf = shmp(handle, chanb->buf[0].shmp);
a6352fd4
MD
295 if (!buf)
296 goto end;
cb14bae9 297 set_shmp(buf->self, chanb->buf[0].shmp._ref);
1d498196
MD
298 ret = lib_ring_buffer_create(buf, chanb, -1,
299 handle, shmobj);
852c2936
MD
300 if (ret)
301 goto free_bufs;
302 }
303 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
304
305 return 0;
306
307free_bufs:
a6352fd4
MD
308 /* We only free the buffer data upon shm teardown */
309end:
852c2936
MD
310 return -ENOMEM;
311}
312
852c2936
MD
313/**
314 * channel_backend_free - destroy the channel
315 * @chan: the channel
316 *
317 * Destroy all channel buffers and frees the channel.
318 */
1d498196 319void channel_backend_free(struct channel_backend *chanb,
38fae1d3 320 struct lttng_ust_shm_handle *handle)
852c2936 321{
45e9e699 322 /* SHM teardown takes care of everything */
852c2936
MD
323}
324
852c2936
MD
325/**
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
331 *
332 * Should be protected by get_subbuf/put_subbuf.
333 * Returns the length copied.
334 */
4cfec15c 335size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
38fae1d3 336 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
852c2936 337{
1d498196 338 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
4cfec15c 339 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
a6352fd4 340 ssize_t orig_len;
4cfec15c 341 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
852c2936
MD
342 unsigned long sb_bindex, id;
343
344 orig_len = len;
345 offset &= chanb->buf_size - 1;
a6352fd4 346
b5a3dfa5 347 if (caa_unlikely(!len))
852c2936 348 return 0;
a6352fd4
MD
349 id = bufb->buf_rsb.id;
350 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 351 rpages = shmp_index(handle, bufb->array, sb_bindex);
a6352fd4
MD
352 /*
353 * Underlying layer should never ask for reads across
354 * subbuffers.
355 */
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));
aead2025 359 memcpy(dest, shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)), len);
852c2936
MD
360 return orig_len;
361}
852c2936 362
852c2936
MD
363/**
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
369 *
370 * return string's length
371 * Should be protected by get_subbuf/put_subbuf.
372 */
4cfec15c 373int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
38fae1d3 374 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
852c2936 375{
1d498196 376 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
4cfec15c 377 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
a6352fd4 378 ssize_t string_len, orig_offset;
852c2936 379 char *str;
4cfec15c 380 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
852c2936
MD
381 unsigned long sb_bindex, id;
382
383 offset &= chanb->buf_size - 1;
852c2936 384 orig_offset = offset;
852c2936
MD
385 id = bufb->buf_rsb.id;
386 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 387 rpages = shmp_index(handle, bufb->array, sb_bindex);
a6352fd4
MD
388 /*
389 * Underlying layer should never ask for reads across
390 * subbuffers.
391 */
392 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
852c2936
MD
393 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
394 && subbuffer_id_is_noref(config, id));
aead2025 395 str = shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
a6352fd4
MD
396 string_len = strnlen(str, len);
397 if (dest && len) {
398 memcpy(dest, str, string_len);
399 ((char *)dest)[0] = 0;
400 }
401 return offset - orig_offset;
852c2936 402}
852c2936
MD
403
404/**
405 * lib_ring_buffer_read_offset_address - get address of a buffer location
406 * @bufb : buffer backend
407 * @offset : offset within the buffer.
408 *
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.
413 */
4cfec15c 414void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 415 size_t offset,
38fae1d3 416 struct lttng_ust_shm_handle *handle)
852c2936 417{
4cfec15c 418 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
1d498196 419 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
4cfec15c 420 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
852c2936
MD
421 unsigned long sb_bindex, id;
422
423 offset &= chanb->buf_size - 1;
852c2936
MD
424 id = bufb->buf_rsb.id;
425 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 426 rpages = shmp_index(handle, bufb->array, sb_bindex);
852c2936
MD
427 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
428 && subbuffer_id_is_noref(config, id));
aead2025 429 return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
852c2936 430}
852c2936
MD
431
432/**
433 * lib_ring_buffer_offset_address - get address of a location within the buffer
434 * @bufb : buffer backend
435 * @offset : offset within the buffer.
436 *
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.
441 */
4cfec15c 442void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 443 size_t offset,
38fae1d3 444 struct lttng_ust_shm_handle *handle)
852c2936 445{
a6352fd4 446 size_t sbidx;
4cfec15c 447 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
1d498196 448 struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend;
4cfec15c 449 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
852c2936
MD
450 unsigned long sb_bindex, id;
451
452 offset &= chanb->buf_size - 1;
453 sbidx = offset >> chanb->subbuf_size_order;
4746ae29 454 id = shmp_index(handle, bufb->buf_wsb, sbidx)->id;
852c2936 455 sb_bindex = subbuffer_id_get_index(config, id);
4746ae29 456 rpages = shmp_index(handle, bufb->array, sb_bindex);
852c2936
MD
457 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
458 && subbuffer_id_is_noref(config, id));
aead2025 459 return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
852c2936 460}
This page took 0.049207 seconds and 4 git commands to generate.