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 | ||
14641deb MD |
9 | #include <urcu/arch.h> |
10 | ||
11 | #include "ust/core.h" | |
12 | ||
8d8a24c8 | 13 | #include <ust/ringbuffer-config.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_free(struct lttng_ust_lib_ring_buffer_backend *bufb) |
852c2936 | 133 | { |
a6352fd4 MD |
134 | /* bufb->buf_wsb will be freed by shm teardown */ |
135 | /* bufb->array[i] will be freed by shm teardown */ | |
136 | /* bufb->array will be freed by shm teardown */ | |
852c2936 MD |
137 | bufb->allocated = 0; |
138 | } | |
139 | ||
4cfec15c | 140 | void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb, |
38fae1d3 | 141 | struct lttng_ust_shm_handle *handle) |
852c2936 | 142 | { |
1d498196 | 143 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 144 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
145 | unsigned long num_subbuf_alloc; |
146 | unsigned int i; | |
147 | ||
148 | num_subbuf_alloc = chanb->num_subbuf; | |
149 | if (chanb->extra_reader_sb) | |
150 | num_subbuf_alloc++; | |
151 | ||
152 | for (i = 0; i < chanb->num_subbuf; i++) | |
4746ae29 | 153 | shmp_index(handle, bufb->buf_wsb, i)->id = subbuffer_id(config, 0, 1, i); |
852c2936 MD |
154 | if (chanb->extra_reader_sb) |
155 | bufb->buf_rsb.id = subbuffer_id(config, 0, 1, | |
156 | num_subbuf_alloc - 1); | |
157 | else | |
158 | bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0); | |
159 | ||
160 | for (i = 0; i < num_subbuf_alloc; i++) { | |
161 | /* Don't reset mmap_offset */ | |
4746ae29 MD |
162 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->records_commit, 0); |
163 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->records_unread, 0); | |
164 | shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->data_size = 0; | |
852c2936 MD |
165 | /* Don't reset backend page and virt addresses */ |
166 | } | |
167 | /* Don't reset num_pages_per_subbuf, cpu, allocated */ | |
168 | v_set(config, &bufb->records_read, 0); | |
169 | } | |
170 | ||
171 | /* | |
172 | * The frontend is responsible for also calling ring_buffer_backend_reset for | |
173 | * each buffer when calling channel_backend_reset. | |
174 | */ | |
175 | void channel_backend_reset(struct channel_backend *chanb) | |
176 | { | |
14641deb | 177 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
4cfec15c | 178 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
179 | |
180 | /* | |
181 | * Don't reset buf_size, subbuf_size, subbuf_size_order, | |
182 | * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf, | |
183 | * priv, notifiers, config, cpumask and name. | |
184 | */ | |
185 | chanb->start_tsc = config->cb.ring_buffer_clock_read(chan); | |
186 | } | |
187 | ||
852c2936 MD |
188 | /** |
189 | * channel_backend_init - initialize a channel backend | |
190 | * @chanb: channel backend | |
191 | * @name: channel name | |
192 | * @config: client ring buffer configuration | |
193 | * @priv: client private data | |
194 | * @parent: dentry of parent directory, %NULL for root directory | |
195 | * @subbuf_size: size of sub-buffers (> PAGE_SIZE, power of 2) | |
196 | * @num_subbuf: number of sub-buffers (power of 2) | |
38fae1d3 | 197 | * @lttng_ust_shm_handle: shared memory handle |
852c2936 MD |
198 | * |
199 | * Returns channel pointer if successful, %NULL otherwise. | |
200 | * | |
201 | * Creates per-cpu channel buffers using the sizes and attributes | |
202 | * specified. The created channel buffer files will be named | |
203 | * name_0...name_N-1. File permissions will be %S_IRUSR. | |
204 | * | |
205 | * Called with CPU hotplug disabled. | |
206 | */ | |
207 | int channel_backend_init(struct channel_backend *chanb, | |
208 | const char *name, | |
4cfec15c | 209 | const struct lttng_ust_lib_ring_buffer_config *config, |
a6352fd4 | 210 | void *priv, size_t subbuf_size, size_t num_subbuf, |
38fae1d3 | 211 | struct lttng_ust_shm_handle *handle) |
852c2936 | 212 | { |
14641deb | 213 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
852c2936 MD |
214 | unsigned int i; |
215 | int ret; | |
6c1f7d8b | 216 | size_t shmsize = 0, num_subbuf_alloc; |
852c2936 MD |
217 | |
218 | if (!name) | |
219 | return -EPERM; | |
220 | ||
221 | if (!(subbuf_size && num_subbuf)) | |
222 | return -EPERM; | |
223 | ||
224 | /* Check that the subbuffer size is larger than a page. */ | |
225 | if (subbuf_size < PAGE_SIZE) | |
226 | return -EINVAL; | |
227 | ||
228 | /* | |
229 | * Make sure the number of subbuffers and subbuffer size are power of 2. | |
230 | */ | |
231 | CHAN_WARN_ON(chanb, hweight32(subbuf_size) != 1); | |
232 | CHAN_WARN_ON(chanb, hweight32(num_subbuf) != 1); | |
233 | ||
234 | ret = subbuffer_id_check_index(config, num_subbuf); | |
235 | if (ret) | |
236 | return ret; | |
237 | ||
238 | chanb->priv = priv; | |
239 | chanb->buf_size = num_subbuf * subbuf_size; | |
240 | chanb->subbuf_size = subbuf_size; | |
241 | chanb->buf_size_order = get_count_order(chanb->buf_size); | |
242 | chanb->subbuf_size_order = get_count_order(subbuf_size); | |
243 | chanb->num_subbuf_order = get_count_order(num_subbuf); | |
244 | chanb->extra_reader_sb = | |
245 | (config->mode == RING_BUFFER_OVERWRITE) ? 1 : 0; | |
246 | chanb->num_subbuf = num_subbuf; | |
a6352fd4 MD |
247 | strncpy(chanb->name, name, NAME_MAX); |
248 | chanb->name[NAME_MAX - 1] = '\0'; | |
193183fb | 249 | memcpy(&chanb->config, config, sizeof(*config)); |
852c2936 | 250 | |
1d498196 | 251 | /* Per-cpu buffer size: control (prior to backend) */ |
4cfec15c MD |
252 | shmsize = offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer)); |
253 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer); | |
1d498196 MD |
254 | |
255 | /* Per-cpu buffer size: backend */ | |
256 | /* num_subbuf + 1 is the worse case */ | |
257 | num_subbuf_alloc = num_subbuf + 1; | |
4cfec15c MD |
258 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp)); |
259 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc; | |
6c1f7d8b | 260 | shmsize += offset_align(shmsize, PAGE_SIZE); |
1d498196 | 261 | shmsize += subbuf_size * num_subbuf_alloc; |
4cfec15c MD |
262 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages)); |
263 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages) * num_subbuf_alloc; | |
264 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer)); | |
265 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer) * num_subbuf; | |
1d498196 MD |
266 | /* Per-cpu buffer size: control (after backend) */ |
267 | shmsize += offset_align(shmsize, __alignof__(struct commit_counters_hot)); | |
268 | shmsize += sizeof(struct commit_counters_hot) * num_subbuf; | |
269 | shmsize += offset_align(shmsize, __alignof__(struct commit_counters_cold)); | |
270 | shmsize += sizeof(struct commit_counters_cold) * num_subbuf; | |
271 | ||
852c2936 | 272 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
4cfec15c | 273 | struct lttng_ust_lib_ring_buffer *buf; |
852c2936 | 274 | /* |
a6352fd4 | 275 | * We need to allocate for all possible cpus. |
852c2936 | 276 | */ |
852c2936 | 277 | for_each_possible_cpu(i) { |
1d498196 MD |
278 | struct shm_object *shmobj; |
279 | ||
280 | shmobj = shm_object_table_append(handle->table, shmsize); | |
afdf9825 MD |
281 | if (!shmobj) |
282 | goto end; | |
4cfec15c MD |
283 | align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer)); |
284 | set_shmp(chanb->buf[i].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer))); | |
1d498196 MD |
285 | buf = shmp(handle, chanb->buf[i].shmp); |
286 | if (!buf) | |
287 | goto end; | |
5d61a504 | 288 | set_shmp(buf->self, chanb->buf[i].shmp._ref); |
1d498196 MD |
289 | ret = lib_ring_buffer_create(buf, chanb, i, |
290 | handle, shmobj); | |
852c2936 MD |
291 | if (ret) |
292 | goto free_bufs; /* cpu hotplug locked */ | |
293 | } | |
852c2936 | 294 | } else { |
1d498196 | 295 | struct shm_object *shmobj; |
4cfec15c | 296 | struct lttng_ust_lib_ring_buffer *buf; |
a6352fd4 | 297 | |
1d498196 | 298 | shmobj = shm_object_table_append(handle->table, shmsize); |
afdf9825 MD |
299 | if (!shmobj) |
300 | goto end; | |
4cfec15c MD |
301 | align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer)); |
302 | set_shmp(chanb->buf[0].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer))); | |
1d498196 | 303 | buf = shmp(handle, chanb->buf[0].shmp); |
a6352fd4 MD |
304 | if (!buf) |
305 | goto end; | |
1d498196 MD |
306 | ret = lib_ring_buffer_create(buf, chanb, -1, |
307 | handle, shmobj); | |
852c2936 MD |
308 | if (ret) |
309 | goto free_bufs; | |
310 | } | |
311 | chanb->start_tsc = config->cb.ring_buffer_clock_read(chan); | |
312 | ||
313 | return 0; | |
314 | ||
315 | free_bufs: | |
316 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
317 | for_each_possible_cpu(i) { | |
4cfec15c | 318 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chanb->buf[i].shmp); |
852c2936 MD |
319 | |
320 | if (!buf->backend.allocated) | |
321 | continue; | |
1d498196 | 322 | lib_ring_buffer_free(buf, handle); |
852c2936 | 323 | } |
a6352fd4 MD |
324 | } |
325 | /* We only free the buffer data upon shm teardown */ | |
326 | end: | |
852c2936 MD |
327 | return -ENOMEM; |
328 | } | |
329 | ||
852c2936 MD |
330 | /** |
331 | * channel_backend_free - destroy the channel | |
332 | * @chan: the channel | |
333 | * | |
334 | * Destroy all channel buffers and frees the channel. | |
335 | */ | |
1d498196 | 336 | void channel_backend_free(struct channel_backend *chanb, |
38fae1d3 | 337 | struct lttng_ust_shm_handle *handle) |
852c2936 | 338 | { |
4cfec15c | 339 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
340 | unsigned int i; |
341 | ||
342 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
343 | for_each_possible_cpu(i) { | |
4cfec15c | 344 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chanb->buf[i].shmp); |
852c2936 MD |
345 | |
346 | if (!buf->backend.allocated) | |
347 | continue; | |
1d498196 | 348 | lib_ring_buffer_free(buf, handle); |
852c2936 | 349 | } |
852c2936 | 350 | } else { |
4cfec15c | 351 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chanb->buf[0].shmp); |
852c2936 MD |
352 | |
353 | CHAN_WARN_ON(chanb, !buf->backend.allocated); | |
1d498196 | 354 | lib_ring_buffer_free(buf, handle); |
852c2936 | 355 | } |
a6352fd4 | 356 | /* We only free the buffer data upon shm teardown */ |
852c2936 MD |
357 | } |
358 | ||
852c2936 MD |
359 | /** |
360 | * lib_ring_buffer_read - read data from ring_buffer_buffer. | |
361 | * @bufb : buffer backend | |
362 | * @offset : offset within the buffer | |
363 | * @dest : destination address | |
364 | * @len : length to copy to destination | |
365 | * | |
366 | * Should be protected by get_subbuf/put_subbuf. | |
367 | * Returns the length copied. | |
368 | */ | |
4cfec15c | 369 | size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset, |
38fae1d3 | 370 | void *dest, size_t len, struct lttng_ust_shm_handle *handle) |
852c2936 | 371 | { |
1d498196 | 372 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 373 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
a6352fd4 | 374 | ssize_t orig_len; |
4cfec15c | 375 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
852c2936 MD |
376 | unsigned long sb_bindex, id; |
377 | ||
378 | orig_len = len; | |
379 | offset &= chanb->buf_size - 1; | |
a6352fd4 | 380 | |
b5a3dfa5 | 381 | if (caa_unlikely(!len)) |
852c2936 | 382 | return 0; |
a6352fd4 MD |
383 | id = bufb->buf_rsb.id; |
384 | sb_bindex = subbuffer_id_get_index(config, id); | |
4746ae29 | 385 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
a6352fd4 MD |
386 | /* |
387 | * Underlying layer should never ask for reads across | |
388 | * subbuffers. | |
389 | */ | |
390 | CHAN_WARN_ON(chanb, offset >= chanb->buf_size); | |
391 | CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE | |
392 | && subbuffer_id_is_noref(config, id)); | |
aead2025 | 393 | memcpy(dest, shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)), len); |
852c2936 MD |
394 | return orig_len; |
395 | } | |
852c2936 | 396 | |
852c2936 MD |
397 | /** |
398 | * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer. | |
399 | * @bufb : buffer backend | |
400 | * @offset : offset within the buffer | |
401 | * @dest : destination address | |
402 | * @len : destination's length | |
403 | * | |
404 | * return string's length | |
405 | * Should be protected by get_subbuf/put_subbuf. | |
406 | */ | |
4cfec15c | 407 | int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset, |
38fae1d3 | 408 | void *dest, size_t len, struct lttng_ust_shm_handle *handle) |
852c2936 | 409 | { |
1d498196 | 410 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 411 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
a6352fd4 | 412 | ssize_t string_len, orig_offset; |
852c2936 | 413 | char *str; |
4cfec15c | 414 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
852c2936 MD |
415 | unsigned long sb_bindex, id; |
416 | ||
417 | offset &= chanb->buf_size - 1; | |
852c2936 | 418 | orig_offset = offset; |
852c2936 MD |
419 | id = bufb->buf_rsb.id; |
420 | sb_bindex = subbuffer_id_get_index(config, id); | |
4746ae29 | 421 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
a6352fd4 MD |
422 | /* |
423 | * Underlying layer should never ask for reads across | |
424 | * subbuffers. | |
425 | */ | |
426 | CHAN_WARN_ON(chanb, offset >= chanb->buf_size); | |
852c2936 MD |
427 | CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE |
428 | && subbuffer_id_is_noref(config, id)); | |
aead2025 | 429 | str = shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)); |
a6352fd4 MD |
430 | string_len = strnlen(str, len); |
431 | if (dest && len) { | |
432 | memcpy(dest, str, string_len); | |
433 | ((char *)dest)[0] = 0; | |
434 | } | |
435 | return offset - orig_offset; | |
852c2936 | 436 | } |
852c2936 MD |
437 | |
438 | /** | |
439 | * lib_ring_buffer_read_offset_address - get address of a buffer location | |
440 | * @bufb : buffer backend | |
441 | * @offset : offset within the buffer. | |
442 | * | |
443 | * Return the address where a given offset is located (for read). | |
444 | * Should be used to get the current subbuffer header pointer. Given we know | |
445 | * it's never on a page boundary, it's safe to write directly to this address, | |
446 | * as long as the write is never bigger than a page size. | |
447 | */ | |
4cfec15c | 448 | void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 449 | size_t offset, |
38fae1d3 | 450 | struct lttng_ust_shm_handle *handle) |
852c2936 | 451 | { |
4cfec15c | 452 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
1d498196 | 453 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 454 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
455 | unsigned long sb_bindex, id; |
456 | ||
457 | offset &= chanb->buf_size - 1; | |
852c2936 MD |
458 | id = bufb->buf_rsb.id; |
459 | sb_bindex = subbuffer_id_get_index(config, id); | |
4746ae29 | 460 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
852c2936 MD |
461 | CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE |
462 | && subbuffer_id_is_noref(config, id)); | |
aead2025 | 463 | return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)); |
852c2936 | 464 | } |
852c2936 MD |
465 | |
466 | /** | |
467 | * lib_ring_buffer_offset_address - get address of a location within the buffer | |
468 | * @bufb : buffer backend | |
469 | * @offset : offset within the buffer. | |
470 | * | |
471 | * Return the address where a given offset is located. | |
472 | * Should be used to get the current subbuffer header pointer. Given we know | |
473 | * it's always at the beginning of a page, it's safe to write directly to this | |
474 | * address, as long as the write is never bigger than a page size. | |
475 | */ | |
4cfec15c | 476 | void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 477 | size_t offset, |
38fae1d3 | 478 | struct lttng_ust_shm_handle *handle) |
852c2936 | 479 | { |
a6352fd4 | 480 | size_t sbidx; |
4cfec15c | 481 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
1d498196 | 482 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 483 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
484 | unsigned long sb_bindex, id; |
485 | ||
486 | offset &= chanb->buf_size - 1; | |
487 | sbidx = offset >> chanb->subbuf_size_order; | |
4746ae29 | 488 | id = shmp_index(handle, bufb->buf_wsb, sbidx)->id; |
852c2936 | 489 | sb_bindex = subbuffer_id_get_index(config, id); |
4746ae29 | 490 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
852c2936 MD |
491 | CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE |
492 | && subbuffer_id_is_noref(config, id)); | |
aead2025 | 493 | return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)); |
852c2936 | 494 | } |