| 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 <stdint.h> |
| 12 | #include <lttng/ust-events.h> |
| 13 | #include "lttng/bitfield.h" |
| 14 | #include "ltt-tracer.h" |
| 15 | #include "../libringbuffer/frontend_types.h" |
| 16 | |
| 17 | struct metadata_packet_header { |
| 18 | uint32_t magic; /* 0x75D11D57 */ |
| 19 | uint8_t uuid[16]; /* Unique Universal Identifier */ |
| 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 major; /* CTF spec major version number */ |
| 27 | uint8_t minor; /* CTF spec minor version number */ |
| 28 | uint8_t header_end[0]; |
| 29 | }; |
| 30 | |
| 31 | struct metadata_record_header { |
| 32 | uint8_t header_end[0]; /* End of header */ |
| 33 | }; |
| 34 | |
| 35 | static const struct lttng_ust_lib_ring_buffer_config client_config; |
| 36 | |
| 37 | static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan) |
| 38 | { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static inline |
| 43 | unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
| 44 | struct channel *chan, size_t offset, |
| 45 | size_t *pre_header_padding, |
| 46 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 47 | { |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | #include "../libringbuffer/api.h" |
| 52 | |
| 53 | static uint64_t client_ring_buffer_clock_read(struct channel *chan) |
| 54 | { |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static |
| 59 | size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
| 60 | struct channel *chan, size_t offset, |
| 61 | size_t *pre_header_padding, |
| 62 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 63 | { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
| 69 | * |
| 70 | * Return header size without padding after the structure. Don't use packed |
| 71 | * structure because gcc generates inefficient code on some architectures |
| 72 | * (powerpc, mips..) |
| 73 | */ |
| 74 | static size_t client_packet_header_size(void) |
| 75 | { |
| 76 | return offsetof(struct metadata_packet_header, header_end); |
| 77 | } |
| 78 | |
| 79 | static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
| 80 | unsigned int subbuf_idx, |
| 81 | struct lttng_ust_shm_handle *handle) |
| 82 | { |
| 83 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 84 | struct metadata_packet_header *header = |
| 85 | (struct metadata_packet_header *) |
| 86 | lib_ring_buffer_offset_address(&buf->backend, |
| 87 | subbuf_idx * chan->backend.subbuf_size, |
| 88 | handle); |
| 89 | struct ltt_channel *ltt_chan = channel_get_private(chan); |
| 90 | |
| 91 | header->magic = TSDL_MAGIC_NUMBER; |
| 92 | memcpy(header->uuid, ltt_chan->uuid, sizeof(ltt_chan->uuid)); |
| 93 | header->checksum = 0; /* 0 if unused */ |
| 94 | header->content_size = 0xFFFFFFFF; /* in bits, for debugging */ |
| 95 | header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */ |
| 96 | header->compression_scheme = 0; /* 0 if unused */ |
| 97 | header->encryption_scheme = 0; /* 0 if unused */ |
| 98 | header->checksum_scheme = 0; /* 0 if unused */ |
| 99 | header->major = CTF_SPEC_MAJOR; |
| 100 | header->minor = CTF_SPEC_MINOR; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * offset is assumed to never be 0 here : never deliver a completely empty |
| 106 | * subbuffer. data_size is between 1 and subbuf_size. |
| 107 | */ |
| 108 | static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
| 109 | unsigned int subbuf_idx, unsigned long data_size, |
| 110 | struct lttng_ust_shm_handle *handle) |
| 111 | { |
| 112 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 113 | struct metadata_packet_header *header = |
| 114 | (struct metadata_packet_header *) |
| 115 | lib_ring_buffer_offset_address(&buf->backend, |
| 116 | subbuf_idx * chan->backend.subbuf_size, |
| 117 | handle); |
| 118 | unsigned long records_lost = 0; |
| 119 | |
| 120 | header->content_size = data_size * CHAR_BIT; /* in bits */ |
| 121 | header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
| 122 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); |
| 123 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
| 124 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); |
| 125 | WARN_ON_ONCE(records_lost != 0); |
| 126 | } |
| 127 | |
| 128 | static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv, |
| 129 | int cpu, const char *name, |
| 130 | struct lttng_ust_shm_handle *handle) |
| 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, |
| 136 | void *priv, int cpu, |
| 137 | struct lttng_ust_shm_handle *handle) |
| 138 | { |
| 139 | } |
| 140 | |
| 141 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
| 142 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 143 | .cb.record_header_size = client_record_header_size, |
| 144 | .cb.subbuffer_header_size = client_packet_header_size, |
| 145 | .cb.buffer_begin = client_buffer_begin, |
| 146 | .cb.buffer_end = client_buffer_end, |
| 147 | .cb.buffer_create = client_buffer_create, |
| 148 | .cb.buffer_finalize = client_buffer_finalize, |
| 149 | |
| 150 | .tsc_bits = 0, |
| 151 | .alloc = RING_BUFFER_ALLOC_GLOBAL, |
| 152 | .sync = RING_BUFFER_SYNC_GLOBAL, |
| 153 | .mode = RING_BUFFER_MODE_TEMPLATE, |
| 154 | .backend = RING_BUFFER_PAGE, |
| 155 | .output = RING_BUFFER_MMAP, |
| 156 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
| 157 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
| 158 | .wakeup = RING_BUFFER_WAKEUP_BY_WRITER, |
| 159 | .client_type = LTTNG_CLIENT_TYPE, |
| 160 | }; |
| 161 | |
| 162 | const struct lttng_ust_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_config.cb; |
| 163 | |
| 164 | static |
| 165 | struct ltt_channel *_channel_create(const char *name, |
| 166 | void *buf_addr, |
| 167 | size_t subbuf_size, size_t num_subbuf, |
| 168 | unsigned int switch_timer_interval, |
| 169 | unsigned int read_timer_interval, |
| 170 | int **shm_fd, int **wait_fd, |
| 171 | uint64_t **memory_map_size, |
| 172 | struct ltt_channel *chan_priv_init) |
| 173 | { |
| 174 | void *priv; |
| 175 | struct ltt_channel *ltt_chan = NULL; |
| 176 | struct lttng_ust_shm_handle *handle; |
| 177 | |
| 178 | handle = channel_create(&client_config, name, |
| 179 | &priv, __alignof__(*ltt_chan), sizeof(*ltt_chan), |
| 180 | chan_priv_init, |
| 181 | buf_addr, subbuf_size, num_subbuf, |
| 182 | switch_timer_interval, read_timer_interval, |
| 183 | shm_fd, wait_fd, memory_map_size); |
| 184 | if (!handle) |
| 185 | return NULL; |
| 186 | ltt_chan = priv; |
| 187 | ltt_chan->handle = handle; |
| 188 | ltt_chan->chan = shmp(ltt_chan->handle, ltt_chan->handle->chan); |
| 189 | return ltt_chan; |
| 190 | } |
| 191 | |
| 192 | static |
| 193 | void ltt_channel_destroy(struct ltt_channel *ltt_chan) |
| 194 | { |
| 195 | channel_destroy(ltt_chan->chan, ltt_chan->handle, 0); |
| 196 | } |
| 197 | |
| 198 | static |
| 199 | struct lttng_ust_lib_ring_buffer *ltt_buffer_read_open(struct channel *chan, |
| 200 | struct lttng_ust_shm_handle *handle, |
| 201 | int **shm_fd, int **wait_fd, |
| 202 | uint64_t **memory_map_size) |
| 203 | { |
| 204 | struct lttng_ust_lib_ring_buffer *buf; |
| 205 | |
| 206 | buf = channel_get_ring_buffer(&client_config, chan, |
| 207 | 0, handle, shm_fd, wait_fd, memory_map_size); |
| 208 | if (!lib_ring_buffer_open_read(buf, handle, 0)) |
| 209 | return buf; |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | static |
| 214 | void ltt_buffer_read_close(struct lttng_ust_lib_ring_buffer *buf, |
| 215 | struct lttng_ust_shm_handle *handle) |
| 216 | { |
| 217 | lib_ring_buffer_release_read(buf, handle, 0); |
| 218 | } |
| 219 | |
| 220 | static |
| 221 | int ltt_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, uint32_t event_id) |
| 222 | { |
| 223 | return lib_ring_buffer_reserve(&client_config, ctx); |
| 224 | } |
| 225 | |
| 226 | static |
| 227 | void ltt_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 228 | { |
| 229 | lib_ring_buffer_commit(&client_config, ctx); |
| 230 | } |
| 231 | |
| 232 | static |
| 233 | void ltt_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src, |
| 234 | size_t len) |
| 235 | { |
| 236 | lib_ring_buffer_write(&client_config, ctx, src, len); |
| 237 | } |
| 238 | |
| 239 | static |
| 240 | size_t ltt_packet_avail_size(struct channel *chan, struct lttng_ust_shm_handle *handle) |
| 241 | |
| 242 | { |
| 243 | unsigned long o_begin; |
| 244 | struct lttng_ust_lib_ring_buffer *buf; |
| 245 | |
| 246 | buf = shmp(handle, chan->backend.buf[0].shmp); /* Only for global buffer ! */ |
| 247 | o_begin = v_read(&client_config, &buf->offset); |
| 248 | if (subbuf_offset(o_begin, chan) != 0) { |
| 249 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan); |
| 250 | } else { |
| 251 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan) |
| 252 | - sizeof(struct metadata_packet_header); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | #if 0 |
| 257 | static |
| 258 | wait_queue_head_t *ltt_get_reader_wait_queue(struct channel *chan) |
| 259 | { |
| 260 | return &chan->read_wait; |
| 261 | } |
| 262 | |
| 263 | static |
| 264 | wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan) |
| 265 | { |
| 266 | return &chan->hp_wait; |
| 267 | } |
| 268 | #endif //0 |
| 269 | |
| 270 | static |
| 271 | int ltt_is_finalized(struct channel *chan) |
| 272 | { |
| 273 | return lib_ring_buffer_channel_is_finalized(chan); |
| 274 | } |
| 275 | |
| 276 | static |
| 277 | int ltt_is_disabled(struct channel *chan) |
| 278 | { |
| 279 | return lib_ring_buffer_channel_is_disabled(chan); |
| 280 | } |
| 281 | |
| 282 | static |
| 283 | int ltt_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle) |
| 284 | { |
| 285 | struct lttng_ust_lib_ring_buffer *buf; |
| 286 | int *shm_fd, *wait_fd; |
| 287 | uint64_t *memory_map_size; |
| 288 | |
| 289 | buf = channel_get_ring_buffer(&client_config, chan, |
| 290 | 0, handle, &shm_fd, &wait_fd, |
| 291 | &memory_map_size); |
| 292 | lib_ring_buffer_switch(&client_config, buf, |
| 293 | SWITCH_ACTIVE, handle); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static struct ltt_transport ltt_relay_transport = { |
| 298 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
| 299 | .ops = { |
| 300 | .channel_create = _channel_create, |
| 301 | .channel_destroy = ltt_channel_destroy, |
| 302 | .buffer_read_open = ltt_buffer_read_open, |
| 303 | .buffer_read_close = ltt_buffer_read_close, |
| 304 | .event_reserve = ltt_event_reserve, |
| 305 | .event_commit = ltt_event_commit, |
| 306 | .event_write = ltt_event_write, |
| 307 | .packet_avail_size = ltt_packet_avail_size, |
| 308 | //.get_reader_wait_queue = ltt_get_reader_wait_queue, |
| 309 | //.get_hp_wait_queue = ltt_get_hp_wait_queue, |
| 310 | .is_finalized = ltt_is_finalized, |
| 311 | .is_disabled = ltt_is_disabled, |
| 312 | .flush_buffer = ltt_flush_buffer, |
| 313 | }, |
| 314 | }; |
| 315 | |
| 316 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
| 317 | { |
| 318 | DBG("LTT : ltt ring buffer client init\n"); |
| 319 | ltt_transport_register(<t_relay_transport); |
| 320 | } |
| 321 | |
| 322 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
| 323 | { |
| 324 | DBG("LTT : ltt ring buffer client exit\n"); |
| 325 | ltt_transport_unregister(<t_relay_transport); |
| 326 | } |