| 1 | |
| 2 | /* LTTng user-space "fast" tracing header |
| 3 | * |
| 4 | * Copyright 2006 Mathieu Desnoyers |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef _LTT_USERTRACE_FAST_H |
| 9 | #define _LTT_USERTRACE_FAST_H |
| 10 | |
| 11 | #ifdef LTT_TRACE |
| 12 | #ifdef LTT_TRACE_FAST |
| 13 | |
| 14 | #include <errno.h> |
| 15 | #include <pthread.h> |
| 16 | #include <stdint.h> |
| 17 | #include <syscall.h> |
| 18 | #include <semaphore.h> |
| 19 | #include <signal.h> |
| 20 | |
| 21 | #include <ltt/ltt-facility-id-user_generic.h> |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | #ifndef LTT_N_SUBBUFS |
| 28 | #define LTT_N_SUBBUFS 2 |
| 29 | #endif //LTT_N_SUBBUFS |
| 30 | |
| 31 | #ifndef LTT_SUBBUF_SIZE_PROCESS |
| 32 | #define LTT_SUBBUF_SIZE_PROCESS 1048576 |
| 33 | #endif //LTT_BUF_SIZE_CPU |
| 34 | |
| 35 | #define LTT_BUF_SIZE_PROCESS (LTT_SUBBUF_SIZE_PROCESS * LTT_N_SUBBUFS) |
| 36 | |
| 37 | #ifndef LTT_USERTRACE_ROOT |
| 38 | #define LTT_USERTRACE_ROOT "/tmp/ltt-usertrace" |
| 39 | #endif //LTT_USERTRACE_ROOT |
| 40 | |
| 41 | |
| 42 | /* Buffer offset macros */ |
| 43 | |
| 44 | #define BUFFER_OFFSET(offset, buf) (offset & (buf->alloc_size-1)) |
| 45 | #define SUBBUF_OFFSET(offset, buf) (offset & (buf->subbuf_size-1)) |
| 46 | #define SUBBUF_ALIGN(offset, buf) \ |
| 47 | (((offset) + buf->subbuf_size) & (~(buf->subbuf_size-1))) |
| 48 | #define SUBBUF_TRUNC(offset, buf) \ |
| 49 | ((offset) & (~(buf->subbuf_size-1))) |
| 50 | #define SUBBUF_INDEX(offset, buf) \ |
| 51 | (BUFFER_OFFSET(offset,buf)/buf->subbuf_size) |
| 52 | |
| 53 | |
| 54 | #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED |
| 55 | #define LTT_TRACER_VERSION_MAJOR 0 |
| 56 | #define LTT_TRACER_VERSION_MINOR 7 |
| 57 | |
| 58 | #ifndef atomic_cmpxchg |
| 59 | #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new)) |
| 60 | #endif //atomic_cmpxchg |
| 61 | |
| 62 | struct ltt_trace_header { |
| 63 | uint32_t magic_number; |
| 64 | uint32_t arch_type; |
| 65 | uint32_t arch_variant; |
| 66 | uint32_t float_word_order; /* Only useful for user space traces */ |
| 67 | uint8_t arch_size; |
| 68 | //uint32_t system_type; |
| 69 | uint8_t major_version; |
| 70 | uint8_t minor_version; |
| 71 | uint8_t flight_recorder; |
| 72 | uint8_t has_heartbeat; |
| 73 | uint8_t has_alignment; /* Event header alignment */ |
| 74 | uint32_t freq_scale; |
| 75 | uint64_t start_freq; |
| 76 | uint64_t start_tsc; |
| 77 | uint64_t start_monotonic; |
| 78 | uint64_t start_time_sec; |
| 79 | uint64_t start_time_usec; |
| 80 | } __attribute((packed)); |
| 81 | |
| 82 | |
| 83 | struct ltt_block_start_header { |
| 84 | struct { |
| 85 | uint64_t cycle_count; |
| 86 | uint64_t freq; /* khz */ |
| 87 | } begin; |
| 88 | struct { |
| 89 | uint64_t cycle_count; |
| 90 | uint64_t freq; /* khz */ |
| 91 | } end; |
| 92 | uint32_t lost_size; /* Size unused at the end of the buffer */ |
| 93 | uint32_t buf_size; /* The size of this sub-buffer */ |
| 94 | struct ltt_trace_header trace; |
| 95 | } __attribute((packed)); |
| 96 | |
| 97 | |
| 98 | |
| 99 | struct ltt_buf { |
| 100 | void *start; |
| 101 | atomic_t offset; |
| 102 | atomic_t consumed; |
| 103 | atomic_t reserve_count[LTT_N_SUBBUFS]; |
| 104 | atomic_t commit_count[LTT_N_SUBBUFS]; |
| 105 | |
| 106 | atomic_t events_lost; |
| 107 | atomic_t corrupted_subbuffers; |
| 108 | sem_t writer_sem; /* semaphore on which the writer waits */ |
| 109 | unsigned int alloc_size; |
| 110 | unsigned int subbuf_size; |
| 111 | }; |
| 112 | |
| 113 | struct ltt_trace_info { |
| 114 | int init; |
| 115 | int filter; |
| 116 | pid_t daemon_id; |
| 117 | int nesting; |
| 118 | struct { |
| 119 | struct ltt_buf process; |
| 120 | char process_buf[LTT_BUF_SIZE_PROCESS] __attribute__ ((aligned (8))); |
| 121 | } channel; |
| 122 | }; |
| 123 | |
| 124 | |
| 125 | struct ltt_event_header_nohb { |
| 126 | uint64_t timestamp; |
| 127 | unsigned char facility_id; |
| 128 | unsigned char event_id; |
| 129 | uint16_t event_size; |
| 130 | } __attribute((packed)); |
| 131 | |
| 132 | extern __thread struct ltt_trace_info *thread_trace_info; |
| 133 | |
| 134 | void ltt_thread_init(void); |
| 135 | |
| 136 | void __attribute__((no_instrument_function)) |
| 137 | ltt_usertrace_fast_buffer_switch(void); |
| 138 | |
| 139 | /* Get the offset of the channel in the ltt_trace_struct */ |
| 140 | #define GET_CHANNEL_INDEX(chan) \ |
| 141 | (unsigned int)&((struct ltt_trace_info*)NULL)->channel.chan |
| 142 | |
| 143 | /* ltt_get_index_from_facility |
| 144 | * |
| 145 | * Get channel index from facility and event id. |
| 146 | * |
| 147 | * @fID : facility ID |
| 148 | * @eID : event number |
| 149 | * |
| 150 | * Get the channel index into which events must be written for the given |
| 151 | * facility and event number. We get this structure offset as soon as possible |
| 152 | * and remember it so we pass through this logic only once per trace call (not |
| 153 | * for every trace). |
| 154 | */ |
| 155 | static inline unsigned int __attribute__((no_instrument_function)) |
| 156 | ltt_get_index_from_facility(ltt_facility_t fID, |
| 157 | uint8_t eID) |
| 158 | { |
| 159 | return GET_CHANNEL_INDEX(process); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | static inline struct ltt_buf * __attribute__((no_instrument_function)) |
| 164 | ltt_get_channel_from_index( |
| 165 | struct ltt_trace_info *trace, unsigned int index) |
| 166 | { |
| 167 | return (struct ltt_buf *)((void*)trace+index); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /* |
| 172 | * ltt_get_header_size |
| 173 | * |
| 174 | * Calculate alignment offset for arch size void*. This is the |
| 175 | * alignment offset of the event header. |
| 176 | * |
| 177 | * Important note : |
| 178 | * The event header must be a size multiple of the void* size. This is necessary |
| 179 | * to be able to calculate statically the alignment offset of the variable |
| 180 | * length data fields that follows. The total offset calculated here : |
| 181 | * |
| 182 | * Alignment of header struct on arch size |
| 183 | * + sizeof(header struct) |
| 184 | * + padding added to end of struct to align on arch size. |
| 185 | * */ |
| 186 | static inline unsigned char __attribute__((no_instrument_function)) |
| 187 | ltt_get_header_size(struct ltt_trace_info *trace, |
| 188 | void *address, |
| 189 | size_t *before_hdr_pad, |
| 190 | size_t *after_hdr_pad, |
| 191 | size_t *header_size) |
| 192 | { |
| 193 | unsigned int padding; |
| 194 | unsigned int header; |
| 195 | |
| 196 | header = sizeof(struct ltt_event_header_nohb); |
| 197 | |
| 198 | /* Padding before the header. Calculated dynamically */ |
| 199 | *before_hdr_pad = ltt_align((unsigned long)address, header); |
| 200 | padding = *before_hdr_pad; |
| 201 | |
| 202 | /* Padding after header, considering header aligned on ltt_align. |
| 203 | * Calculated statically if header size if known. */ |
| 204 | *after_hdr_pad = ltt_align(header, sizeof(void*)); |
| 205 | padding += *after_hdr_pad; |
| 206 | |
| 207 | *header_size = header; |
| 208 | |
| 209 | return header+padding; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /* ltt_write_event_header |
| 214 | * |
| 215 | * Writes the event header to the pointer. |
| 216 | * |
| 217 | * @channel : pointer to the channel structure |
| 218 | * @ptr : buffer pointer |
| 219 | * @fID : facility ID |
| 220 | * @eID : event ID |
| 221 | * @event_size : size of the event, excluding the event header. |
| 222 | * @offset : offset of the beginning of the header, for alignment. |
| 223 | * Calculated by ltt_get_event_header_size. |
| 224 | * @tsc : time stamp counter. |
| 225 | */ |
| 226 | static inline void __attribute__((no_instrument_function)) |
| 227 | ltt_write_event_header( |
| 228 | struct ltt_trace_info *trace, struct ltt_buf *buf, |
| 229 | void *ptr, ltt_facility_t fID, uint32_t eID, size_t event_size, |
| 230 | size_t offset, uint64_t tsc) |
| 231 | { |
| 232 | struct ltt_event_header_nohb *nohb; |
| 233 | |
| 234 | event_size = min(event_size, 0xFFFFU); |
| 235 | nohb = (struct ltt_event_header_nohb *)(ptr+offset); |
| 236 | nohb->timestamp = (uint64_t)tsc; |
| 237 | nohb->facility_id = fID; |
| 238 | nohb->event_id = eID; |
| 239 | nohb->event_size = (uint16_t)event_size; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | static inline uint64_t __attribute__((no_instrument_function)) |
| 245 | ltt_get_timestamp() |
| 246 | { |
| 247 | return get_cycles(); |
| 248 | } |
| 249 | |
| 250 | static inline unsigned int __attribute__((no_instrument_function)) |
| 251 | ltt_subbuf_header_len(struct ltt_buf *buf) |
| 252 | { |
| 253 | return sizeof(struct ltt_block_start_header); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | |
| 258 | static inline void __attribute__((no_instrument_function)) |
| 259 | ltt_write_trace_header(struct ltt_trace_header *header) |
| 260 | { |
| 261 | header->magic_number = LTT_TRACER_MAGIC_NUMBER; |
| 262 | header->major_version = LTT_TRACER_VERSION_MAJOR; |
| 263 | header->minor_version = LTT_TRACER_VERSION_MINOR; |
| 264 | header->float_word_order = 0; //FIXME |
| 265 | header->arch_type = 0; //FIXME LTT_ARCH_TYPE; |
| 266 | header->arch_size = sizeof(void*); |
| 267 | header->arch_variant = 0; //FIXME LTT_ARCH_VARIANT; |
| 268 | header->flight_recorder = 0; |
| 269 | header->has_heartbeat = 0; |
| 270 | |
| 271 | #ifndef LTT_PACK |
| 272 | header->has_alignment = sizeof(void*); |
| 273 | #else |
| 274 | header->has_alignment = 0; |
| 275 | #endif |
| 276 | |
| 277 | //FIXME |
| 278 | header->freq_scale = 0; |
| 279 | header->start_freq = 0; |
| 280 | header->start_tsc = 0; |
| 281 | header->start_monotonic = 0; |
| 282 | header->start_time_sec = 0; |
| 283 | header->start_time_usec = 0; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | static inline void __attribute__((no_instrument_function)) |
| 288 | ltt_buffer_begin_callback(struct ltt_buf *buf, |
| 289 | uint64_t tsc, unsigned int subbuf_idx) |
| 290 | { |
| 291 | struct ltt_block_start_header *header = |
| 292 | (struct ltt_block_start_header*) |
| 293 | (buf->start + (subbuf_idx*buf->subbuf_size)); |
| 294 | |
| 295 | header->begin.cycle_count = tsc; |
| 296 | header->begin.freq = 0; //ltt_frequency(); |
| 297 | |
| 298 | header->lost_size = 0xFFFFFFFF; // for debugging... |
| 299 | |
| 300 | header->buf_size = buf->subbuf_size; |
| 301 | |
| 302 | ltt_write_trace_header(&header->trace); |
| 303 | |
| 304 | } |
| 305 | |
| 306 | |
| 307 | |
| 308 | static inline void __attribute__((no_instrument_function)) |
| 309 | ltt_buffer_end_callback(struct ltt_buf *buf, |
| 310 | uint64_t tsc, unsigned int offset, unsigned int subbuf_idx) |
| 311 | { |
| 312 | struct ltt_block_start_header *header = |
| 313 | (struct ltt_block_start_header*) |
| 314 | (buf->start + (subbuf_idx*buf->subbuf_size)); |
| 315 | /* offset is assumed to never be 0 here : never deliver a completely |
| 316 | * empty subbuffer. */ |
| 317 | /* The lost size is between 0 and subbuf_size-1 */ |
| 318 | header->lost_size = SUBBUF_OFFSET((buf->subbuf_size - offset), |
| 319 | buf); |
| 320 | header->end.cycle_count = tsc; |
| 321 | header->end.freq = 0; //ltt_frequency(); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | static inline void __attribute__((no_instrument_function)) |
| 326 | ltt_deliver_callback(struct ltt_buf *buf, |
| 327 | unsigned subbuf_idx, |
| 328 | void *subbuf) |
| 329 | { |
| 330 | ltt_usertrace_fast_buffer_switch(); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /* ltt_reserve_slot |
| 335 | * |
| 336 | * Atomic slot reservation in a LTTng buffer. It will take care of |
| 337 | * sub-buffer switching. |
| 338 | * |
| 339 | * Parameters: |
| 340 | * |
| 341 | * @trace : the trace structure to log to. |
| 342 | * @buf : the buffer to reserve space into. |
| 343 | * @data_size : size of the variable length data to log. |
| 344 | * @slot_size : pointer to total size of the slot (out) |
| 345 | * @tsc : pointer to the tsc at the slot reservation (out) |
| 346 | * @before_hdr_pad : dynamic padding before the event header. |
| 347 | * @after_hdr_pad : dynamic padding after the event header. |
| 348 | * |
| 349 | * Return : NULL if not enough space, else returns the pointer |
| 350 | * to the beginning of the reserved slot. */ |
| 351 | static inline void * __attribute__((no_instrument_function)) ltt_reserve_slot( |
| 352 | struct ltt_trace_info *trace, |
| 353 | struct ltt_buf *ltt_buf, |
| 354 | unsigned int data_size, |
| 355 | size_t *slot_size, |
| 356 | uint64_t *tsc, |
| 357 | size_t *before_hdr_pad, |
| 358 | size_t *after_hdr_pad, |
| 359 | size_t *header_size) |
| 360 | { |
| 361 | int offset_begin, offset_end, offset_old; |
| 362 | //int has_switch; |
| 363 | int begin_switch, end_switch_current, end_switch_old; |
| 364 | int reserve_commit_diff = 0; |
| 365 | unsigned int size; |
| 366 | int consumed_old, consumed_new; |
| 367 | int commit_count, reserve_count; |
| 368 | int ret; |
| 369 | |
| 370 | do { |
| 371 | offset_old = atomic_read(<t_buf->offset); |
| 372 | offset_begin = offset_old; |
| 373 | //has_switch = 0; |
| 374 | begin_switch = 0; |
| 375 | end_switch_current = 0; |
| 376 | end_switch_old = 0; |
| 377 | *tsc = ltt_get_timestamp(); |
| 378 | if(*tsc == 0) { |
| 379 | /* Error in getting the timestamp, event lost */ |
| 380 | atomic_inc(<t_buf->events_lost); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | if(SUBBUF_OFFSET(offset_begin, ltt_buf) == 0) { |
| 385 | begin_switch = 1; /* For offset_begin */ |
| 386 | } else { |
| 387 | size = ltt_get_header_size(trace, ltt_buf->start + offset_begin, |
| 388 | before_hdr_pad, after_hdr_pad, header_size) |
| 389 | + data_size; |
| 390 | |
| 391 | if((SUBBUF_OFFSET(offset_begin, ltt_buf)+size)>ltt_buf->subbuf_size) { |
| 392 | //has_switch = 1; |
| 393 | end_switch_old = 1; /* For offset_old */ |
| 394 | begin_switch = 1; /* For offset_begin */ |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if(begin_switch) { |
| 399 | if(end_switch_old) { |
| 400 | offset_begin = SUBBUF_ALIGN(offset_begin, ltt_buf); |
| 401 | } |
| 402 | offset_begin = offset_begin + ltt_subbuf_header_len(ltt_buf); |
| 403 | /* Test new buffer integrity */ |
| 404 | reserve_commit_diff = |
| 405 | atomic_read(<t_buf->reserve_count[SUBBUF_INDEX(offset_begin, |
| 406 | ltt_buf)]) |
| 407 | - atomic_read(<t_buf->commit_count[SUBBUF_INDEX(offset_begin, |
| 408 | ltt_buf)]); |
| 409 | if(reserve_commit_diff == 0) { |
| 410 | /* Next buffer not corrupted. */ |
| 411 | //if((SUBBUF_TRUNC(offset_begin, ltt_buf) |
| 412 | // - SUBBUF_TRUNC(atomic_read(<t_buf->consumed), ltt_buf)) |
| 413 | // >= ltt_buf->alloc_size) { |
| 414 | /* sem_wait is not signal safe. Disable signals around it. */ |
| 415 | { |
| 416 | sigset_t oldset, set; |
| 417 | |
| 418 | /* Disable signals */ |
| 419 | ret = sigfillset(&set); |
| 420 | if(ret) perror("LTT Error in sigfillset\n"); |
| 421 | |
| 422 | ret = pthread_sigmask(SIG_BLOCK, &set, &oldset); |
| 423 | if(ret) perror("LTT Error in pthread_sigmask\n"); |
| 424 | |
| 425 | sem_wait(<t_buf->writer_sem); |
| 426 | |
| 427 | /* Enable signals */ |
| 428 | ret = pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
| 429 | if(ret) perror("LTT Error in pthread_sigmask\n"); |
| 430 | } |
| 431 | |
| 432 | /* go on with the write */ |
| 433 | |
| 434 | //} else { |
| 435 | // /* next buffer not corrupted, we are either in overwrite mode or |
| 436 | // * the buffer is not full. It's safe to write in this new subbuffer.*/ |
| 437 | //} |
| 438 | } else { |
| 439 | /* Next subbuffer corrupted. Force pushing reader even in normal |
| 440 | * mode. It's safe to write in this new subbuffer. */ |
| 441 | sem_post(<t_buf->writer_sem); |
| 442 | } |
| 443 | size = ltt_get_header_size(trace, ltt_buf->start + offset_begin, |
| 444 | before_hdr_pad, after_hdr_pad, header_size) + data_size; |
| 445 | if((SUBBUF_OFFSET(offset_begin,ltt_buf)+size)>ltt_buf->subbuf_size) { |
| 446 | /* Event too big for subbuffers, report error, don't complete |
| 447 | * the sub-buffer switch. */ |
| 448 | atomic_inc(<t_buf->events_lost); |
| 449 | return NULL; |
| 450 | } else { |
| 451 | /* We just made a successful buffer switch and the event fits in the |
| 452 | * new subbuffer. Let's write. */ |
| 453 | } |
| 454 | } else { |
| 455 | /* Event fits in the current buffer and we are not on a switch boundary. |
| 456 | * It's safe to write */ |
| 457 | } |
| 458 | offset_end = offset_begin + size; |
| 459 | |
| 460 | if((SUBBUF_OFFSET(offset_end, ltt_buf)) == 0) { |
| 461 | /* The offset_end will fall at the very beginning of the next subbuffer. |
| 462 | */ |
| 463 | end_switch_current = 1; /* For offset_begin */ |
| 464 | } |
| 465 | |
| 466 | } while(atomic_cmpxchg(<t_buf->offset, offset_old, offset_end) |
| 467 | != offset_old); |
| 468 | |
| 469 | |
| 470 | /* Push the reader if necessary */ |
| 471 | do { |
| 472 | consumed_old = atomic_read(<t_buf->consumed); |
| 473 | /* If buffer is in overwrite mode, push the reader consumed count if |
| 474 | the write position has reached it and we are not at the first |
| 475 | iteration (don't push the reader farther than the writer). |
| 476 | This operation can be done concurrently by many writers in the |
| 477 | same buffer, the writer being at the fartest write position sub-buffer |
| 478 | index in the buffer being the one which will win this loop. */ |
| 479 | /* If the buffer is not in overwrite mode, pushing the reader only |
| 480 | happen if a sub-buffer is corrupted */ |
| 481 | if((SUBBUF_TRUNC(offset_end-1, ltt_buf) |
| 482 | - SUBBUF_TRUNC(consumed_old, ltt_buf)) |
| 483 | >= ltt_buf->alloc_size) |
| 484 | consumed_new = SUBBUF_ALIGN(consumed_old, ltt_buf); |
| 485 | else { |
| 486 | consumed_new = consumed_old; |
| 487 | break; |
| 488 | } |
| 489 | } while(atomic_cmpxchg(<t_buf->consumed, consumed_old, consumed_new) |
| 490 | != consumed_old); |
| 491 | |
| 492 | if(consumed_old != consumed_new) { |
| 493 | /* Reader pushed : we are the winner of the push, we can therefore |
| 494 | reequilibrate reserve and commit. Atomic increment of the commit |
| 495 | count permits other writers to play around with this variable |
| 496 | before us. We keep track of corrupted_subbuffers even in overwrite mode : |
| 497 | we never want to write over a non completely committed sub-buffer : |
| 498 | possible causes : the buffer size is too low compared to the unordered |
| 499 | data input, or there is a writer who died between the reserve and the |
| 500 | commit. */ |
| 501 | if(reserve_commit_diff) { |
| 502 | /* We have to alter the sub-buffer commit count : a sub-buffer is |
| 503 | corrupted. We do not deliver it. */ |
| 504 | atomic_add(reserve_commit_diff, |
| 505 | <t_buf->commit_count[SUBBUF_INDEX(offset_begin, ltt_buf)]); |
| 506 | atomic_inc(<t_buf->corrupted_subbuffers); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | if(end_switch_old) { |
| 512 | /* old subbuffer */ |
| 513 | /* Concurrency safe because we are the last and only thread to alter this |
| 514 | sub-buffer. As long as it is not delivered and read, no other thread can |
| 515 | alter the offset, alter the reserve_count or call the |
| 516 | client_buffer_end_callback on this sub-buffer. |
| 517 | The only remaining threads could be the ones with pending commits. They |
| 518 | will have to do the deliver themself. |
| 519 | Not concurrency safe in overwrite mode. We detect corrupted subbuffers |
| 520 | with commit and reserve counts. We keep a corrupted sub-buffers count |
| 521 | and push the readers across these sub-buffers. |
| 522 | Not concurrency safe if a writer is stalled in a subbuffer and |
| 523 | another writer switches in, finding out it's corrupted. The result will |
| 524 | be than the old (uncommited) subbuffer will be declared corrupted, and |
| 525 | that the new subbuffer will be declared corrupted too because of the |
| 526 | commit count adjustment. |
| 527 | Note : offset_old should never be 0 here.*/ |
| 528 | ltt_buffer_end_callback(ltt_buf, *tsc, offset_old, |
| 529 | SUBBUF_INDEX((offset_old-1), ltt_buf)); |
| 530 | /* Setting this reserve_count will allow the sub-buffer to be delivered by |
| 531 | the last committer. */ |
| 532 | reserve_count = |
| 533 | atomic_add_return((SUBBUF_OFFSET((offset_old-1), ltt_buf)+1), |
| 534 | <t_buf->reserve_count[SUBBUF_INDEX((offset_old-1), ltt_buf)]); |
| 535 | if(reserve_count |
| 536 | == atomic_read(<t_buf->commit_count[SUBBUF_INDEX((offset_old-1), |
| 537 | ltt_buf)])) { |
| 538 | ltt_deliver_callback(ltt_buf, SUBBUF_INDEX((offset_old-1), ltt_buf), |
| 539 | NULL); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if(begin_switch) { |
| 544 | /* New sub-buffer */ |
| 545 | /* This code can be executed unordered : writers may already have written |
| 546 | to the sub-buffer before this code gets executed, caution. */ |
| 547 | /* The commit makes sure that this code is executed before the deliver |
| 548 | of this sub-buffer */ |
| 549 | ltt_buffer_begin_callback(ltt_buf, *tsc, SUBBUF_INDEX(offset_begin, ltt_buf)); |
| 550 | commit_count = atomic_add_return(ltt_subbuf_header_len(ltt_buf), |
| 551 | <t_buf->commit_count[SUBBUF_INDEX(offset_begin, ltt_buf)]); |
| 552 | /* Check if the written buffer has to be delivered */ |
| 553 | if(commit_count |
| 554 | == atomic_read(<t_buf->reserve_count[SUBBUF_INDEX(offset_begin, |
| 555 | ltt_buf)])) { |
| 556 | ltt_deliver_callback(ltt_buf, SUBBUF_INDEX(offset_begin, ltt_buf), NULL); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | if(end_switch_current) { |
| 561 | /* current subbuffer */ |
| 562 | /* Concurrency safe because we are the last and only thread to alter this |
| 563 | sub-buffer. As long as it is not delivered and read, no other thread can |
| 564 | alter the offset, alter the reserve_count or call the |
| 565 | client_buffer_end_callback on this sub-buffer. |
| 566 | The only remaining threads could be the ones with pending commits. They |
| 567 | will have to do the deliver themself. |
| 568 | Not concurrency safe in overwrite mode. We detect corrupted subbuffers |
| 569 | with commit and reserve counts. We keep a corrupted sub-buffers count |
| 570 | and push the readers across these sub-buffers. |
| 571 | Not concurrency safe if a writer is stalled in a subbuffer and |
| 572 | another writer switches in, finding out it's corrupted. The result will |
| 573 | be than the old (uncommited) subbuffer will be declared corrupted, and |
| 574 | that the new subbuffer will be declared corrupted too because of the |
| 575 | commit count adjustment. */ |
| 576 | ltt_buffer_end_callback(ltt_buf, *tsc, offset_end, |
| 577 | SUBBUF_INDEX((offset_end-1), ltt_buf)); |
| 578 | /* Setting this reserve_count will allow the sub-buffer to be delivered by |
| 579 | the last committer. */ |
| 580 | reserve_count = |
| 581 | atomic_add_return((SUBBUF_OFFSET((offset_end-1), ltt_buf)+1), |
| 582 | <t_buf->reserve_count[SUBBUF_INDEX((offset_end-1), ltt_buf)]); |
| 583 | if(reserve_count |
| 584 | == atomic_read(<t_buf->commit_count[SUBBUF_INDEX((offset_end-1), |
| 585 | ltt_buf)])) { |
| 586 | ltt_deliver_callback(ltt_buf, SUBBUF_INDEX((offset_end-1), ltt_buf), NULL); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | *slot_size = size; |
| 591 | |
| 592 | //BUG_ON(*slot_size != (data_size + *before_hdr_pad + *after_hdr_pad + *header_size)); |
| 593 | //BUG_ON(*slot_size != (offset_end - offset_begin)); |
| 594 | |
| 595 | return ltt_buf->start + BUFFER_OFFSET(offset_begin, ltt_buf); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | /* ltt_commit_slot |
| 600 | * |
| 601 | * Atomic unordered slot commit. Increments the commit count in the |
| 602 | * specified sub-buffer, and delivers it if necessary. |
| 603 | * |
| 604 | * Parameters: |
| 605 | * |
| 606 | * @buf : the buffer to commit to. |
| 607 | * @reserved : address of the beginnig of the reserved slot. |
| 608 | * @slot_size : size of the reserved slot. |
| 609 | * |
| 610 | */ |
| 611 | static inline void __attribute__((no_instrument_function)) ltt_commit_slot( |
| 612 | struct ltt_buf *ltt_buf, |
| 613 | void *reserved, |
| 614 | unsigned int slot_size) |
| 615 | { |
| 616 | unsigned int offset_begin = reserved - ltt_buf->start; |
| 617 | int commit_count; |
| 618 | |
| 619 | commit_count = atomic_add_return(slot_size, |
| 620 | <t_buf->commit_count[SUBBUF_INDEX(offset_begin, |
| 621 | ltt_buf)]); |
| 622 | |
| 623 | /* Check if all commits have been done */ |
| 624 | if(commit_count == |
| 625 | atomic_read(<t_buf->reserve_count[SUBBUF_INDEX(offset_begin, ltt_buf)])) { |
| 626 | ltt_deliver_callback(ltt_buf, SUBBUF_INDEX(offset_begin, ltt_buf), NULL); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | #ifdef __cplusplus |
| 631 | } /* end of extern "C" */ |
| 632 | #endif |
| 633 | |
| 634 | #endif //LTT_TRACE_FAST |
| 635 | #endif //LTT_TRACE |
| 636 | #endif //_LTT_USERTRACE_FAST_H |