Remove LTTNG_HIDDEN macro
[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;
38fae1d3 72 struct lttng_ust_shm_handle *handle = ctx->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,
140 const char *src, size_t len, int pad)
141{
a44c74d9
MD
142 struct channel_backend *chanb = &ctx->chan->backend;
143 struct lttng_ust_shm_handle *handle = ctx->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
852c2936
MD
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 */
190static inline
191unsigned long lib_ring_buffer_get_records_unread(
4cfec15c
MD
192 const struct lttng_ust_lib_ring_buffer_config *config,
193 struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 194 struct lttng_ust_shm_handle *handle)
852c2936 195{
4cfec15c 196 struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend;
15500a1b 197 unsigned long records_unread = 0, sb_bindex;
852c2936 198 unsigned int i;
15500a1b 199 struct channel *chan;
852c2936 200
15500a1b
MD
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);
852c2936
MD
220 }
221 if (config->mode == RING_BUFFER_OVERWRITE) {
15500a1b
MD
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);
852c2936
MD
233 }
234 return records_unread;
235}
236
e92f3e28 237#endif /* _LTTNG_RING_BUFFER_BACKEND_H */
This page took 0.041645 seconds and 4 git commands to generate.