| 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 | #include <errno.h> |
| 12 | #include <asm/atomic.h> |
| 13 | #include <pthread.h> |
| 14 | #include <stdint.h> |
| 15 | #include <syscall.h> |
| 16 | #include <linux/futex.h> |
| 17 | |
| 18 | #ifndef futex |
| 19 | static inline _syscall6(long, futex, unsigned long, uaddr, int, op, int, val, |
| 20 | unsigned long, timeout, unsigned long, uaddr2, int, val2) |
| 21 | #endif //futex |
| 22 | |
| 23 | |
| 24 | #ifndef LTT_N_SUBBUFS |
| 25 | #define LTT_N_SUBBUFS 2 |
| 26 | #endif //LTT_N_SUBBUFS |
| 27 | |
| 28 | #ifndef LTT_SUBBUF_SIZE_CPU |
| 29 | #define LTT_SUBBUF_SIZE_CPU 1048576 |
| 30 | #endif //LTT_BUF_SIZE_CPU |
| 31 | |
| 32 | #define LTT_BUF_SIZE_CPU (LTT_SUBBUF_SIZE_CPU * LTT_N_SUBBUFS) |
| 33 | |
| 34 | #ifndef LTT_SUBBUF_SIZE_FACILITIES |
| 35 | #define LTT_SUBBUF_SIZE_FACILITIES 4096 |
| 36 | #endif //LTT_BUF_SIZE_FACILITIES |
| 37 | |
| 38 | #define LTT_BUF_SIZE_FACILITIES (LTT_SUBBUF_SIZE_FACILITIES * LTT_N_SUBBUFS) |
| 39 | |
| 40 | #ifndef LTT_USERTRACE_ROOT |
| 41 | #define LTT_USERTRACE_ROOT "/tmp/ltt-usertrace" |
| 42 | #endif //LTT_USERTRACE_ROOT |
| 43 | |
| 44 | |
| 45 | /* Buffer offset macros */ |
| 46 | |
| 47 | #define BUFFER_OFFSET(offset, buf) (offset & (buf->alloc_size-1)) |
| 48 | #define SUBBUF_OFFSET(offset, buf) (offset & (buf->subbuf_size-1)) |
| 49 | #define SUBBUF_ALIGN(offset, buf) \ |
| 50 | (((offset) + buf->subbuf_size) & (~(buf->subbuf_size-1))) |
| 51 | #define SUBBUF_TRUNC(offset, buf) \ |
| 52 | ((offset) & (~(buf->subbuf_size-1))) |
| 53 | #define SUBBUF_INDEX(offset, buf) \ |
| 54 | (BUFFER_OFFSET(offset,buf)/buf->subbuf_size) |
| 55 | |
| 56 | |
| 57 | #define LTT_TRACER_MAGIC_NUMBER 0x00D6B7ED |
| 58 | #define LTT_TRACER_VERSION_MAJOR 0 |
| 59 | #define LTT_TRACER_VERSION_MINOR 7 |
| 60 | |
| 61 | #ifndef atomic_cmpxchg |
| 62 | #define atomic_cmpxchg(v, old, new) ((int)cmpxchg(&((v)->counter), old, new)) |
| 63 | #endif //atomic_cmpxchg |
| 64 | struct ltt_trace_header { |
| 65 | uint32_t magic_number; |
| 66 | uint32_t arch_type; |
| 67 | uint32_t arch_variant; |
| 68 | uint32_t float_word_order; /* Only useful for user space traces */ |
| 69 | uint8_t arch_size; |
| 70 | //uint32_t system_type; |
| 71 | uint8_t major_version; |
| 72 | uint8_t minor_version; |
| 73 | uint8_t flight_recorder; |
| 74 | uint8_t has_heartbeat; |
| 75 | uint8_t has_alignment; /* Event header alignment */ |
| 76 | uint32_t freq_scale; |
| 77 | uint64_t start_freq; |
| 78 | uint64_t start_tsc; |
| 79 | uint64_t start_monotonic; |
| 80 | uint64_t start_time_sec; |
| 81 | uint64_t start_time_usec; |
| 82 | } __attribute((packed)); |
| 83 | |
| 84 | |
| 85 | struct ltt_block_start_header { |
| 86 | struct { |
| 87 | uint64_t cycle_count; |
| 88 | uint64_t freq; /* khz */ |
| 89 | } begin; |
| 90 | struct { |
| 91 | uint64_t cycle_count; |
| 92 | uint64_t freq; /* khz */ |
| 93 | } end; |
| 94 | uint32_t lost_size; /* Size unused at the end of the buffer */ |
| 95 | uint32_t buf_size; /* The size of this sub-buffer */ |
| 96 | struct ltt_trace_header trace; |
| 97 | } __attribute((packed)); |
| 98 | |
| 99 | |
| 100 | |
| 101 | struct ltt_buf { |
| 102 | void *start; |
| 103 | atomic_t offset; |
| 104 | atomic_t consumed; |
| 105 | atomic_t reserve_count[LTT_N_SUBBUFS]; |
| 106 | atomic_t commit_count[LTT_N_SUBBUFS]; |
| 107 | |
| 108 | atomic_t events_lost; |
| 109 | atomic_t corrupted_subbuffers; |
| 110 | atomic_t full; /* futex on which the writer waits : 1 : full */ |
| 111 | unsigned int alloc_size; |
| 112 | unsigned int subbuf_size; |
| 113 | }; |
| 114 | |
| 115 | struct ltt_trace_info { |
| 116 | int init; |
| 117 | int filter; |
| 118 | pid_t daemon_id; |
| 119 | atomic_t nesting; |
| 120 | struct { |
| 121 | struct ltt_buf facilities; |
| 122 | struct ltt_buf cpu; |
| 123 | char facilities_buf[LTT_BUF_SIZE_FACILITIES] __attribute__ ((aligned (8))); |
| 124 | char cpu_buf[LTT_BUF_SIZE_CPU] __attribute__ ((aligned (8))); |
| 125 | } channel; |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | |
| 130 | extern __thread struct ltt_trace_info *thread_trace_info; |
| 131 | |
| 132 | void ltt_thread_init(void); |
| 133 | |
| 134 | void ltt_usertrace_fast_buffer_switch(void); |
| 135 | |
| 136 | |
| 137 | |
| 138 | static inline uint64_t ltt_get_timestamp() |
| 139 | { |
| 140 | return get_cycles(); |
| 141 | } |
| 142 | |
| 143 | static inline unsigned int ltt_subbuf_header_len(struct ltt_buf *buf) |
| 144 | { |
| 145 | return sizeof(struct ltt_block_start_header); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | static inline void ltt_write_trace_header(struct ltt_trace_header *header) |
| 151 | { |
| 152 | header->magic_number = LTT_TRACER_MAGIC_NUMBER; |
| 153 | header->major_version = LTT_TRACER_VERSION_MAJOR; |
| 154 | header->minor_version = LTT_TRACER_VERSION_MINOR; |
| 155 | header->float_word_order = 0; //FIXME |
| 156 | header->arch_type = 0; //FIXME LTT_ARCH_TYPE; |
| 157 | header->arch_size = sizeof(void*); |
| 158 | header->arch_variant = 0; //FIXME LTT_ARCH_VARIANT; |
| 159 | header->flight_recorder = 0; |
| 160 | header->has_heartbeat = 0; |
| 161 | |
| 162 | #ifdef CONFIG_LTT_ALIGNMENT |
| 163 | header->has_alignment = sizeof(void*); |
| 164 | #else |
| 165 | header->has_alignment = 0; |
| 166 | #endif |
| 167 | |
| 168 | //FIXME |
| 169 | header->freq_scale = 0; |
| 170 | header->start_freq = 0; |
| 171 | header->start_tsc = 0; |
| 172 | header->start_monotonic = 0; |
| 173 | header->start_time_sec = 0; |
| 174 | header->start_time_usec = 0; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | static inline void ltt_buffer_begin_callback(struct ltt_buf *buf, |
| 179 | uint64_t tsc, unsigned int subbuf_idx) |
| 180 | { |
| 181 | struct ltt_block_start_header *header = |
| 182 | (struct ltt_block_start_header*) |
| 183 | (buf->start + (subbuf_idx*buf->subbuf_size)); |
| 184 | |
| 185 | header->begin.cycle_count = tsc; |
| 186 | header->begin.freq = 0; //ltt_frequency(); |
| 187 | |
| 188 | header->lost_size = 0xFFFFFFFF; // for debugging... |
| 189 | |
| 190 | header->buf_size = buf->subbuf_size; |
| 191 | |
| 192 | ltt_write_trace_header(&header->trace); |
| 193 | |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | static inline void ltt_buffer_end_callback(struct ltt_buf *buf, |
| 199 | uint64_t tsc, unsigned int offset, unsigned int subbuf_idx) |
| 200 | { |
| 201 | struct ltt_block_start_header *header = |
| 202 | (struct ltt_block_start_header*) |
| 203 | (buf->start + (subbuf_idx*buf->subbuf_size)); |
| 204 | /* offset is assumed to never be 0 here : never deliver a completely |
| 205 | * empty subbuffer. */ |
| 206 | /* The lost size is between 0 and subbuf_size-1 */ |
| 207 | header->lost_size = SUBBUF_OFFSET((buf->subbuf_size - offset), |
| 208 | buf); |
| 209 | header->end.cycle_count = tsc; |
| 210 | header->end.freq = 0; //ltt_frequency(); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | static inline void ltt_deliver_callback(struct ltt_buf *buf, |
| 215 | unsigned subbuf_idx, |
| 216 | void *subbuf) |
| 217 | { |
| 218 | ltt_usertrace_fast_buffer_switch(); |
| 219 | } |
| 220 | #endif //_LTT_USERTRACE_FAST_H |