Refactoring: hide internal fields of ring buffer context
[lttng-ust.git] / liblttng-ust / lttng-ring-buffer-metadata-client.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng lib ring buffer client template.
7 */
8
9 #include <limits.h>
10 #include <stddef.h>
11 #include <stdint.h>
12
13 #include <ust-events-internal.h>
14 #include "ust-bitfield.h"
15 #include "ust-compat.h"
16 #include "lttng-tracer.h"
17 #include "../libringbuffer/frontend_types.h"
18 #include <urcu/tls-compat.h>
19
20 struct metadata_packet_header {
21 uint32_t magic; /* 0x75D11D57 */
22 uint8_t uuid[LTTNG_UST_UUID_LEN]; /* Unique Universal Identifier */
23 uint32_t checksum; /* 0 if unused */
24 uint32_t content_size; /* in bits */
25 uint32_t packet_size; /* in bits */
26 uint8_t compression_scheme; /* 0 if unused */
27 uint8_t encryption_scheme; /* 0 if unused */
28 uint8_t checksum_scheme; /* 0 if unused */
29 uint8_t major; /* CTF spec major version number */
30 uint8_t minor; /* CTF spec minor version number */
31 uint8_t header_end[0];
32 };
33
34 struct metadata_record_header {
35 uint8_t header_end[0]; /* End of header */
36 };
37
38 static const struct lttng_ust_lib_ring_buffer_config client_config;
39
40 /* No nested use supported for metadata ring buffer. */
41 static DEFINE_URCU_TLS(struct lttng_ust_lib_ring_buffer_ctx_private, private_ctx);
42
43 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan)
44 {
45 return 0;
46 }
47
48 static inline
49 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
50 struct lttng_ust_lib_ring_buffer_channel *chan,
51 size_t offset,
52 size_t *pre_header_padding,
53 struct lttng_ust_lib_ring_buffer_ctx *ctx,
54 void *client_ctx)
55 {
56 return 0;
57 }
58
59 #include "../libringbuffer/api.h"
60 #include "lttng-rb-clients.h"
61
62 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan)
63 {
64 return 0;
65 }
66
67 static
68 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
69 struct lttng_ust_lib_ring_buffer_channel *chan,
70 size_t offset,
71 size_t *pre_header_padding,
72 struct lttng_ust_lib_ring_buffer_ctx *ctx,
73 void *client_ctx)
74 {
75 return 0;
76 }
77
78 /**
79 * client_packet_header_size - called on buffer-switch to a new sub-buffer
80 *
81 * Return header size without padding after the structure. Don't use packed
82 * structure because gcc generates inefficient code on some architectures
83 * (powerpc, mips..)
84 */
85 static size_t client_packet_header_size(void)
86 {
87 return offsetof(struct metadata_packet_header, header_end);
88 }
89
90 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
91 unsigned int subbuf_idx,
92 struct lttng_ust_shm_handle *handle)
93 {
94 struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan);
95 struct metadata_packet_header *header =
96 (struct metadata_packet_header *)
97 lib_ring_buffer_offset_address(&buf->backend,
98 subbuf_idx * chan->backend.subbuf_size,
99 handle);
100 struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan);
101
102 assert(header);
103 if (!header)
104 return;
105 header->magic = TSDL_MAGIC_NUMBER;
106 memcpy(header->uuid, lttng_chan->priv->uuid, sizeof(lttng_chan->priv->uuid));
107 header->checksum = 0; /* 0 if unused */
108 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
109 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
110 header->compression_scheme = 0; /* 0 if unused */
111 header->encryption_scheme = 0; /* 0 if unused */
112 header->checksum_scheme = 0; /* 0 if unused */
113 header->major = CTF_SPEC_MAJOR;
114 header->minor = CTF_SPEC_MINOR;
115 }
116
117 /*
118 * offset is assumed to never be 0 here : never deliver a completely empty
119 * subbuffer. data_size is between 1 and subbuf_size.
120 */
121 static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
122 unsigned int subbuf_idx, unsigned long data_size,
123 struct lttng_ust_shm_handle *handle)
124 {
125 struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan);
126 struct metadata_packet_header *header =
127 (struct metadata_packet_header *)
128 lib_ring_buffer_offset_address(&buf->backend,
129 subbuf_idx * chan->backend.subbuf_size,
130 handle);
131 unsigned long records_lost = 0;
132
133 assert(header);
134 if (!header)
135 return;
136 header->content_size = data_size * CHAR_BIT; /* in bits */
137 header->packet_size = LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
138 /*
139 * We do not care about the records lost count, because the metadata
140 * channel waits and retry.
141 */
142 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
143 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
144 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
145 WARN_ON_ONCE(records_lost != 0);
146 }
147
148 static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
149 int cpu, const char *name,
150 struct lttng_ust_shm_handle *handle)
151 {
152 return 0;
153 }
154
155 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf,
156 void *priv, int cpu,
157 struct lttng_ust_shm_handle *handle)
158 {
159 }
160
161 static const
162 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = {
163 .parent = {
164 .ring_buffer_clock_read = client_ring_buffer_clock_read,
165 .record_header_size = client_record_header_size,
166 .subbuffer_header_size = client_packet_header_size,
167 .buffer_begin = client_buffer_begin,
168 .buffer_end = client_buffer_end,
169 .buffer_create = client_buffer_create,
170 .buffer_finalize = client_buffer_finalize,
171 },
172 };
173
174 static const struct lttng_ust_lib_ring_buffer_config client_config = {
175 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
176 .cb.record_header_size = client_record_header_size,
177 .cb.subbuffer_header_size = client_packet_header_size,
178 .cb.buffer_begin = client_buffer_begin,
179 .cb.buffer_end = client_buffer_end,
180 .cb.buffer_create = client_buffer_create,
181 .cb.buffer_finalize = client_buffer_finalize,
182
183 .tsc_bits = 0,
184 .alloc = RING_BUFFER_ALLOC_GLOBAL,
185 .sync = RING_BUFFER_SYNC_GLOBAL,
186 .mode = RING_BUFFER_MODE_TEMPLATE,
187 .backend = RING_BUFFER_PAGE,
188 .output = RING_BUFFER_MMAP,
189 .oops = RING_BUFFER_OOPS_CONSISTENCY,
190 .ipi = RING_BUFFER_NO_IPI_BARRIER,
191 .wakeup = RING_BUFFER_WAKEUP_BY_WRITER,
192 .client_type = LTTNG_CLIENT_TYPE,
193
194 .cb_ptr = &client_cb.parent,
195 };
196
197 static
198 struct lttng_ust_channel_buffer *_channel_create(const char *name,
199 void *buf_addr,
200 size_t subbuf_size, size_t num_subbuf,
201 unsigned int switch_timer_interval,
202 unsigned int read_timer_interval,
203 unsigned char *uuid,
204 uint32_t chan_id,
205 const int *stream_fds, int nr_stream_fds,
206 int64_t blocking_timeout)
207 {
208 struct lttng_ust_abi_channel_config chan_priv_init;
209 struct lttng_ust_shm_handle *handle;
210 struct lttng_ust_channel_buffer *lttng_chan_buf;
211
212 lttng_chan_buf = lttng_ust_alloc_channel_buffer();
213 if (!lttng_chan_buf)
214 return NULL;
215 memcpy(lttng_chan_buf->priv->uuid, uuid, LTTNG_UST_UUID_LEN);
216 lttng_chan_buf->priv->id = chan_id;
217
218 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
219 memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN);
220 chan_priv_init.id = chan_id;
221
222 handle = channel_create(&client_config, name,
223 __alignof__(struct lttng_ust_channel_buffer),
224 sizeof(struct lttng_ust_channel_buffer),
225 &chan_priv_init,
226 lttng_chan_buf, buf_addr, subbuf_size, num_subbuf,
227 switch_timer_interval, read_timer_interval,
228 stream_fds, nr_stream_fds, blocking_timeout);
229 if (!handle)
230 goto error;
231 lttng_chan_buf->priv->rb_chan = shmp(handle, handle->chan);
232 return lttng_chan_buf;
233
234 error:
235 lttng_ust_free_channel_common(lttng_chan_buf->parent);
236 return NULL;
237 }
238
239 static
240 void lttng_channel_destroy(struct lttng_ust_channel_buffer *lttng_chan_buf)
241 {
242 channel_destroy(lttng_chan_buf->priv->rb_chan, lttng_chan_buf->priv->rb_chan->handle, 1);
243 lttng_ust_free_channel_common(lttng_chan_buf->parent);
244 }
245
246 static
247 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx)
248 {
249 int ret;
250
251 memset(&private_ctx, 0, sizeof(private_ctx));
252 private_ctx.pub = ctx;
253 private_ctx.chan = ctx->client_priv;
254 ctx->priv = &private_ctx;
255 ret = lib_ring_buffer_reserve(&client_config, ctx, NULL);
256 if (ret)
257 return ret;
258 if (lib_ring_buffer_backend_get_pages(&client_config, ctx,
259 &ctx->priv->backend_pages))
260 return -EPERM;
261 return 0;
262 }
263
264 static
265 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx)
266 {
267 lib_ring_buffer_commit(&client_config, ctx);
268 }
269
270 static
271 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx,
272 const void *src, size_t len, size_t alignment)
273 {
274 lttng_ust_lib_ring_buffer_align_ctx(ctx, alignment);
275 lib_ring_buffer_write(&client_config, ctx, src, len);
276 }
277
278 static
279 size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer *chan)
280 {
281 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
282 unsigned long o_begin;
283 struct lttng_ust_lib_ring_buffer *buf;
284
285 buf = shmp(rb_chan->handle, rb_chan->backend.buf[0].shmp); /* Only for global buffer ! */
286 o_begin = v_read(&client_config, &buf->offset);
287 if (subbuf_offset(o_begin, rb_chan) != 0) {
288 return rb_chan->backend.subbuf_size - subbuf_offset(o_begin, rb_chan);
289 } else {
290 return rb_chan->backend.subbuf_size - subbuf_offset(o_begin, rb_chan)
291 - sizeof(struct metadata_packet_header);
292 }
293 }
294
295 static
296 int lttng_is_finalized(struct lttng_ust_channel_buffer *chan)
297 {
298 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
299
300 return lib_ring_buffer_channel_is_finalized(rb_chan);
301 }
302
303 static
304 int lttng_is_disabled(struct lttng_ust_channel_buffer *chan)
305 {
306 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
307
308 return lib_ring_buffer_channel_is_disabled(rb_chan);
309 }
310
311 static
312 int lttng_flush_buffer(struct lttng_ust_channel_buffer *chan)
313 {
314 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
315 struct lttng_ust_lib_ring_buffer *buf;
316 int shm_fd, wait_fd, wakeup_fd;
317 uint64_t memory_map_size;
318
319 buf = channel_get_ring_buffer(&client_config, rb_chan,
320 0, rb_chan->handle, &shm_fd, &wait_fd, &wakeup_fd,
321 &memory_map_size);
322 lib_ring_buffer_switch(&client_config, buf,
323 SWITCH_ACTIVE, rb_chan->handle);
324 return 0;
325 }
326
327 static struct lttng_transport lttng_relay_transport = {
328 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap",
329 .ops = {
330 .struct_size = sizeof(struct lttng_ust_channel_buffer_ops),
331
332 .priv = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private, {
333 .pub = &lttng_relay_transport.ops,
334 .channel_create = _channel_create,
335 .channel_destroy = lttng_channel_destroy,
336 .packet_avail_size = lttng_packet_avail_size,
337 .is_finalized = lttng_is_finalized,
338 .is_disabled = lttng_is_disabled,
339 .flush_buffer = lttng_flush_buffer,
340 }),
341 .event_reserve = lttng_event_reserve,
342 .event_commit = lttng_event_commit,
343 .event_write = lttng_event_write,
344 },
345 .client_config = &client_config,
346 };
347
348 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
349 {
350 DBG("LTT : ltt ring buffer client \"%s\" init\n",
351 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
352 lttng_transport_register(&lttng_relay_transport);
353 }
354
355 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
356 {
357 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
358 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
359 lttng_transport_unregister(&lttng_relay_transport);
360 }
This page took 0.0387 seconds and 5 git commands to generate.