| 1 | /* |
| 2 | * lttng-ring-buffer-client.h |
| 3 | * |
| 4 | * LTTng lib ring buffer client template. |
| 5 | * |
| 6 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include "lib/bitfield.h" |
| 26 | #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ |
| 27 | #include "wrapper/trace-clock.h" |
| 28 | #include "lttng-events.h" |
| 29 | #include "lttng-tracer.h" |
| 30 | #include "wrapper/ringbuffer/frontend_types.h" |
| 31 | |
| 32 | #define LTTNG_COMPACT_EVENT_BITS 5 |
| 33 | #define LTTNG_COMPACT_TSC_BITS 27 |
| 34 | |
| 35 | static struct lttng_transport lttng_relay_transport; |
| 36 | |
| 37 | /* |
| 38 | * Keep the natural field alignment for _each field_ within this structure if |
| 39 | * you ever add/remove a field from this header. Packed attribute is not used |
| 40 | * because gcc generates poor code on at least powerpc and mips. Don't ever |
| 41 | * let gcc add padding between the structure elements. |
| 42 | * |
| 43 | * The guarantee we have with timestamps is that all the events in a |
| 44 | * packet are included (inclusive) within the begin/end timestamps of |
| 45 | * the packet. Another guarantee we have is that the "timestamp begin", |
| 46 | * as well as the event timestamps, are monotonically increasing (never |
| 47 | * decrease) when moving forward in a stream (physically). But this |
| 48 | * guarantee does not apply to "timestamp end", because it is sampled at |
| 49 | * commit time, which is not ordered with respect to space reservation. |
| 50 | */ |
| 51 | |
| 52 | struct packet_header { |
| 53 | /* Trace packet header */ |
| 54 | uint32_t magic; /* |
| 55 | * Trace magic number. |
| 56 | * contains endianness information. |
| 57 | */ |
| 58 | uint8_t uuid[16]; |
| 59 | uint32_t stream_id; |
| 60 | |
| 61 | struct { |
| 62 | /* Stream packet context */ |
| 63 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ |
| 64 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ |
| 65 | uint64_t content_size; /* Size of data in subbuffer */ |
| 66 | uint64_t packet_size; /* Subbuffer size (include padding) */ |
| 67 | unsigned long events_discarded; /* |
| 68 | * Events lost in this subbuffer since |
| 69 | * the beginning of the trace. |
| 70 | * (may overflow) |
| 71 | */ |
| 72 | uint32_t cpu_id; /* CPU id associated with stream */ |
| 73 | uint8_t header_end; /* End of header */ |
| 74 | } ctx; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan) |
| 79 | { |
| 80 | return trace_clock_read64(); |
| 81 | } |
| 82 | |
| 83 | static inline |
| 84 | size_t ctx_get_size(size_t offset, struct lttng_ctx *ctx) |
| 85 | { |
| 86 | int i; |
| 87 | size_t orig_offset = offset; |
| 88 | |
| 89 | if (likely(!ctx)) |
| 90 | return 0; |
| 91 | offset += lib_ring_buffer_align(offset, ctx->largest_align); |
| 92 | for (i = 0; i < ctx->nr_fields; i++) |
| 93 | offset += ctx->fields[i].get_size(offset); |
| 94 | return offset - orig_offset; |
| 95 | } |
| 96 | |
| 97 | static inline |
| 98 | void ctx_record(struct lib_ring_buffer_ctx *bufctx, |
| 99 | struct lttng_channel *chan, |
| 100 | struct lttng_ctx *ctx) |
| 101 | { |
| 102 | int i; |
| 103 | |
| 104 | if (likely(!ctx)) |
| 105 | return; |
| 106 | lib_ring_buffer_align_ctx(bufctx, ctx->largest_align); |
| 107 | for (i = 0; i < ctx->nr_fields; i++) |
| 108 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * record_header_size - Calculate the header size and padding necessary. |
| 113 | * @config: ring buffer instance configuration |
| 114 | * @chan: channel |
| 115 | * @offset: offset in the write buffer |
| 116 | * @pre_header_padding: padding to add before the header (output) |
| 117 | * @ctx: reservation context |
| 118 | * |
| 119 | * Returns the event header size (including padding). |
| 120 | * |
| 121 | * The payload must itself determine its own alignment from the biggest type it |
| 122 | * contains. |
| 123 | */ |
| 124 | static __inline__ |
| 125 | unsigned char record_header_size(const struct lib_ring_buffer_config *config, |
| 126 | struct channel *chan, size_t offset, |
| 127 | size_t *pre_header_padding, |
| 128 | struct lib_ring_buffer_ctx *ctx) |
| 129 | { |
| 130 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 131 | struct lttng_event *event = ctx->priv; |
| 132 | size_t orig_offset = offset; |
| 133 | size_t padding; |
| 134 | |
| 135 | switch (lttng_chan->header_type) { |
| 136 | case 1: /* compact */ |
| 137 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 138 | offset += padding; |
| 139 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 140 | offset += sizeof(uint32_t); /* id and timestamp */ |
| 141 | } else { |
| 142 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
| 143 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; |
| 144 | /* Align extended struct on largest member */ |
| 145 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 146 | offset += sizeof(uint32_t); /* id */ |
| 147 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 148 | offset += sizeof(uint64_t); /* timestamp */ |
| 149 | } |
| 150 | break; |
| 151 | case 2: /* large */ |
| 152 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
| 153 | offset += padding; |
| 154 | offset += sizeof(uint16_t); |
| 155 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 156 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 157 | offset += sizeof(uint32_t); /* timestamp */ |
| 158 | } else { |
| 159 | /* Align extended struct on largest member */ |
| 160 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 161 | offset += sizeof(uint32_t); /* id */ |
| 162 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 163 | offset += sizeof(uint64_t); /* timestamp */ |
| 164 | } |
| 165 | break; |
| 166 | default: |
| 167 | padding = 0; |
| 168 | WARN_ON_ONCE(1); |
| 169 | } |
| 170 | offset += ctx_get_size(offset, event->ctx); |
| 171 | offset += ctx_get_size(offset, lttng_chan->ctx); |
| 172 | |
| 173 | *pre_header_padding = padding; |
| 174 | return offset - orig_offset; |
| 175 | } |
| 176 | |
| 177 | #include "wrapper/ringbuffer/api.h" |
| 178 | |
| 179 | static |
| 180 | void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config, |
| 181 | struct lib_ring_buffer_ctx *ctx, |
| 182 | uint32_t event_id); |
| 183 | |
| 184 | /* |
| 185 | * lttng_write_event_header |
| 186 | * |
| 187 | * Writes the event header to the offset (already aligned on 32-bits). |
| 188 | * |
| 189 | * @config: ring buffer instance configuration |
| 190 | * @ctx: reservation context |
| 191 | * @event_id: event ID |
| 192 | */ |
| 193 | static __inline__ |
| 194 | void lttng_write_event_header(const struct lib_ring_buffer_config *config, |
| 195 | struct lib_ring_buffer_ctx *ctx, |
| 196 | uint32_t event_id) |
| 197 | { |
| 198 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 199 | struct lttng_event *event = ctx->priv; |
| 200 | |
| 201 | if (unlikely(ctx->rflags)) |
| 202 | goto slow_path; |
| 203 | |
| 204 | switch (lttng_chan->header_type) { |
| 205 | case 1: /* compact */ |
| 206 | { |
| 207 | uint32_t id_time = 0; |
| 208 | |
| 209 | bt_bitfield_write(&id_time, uint32_t, |
| 210 | 0, |
| 211 | LTTNG_COMPACT_EVENT_BITS, |
| 212 | event_id); |
| 213 | bt_bitfield_write(&id_time, uint32_t, |
| 214 | LTTNG_COMPACT_EVENT_BITS, |
| 215 | LTTNG_COMPACT_TSC_BITS, |
| 216 | ctx->tsc); |
| 217 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 218 | break; |
| 219 | } |
| 220 | case 2: /* large */ |
| 221 | { |
| 222 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 223 | uint16_t id = event_id; |
| 224 | |
| 225 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 226 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 227 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 228 | break; |
| 229 | } |
| 230 | default: |
| 231 | WARN_ON_ONCE(1); |
| 232 | } |
| 233 | |
| 234 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
| 235 | ctx_record(ctx, lttng_chan, event->ctx); |
| 236 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 237 | |
| 238 | return; |
| 239 | |
| 240 | slow_path: |
| 241 | lttng_write_event_header_slow(config, ctx, event_id); |
| 242 | } |
| 243 | |
| 244 | static |
| 245 | void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config, |
| 246 | struct lib_ring_buffer_ctx *ctx, |
| 247 | uint32_t event_id) |
| 248 | { |
| 249 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 250 | struct lttng_event *event = ctx->priv; |
| 251 | |
| 252 | switch (lttng_chan->header_type) { |
| 253 | case 1: /* compact */ |
| 254 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 255 | uint32_t id_time = 0; |
| 256 | |
| 257 | bt_bitfield_write(&id_time, uint32_t, |
| 258 | 0, |
| 259 | LTTNG_COMPACT_EVENT_BITS, |
| 260 | event_id); |
| 261 | bt_bitfield_write(&id_time, uint32_t, |
| 262 | LTTNG_COMPACT_EVENT_BITS, |
| 263 | LTTNG_COMPACT_TSC_BITS, ctx->tsc); |
| 264 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 265 | } else { |
| 266 | uint8_t id = 0; |
| 267 | uint64_t timestamp = ctx->tsc; |
| 268 | |
| 269 | bt_bitfield_write(&id, uint8_t, |
| 270 | 0, |
| 271 | LTTNG_COMPACT_EVENT_BITS, |
| 272 | 31); |
| 273 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 274 | /* Align extended struct on largest member */ |
| 275 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 276 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 277 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 278 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 279 | } |
| 280 | break; |
| 281 | case 2: /* large */ |
| 282 | { |
| 283 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 284 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 285 | uint16_t id = event_id; |
| 286 | |
| 287 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 288 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 289 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 290 | } else { |
| 291 | uint16_t id = 65535; |
| 292 | uint64_t timestamp = ctx->tsc; |
| 293 | |
| 294 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 295 | /* Align extended struct on largest member */ |
| 296 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 297 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 298 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 299 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 300 | } |
| 301 | break; |
| 302 | } |
| 303 | default: |
| 304 | WARN_ON_ONCE(1); |
| 305 | } |
| 306 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
| 307 | ctx_record(ctx, lttng_chan, event->ctx); |
| 308 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 309 | } |
| 310 | |
| 311 | static const struct lib_ring_buffer_config client_config; |
| 312 | |
| 313 | static u64 client_ring_buffer_clock_read(struct channel *chan) |
| 314 | { |
| 315 | return lib_ring_buffer_clock_read(chan); |
| 316 | } |
| 317 | |
| 318 | static |
| 319 | size_t client_record_header_size(const struct lib_ring_buffer_config *config, |
| 320 | struct channel *chan, size_t offset, |
| 321 | size_t *pre_header_padding, |
| 322 | struct lib_ring_buffer_ctx *ctx) |
| 323 | { |
| 324 | return record_header_size(config, chan, offset, |
| 325 | pre_header_padding, ctx); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
| 330 | * |
| 331 | * Return header size without padding after the structure. Don't use packed |
| 332 | * structure because gcc generates inefficient code on some architectures |
| 333 | * (powerpc, mips..) |
| 334 | */ |
| 335 | static size_t client_packet_header_size(void) |
| 336 | { |
| 337 | return offsetof(struct packet_header, ctx.header_end); |
| 338 | } |
| 339 | |
| 340 | static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc, |
| 341 | unsigned int subbuf_idx) |
| 342 | { |
| 343 | struct channel *chan = buf->backend.chan; |
| 344 | struct packet_header *header = |
| 345 | (struct packet_header *) |
| 346 | lib_ring_buffer_offset_address(&buf->backend, |
| 347 | subbuf_idx * chan->backend.subbuf_size); |
| 348 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 349 | struct lttng_session *session = lttng_chan->session; |
| 350 | |
| 351 | header->magic = CTF_MAGIC_NUMBER; |
| 352 | memcpy(header->uuid, session->uuid.b, sizeof(session->uuid)); |
| 353 | header->stream_id = lttng_chan->id; |
| 354 | header->ctx.timestamp_begin = tsc; |
| 355 | header->ctx.timestamp_end = 0; |
| 356 | header->ctx.content_size = ~0ULL; /* for debugging */ |
| 357 | header->ctx.packet_size = ~0ULL; |
| 358 | header->ctx.events_discarded = 0; |
| 359 | header->ctx.cpu_id = buf->backend.cpu; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * offset is assumed to never be 0 here : never deliver a completely empty |
| 364 | * subbuffer. data_size is between 1 and subbuf_size. |
| 365 | */ |
| 366 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, |
| 367 | unsigned int subbuf_idx, unsigned long data_size) |
| 368 | { |
| 369 | struct channel *chan = buf->backend.chan; |
| 370 | struct packet_header *header = |
| 371 | (struct packet_header *) |
| 372 | lib_ring_buffer_offset_address(&buf->backend, |
| 373 | subbuf_idx * chan->backend.subbuf_size); |
| 374 | unsigned long records_lost = 0; |
| 375 | |
| 376 | header->ctx.timestamp_end = tsc; |
| 377 | header->ctx.content_size = |
| 378 | (uint64_t) data_size * CHAR_BIT; /* in bits */ |
| 379 | header->ctx.packet_size = |
| 380 | (uint64_t) PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
| 381 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); |
| 382 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
| 383 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); |
| 384 | header->ctx.events_discarded = records_lost; |
| 385 | } |
| 386 | |
| 387 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, |
| 388 | int cpu, const char *name) |
| 389 | { |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) |
| 394 | { |
| 395 | } |
| 396 | |
| 397 | static struct packet_header *client_packet_header( |
| 398 | const struct lib_ring_buffer_config *config, |
| 399 | struct lib_ring_buffer *buf) |
| 400 | { |
| 401 | return lib_ring_buffer_read_offset_address(&buf->backend, 0); |
| 402 | } |
| 403 | |
| 404 | static int client_timestamp_begin(const struct lib_ring_buffer_config *config, |
| 405 | struct lib_ring_buffer *buf, |
| 406 | uint64_t *timestamp_begin) |
| 407 | { |
| 408 | struct packet_header *header = client_packet_header(config, buf); |
| 409 | *timestamp_begin = header->ctx.timestamp_begin; |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int client_timestamp_end(const struct lib_ring_buffer_config *config, |
| 415 | struct lib_ring_buffer *buf, |
| 416 | uint64_t *timestamp_end) |
| 417 | { |
| 418 | struct packet_header *header = client_packet_header(config, buf); |
| 419 | *timestamp_end = header->ctx.timestamp_end; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int client_events_discarded(const struct lib_ring_buffer_config *config, |
| 425 | struct lib_ring_buffer *buf, |
| 426 | uint64_t *events_discarded) |
| 427 | { |
| 428 | struct packet_header *header = client_packet_header(config, buf); |
| 429 | *events_discarded = header->ctx.events_discarded; |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static int client_content_size(const struct lib_ring_buffer_config *config, |
| 435 | struct lib_ring_buffer *buf, |
| 436 | uint64_t *content_size) |
| 437 | { |
| 438 | struct packet_header *header = client_packet_header(config, buf); |
| 439 | *content_size = header->ctx.content_size; |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int client_packet_size(const struct lib_ring_buffer_config *config, |
| 445 | struct lib_ring_buffer *buf, |
| 446 | uint64_t *packet_size) |
| 447 | { |
| 448 | struct packet_header *header = client_packet_header(config, buf); |
| 449 | *packet_size = header->ctx.packet_size; |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static int client_stream_id(const struct lib_ring_buffer_config *config, |
| 455 | struct lib_ring_buffer *buf, |
| 456 | uint64_t *stream_id) |
| 457 | { |
| 458 | struct packet_header *header = client_packet_header(config, buf); |
| 459 | *stream_id = header->stream_id; |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static int client_current_timestamp(const struct lib_ring_buffer_config *config, |
| 465 | struct lib_ring_buffer *bufb, |
| 466 | uint64_t *ts) |
| 467 | { |
| 468 | *ts = config->cb.ring_buffer_clock_read(bufb->backend.chan); |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static const struct lib_ring_buffer_config client_config = { |
| 474 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 475 | .cb.record_header_size = client_record_header_size, |
| 476 | .cb.subbuffer_header_size = client_packet_header_size, |
| 477 | .cb.buffer_begin = client_buffer_begin, |
| 478 | .cb.buffer_end = client_buffer_end, |
| 479 | .cb.buffer_create = client_buffer_create, |
| 480 | .cb.buffer_finalize = client_buffer_finalize, |
| 481 | |
| 482 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
| 483 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
| 484 | .sync = RING_BUFFER_SYNC_PER_CPU, |
| 485 | .mode = RING_BUFFER_MODE_TEMPLATE, |
| 486 | .backend = RING_BUFFER_PAGE, |
| 487 | .output = RING_BUFFER_OUTPUT_TEMPLATE, |
| 488 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
| 489 | .ipi = RING_BUFFER_IPI_BARRIER, |
| 490 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, |
| 491 | }; |
| 492 | |
| 493 | static |
| 494 | void release_priv_ops(void *priv_ops) |
| 495 | { |
| 496 | module_put(THIS_MODULE); |
| 497 | } |
| 498 | |
| 499 | static |
| 500 | void lttng_channel_destroy(struct channel *chan) |
| 501 | { |
| 502 | channel_destroy(chan); |
| 503 | } |
| 504 | |
| 505 | static |
| 506 | struct channel *_channel_create(const char *name, |
| 507 | struct lttng_channel *lttng_chan, void *buf_addr, |
| 508 | size_t subbuf_size, size_t num_subbuf, |
| 509 | unsigned int switch_timer_interval, |
| 510 | unsigned int read_timer_interval) |
| 511 | { |
| 512 | struct channel *chan; |
| 513 | |
| 514 | chan = channel_create(&client_config, name, lttng_chan, buf_addr, |
| 515 | subbuf_size, num_subbuf, switch_timer_interval, |
| 516 | read_timer_interval); |
| 517 | if (chan) { |
| 518 | /* |
| 519 | * Ensure this module is not unloaded before we finish |
| 520 | * using lttng_relay_transport.ops. |
| 521 | */ |
| 522 | if (!try_module_get(THIS_MODULE)) { |
| 523 | printk(KERN_WARNING "LTT : Can't lock transport module.\n"); |
| 524 | goto error; |
| 525 | } |
| 526 | chan->backend.priv_ops = <tng_relay_transport.ops; |
| 527 | chan->backend.release_priv_ops = release_priv_ops; |
| 528 | } |
| 529 | return chan; |
| 530 | |
| 531 | error: |
| 532 | lttng_channel_destroy(chan); |
| 533 | return NULL; |
| 534 | } |
| 535 | |
| 536 | static |
| 537 | struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan) |
| 538 | { |
| 539 | struct lib_ring_buffer *buf; |
| 540 | int cpu; |
| 541 | |
| 542 | for_each_channel_cpu(cpu, chan) { |
| 543 | buf = channel_get_ring_buffer(&client_config, chan, cpu); |
| 544 | if (!lib_ring_buffer_open_read(buf)) |
| 545 | return buf; |
| 546 | } |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | static |
| 551 | int lttng_buffer_has_read_closed_stream(struct channel *chan) |
| 552 | { |
| 553 | struct lib_ring_buffer *buf; |
| 554 | int cpu; |
| 555 | |
| 556 | for_each_channel_cpu(cpu, chan) { |
| 557 | buf = channel_get_ring_buffer(&client_config, chan, cpu); |
| 558 | if (!atomic_long_read(&buf->active_readers)) |
| 559 | return 1; |
| 560 | } |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static |
| 565 | void lttng_buffer_read_close(struct lib_ring_buffer *buf) |
| 566 | { |
| 567 | lib_ring_buffer_release_read(buf); |
| 568 | } |
| 569 | |
| 570 | static |
| 571 | int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx, |
| 572 | uint32_t event_id) |
| 573 | { |
| 574 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 575 | int ret, cpu; |
| 576 | |
| 577 | cpu = lib_ring_buffer_get_cpu(&client_config); |
| 578 | if (cpu < 0) |
| 579 | return -EPERM; |
| 580 | ctx->cpu = cpu; |
| 581 | |
| 582 | switch (lttng_chan->header_type) { |
| 583 | case 1: /* compact */ |
| 584 | if (event_id > 30) |
| 585 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 586 | break; |
| 587 | case 2: /* large */ |
| 588 | if (event_id > 65534) |
| 589 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 590 | break; |
| 591 | default: |
| 592 | WARN_ON_ONCE(1); |
| 593 | } |
| 594 | |
| 595 | ret = lib_ring_buffer_reserve(&client_config, ctx); |
| 596 | if (ret) |
| 597 | goto put; |
| 598 | lttng_write_event_header(&client_config, ctx, event_id); |
| 599 | return 0; |
| 600 | put: |
| 601 | lib_ring_buffer_put_cpu(&client_config); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static |
| 606 | void lttng_event_commit(struct lib_ring_buffer_ctx *ctx) |
| 607 | { |
| 608 | lib_ring_buffer_commit(&client_config, ctx); |
| 609 | lib_ring_buffer_put_cpu(&client_config); |
| 610 | } |
| 611 | |
| 612 | static |
| 613 | void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src, |
| 614 | size_t len) |
| 615 | { |
| 616 | lib_ring_buffer_write(&client_config, ctx, src, len); |
| 617 | } |
| 618 | |
| 619 | static |
| 620 | void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx, |
| 621 | const void __user *src, size_t len) |
| 622 | { |
| 623 | lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len); |
| 624 | } |
| 625 | |
| 626 | static |
| 627 | void lttng_event_memset(struct lib_ring_buffer_ctx *ctx, |
| 628 | int c, size_t len) |
| 629 | { |
| 630 | lib_ring_buffer_memset(&client_config, ctx, c, len); |
| 631 | } |
| 632 | |
| 633 | static |
| 634 | void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src, |
| 635 | size_t len) |
| 636 | { |
| 637 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); |
| 638 | } |
| 639 | |
| 640 | static |
| 641 | void lttng_event_strcpy_from_user(struct lib_ring_buffer_ctx *ctx, |
| 642 | const char __user *src, size_t len) |
| 643 | { |
| 644 | lib_ring_buffer_strcpy_from_user_inatomic(&client_config, ctx, src, |
| 645 | len, '#'); |
| 646 | } |
| 647 | |
| 648 | static |
| 649 | wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu) |
| 650 | { |
| 651 | struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config, |
| 652 | chan, cpu); |
| 653 | return &buf->write_wait; |
| 654 | } |
| 655 | |
| 656 | static |
| 657 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
| 658 | { |
| 659 | return &chan->hp_wait; |
| 660 | } |
| 661 | |
| 662 | static |
| 663 | int lttng_is_finalized(struct channel *chan) |
| 664 | { |
| 665 | return lib_ring_buffer_channel_is_finalized(chan); |
| 666 | } |
| 667 | |
| 668 | static |
| 669 | int lttng_is_disabled(struct channel *chan) |
| 670 | { |
| 671 | return lib_ring_buffer_channel_is_disabled(chan); |
| 672 | } |
| 673 | |
| 674 | static struct lttng_transport lttng_relay_transport = { |
| 675 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
| 676 | .owner = THIS_MODULE, |
| 677 | .ops = { |
| 678 | .channel_create = _channel_create, |
| 679 | .channel_destroy = lttng_channel_destroy, |
| 680 | .buffer_read_open = lttng_buffer_read_open, |
| 681 | .buffer_has_read_closed_stream = |
| 682 | lttng_buffer_has_read_closed_stream, |
| 683 | .buffer_read_close = lttng_buffer_read_close, |
| 684 | .event_reserve = lttng_event_reserve, |
| 685 | .event_commit = lttng_event_commit, |
| 686 | .event_write = lttng_event_write, |
| 687 | .event_write_from_user = lttng_event_write_from_user, |
| 688 | .event_memset = lttng_event_memset, |
| 689 | .event_strcpy = lttng_event_strcpy, |
| 690 | .event_strcpy_from_user = lttng_event_strcpy_from_user, |
| 691 | .packet_avail_size = NULL, /* Would be racy anyway */ |
| 692 | .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue, |
| 693 | .get_hp_wait_queue = lttng_get_hp_wait_queue, |
| 694 | .is_finalized = lttng_is_finalized, |
| 695 | .is_disabled = lttng_is_disabled, |
| 696 | .timestamp_begin = client_timestamp_begin, |
| 697 | .timestamp_end = client_timestamp_end, |
| 698 | .events_discarded = client_events_discarded, |
| 699 | .content_size = client_content_size, |
| 700 | .packet_size = client_packet_size, |
| 701 | .stream_id = client_stream_id, |
| 702 | .current_timestamp = client_current_timestamp, |
| 703 | }, |
| 704 | }; |
| 705 | |
| 706 | static int __init lttng_ring_buffer_client_init(void) |
| 707 | { |
| 708 | /* |
| 709 | * This vmalloc sync all also takes care of the lib ring buffer |
| 710 | * vmalloc'd module pages when it is built as a module into LTTng. |
| 711 | */ |
| 712 | wrapper_vmalloc_sync_all(); |
| 713 | lttng_transport_register(<tng_relay_transport); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | module_init(lttng_ring_buffer_client_init); |
| 718 | |
| 719 | static void __exit lttng_ring_buffer_client_exit(void) |
| 720 | { |
| 721 | lttng_transport_unregister(<tng_relay_transport); |
| 722 | } |
| 723 | |
| 724 | module_exit(lttng_ring_buffer_client_exit); |
| 725 | |
| 726 | MODULE_LICENSE("GPL and additional rights"); |
| 727 | MODULE_AUTHOR("Mathieu Desnoyers"); |
| 728 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING |
| 729 | " client"); |