Namespace 'struct channel' under 'lttng_ust_lib_ring_buffer_'
[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 __attribute__((visibility("hidden")))
27 extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb,
28 size_t offset, void *dest, size_t len,
29 struct lttng_ust_shm_handle *handle);
30
31 __attribute__((visibility("hidden")))
32 extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb,
33 size_t offset, void *dest, size_t len,
34 struct lttng_ust_shm_handle *handle);
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 __attribute__((visibility("hidden")))
43 extern void *
44 lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
45 size_t offset,
46 struct lttng_ust_shm_handle *handle);
47
48 __attribute__((visibility("hidden")))
49 extern void *
50 lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
51 size_t offset,
52 struct lttng_ust_shm_handle *handle);
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 __attribute__((always_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 {
71 struct channel_backend *chanb = &ctx->chan->backend;
72 struct lttng_ust_shm_handle *handle = ctx->handle;
73 size_t offset = ctx->buf_offset;
74 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
75 void *p;
76
77 if (caa_unlikely(!len))
78 return;
79 /*
80 * Underlying layer should never ask for writes across
81 * subbuffers.
82 */
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 }
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);
93 ctx->buf_offset += len;
94 }
95
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 */
101 static inline __attribute__((always_inline))
102 size_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 */
137 static inline __attribute__((always_inline))
138 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config *config,
139 struct lttng_ust_lib_ring_buffer_ctx *ctx,
140 const char *src, size_t len, int pad)
141 {
142 struct channel_backend *chanb = &ctx->chan->backend;
143 struct lttng_ust_shm_handle *handle = ctx->handle;
144 size_t count;
145 size_t offset = ctx->buf_offset;
146 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
147 void *p;
148
149 if (caa_unlikely(!len))
150 return;
151 /*
152 * Underlying layer should never ask for writes across
153 * subbuffers.
154 */
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);
166 offset += count;
167 /* Padding */
168 if (caa_unlikely(count < len - 1)) {
169 size_t pad_len = len - 1 - count;
170
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);
175 offset += pad_len;
176 }
177 /* Final '\0' */
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);
182 ctx->buf_offset += len;
183 }
184
185 /*
186 * This accessor counts the number of unread records in a buffer.
187 * It only provides a consistent value if no reads not writes are performed
188 * concurrently.
189 */
190 static inline
191 unsigned long lib_ring_buffer_get_records_unread(
192 const struct lttng_ust_lib_ring_buffer_config *config,
193 struct lttng_ust_lib_ring_buffer *buf,
194 struct lttng_ust_shm_handle *handle)
195 {
196 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
197 unsigned long records_unread = 0, sb_bindex;
198 unsigned int i;
199 struct lttng_ust_lib_ring_buffer_channel *chan;
200
201 chan = shmp(handle, bufb->chan);
202 if (!chan)
203 return 0;
204 for (i = 0; i < chan->backend.num_subbuf; i++) {
205 struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb;
206 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
207 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
208
209 wsb = shmp_index(handle, bufb->buf_wsb, i);
210 if (!wsb)
211 return 0;
212 sb_bindex = subbuffer_id_get_index(config, wsb->id);
213 rpages = shmp_index(handle, bufb->array, sb_bindex);
214 if (!rpages)
215 return 0;
216 backend_pages = shmp(handle, rpages->shmp);
217 if (!backend_pages)
218 return 0;
219 records_unread += v_read(config, &backend_pages->records_unread);
220 }
221 if (config->mode == RING_BUFFER_OVERWRITE) {
222 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
223 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
224
225 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
226 rpages = shmp_index(handle, bufb->array, sb_bindex);
227 if (!rpages)
228 return 0;
229 backend_pages = shmp(handle, rpages->shmp);
230 if (!backend_pages)
231 return 0;
232 records_unread += v_read(config, &backend_pages->records_unread);
233 }
234 return records_unread;
235 }
236
237 #endif /* _LTTNG_RING_BUFFER_BACKEND_H */
This page took 0.033772 seconds and 4 git commands to generate.