Commit timestamp guarantees
[lttng-modules.git] / ltt-ring-buffer-metadata-client.h
CommitLineData
881833e3
MD
1/*
2 * ltt-ring-buffer-client.h
3 *
4 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng lib ring buffer client template.
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
11#include <linux/module.h>
12#include <linux/types.h>
13#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
14#include "ltt-events.h"
15#include "ltt-tracer.h"
16
881833e3
MD
17struct metadata_packet_header {
18 uint32_t magic; /* 0x75D11D57 */
1ec3f75a 19 uint8_t uuid[16]; /* Unique Universal Identifier */
881833e3
MD
20 uint32_t checksum; /* 0 if unused */
21 uint32_t content_size; /* in bits */
22 uint32_t packet_size; /* in bits */
23 uint8_t compression_scheme; /* 0 if unused */
24 uint8_t encryption_scheme; /* 0 if unused */
25 uint8_t checksum_scheme; /* 0 if unused */
26 uint8_t header_end[0];
27};
28
29struct metadata_record_header {
30 uint8_t header_end[0]; /* End of header */
31};
32
33static const struct lib_ring_buffer_config client_config;
34
35static inline
36u64 lib_ring_buffer_clock_read(struct channel *chan)
37{
38 return 0;
39}
40
41static inline
42unsigned char record_header_size(const struct lib_ring_buffer_config *config,
43 struct channel *chan, size_t offset,
64c796d8 44 size_t *pre_header_padding,
881833e3
MD
45 struct lib_ring_buffer_ctx *ctx)
46{
47 return 0;
48}
49
50#include "wrapper/ringbuffer/api.h"
51
52static u64 client_ring_buffer_clock_read(struct channel *chan)
53{
54 return 0;
55}
56
57static
58size_t client_record_header_size(const struct lib_ring_buffer_config *config,
59 struct channel *chan, size_t offset,
881833e3 60 size_t *pre_header_padding,
881833e3
MD
61 struct lib_ring_buffer_ctx *ctx)
62{
63 return 0;
64}
65
66/**
67 * client_packet_header_size - called on buffer-switch to a new sub-buffer
68 *
69 * Return header size without padding after the structure. Don't use packed
70 * structure because gcc generates inefficient code on some architectures
71 * (powerpc, mips..)
72 */
73static size_t client_packet_header_size(void)
74{
75 return offsetof(struct metadata_packet_header, header_end);
76}
77
78static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
79 unsigned int subbuf_idx)
80{
81 struct channel *chan = buf->backend.chan;
82 struct metadata_packet_header *header =
83 (struct metadata_packet_header *)
84 lib_ring_buffer_offset_address(&buf->backend,
85 subbuf_idx * chan->backend.subbuf_size);
9115fbdc
MD
86 struct ltt_channel *ltt_chan = channel_get_private(chan);
87 struct ltt_session *session = ltt_chan->session;
881833e3
MD
88
89 header->magic = TSDL_MAGIC_NUMBER;
1ec3f75a 90 memcpy(header->uuid, session->uuid.b, sizeof(session->uuid));
881833e3
MD
91 header->checksum = 0; /* 0 if unused */
92 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
93 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
94 header->compression_scheme = 0; /* 0 if unused */
95 header->encryption_scheme = 0; /* 0 if unused */
96 header->checksum_scheme = 0; /* 0 if unused */
97}
98
99/*
100 * offset is assumed to never be 0 here : never deliver a completely empty
101 * subbuffer. data_size is between 1 and subbuf_size.
102 */
103static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
104 unsigned int subbuf_idx, unsigned long data_size)
105{
106 struct channel *chan = buf->backend.chan;
d793d5e1 107 struct metadata_packet_header *header =
1ec3f75a 108 (struct metadata_packet_header *)
881833e3
MD
109 lib_ring_buffer_offset_address(&buf->backend,
110 subbuf_idx * chan->backend.subbuf_size);
111 unsigned long records_lost = 0;
112
113 header->content_size = data_size * CHAR_BIT; /* in bits */
114 header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
115 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
116 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
117 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
118 WARN_ON_ONCE(records_lost != 0);
119}
120
121static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
122 int cpu, const char *name)
123{
124 return 0;
125}
126
127static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
128{
129}
130
131static const struct lib_ring_buffer_config client_config = {
132 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
133 .cb.record_header_size = client_record_header_size,
134 .cb.subbuffer_header_size = client_packet_header_size,
135 .cb.buffer_begin = client_buffer_begin,
136 .cb.buffer_end = client_buffer_end,
137 .cb.buffer_create = client_buffer_create,
138 .cb.buffer_finalize = client_buffer_finalize,
139
140 .tsc_bits = 0,
141 .alloc = RING_BUFFER_ALLOC_GLOBAL,
142 .sync = RING_BUFFER_SYNC_GLOBAL,
143 .mode = RING_BUFFER_MODE_TEMPLATE,
144 .backend = RING_BUFFER_PAGE,
2db1399a 145 .output = RING_BUFFER_OUTPUT_TEMPLATE,
881833e3
MD
146 .oops = RING_BUFFER_OOPS_CONSISTENCY,
147 .ipi = RING_BUFFER_IPI_BARRIER,
148 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
149};
150
151static
152struct channel *_channel_create(const char *name,
89fff18e 153 struct ltt_channel *ltt_chan, void *buf_addr,
881833e3
MD
154 size_t subbuf_size, size_t num_subbuf,
155 unsigned int switch_timer_interval,
156 unsigned int read_timer_interval)
157{
89fff18e 158 return channel_create(&client_config, name, ltt_chan, buf_addr,
881833e3
MD
159 subbuf_size, num_subbuf, switch_timer_interval,
160 read_timer_interval);
161}
162
163static
164void ltt_channel_destroy(struct channel *chan)
165{
166 channel_destroy(chan);
167}
168
169static
170struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan)
171{
172 struct lib_ring_buffer *buf;
881833e3 173
cd4bd11f
MD
174 buf = channel_get_ring_buffer(&client_config, chan, 0);
175 if (!lib_ring_buffer_open_read(buf))
176 return buf;
881833e3
MD
177 return NULL;
178}
179
f71ecafa
MD
180static
181int ltt_buffer_has_read_closed_stream(struct channel *chan)
182{
183 struct lib_ring_buffer *buf;
184 int cpu;
185
186 for_each_channel_cpu(cpu, chan) {
187 buf = channel_get_ring_buffer(&client_config, chan, cpu);
188 if (!atomic_long_read(&buf->active_readers))
189 return 1;
190 }
191 return 0;
192}
193
881833e3
MD
194static
195void ltt_buffer_read_close(struct lib_ring_buffer *buf)
196{
197 lib_ring_buffer_release_read(buf);
881833e3
MD
198}
199
c099397a 200static
64c796d8 201int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id)
881833e3
MD
202{
203 return lib_ring_buffer_reserve(&client_config, ctx);
204}
205
c099397a 206static
881833e3
MD
207void ltt_event_commit(struct lib_ring_buffer_ctx *ctx)
208{
209 lib_ring_buffer_commit(&client_config, ctx);
210}
211
c099397a 212static
881833e3
MD
213void ltt_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
214 size_t len)
215{
216 lib_ring_buffer_write(&client_config, ctx, src, len);
217}
218
1ec3f75a
MD
219static
220size_t ltt_packet_avail_size(struct channel *chan)
221
222{
223 unsigned long o_begin;
224 struct lib_ring_buffer *buf;
225
226 buf = chan->backend.buf; /* Only for global buffer ! */
227 o_begin = v_read(&client_config, &buf->offset);
228 if (subbuf_offset(o_begin, chan) != 0) {
229 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan);
230 } else {
231 return chan->backend.subbuf_size - subbuf_offset(o_begin, chan)
232 - sizeof(struct metadata_packet_header);
233 }
234}
235
c099397a 236static
24cedcfe 237wait_queue_head_t *ltt_get_reader_wait_queue(struct channel *chan)
c099397a 238{
24cedcfe
MD
239 return &chan->read_wait;
240}
241
242static
243wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan)
244{
245 return &chan->hp_wait;
246}
247
248static
249int ltt_is_finalized(struct channel *chan)
250{
251 return lib_ring_buffer_channel_is_finalized(chan);
c099397a
MD
252}
253
254ec7bc
MD
254static
255int ltt_is_disabled(struct channel *chan)
256{
257 return lib_ring_buffer_channel_is_disabled(chan);
258}
259
881833e3
MD
260static struct ltt_transport ltt_relay_transport = {
261 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
262 .owner = THIS_MODULE,
263 .ops = {
264 .channel_create = _channel_create,
265 .channel_destroy = ltt_channel_destroy,
266 .buffer_read_open = ltt_buffer_read_open,
f71ecafa
MD
267 .buffer_has_read_closed_stream =
268 ltt_buffer_has_read_closed_stream,
881833e3
MD
269 .buffer_read_close = ltt_buffer_read_close,
270 .event_reserve = ltt_event_reserve,
271 .event_commit = ltt_event_commit,
272 .event_write = ltt_event_write,
1ec3f75a 273 .packet_avail_size = ltt_packet_avail_size,
c099397a 274 .get_reader_wait_queue = ltt_get_reader_wait_queue,
24cedcfe
MD
275 .get_hp_wait_queue = ltt_get_hp_wait_queue,
276 .is_finalized = ltt_is_finalized,
254ec7bc 277 .is_disabled = ltt_is_disabled,
881833e3
MD
278 },
279};
280
281static int __init ltt_ring_buffer_client_init(void)
282{
283 /*
284 * This vmalloc sync all also takes care of the lib ring buffer
285 * vmalloc'd module pages when it is built as a module into LTTng.
286 */
287 wrapper_vmalloc_sync_all();
881833e3
MD
288 ltt_transport_register(&ltt_relay_transport);
289 return 0;
290}
291
292module_init(ltt_ring_buffer_client_init);
293
294static void __exit ltt_ring_buffer_client_exit(void)
295{
881833e3
MD
296 ltt_transport_unregister(&ltt_relay_transport);
297}
298
299module_exit(ltt_ring_buffer_client_exit);
300
301MODULE_LICENSE("GPL and additional rights");
302MODULE_AUTHOR("Mathieu Desnoyers");
303MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
304 " client");
This page took 0.035933 seconds and 4 git commands to generate.