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