| 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_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_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_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_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_ring_buffer_config *config, |
| 68 | struct lttng_ust_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_ring_buffer_config *config, |
| 73 | struct lttng_ust_ring_buffer_ctx *ctx, |
| 74 | const void *src, size_t len) |
| 75 | { |
| 76 | struct lttng_ust_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_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_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( |
| 113 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
| 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 | */ |
| 148 | static inline |
| 149 | void lib_ring_buffer_strcpy(const struct lttng_ust_ring_buffer_config *config, |
| 150 | struct lttng_ust_ring_buffer_ctx *ctx, |
| 151 | const char *src, size_t len, char pad) |
| 152 | __attribute__((always_inline)); |
| 153 | static inline |
| 154 | void lib_ring_buffer_strcpy(const struct lttng_ust_ring_buffer_config *config, |
| 155 | struct lttng_ust_ring_buffer_ctx *ctx, |
| 156 | const char *src, size_t len, char pad) |
| 157 | { |
| 158 | struct lttng_ust_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; |
| 161 | size_t count; |
| 162 | size_t offset = ctx_private->buf_offset; |
| 163 | struct lttng_ust_ring_buffer_backend_pages *backend_pages; |
| 164 | void *p; |
| 165 | |
| 166 | if (caa_unlikely(!len)) |
| 167 | return; |
| 168 | /* |
| 169 | * Underlying layer should never ask for writes across |
| 170 | * subbuffers. |
| 171 | */ |
| 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); |
| 183 | offset += count; |
| 184 | /* Padding */ |
| 185 | if (caa_unlikely(count < len - 1)) { |
| 186 | size_t pad_len = len - 1 - count; |
| 187 | |
| 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); |
| 192 | offset += pad_len; |
| 193 | } |
| 194 | /* Final '\0' */ |
| 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); |
| 199 | ctx_private->buf_offset += len; |
| 200 | } |
| 201 | |
| 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 | */ |
| 218 | static inline |
| 219 | void lib_ring_buffer_pstrcpy(const struct lttng_ust_ring_buffer_config *config, |
| 220 | struct lttng_ust_ring_buffer_ctx *ctx, |
| 221 | const char *src, size_t len, char pad) |
| 222 | __attribute__((always_inline)); |
| 223 | static inline |
| 224 | void lib_ring_buffer_pstrcpy(const struct lttng_ust_ring_buffer_config *config, |
| 225 | struct lttng_ust_ring_buffer_ctx *ctx, |
| 226 | const char *src, size_t len, char pad) |
| 227 | { |
| 228 | struct lttng_ust_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; |
| 231 | size_t count; |
| 232 | size_t offset = ctx_private->buf_offset; |
| 233 | struct lttng_ust_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 | } |
| 263 | ctx_private->buf_offset += len; |
| 264 | } |
| 265 | |
| 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 | */ |
| 271 | static inline |
| 272 | unsigned long lib_ring_buffer_get_records_unread( |
| 273 | const struct lttng_ust_ring_buffer_config *config, |
| 274 | struct lttng_ust_ring_buffer *buf, |
| 275 | struct lttng_ust_shm_handle *handle) |
| 276 | { |
| 277 | struct lttng_ust_ring_buffer_backend *bufb = &buf->backend; |
| 278 | unsigned long records_unread = 0, sb_bindex; |
| 279 | unsigned int i; |
| 280 | struct lttng_ust_ring_buffer_channel *chan; |
| 281 | |
| 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_ring_buffer_backend_subbuffer *wsb; |
| 287 | struct lttng_ust_ring_buffer_backend_pages_shmp *rpages; |
| 288 | struct lttng_ust_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); |
| 301 | } |
| 302 | if (config->mode == RING_BUFFER_OVERWRITE) { |
| 303 | struct lttng_ust_ring_buffer_backend_pages_shmp *rpages; |
| 304 | struct lttng_ust_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); |
| 314 | } |
| 315 | return records_unread; |
| 316 | } |
| 317 | |
| 318 | #endif /* _LTTNG_RING_BUFFER_BACKEND_H */ |