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