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