Move private ABI counter client symbols to dedicated header
[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
ddabe860 26__attribute__((visibility("hidden")))
4cfec15c 27extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 28 size_t offset, void *dest, size_t len,
38fae1d3 29 struct lttng_ust_shm_handle *handle);
852c2936 30
ddabe860 31__attribute__((visibility("hidden")))
4cfec15c 32extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 33 size_t offset, void *dest, size_t len,
38fae1d3 34 struct lttng_ust_shm_handle *handle);
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 */
ddabe860 42__attribute__((visibility("hidden")))
852c2936 43extern void *
4cfec15c 44lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 45 size_t offset,
38fae1d3 46 struct lttng_ust_shm_handle *handle);
ddabe860
MJ
47
48__attribute__((visibility("hidden")))
852c2936 49extern void *
4cfec15c 50lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 51 size_t offset,
38fae1d3 52 struct lttng_ust_shm_handle *handle);
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 */
00d0f8eb 66static inline __attribute__((always_inline))
4cfec15c
MD
67void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config *config,
68 struct lttng_ust_lib_ring_buffer_ctx *ctx,
852c2936
MD
69 const void *src, size_t len)
70{
852c2936 71 struct channel_backend *chanb = &ctx->chan->backend;
aab8b172 72 struct lttng_ust_shm_handle *handle = ctx->chan->handle;
852c2936 73 size_t offset = ctx->buf_offset;
15500a1b
MD
74 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
75 void *p;
852c2936 76
0bf3c920
MD
77 if (caa_unlikely(!len))
78 return;
a6352fd4
MD
79 /*
80 * Underlying layer should never ask for writes across
81 * subbuffers.
82 */
a3492932
MD
83 CHAN_WARN_ON(chanb, (offset & (chanb->buf_size - 1)) + len > chanb->buf_size);
84 backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
85 if (caa_unlikely(!backend_pages)) {
86 if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages))
87 return;
88 }
15500a1b
MD
89 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
90 if (caa_unlikely(!p))
91 return;
92 lib_ring_buffer_do_copy(config, p, src, len);
852c2936
MD
93 ctx->buf_offset += len;
94}
95
a44c74d9
MD
96/*
97 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
98 * terminating character is found in @src. Returns the number of bytes
99 * copied. Does *not* terminate @dest with NULL terminating character.
100 */
00d0f8eb 101static inline __attribute__((always_inline))
a44c74d9
MD
102size_t lib_ring_buffer_do_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
103 char *dest, const char *src, size_t len)
104{
105 size_t count;
106
107 for (count = 0; count < len; count++) {
108 char c;
109
110 /*
111 * Only read source character once, in case it is
112 * modified concurrently.
113 */
114 c = CMM_LOAD_SHARED(src[count]);
115 if (!c)
116 break;
117 lib_ring_buffer_do_copy(config, &dest[count], &c, 1);
118 }
119 return count;
120}
121
122/**
123 * lib_ring_buffer_strcpy - write string data to a buffer backend
124 * @config : ring buffer instance configuration
125 * @ctx: ring buffer context. (input arguments only)
126 * @src : source pointer to copy from
127 * @len : length of data to copy
128 * @pad : character to use for padding
129 *
130 * This function copies @len - 1 bytes of string data from a source
131 * pointer to a buffer backend, followed by a terminating '\0'
132 * character, at the current context offset. This is more or less a
133 * buffer backend-specific strncpy() operation. If a terminating '\0'
134 * character is found in @src before @len - 1 characters are copied, pad
135 * the buffer with @pad characters (e.g. '#').
136 */
00d0f8eb 137static inline __attribute__((always_inline))
a44c74d9
MD
138void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
139 struct lttng_ust_lib_ring_buffer_ctx *ctx,
b4c8bf2f 140 const char *src, size_t len, char pad)
a44c74d9 141{
a44c74d9 142 struct channel_backend *chanb = &ctx->chan->backend;
aab8b172 143 struct lttng_ust_shm_handle *handle = ctx->chan->handle;
a3492932 144 size_t count;
a44c74d9 145 size_t offset = ctx->buf_offset;
a3492932
MD
146 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
147 void *p;
a44c74d9
MD
148
149 if (caa_unlikely(!len))
150 return;
a44c74d9
MD
151 /*
152 * Underlying layer should never ask for writes across
153 * subbuffers.
154 */
a3492932
MD
155 CHAN_WARN_ON(chanb, (offset & (chanb->buf_size - 1)) + len > chanb->buf_size);
156 backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
157 if (caa_unlikely(!backend_pages)) {
158 if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages))
159 return;
160 }
161 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
162 if (caa_unlikely(!p))
163 return;
164
165 count = lib_ring_buffer_do_strcpy(config, p, src, len - 1);
a44c74d9
MD
166 offset += count;
167 /* Padding */
168 if (caa_unlikely(count < len - 1)) {
169 size_t pad_len = len - 1 - count;
170
a3492932
MD
171 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
172 if (caa_unlikely(!p))
173 return;
174 lib_ring_buffer_do_memset(p, pad, pad_len);
a44c74d9
MD
175 offset += pad_len;
176 }
177 /* Final '\0' */
a3492932
MD
178 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
179 if (caa_unlikely(!p))
180 return;
181 lib_ring_buffer_do_memset(p, '\0', 1);
a44c74d9
MD
182 ctx->buf_offset += len;
183}
184
879f9b0a
MD
185/**
186 * lib_ring_buffer_pstrcpy - write to a buffer backend P-string
187 * @config : ring buffer instance configuration
188 * @ctx: ring buffer context. (input arguments only)
189 * @src : source pointer to copy from
190 * @len : length of data to copy
191 * @pad : character to use for padding
192 *
193 * This function copies up to @len bytes of data from a source pointer
194 * to a Pascal String into the buffer backend. If a terminating '\0'
195 * character is found in @src before @len characters are copied, pad the
196 * buffer with @pad characters (e.g. '\0').
197 *
198 * The length of the pascal strings in the ring buffer is explicit: it
199 * is either the array or sequence length.
200 */
201static inline __attribute__((always_inline))
202void lib_ring_buffer_pstrcpy(const struct lttng_ust_lib_ring_buffer_config *config,
203 struct lttng_ust_lib_ring_buffer_ctx *ctx,
204 const char *src, size_t len, char pad)
205{
206 struct channel_backend *chanb = &ctx->chan->backend;
aab8b172 207 struct lttng_ust_shm_handle *handle = ctx->chan->handle;
879f9b0a
MD
208 size_t count;
209 size_t offset = ctx->buf_offset;
210 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
211 void *p;
212
213 if (caa_unlikely(!len))
214 return;
215 /*
216 * Underlying layer should never ask for writes across
217 * subbuffers.
218 */
219 CHAN_WARN_ON(chanb, (offset & (chanb->buf_size - 1)) + len > chanb->buf_size);
220 backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx);
221 if (caa_unlikely(!backend_pages)) {
222 if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages))
223 return;
224 }
225 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
226 if (caa_unlikely(!p))
227 return;
228
229 count = lib_ring_buffer_do_strcpy(config, p, src, len);
230 offset += count;
231 /* Padding */
232 if (caa_unlikely(count < len)) {
233 size_t pad_len = len - count;
234
235 p = shmp_index(handle, backend_pages->p, offset & (chanb->subbuf_size - 1));
236 if (caa_unlikely(!p))
237 return;
238 lib_ring_buffer_do_memset(p, pad, pad_len);
239 }
240 ctx->buf_offset += len;
241}
242
852c2936
MD
243/*
244 * This accessor counts the number of unread records in a buffer.
245 * It only provides a consistent value if no reads not writes are performed
246 * concurrently.
247 */
248static inline
249unsigned long lib_ring_buffer_get_records_unread(
4cfec15c
MD
250 const struct lttng_ust_lib_ring_buffer_config *config,
251 struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 252 struct lttng_ust_shm_handle *handle)
852c2936 253{
4cfec15c 254 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
15500a1b 255 unsigned long records_unread = 0, sb_bindex;
852c2936 256 unsigned int i;
5198080d 257 struct lttng_ust_lib_ring_buffer_channel *chan;
852c2936 258
15500a1b
MD
259 chan = shmp(handle, bufb->chan);
260 if (!chan)
261 return 0;
262 for (i = 0; i < chan->backend.num_subbuf; i++) {
263 struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb;
264 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
265 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
266
267 wsb = shmp_index(handle, bufb->buf_wsb, i);
268 if (!wsb)
269 return 0;
270 sb_bindex = subbuffer_id_get_index(config, wsb->id);
271 rpages = shmp_index(handle, bufb->array, sb_bindex);
272 if (!rpages)
273 return 0;
274 backend_pages = shmp(handle, rpages->shmp);
275 if (!backend_pages)
276 return 0;
277 records_unread += v_read(config, &backend_pages->records_unread);
852c2936
MD
278 }
279 if (config->mode == RING_BUFFER_OVERWRITE) {
15500a1b
MD
280 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
281 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
282
283 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
284 rpages = shmp_index(handle, bufb->array, sb_bindex);
285 if (!rpages)
286 return 0;
287 backend_pages = shmp(handle, rpages->shmp);
288 if (!backend_pages)
289 return 0;
290 records_unread += v_read(config, &backend_pages->records_unread);
852c2936
MD
291 }
292 return records_unread;
293}
294
e92f3e28 295#endif /* _LTTNG_RING_BUFFER_BACKEND_H */
This page took 0.042674 seconds and 4 git commands to generate.