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