Add metadata transport
[deliverable/lttng-modules.git] / ltt-ring-buffer-metadata-client.h
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
17 #ifndef CHAR_BIT
18 #define CHAR_BIT 8
19 #endif
20
21 struct metadata_packet_header {
22 uint32_t magic; /* 0x75D11D57 */
23 uint8_t trace_uuid[16]; /* 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 header_end[0];
31 };
32
33 struct metadata_record_header {
34 uint8_t header_end[0]; /* End of header */
35 };
36
37 static const struct lib_ring_buffer_config client_config;
38
39 static inline
40 u64 lib_ring_buffer_clock_read(struct channel *chan)
41 {
42 return 0;
43 }
44
45 static inline
46 unsigned char record_header_size(const struct lib_ring_buffer_config *config,
47 struct channel *chan, size_t offset,
48 size_t data_size, size_t *pre_header_padding,
49 unsigned int rflags,
50 struct lib_ring_buffer_ctx *ctx)
51 {
52 return 0;
53 }
54
55 #include "wrapper/ringbuffer/api.h"
56
57 static u64 client_ring_buffer_clock_read(struct channel *chan)
58 {
59 return 0;
60 }
61
62 static
63 size_t client_record_header_size(const struct lib_ring_buffer_config *config,
64 struct channel *chan, size_t offset,
65 size_t data_size,
66 size_t *pre_header_padding,
67 unsigned int rflags,
68 struct lib_ring_buffer_ctx *ctx)
69 {
70 return 0;
71 }
72
73 /**
74 * client_packet_header_size - called on buffer-switch to a new sub-buffer
75 *
76 * Return header size without padding after the structure. Don't use packed
77 * structure because gcc generates inefficient code on some architectures
78 * (powerpc, mips..)
79 */
80 static size_t client_packet_header_size(void)
81 {
82 return offsetof(struct metadata_packet_header, header_end);
83 }
84
85 static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc,
86 unsigned int subbuf_idx)
87 {
88 struct channel *chan = buf->backend.chan;
89 struct metadata_packet_header *header =
90 (struct metadata_packet_header *)
91 lib_ring_buffer_offset_address(&buf->backend,
92 subbuf_idx * chan->backend.subbuf_size);
93
94 header->magic = TSDL_MAGIC_NUMBER;
95 /* TODO */
96 //header->trace_uuid = ; /* Unique Universal Identifier */
97 header->checksum = 0; /* 0 if unused */
98 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
99 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
100 header->compression_scheme = 0; /* 0 if unused */
101 header->encryption_scheme = 0; /* 0 if unused */
102 header->checksum_scheme = 0; /* 0 if unused */
103 }
104
105 /*
106 * offset is assumed to never be 0 here : never deliver a completely empty
107 * subbuffer. data_size is between 1 and subbuf_size.
108 */
109 static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc,
110 unsigned int subbuf_idx, unsigned long data_size)
111 {
112 struct channel *chan = buf->backend.chan;
113 struct packet_header *header =
114 (struct packet_header *)
115 lib_ring_buffer_offset_address(&buf->backend,
116 subbuf_idx * chan->backend.subbuf_size);
117 unsigned long records_lost = 0;
118
119 header->content_size = data_size * CHAR_BIT; /* in bits */
120 header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
121 records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf);
122 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
123 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
124 WARN_ON_ONCE(records_lost != 0);
125 }
126
127 static int client_buffer_create(struct lib_ring_buffer *buf, void *priv,
128 int cpu, const char *name)
129 {
130 return 0;
131 }
132
133 static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu)
134 {
135 }
136
137 static const struct lib_ring_buffer_config client_config = {
138 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
139 .cb.record_header_size = client_record_header_size,
140 .cb.subbuffer_header_size = client_packet_header_size,
141 .cb.buffer_begin = client_buffer_begin,
142 .cb.buffer_end = client_buffer_end,
143 .cb.buffer_create = client_buffer_create,
144 .cb.buffer_finalize = client_buffer_finalize,
145
146 .tsc_bits = 0,
147 .alloc = RING_BUFFER_ALLOC_GLOBAL,
148 .sync = RING_BUFFER_SYNC_GLOBAL,
149 .mode = RING_BUFFER_MODE_TEMPLATE,
150 .backend = RING_BUFFER_PAGE,
151 .output = RING_BUFFER_SPLICE,
152 .oops = RING_BUFFER_OOPS_CONSISTENCY,
153 .ipi = RING_BUFFER_IPI_BARRIER,
154 .wakeup = RING_BUFFER_WAKEUP_BY_TIMER,
155 };
156
157 static
158 struct channel *_channel_create(const char *name,
159 struct ltt_session *session, void *buf_addr,
160 size_t subbuf_size, size_t num_subbuf,
161 unsigned int switch_timer_interval,
162 unsigned int read_timer_interval)
163 {
164 return channel_create(&client_config, name, session, buf_addr,
165 subbuf_size, num_subbuf, switch_timer_interval,
166 read_timer_interval);
167 }
168
169 static
170 void ltt_channel_destroy(struct channel *chan)
171 {
172 channel_destroy(chan);
173 }
174
175 static
176 struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan)
177 {
178 struct lib_ring_buffer *buf;
179 int cpu;
180
181 for_each_channel_cpu(cpu, chan) {
182 buf = channel_get_ring_buffer(&client_config, chan, cpu);
183 if (!lib_ring_buffer_open_read(buf))
184 return buf;
185 }
186 return NULL;
187 }
188
189 static
190 void ltt_buffer_read_close(struct lib_ring_buffer *buf)
191 {
192 lib_ring_buffer_release_read(buf);
193
194 }
195
196 int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx)
197 {
198 return lib_ring_buffer_reserve(&client_config, ctx);
199 }
200
201 void ltt_event_commit(struct lib_ring_buffer_ctx *ctx)
202 {
203 lib_ring_buffer_commit(&client_config, ctx);
204 }
205
206 void ltt_event_write(struct lib_ring_buffer_ctx *ctx, const void *src,
207 size_t len)
208 {
209 lib_ring_buffer_write(&client_config, ctx, src, len);
210 }
211
212 static struct ltt_transport ltt_relay_transport = {
213 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING,
214 .owner = THIS_MODULE,
215 .ops = {
216 .channel_create = _channel_create,
217 .channel_destroy = ltt_channel_destroy,
218 .buffer_read_open = ltt_buffer_read_open,
219 .buffer_read_close = ltt_buffer_read_close,
220 .event_reserve = ltt_event_reserve,
221 .event_commit = ltt_event_commit,
222 .event_write = ltt_event_write,
223 },
224 };
225
226 static int __init ltt_ring_buffer_client_init(void)
227 {
228 /*
229 * This vmalloc sync all also takes care of the lib ring buffer
230 * vmalloc'd module pages when it is built as a module into LTTng.
231 */
232 wrapper_vmalloc_sync_all();
233 printk(KERN_INFO "LTT : ltt ring buffer metadata client init\n");
234 ltt_transport_register(&ltt_relay_transport);
235 return 0;
236 }
237
238 module_init(ltt_ring_buffer_client_init);
239
240 static void __exit ltt_ring_buffer_client_exit(void)
241 {
242 printk(KERN_INFO "LTT : ltt ring buffer metadata client exit\n");
243 ltt_transport_unregister(&ltt_relay_transport);
244 }
245
246 module_exit(ltt_ring_buffer_client_exit);
247
248 MODULE_LICENSE("GPL and additional rights");
249 MODULE_AUTHOR("Mathieu Desnoyers");
250 MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING
251 " client");
This page took 0.038397 seconds and 6 git commands to generate.