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