Fix: pass private data to context callbacks
[lttng-ust.git] / libringbuffer / backend.h
CommitLineData
852c2936 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
852c2936 3 *
e92f3e28
MD
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * Ring buffer backend (API).
852c2936
MD
7 *
8 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
9 * the reader in flight recorder mode.
10 */
11
c0c0989a
MJ
12#ifndef _LTTNG_RING_BUFFER_BACKEND_H
13#define _LTTNG_RING_BUFFER_BACKEND_H
14
b4051ad8 15#include <stddef.h>
14641deb
MD
16#include <unistd.h>
17
852c2936 18/* Internal helpers */
4931a13e
MD
19#include "backend_internal.h"
20#include "frontend_internal.h"
852c2936
MD
21
22/* Ring buffer backend API */
23
24/* Ring buffer backend access (read/write) */
25
4cfec15c 26extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 27 size_t offset, void *dest, size_t len,
1d18d519
MJ
28 struct lttng_ust_shm_handle *handle)
29 __attribute__((visibility("hidden")));
852c2936 30
4cfec15c 31extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 32 size_t offset, void *dest, size_t len,
1d18d519
MJ
33 struct lttng_ust_shm_handle *handle)
34 __attribute__((visibility("hidden")));
852c2936 35
852c2936
MD
36/*
37 * Return the address where a given offset is located.
38 * Should be used to get the current subbuffer header pointer. Given we know
39 * it's never on a page boundary, it's safe to write directly to this address,
40 * as long as the write is never bigger than a page size.
41 */
42extern void *
4cfec15c 43lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 44 size_t offset,
1d18d519
MJ
45 struct lttng_ust_shm_handle *handle)
46 __attribute__((visibility("hidden")));
ddabe860 47
852c2936 48extern void *
4cfec15c 49lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 50 size_t offset,
1d18d519
MJ
51 struct lttng_ust_shm_handle *handle)
52 __attribute__((visibility("hidden")));
852c2936
MD
53
54/**
55 * lib_ring_buffer_write - write data to a buffer backend
56 * @config : ring buffer instance configuration
57 * @ctx: ring buffer context. (input arguments only)
58 * @src : source pointer to copy from
59 * @len : length of data to copy
60 *
61 * This function copies "len" bytes of data from a source pointer to a buffer
62 * backend, at the current context offset. This is more or less a buffer
63 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
64 * if copy is crossing a page boundary.
65 */
5f6daaef
MJ
66static inline
67void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config *config,
68 struct lttng_ust_lib_ring_buffer_ctx *ctx,
69 const void *src, size_t len)
70 __attribute__((always_inline));
71static inline
4cfec15c
MD
72void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config *config,
73 struct lttng_ust_lib_ring_buffer_ctx *ctx,
852c2936
MD
74 const void *src, size_t len)
75{
8936b6c0
MD
76 struct lttng_ust_lib_ring_buffer_ctx_private *ctx_private = ctx->priv;
77 struct channel_backend *chanb = &ctx_private->chan->backend;
78 struct lttng_ust_shm_handle *handle = ctx_private->chan->handle;
79 size_t offset = ctx_private->buf_offset;
15500a1b
MD
80 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
81 void *p;
852c2936 82
0bf3c920
MD
83 if (caa_unlikely(!len))
84 return;
a6352fd4
MD
85 /*
86 * Underlying layer should never ask for writes across
87 * subbuffers.
88 */
a3492932
MD
89 CHAN_WARN_ON(chanb, (offset & (chanb->buf_size - 1)) + len > chanb->buf_size);
90 backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
91 if (caa_unlikely(!backend_pages)) {
92 if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages))
93 return;
94 }
15500a1b
MD
95 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
96 if (caa_unlikely(!p))
97 return;
98 lib_ring_buffer_do_copy(config, p, src, len);
8936b6c0 99 ctx_private->buf_offset += len;
852c2936
MD
100}
101
a44c74d9
MD
102/*
103 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
104 * terminating character is found in @src. Returns the number of bytes
105 * copied. Does *not* terminate @dest with NULL terminating character.
106 */
5f6daaef
MJ
107static inline
108size_t lib_ring_buffer_do_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
109 char *dest, const char *src, size_t len)
110 __attribute__((always_inline));
111static inline
2208d8b5
MJ
112size_t lib_ring_buffer_do_strcpy(
113 const struct lttng_ust_lib_ring_buffer_config *config __attribute__((unused)),
a44c74d9
MD
114 char *dest, const char *src, size_t len)
115{
116 size_t count;
117
118 for (count = 0; count < len; count++) {
119 char c;
120
121 /*
122 * Only read source character once, in case it is
123 * modified concurrently.
124 */
125 c = CMM_LOAD_SHARED(src[count]);
126 if (!c)
127 break;
128 lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
129 }
130 return count;
131}
132
133/**
134 * lib_ring_buffer_strcpy - write string data to a buffer backend
135 * @config : ring buffer instance configuration
136 * @ctx: ring buffer context. (input arguments only)
137 * @src : source pointer to copy from
138 * @len : length of data to copy
139 * @pad : character to use for padding
140 *
141 * This function copies @len - 1 bytes of string data from a source
142 * pointer to a buffer backend, followed by a terminating '\0'
143 * character, at the current context offset. This is more or less a
144 * buffer backend-specific strncpy() operation. If a terminating '\0'
145 * character is found in @src before @len - 1 characters are copied, pad
146 * the buffer with @pad characters (e.g. '#').
147 */
5f6daaef
MJ
148static inline
149void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
150 struct lttng_ust_lib_ring_buffer_ctx *ctx,
151 const char *src, size_t len, char pad)
152 __attribute__((always_inline));
153static inline
a44c74d9
MD
154void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
155 struct lttng_ust_lib_ring_buffer_ctx *ctx,
b4c8bf2f 156 const char *src, size_t len, char pad)
a44c74d9 157{
8936b6c0
MD
158 struct lttng_ust_lib_ring_buffer_ctx_private *ctx_private = ctx->priv;
159 struct channel_backend *chanb = &ctx_private->chan->backend;
160 struct lttng_ust_shm_handle *handle = ctx_private->chan->handle;
a3492932 161 size_t count;
8936b6c0 162 size_t offset = ctx_private->buf_offset;
a3492932
MD
163 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
164 void *p;
a44c74d9
MD
165
166 if (caa_unlikely(!len))
167 return;
a44c74d9
MD
168 /*
169 * Underlying layer should never ask for writes across
170 * subbuffers.
171 */
a3492932
MD
172 CHAN_WARN_ON(chanb, (offset & (chanb->buf_size - 1)) + len > chanb->buf_size);
173 backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
174 if (caa_unlikely(!backend_pages)) {
175 if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages))
176 return;
177 }
178 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
179 if (caa_unlikely(!p))
180 return;
181
182 count = lib_ring_buffer_do_strcpy(config, p, src, len - 1);
a44c74d9
MD
183 offset += count;
184 /* Padding */
185 if (caa_unlikely(count < len - 1)) {
186 size_t pad_len = len - 1 - count;
187
a3492932
MD
188 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
189 if (caa_unlikely(!p))
190 return;
191 lib_ring_buffer_do_memset(p, pad, pad_len);
a44c74d9
MD
192 offset += pad_len;
193 }
194 /* Final '\0' */
a3492932
MD
195 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
196 if (caa_unlikely(!p))
197 return;
198 lib_ring_buffer_do_memset(p, '\0', 1);
8936b6c0 199 ctx_private->buf_offset += len;
a44c74d9
MD
200}
201
879f9b0a
MD
202/**
203 * lib_ring_buffer_pstrcpy - write to a buffer backend P-string
204 * @config : ring buffer instance configuration
205 * @ctx: ring buffer context. (input arguments only)
206 * @src : source pointer to copy from
207 * @len : length of data to copy
208 * @pad : character to use for padding
209 *
210 * This function copies up to @len bytes of data from a source pointer
211 * to a Pascal String into the buffer backend. If a terminating '\0'
212 * character is found in @src before @len characters are copied, pad the
213 * buffer with @pad characters (e.g. '\0').
214 *
215 * The length of the pascal strings in the ring buffer is explicit: it
216 * is either the array or sequence length.
217 */
5f6daaef
MJ
218static inline
219void lib_ring_buffer_pstrcpy(const struct lttng_ust_lib_ring_buffer_config *config,
220 struct lttng_ust_lib_ring_buffer_ctx *ctx,
221 const char *src, size_t len, char pad)
222 __attribute__((always_inline));
223static inline
879f9b0a
MD
224void lib_ring_buffer_pstrcpy(const struct lttng_ust_lib_ring_buffer_config *config,
225 struct lttng_ust_lib_ring_buffer_ctx *ctx,
226 const char *src, size_t len, char pad)
227{
8936b6c0
MD
228 struct lttng_ust_lib_ring_buffer_ctx_private *ctx_private = ctx->priv;
229 struct channel_backend *chanb = &ctx_private->chan->backend;
230 struct lttng_ust_shm_handle *handle = ctx_private->chan->handle;
879f9b0a 231 size_t count;
8936b6c0 232 size_t offset = ctx_private->buf_offset;
879f9b0a
MD
233 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
234 void *p;
235
236 if (caa_unlikely(!len))
237 return;
238 /*
239 * Underlying layer should never ask for writes across
240 * subbuffers.
241 */
242 CHAN_WARN_ON(chanb, (offset & (chanb->buf_size - 1)) + len > chanb->buf_size);
243 backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
244 if (caa_unlikely(!backend_pages)) {
245 if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages))
246 return;
247 }
248 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
249 if (caa_unlikely(!p))
250 return;
251
252 count = lib_ring_buffer_do_strcpy(config, p, src, len);
253 offset += count;
254 /* Padding */
255 if (caa_unlikely(count < len)) {
256 size_t pad_len = len - count;
257
258 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
259 if (caa_unlikely(!p))
260 return;
261 lib_ring_buffer_do_memset(p, pad, pad_len);
262 }
8936b6c0 263 ctx_private->buf_offset += len;
879f9b0a
MD
264}
265
852c2936
MD
266/*
267 * This accessor counts the number of unread records in a buffer.
268 * It only provides a consistent value if no reads not writes are performed
269 * concurrently.
270 */
271static inline
272unsigned long lib_ring_buffer_get_records_unread(
4cfec15c
MD
273 const struct lttng_ust_lib_ring_buffer_config *config,
274 struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 275 struct lttng_ust_shm_handle *handle)
852c2936 276{
4cfec15c 277 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
15500a1b 278 unsigned long records_unread = 0, sb_bindex;
852c2936 279 unsigned int i;
5198080d 280 struct lttng_ust_lib_ring_buffer_channel *chan;
852c2936 281
15500a1b
MD
282 chan = shmp(handle, bufb->chan);
283 if (!chan)
284 return 0;
285 for (i = 0; i < chan->backend.num_subbuf; i++) {
286 struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb;
287 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
288 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
289
290 wsb = shmp_index(handle, bufb->buf_wsb, i);
291 if (!wsb)
292 return 0;
293 sb_bindex = subbuffer_id_get_index(config, wsb->id);
294 rpages = shmp_index(handle, bufb->array, sb_bindex);
295 if (!rpages)
296 return 0;
297 backend_pages = shmp(handle, rpages->shmp);
298 if (!backend_pages)
299 return 0;
300 records_unread += v_read(config, &backend_pages->records_unread);
852c2936
MD
301 }
302 if (config->mode == RING_BUFFER_OVERWRITE) {
15500a1b
MD
303 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
304 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
305
306 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
307 rpages = shmp_index(handle, bufb->array, sb_bindex);
308 if (!rpages)
309 return 0;
310 backend_pages = shmp(handle, rpages->shmp);
311 if (!backend_pages)
312 return 0;
313 records_unread += v_read(config, &backend_pages->records_unread);
852c2936
MD
314 }
315 return records_unread;
316}
317
e92f3e28 318#endif /* _LTTNG_RING_BUFFER_BACKEND_H */
This page took 0.045562 seconds and 4 git commands to generate.