| 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 <stdint.h> |
| 24 | #include <lttng/ust-events.h> |
| 25 | #include "lttng/bitfield.h" |
| 26 | #include "clock.h" |
| 27 | #include "lttng-tracer.h" |
| 28 | #include "../libringbuffer/frontend_types.h" |
| 29 | |
| 30 | #define LTTNG_COMPACT_EVENT_BITS 5 |
| 31 | #define LTTNG_COMPACT_TSC_BITS 27 |
| 32 | |
| 33 | /* |
| 34 | * Keep the natural field alignment for _each field_ within this structure if |
| 35 | * you ever add/remove a field from this header. Packed attribute is not used |
| 36 | * because gcc generates poor code on at least powerpc and mips. Don't ever |
| 37 | * let gcc add padding between the structure elements. |
| 38 | */ |
| 39 | |
| 40 | struct packet_header { |
| 41 | /* Trace packet header */ |
| 42 | uint32_t magic; /* |
| 43 | * Trace magic number. |
| 44 | * contains endianness information. |
| 45 | */ |
| 46 | uint8_t uuid[LTTNG_UST_UUID_LEN]; |
| 47 | uint32_t stream_id; |
| 48 | |
| 49 | struct { |
| 50 | /* Stream packet context */ |
| 51 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ |
| 52 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ |
| 53 | uint64_t content_size; /* Size of data in subbuffer */ |
| 54 | uint64_t packet_size; /* Subbuffer size (include padding) */ |
| 55 | unsigned long events_discarded; /* |
| 56 | * Events lost in this subbuffer since |
| 57 | * the beginning of the trace. |
| 58 | * (may overflow) |
| 59 | */ |
| 60 | uint32_t cpu_id; /* CPU id associated with stream */ |
| 61 | uint8_t header_end; /* End of header */ |
| 62 | } ctx; |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan) |
| 67 | { |
| 68 | return trace_clock_read64(); |
| 69 | } |
| 70 | |
| 71 | static inline |
| 72 | size_t ctx_get_size(size_t offset, struct lttng_ctx *ctx) |
| 73 | { |
| 74 | int i; |
| 75 | size_t orig_offset = offset; |
| 76 | |
| 77 | if (caa_likely(!ctx)) |
| 78 | return 0; |
| 79 | offset += lib_ring_buffer_align(offset, ctx->largest_align); |
| 80 | for (i = 0; i < ctx->nr_fields; i++) |
| 81 | offset += ctx->fields[i].get_size(offset); |
| 82 | return offset - orig_offset; |
| 83 | } |
| 84 | |
| 85 | static inline |
| 86 | void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx, |
| 87 | struct lttng_channel *chan, |
| 88 | struct lttng_ctx *ctx) |
| 89 | { |
| 90 | int i; |
| 91 | |
| 92 | if (caa_likely(!ctx)) |
| 93 | return; |
| 94 | lib_ring_buffer_align_ctx(bufctx, ctx->largest_align); |
| 95 | for (i = 0; i < ctx->nr_fields; i++) |
| 96 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * record_header_size - Calculate the header size and padding necessary. |
| 101 | * @config: ring buffer instance configuration |
| 102 | * @chan: channel |
| 103 | * @offset: offset in the write buffer |
| 104 | * @pre_header_padding: padding to add before the header (output) |
| 105 | * @ctx: reservation context |
| 106 | * |
| 107 | * Returns the event header size (including padding). |
| 108 | * |
| 109 | * The payload must itself determine its own alignment from the biggest type it |
| 110 | * contains. |
| 111 | */ |
| 112 | static __inline__ |
| 113 | unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
| 114 | struct channel *chan, size_t offset, |
| 115 | size_t *pre_header_padding, |
| 116 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 117 | { |
| 118 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 119 | struct lttng_event *event = ctx->priv; |
| 120 | size_t orig_offset = offset; |
| 121 | size_t padding; |
| 122 | |
| 123 | switch (lttng_chan->header_type) { |
| 124 | case 1: /* compact */ |
| 125 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 126 | offset += padding; |
| 127 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 128 | offset += sizeof(uint32_t); /* id and timestamp */ |
| 129 | } else { |
| 130 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
| 131 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; |
| 132 | /* Align extended struct on largest member */ |
| 133 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 134 | offset += sizeof(uint32_t); /* id */ |
| 135 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 136 | offset += sizeof(uint64_t); /* timestamp */ |
| 137 | } |
| 138 | break; |
| 139 | case 2: /* large */ |
| 140 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
| 141 | offset += padding; |
| 142 | offset += sizeof(uint16_t); |
| 143 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 144 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 145 | offset += sizeof(uint32_t); /* timestamp */ |
| 146 | } else { |
| 147 | /* Align extended struct on largest member */ |
| 148 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 149 | offset += sizeof(uint32_t); /* id */ |
| 150 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 151 | offset += sizeof(uint64_t); /* timestamp */ |
| 152 | } |
| 153 | break; |
| 154 | default: |
| 155 | padding = 0; |
| 156 | WARN_ON_ONCE(1); |
| 157 | } |
| 158 | offset += ctx_get_size(offset, event->ctx); |
| 159 | offset += ctx_get_size(offset, lttng_chan->ctx); |
| 160 | |
| 161 | *pre_header_padding = padding; |
| 162 | return offset - orig_offset; |
| 163 | } |
| 164 | |
| 165 | #include "../libringbuffer/api.h" |
| 166 | #include "lttng-rb-clients.h" |
| 167 | |
| 168 | static |
| 169 | void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
| 170 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 171 | uint32_t event_id); |
| 172 | |
| 173 | /* |
| 174 | * lttng_write_event_header |
| 175 | * |
| 176 | * Writes the event header to the offset (already aligned on 32-bits). |
| 177 | * |
| 178 | * @config: ring buffer instance configuration |
| 179 | * @ctx: reservation context |
| 180 | * @event_id: event ID |
| 181 | */ |
| 182 | static __inline__ |
| 183 | void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config, |
| 184 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 185 | uint32_t event_id) |
| 186 | { |
| 187 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 188 | struct lttng_event *event = ctx->priv; |
| 189 | |
| 190 | if (caa_unlikely(ctx->rflags)) |
| 191 | goto slow_path; |
| 192 | |
| 193 | switch (lttng_chan->header_type) { |
| 194 | case 1: /* compact */ |
| 195 | { |
| 196 | uint32_t id_time = 0; |
| 197 | |
| 198 | bt_bitfield_write(&id_time, uint32_t, |
| 199 | 0, |
| 200 | LTTNG_COMPACT_EVENT_BITS, |
| 201 | event_id); |
| 202 | bt_bitfield_write(&id_time, uint32_t, |
| 203 | LTTNG_COMPACT_EVENT_BITS, |
| 204 | LTTNG_COMPACT_TSC_BITS, |
| 205 | ctx->tsc); |
| 206 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 207 | break; |
| 208 | } |
| 209 | case 2: /* large */ |
| 210 | { |
| 211 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 212 | uint16_t id = event_id; |
| 213 | |
| 214 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 215 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 216 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 217 | break; |
| 218 | } |
| 219 | default: |
| 220 | WARN_ON_ONCE(1); |
| 221 | } |
| 222 | |
| 223 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
| 224 | ctx_record(ctx, lttng_chan, event->ctx); |
| 225 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 226 | |
| 227 | return; |
| 228 | |
| 229 | slow_path: |
| 230 | lttng_write_event_header_slow(config, ctx, event_id); |
| 231 | } |
| 232 | |
| 233 | static |
| 234 | void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
| 235 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 236 | uint32_t event_id) |
| 237 | { |
| 238 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 239 | struct lttng_event *event = ctx->priv; |
| 240 | |
| 241 | switch (lttng_chan->header_type) { |
| 242 | case 1: /* compact */ |
| 243 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 244 | uint32_t id_time = 0; |
| 245 | |
| 246 | bt_bitfield_write(&id_time, uint32_t, |
| 247 | 0, |
| 248 | LTTNG_COMPACT_EVENT_BITS, |
| 249 | event_id); |
| 250 | bt_bitfield_write(&id_time, uint32_t, |
| 251 | LTTNG_COMPACT_EVENT_BITS, |
| 252 | LTTNG_COMPACT_TSC_BITS, |
| 253 | ctx->tsc); |
| 254 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
| 255 | } else { |
| 256 | uint8_t id = 0; |
| 257 | uint64_t timestamp = ctx->tsc; |
| 258 | |
| 259 | bt_bitfield_write(&id, uint8_t, |
| 260 | 0, |
| 261 | LTTNG_COMPACT_EVENT_BITS, |
| 262 | 31); |
| 263 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 264 | /* Align extended struct on largest member */ |
| 265 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 266 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 267 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 268 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 269 | } |
| 270 | break; |
| 271 | case 2: /* large */ |
| 272 | { |
| 273 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
| 274 | uint32_t timestamp = (uint32_t) ctx->tsc; |
| 275 | uint16_t id = event_id; |
| 276 | |
| 277 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 278 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
| 279 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 280 | } else { |
| 281 | uint16_t id = 65535; |
| 282 | uint64_t timestamp = ctx->tsc; |
| 283 | |
| 284 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
| 285 | /* Align extended struct on largest member */ |
| 286 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 287 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
| 288 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
| 289 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
| 290 | } |
| 291 | break; |
| 292 | } |
| 293 | default: |
| 294 | WARN_ON_ONCE(1); |
| 295 | } |
| 296 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
| 297 | ctx_record(ctx, lttng_chan, event->ctx); |
| 298 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
| 299 | } |
| 300 | |
| 301 | static const struct lttng_ust_lib_ring_buffer_config client_config; |
| 302 | |
| 303 | static uint64_t client_ring_buffer_clock_read(struct channel *chan) |
| 304 | { |
| 305 | return lib_ring_buffer_clock_read(chan); |
| 306 | } |
| 307 | |
| 308 | static |
| 309 | size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
| 310 | struct channel *chan, size_t offset, |
| 311 | size_t *pre_header_padding, |
| 312 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 313 | { |
| 314 | return record_header_size(config, chan, offset, |
| 315 | pre_header_padding, ctx); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
| 320 | * |
| 321 | * Return header size without padding after the structure. Don't use packed |
| 322 | * structure because gcc generates inefficient code on some architectures |
| 323 | * (powerpc, mips..) |
| 324 | */ |
| 325 | static size_t client_packet_header_size(void) |
| 326 | { |
| 327 | return offsetof(struct packet_header, ctx.header_end); |
| 328 | } |
| 329 | |
| 330 | static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
| 331 | unsigned int subbuf_idx, |
| 332 | struct lttng_ust_shm_handle *handle) |
| 333 | { |
| 334 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 335 | struct packet_header *header = |
| 336 | (struct packet_header *) |
| 337 | lib_ring_buffer_offset_address(&buf->backend, |
| 338 | subbuf_idx * chan->backend.subbuf_size, |
| 339 | handle); |
| 340 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
| 341 | |
| 342 | assert(header); |
| 343 | if (!header) |
| 344 | return; |
| 345 | header->magic = CTF_MAGIC_NUMBER; |
| 346 | memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid)); |
| 347 | header->stream_id = lttng_chan->id; |
| 348 | header->ctx.timestamp_begin = tsc; |
| 349 | header->ctx.timestamp_end = 0; |
| 350 | header->ctx.content_size = ~0ULL; /* for debugging */ |
| 351 | header->ctx.packet_size = ~0ULL; |
| 352 | header->ctx.events_discarded = 0; |
| 353 | header->ctx.cpu_id = buf->backend.cpu; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * offset is assumed to never be 0 here : never deliver a completely empty |
| 358 | * subbuffer. data_size is between 1 and subbuf_size. |
| 359 | */ |
| 360 | static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
| 361 | unsigned int subbuf_idx, unsigned long data_size, |
| 362 | struct lttng_ust_shm_handle *handle) |
| 363 | { |
| 364 | struct channel *chan = shmp(handle, buf->backend.chan); |
| 365 | struct packet_header *header = |
| 366 | (struct packet_header *) |
| 367 | lib_ring_buffer_offset_address(&buf->backend, |
| 368 | subbuf_idx * chan->backend.subbuf_size, |
| 369 | handle); |
| 370 | unsigned long records_lost = 0; |
| 371 | |
| 372 | assert(header); |
| 373 | if (!header) |
| 374 | return; |
| 375 | header->ctx.timestamp_end = tsc; |
| 376 | header->ctx.content_size = |
| 377 | (uint64_t) data_size * CHAR_BIT; /* in bits */ |
| 378 | header->ctx.packet_size = |
| 379 | (uint64_t) PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
| 380 | |
| 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 lttng_ust_lib_ring_buffer *buf, void *priv, |
| 388 | int cpu, const char *name, struct lttng_ust_shm_handle *handle) |
| 389 | { |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle) |
| 394 | { |
| 395 | } |
| 396 | |
| 397 | static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config *config, |
| 398 | size_t *offset, size_t *length) |
| 399 | { |
| 400 | *offset = offsetof(struct packet_header, ctx.content_size); |
| 401 | *length = sizeof(((struct packet_header *) NULL)->ctx.content_size); |
| 402 | } |
| 403 | |
| 404 | static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config *config, |
| 405 | size_t *offset, size_t *length) |
| 406 | { |
| 407 | *offset = offsetof(struct packet_header, ctx.packet_size); |
| 408 | *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size); |
| 409 | } |
| 410 | |
| 411 | static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf, |
| 412 | struct lttng_ust_shm_handle *handle) |
| 413 | { |
| 414 | return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle); |
| 415 | } |
| 416 | |
| 417 | static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer *buf, |
| 418 | struct lttng_ust_shm_handle *handle, |
| 419 | uint64_t *timestamp_begin) |
| 420 | { |
| 421 | struct packet_header *header; |
| 422 | |
| 423 | header = client_packet_header(buf, handle); |
| 424 | if (!header) |
| 425 | return -1; |
| 426 | *timestamp_begin = header->ctx.timestamp_begin; |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int client_timestamp_end(struct lttng_ust_lib_ring_buffer *buf, |
| 431 | struct lttng_ust_shm_handle *handle, |
| 432 | uint64_t *timestamp_end) |
| 433 | { |
| 434 | struct packet_header *header; |
| 435 | |
| 436 | header = client_packet_header(buf, handle); |
| 437 | if (!header) |
| 438 | return -1; |
| 439 | *timestamp_end = header->ctx.timestamp_end; |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int client_events_discarded(struct lttng_ust_lib_ring_buffer *buf, |
| 444 | struct lttng_ust_shm_handle *handle, |
| 445 | uint64_t *events_discarded) |
| 446 | { |
| 447 | struct packet_header *header; |
| 448 | |
| 449 | header = client_packet_header(buf, handle); |
| 450 | if (!header) |
| 451 | return -1; |
| 452 | *events_discarded = header->ctx.events_discarded; |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int client_content_size(struct lttng_ust_lib_ring_buffer *buf, |
| 457 | struct lttng_ust_shm_handle *handle, |
| 458 | uint64_t *content_size) |
| 459 | { |
| 460 | struct packet_header *header; |
| 461 | |
| 462 | header = client_packet_header(buf, handle); |
| 463 | if (!header) |
| 464 | return -1; |
| 465 | *content_size = header->ctx.content_size; |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf, |
| 470 | struct lttng_ust_shm_handle *handle, |
| 471 | uint64_t *packet_size) |
| 472 | { |
| 473 | struct packet_header *header; |
| 474 | |
| 475 | header = client_packet_header(buf, handle); |
| 476 | if (!header) |
| 477 | return -1; |
| 478 | *packet_size = header->ctx.packet_size; |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf, |
| 483 | struct lttng_ust_shm_handle *handle, |
| 484 | uint64_t *stream_id) |
| 485 | { |
| 486 | struct packet_header *header; |
| 487 | |
| 488 | header = client_packet_header(buf, handle); |
| 489 | if (!header) |
| 490 | return -1; |
| 491 | *stream_id = header->stream_id; |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf, |
| 496 | struct lttng_ust_shm_handle *handle, |
| 497 | uint64_t *ts) |
| 498 | { |
| 499 | struct channel *chan; |
| 500 | |
| 501 | chan = shmp(handle, handle->chan); |
| 502 | *ts = client_ring_buffer_clock_read(chan); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static const |
| 508 | struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = { |
| 509 | .parent = { |
| 510 | .ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 511 | .record_header_size = client_record_header_size, |
| 512 | .subbuffer_header_size = client_packet_header_size, |
| 513 | .buffer_begin = client_buffer_begin, |
| 514 | .buffer_end = client_buffer_end, |
| 515 | .buffer_create = client_buffer_create, |
| 516 | .buffer_finalize = client_buffer_finalize, |
| 517 | .content_size_field = client_content_size_field, |
| 518 | .packet_size_field = client_packet_size_field, |
| 519 | }, |
| 520 | .timestamp_begin = client_timestamp_begin, |
| 521 | .timestamp_end = client_timestamp_end, |
| 522 | .events_discarded = client_events_discarded, |
| 523 | .content_size = client_content_size, |
| 524 | .packet_size = client_packet_size, |
| 525 | .stream_id = client_stream_id, |
| 526 | .current_timestamp = client_current_timestamp, |
| 527 | }; |
| 528 | |
| 529 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
| 530 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 531 | .cb.record_header_size = client_record_header_size, |
| 532 | .cb.subbuffer_header_size = client_packet_header_size, |
| 533 | .cb.buffer_begin = client_buffer_begin, |
| 534 | .cb.buffer_end = client_buffer_end, |
| 535 | .cb.buffer_create = client_buffer_create, |
| 536 | .cb.buffer_finalize = client_buffer_finalize, |
| 537 | .cb.content_size_field = client_content_size_field, |
| 538 | .cb.packet_size_field = client_packet_size_field, |
| 539 | |
| 540 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
| 541 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
| 542 | .sync = RING_BUFFER_SYNC_GLOBAL, |
| 543 | .mode = RING_BUFFER_MODE_TEMPLATE, |
| 544 | .backend = RING_BUFFER_PAGE, |
| 545 | .output = RING_BUFFER_MMAP, |
| 546 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
| 547 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
| 548 | .wakeup = LTTNG_CLIENT_WAKEUP, |
| 549 | .client_type = LTTNG_CLIENT_TYPE, |
| 550 | |
| 551 | .cb_ptr = &client_cb.parent, |
| 552 | }; |
| 553 | |
| 554 | const struct lttng_ust_client_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_cb; |
| 555 | |
| 556 | static |
| 557 | struct lttng_channel *_channel_create(const char *name, |
| 558 | void *buf_addr, |
| 559 | size_t subbuf_size, size_t num_subbuf, |
| 560 | unsigned int switch_timer_interval, |
| 561 | unsigned int read_timer_interval, |
| 562 | unsigned char *uuid, |
| 563 | uint32_t chan_id, |
| 564 | const int *stream_fds, int nr_stream_fds) |
| 565 | { |
| 566 | struct lttng_channel chan_priv_init; |
| 567 | struct lttng_ust_shm_handle *handle; |
| 568 | struct lttng_channel *lttng_chan; |
| 569 | void *priv; |
| 570 | |
| 571 | memset(&chan_priv_init, 0, sizeof(chan_priv_init)); |
| 572 | memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN); |
| 573 | chan_priv_init.id = chan_id; |
| 574 | handle = channel_create(&client_config, name, |
| 575 | &priv, __alignof__(struct lttng_channel), |
| 576 | sizeof(struct lttng_channel), |
| 577 | &chan_priv_init, |
| 578 | buf_addr, subbuf_size, num_subbuf, |
| 579 | switch_timer_interval, read_timer_interval, |
| 580 | stream_fds, nr_stream_fds); |
| 581 | if (!handle) |
| 582 | return NULL; |
| 583 | lttng_chan = priv; |
| 584 | lttng_chan->handle = handle; |
| 585 | lttng_chan->chan = shmp(handle, handle->chan); |
| 586 | return lttng_chan; |
| 587 | } |
| 588 | |
| 589 | static |
| 590 | void lttng_channel_destroy(struct lttng_channel *chan) |
| 591 | { |
| 592 | channel_destroy(chan->chan, chan->handle, 1); |
| 593 | } |
| 594 | |
| 595 | static |
| 596 | int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 597 | uint32_t event_id) |
| 598 | { |
| 599 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
| 600 | int ret, cpu; |
| 601 | |
| 602 | cpu = lib_ring_buffer_get_cpu(&client_config); |
| 603 | if (cpu < 0) |
| 604 | return -EPERM; |
| 605 | ctx->cpu = cpu; |
| 606 | |
| 607 | switch (lttng_chan->header_type) { |
| 608 | case 1: /* compact */ |
| 609 | if (event_id > 30) |
| 610 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 611 | break; |
| 612 | case 2: /* large */ |
| 613 | if (event_id > 65534) |
| 614 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
| 615 | break; |
| 616 | default: |
| 617 | WARN_ON_ONCE(1); |
| 618 | } |
| 619 | |
| 620 | ret = lib_ring_buffer_reserve(&client_config, ctx); |
| 621 | if (ret) |
| 622 | goto put; |
| 623 | lttng_write_event_header(&client_config, ctx, event_id); |
| 624 | return 0; |
| 625 | put: |
| 626 | lib_ring_buffer_put_cpu(&client_config); |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | static |
| 631 | void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
| 632 | { |
| 633 | lib_ring_buffer_commit(&client_config, ctx); |
| 634 | lib_ring_buffer_put_cpu(&client_config); |
| 635 | } |
| 636 | |
| 637 | static |
| 638 | void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src, |
| 639 | size_t len) |
| 640 | { |
| 641 | lib_ring_buffer_write(&client_config, ctx, src, len); |
| 642 | } |
| 643 | |
| 644 | static |
| 645 | void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx *ctx, const char *src, |
| 646 | size_t len) |
| 647 | { |
| 648 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); |
| 649 | } |
| 650 | |
| 651 | #if 0 |
| 652 | static |
| 653 | wait_queue_head_t *lttng_get_reader_wait_queue(struct channel *chan) |
| 654 | { |
| 655 | return &chan->read_wait; |
| 656 | } |
| 657 | |
| 658 | static |
| 659 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
| 660 | { |
| 661 | return &chan->hp_wait; |
| 662 | } |
| 663 | #endif //0 |
| 664 | |
| 665 | static |
| 666 | int lttng_is_finalized(struct channel *chan) |
| 667 | { |
| 668 | return lib_ring_buffer_channel_is_finalized(chan); |
| 669 | } |
| 670 | |
| 671 | static |
| 672 | int lttng_is_disabled(struct channel *chan) |
| 673 | { |
| 674 | return lib_ring_buffer_channel_is_disabled(chan); |
| 675 | } |
| 676 | |
| 677 | static |
| 678 | int lttng_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle) |
| 679 | { |
| 680 | struct lttng_ust_lib_ring_buffer *buf; |
| 681 | int cpu; |
| 682 | |
| 683 | for_each_channel_cpu(cpu, chan) { |
| 684 | int shm_fd, wait_fd, wakeup_fd; |
| 685 | uint64_t memory_map_size; |
| 686 | |
| 687 | buf = channel_get_ring_buffer(&client_config, chan, |
| 688 | cpu, handle, &shm_fd, &wait_fd, |
| 689 | &wakeup_fd, &memory_map_size); |
| 690 | lib_ring_buffer_switch(&client_config, buf, |
| 691 | SWITCH_ACTIVE, handle); |
| 692 | } |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static struct lttng_transport lttng_relay_transport = { |
| 697 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
| 698 | .ops = { |
| 699 | .channel_create = _channel_create, |
| 700 | .channel_destroy = lttng_channel_destroy, |
| 701 | .u.has_strcpy = 1, |
| 702 | .event_reserve = lttng_event_reserve, |
| 703 | .event_commit = lttng_event_commit, |
| 704 | .event_write = lttng_event_write, |
| 705 | .packet_avail_size = NULL, /* Would be racy anyway */ |
| 706 | //.get_reader_wait_queue = lttng_get_reader_wait_queue, |
| 707 | //.get_hp_wait_queue = lttng_get_hp_wait_queue, |
| 708 | .is_finalized = lttng_is_finalized, |
| 709 | .is_disabled = lttng_is_disabled, |
| 710 | .flush_buffer = lttng_flush_buffer, |
| 711 | .event_strcpy = lttng_event_strcpy, |
| 712 | }, |
| 713 | .client_config = &client_config, |
| 714 | }; |
| 715 | |
| 716 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
| 717 | { |
| 718 | DBG("LTT : ltt ring buffer client \"%s\" init\n", |
| 719 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); |
| 720 | lttng_transport_register(<tng_relay_transport); |
| 721 | } |
| 722 | |
| 723 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
| 724 | { |
| 725 | DBG("LTT : ltt ring buffer client \"%s\" exit\n", |
| 726 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); |
| 727 | lttng_transport_unregister(<tng_relay_transport); |
| 728 | } |