| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2003-2004 Xiangxiu Yang |
| 3 | * 2006 Mathieu Desnoyers |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License Version 2.1 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifndef LTT_PRIVATE_H |
| 21 | #define LTT_PRIVATE_H |
| 22 | |
| 23 | #include <glib.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <ltt/ltt.h> |
| 26 | #include <endian.h> |
| 27 | |
| 28 | |
| 29 | #ifndef max |
| 30 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 31 | #endif |
| 32 | |
| 33 | #ifndef min |
| 34 | #define min(a,b) ((a)<(b)?(a):(b)) |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | |
| 39 | #define LTT_MAGIC_NUMBER 0x00D6B7ED |
| 40 | #define LTT_REV_MAGIC_NUMBER 0xEDB7D600 |
| 41 | |
| 42 | #define NSEC_PER_USEC 1000 |
| 43 | |
| 44 | #define LTT_PACKED_STRUCT __attribute__ ((packed)) |
| 45 | |
| 46 | /* Hardcoded facilities */ |
| 47 | #define LTT_FACILITY_CORE 0 |
| 48 | |
| 49 | /* Byte ordering */ |
| 50 | #define LTT_GET_BO(t) ((t)->reverse_bo) |
| 51 | |
| 52 | #define LTT_HAS_FLOAT(t) ((t)->float_word_order!=0) |
| 53 | #define LTT_GET_FLOAT_BO(t) \ |
| 54 | (((t)->float_word_order==__BYTE_ORDER)?0:1) |
| 55 | |
| 56 | #define SEQUENCE_AVG_ELEMENTS 1000 |
| 57 | |
| 58 | /* Hardcoded core events */ |
| 59 | enum ltt_core_events { |
| 60 | LTT_EVENT_FACILITY_LOAD, |
| 61 | LTT_EVENT_FACILITY_UNLOAD, |
| 62 | LTT_EVENT_HEARTBEAT, |
| 63 | LTT_EVENT_STATE_DUMP_FACILITY_LOAD |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | #if 0 |
| 68 | /* enumeration definition */ |
| 69 | |
| 70 | typedef enum _BuildinEvent{ |
| 71 | TRACE_FACILITY_LOAD = 0, |
| 72 | TRACE_BLOCK_START = 17, |
| 73 | TRACE_BLOCK_END = 18, |
| 74 | TRACE_TIME_HEARTBEAT= 19 |
| 75 | } BuildinEvent; |
| 76 | |
| 77 | |
| 78 | /* structure definition */ |
| 79 | |
| 80 | typedef struct _FacilityLoad{ |
| 81 | gchar * name; |
| 82 | LttChecksum checksum; |
| 83 | guint32 base_code; |
| 84 | } LTT_PACKED_STRUCT FacilityLoad; |
| 85 | |
| 86 | typedef struct _BlockStart { |
| 87 | LttTime time; //Time stamp of this block |
| 88 | LttCycleCount cycle_count; //cycle count of the event |
| 89 | guint32 block_id; //block id |
| 90 | } LTT_PACKED_STRUCT BlockStart; |
| 91 | |
| 92 | typedef struct _BlockEnd { |
| 93 | LttTime time; //Time stamp of this block |
| 94 | LttCycleCount cycle_count; //cycle count of the event |
| 95 | guint32 block_id; //block id |
| 96 | } LTT_PACKED_STRUCT BlockEnd; |
| 97 | #endif //0 |
| 98 | |
| 99 | |
| 100 | typedef guint8 uint8_t; |
| 101 | typedef guint16 uint16_t; |
| 102 | typedef guint32 uint32_t; |
| 103 | typedef guint64 uint64_t; |
| 104 | |
| 105 | /* Hardcoded facility load event : this plus an preceding "name" string */ |
| 106 | struct LttFacilityLoad { |
| 107 | guint32 checksum; |
| 108 | guint32 id; |
| 109 | guint32 int_size; |
| 110 | guint32 long_size; |
| 111 | guint32 pointer_size; |
| 112 | guint32 size_t_size; |
| 113 | guint32 has_alignment; |
| 114 | } LTT_PACKED_STRUCT; |
| 115 | |
| 116 | struct LttFacilityUnload { |
| 117 | guint32 id; |
| 118 | } LTT_PACKED_STRUCT; |
| 119 | |
| 120 | struct LttStateDumpFacilityLoad { |
| 121 | guint32 checksum; |
| 122 | guint32 id; |
| 123 | guint32 int_size; |
| 124 | guint32 long_size; |
| 125 | guint32 pointer_size; |
| 126 | guint32 size_t_size; |
| 127 | guint32 has_alignment; |
| 128 | } LTT_PACKED_STRUCT; |
| 129 | |
| 130 | /* Empty event */ |
| 131 | typedef struct _TimeHeartbeat { |
| 132 | } LTT_PACKED_STRUCT TimeHeartbeat; |
| 133 | |
| 134 | struct ltt_event_header_hb { |
| 135 | uint32_t timestamp; |
| 136 | unsigned char facility_id; |
| 137 | unsigned char event_id; |
| 138 | uint16_t event_size; |
| 139 | } LTT_PACKED_STRUCT; |
| 140 | |
| 141 | struct ltt_event_header_nohb { |
| 142 | uint64_t timestamp; |
| 143 | unsigned char facility_id; |
| 144 | unsigned char event_id; |
| 145 | uint16_t event_size; |
| 146 | } LTT_PACKED_STRUCT; |
| 147 | |
| 148 | |
| 149 | /* Block and trace headers */ |
| 150 | |
| 151 | struct ltt_trace_header_any { |
| 152 | uint32_t magic_number; |
| 153 | uint32_t arch_type; |
| 154 | uint32_t arch_variant; |
| 155 | uint32_t float_word_order; |
| 156 | uint8_t arch_size; |
| 157 | uint8_t major_version; |
| 158 | uint8_t minor_version; |
| 159 | uint8_t flight_recorder; |
| 160 | uint8_t has_heartbeat; |
| 161 | uint8_t has_alignment; /* Event header alignment */ |
| 162 | uint32_t freq_scale; |
| 163 | } LTT_PACKED_STRUCT; |
| 164 | |
| 165 | |
| 166 | /* For version 0.3 */ |
| 167 | |
| 168 | struct ltt_trace_header_0_3 { |
| 169 | uint32_t magic_number; |
| 170 | uint32_t arch_type; |
| 171 | uint32_t arch_variant; |
| 172 | uint32_t float_word_order; |
| 173 | uint8_t arch_size; |
| 174 | uint8_t major_version; |
| 175 | uint8_t minor_version; |
| 176 | uint8_t flight_recorder; |
| 177 | uint8_t has_heartbeat; |
| 178 | uint8_t has_alignment; /* Event header alignment */ |
| 179 | uint32_t freq_scale; |
| 180 | } LTT_PACKED_STRUCT; |
| 181 | |
| 182 | /* For version 0.7 */ |
| 183 | |
| 184 | struct ltt_trace_header_0_7 { |
| 185 | uint32_t magic_number; |
| 186 | uint32_t arch_type; |
| 187 | uint32_t arch_variant; |
| 188 | uint32_t float_word_order; |
| 189 | uint8_t arch_size; |
| 190 | uint8_t major_version; |
| 191 | uint8_t minor_version; |
| 192 | uint8_t flight_recorder; |
| 193 | uint8_t has_heartbeat; |
| 194 | uint8_t has_alignment; /* Event header alignment */ |
| 195 | uint32_t freq_scale; |
| 196 | uint64_t start_freq; |
| 197 | uint64_t start_tsc; |
| 198 | uint64_t start_monotonic; |
| 199 | uint64_t start_time_sec; |
| 200 | uint64_t start_time_usec; |
| 201 | } LTT_PACKED_STRUCT; |
| 202 | |
| 203 | |
| 204 | struct ltt_block_start_header { |
| 205 | struct { |
| 206 | uint64_t cycle_count; |
| 207 | uint64_t freq; |
| 208 | } begin; |
| 209 | struct { |
| 210 | uint64_t cycle_count; |
| 211 | uint64_t freq; |
| 212 | } end; |
| 213 | uint32_t lost_size; /* Size unused at the end of the buffer */ |
| 214 | uint32_t buf_size; /* The size of this sub-buffer */ |
| 215 | struct ltt_trace_header_any trace[0]; |
| 216 | } LTT_PACKED_STRUCT; |
| 217 | |
| 218 | |
| 219 | struct _LttType{ |
| 220 | // LTTV does not care about type names. Everything is a field. |
| 221 | // GQuark type_name; //type name if it is a named type |
| 222 | gchar * fmt; |
| 223 | guint size; |
| 224 | LttTypeEnum type_class; //which type |
| 225 | GHashTable *enum_map; //maps enum labels to numbers. |
| 226 | GArray *fields; // Array of LttFields, for array, sequence, union, struct. |
| 227 | GData *fields_by_name; |
| 228 | guint network; // Is the type in network byte order ? |
| 229 | }; |
| 230 | |
| 231 | struct _LttEventType{ |
| 232 | GQuark name; |
| 233 | gchar * description; |
| 234 | guint index; //id of the event type within the facility |
| 235 | LttFacility * facility; //the facility that contains the event type |
| 236 | GArray * fields; //event's fields (LttField) |
| 237 | GData *fields_by_name; |
| 238 | }; |
| 239 | |
| 240 | /* Structure LttEvent and LttEventPosition must begin with the _exact_ same |
| 241 | * fields in the exact same order. LttEventPosition is a parent of LttEvent. */ |
| 242 | struct _LttEvent{ |
| 243 | |
| 244 | /* Begin of LttEventPosition fields */ |
| 245 | LttTracefile *tracefile; |
| 246 | unsigned int block; |
| 247 | unsigned int offset; |
| 248 | |
| 249 | /* Timekeeping */ |
| 250 | uint64_t tsc; /* Current timestamp counter */ |
| 251 | |
| 252 | /* End of LttEventPosition fields */ |
| 253 | |
| 254 | guint32 timestamp; /* truncated timestamp */ |
| 255 | |
| 256 | unsigned char facility_id; /* facility ID are never reused. */ |
| 257 | unsigned char event_id; |
| 258 | |
| 259 | LttTime event_time; |
| 260 | |
| 261 | void * data; //event data |
| 262 | guint data_size; |
| 263 | guint event_size; //event_size field of the header : |
| 264 | //used to verify data_size from facility. |
| 265 | |
| 266 | int count; //the number of overflow of cycle count |
| 267 | gint64 overflow_nsec; //precalculated nsec for overflows |
| 268 | }; |
| 269 | |
| 270 | struct _LttEventPosition{ |
| 271 | LttTracefile *tracefile; |
| 272 | unsigned int block; |
| 273 | unsigned int offset; |
| 274 | |
| 275 | /* Timekeeping */ |
| 276 | uint64_t tsc; /* Current timestamp counter */ |
| 277 | }; |
| 278 | |
| 279 | |
| 280 | enum field_status { FIELD_UNKNOWN, FIELD_VARIABLE, FIELD_FIXED }; |
| 281 | |
| 282 | struct _LttField{ |
| 283 | GQuark name; |
| 284 | gchar *description; |
| 285 | LttType field_type; //field type |
| 286 | |
| 287 | off_t offset_root; //offset from the root |
| 288 | enum field_status fixed_root; //offset fixed according to the root |
| 289 | |
| 290 | guint field_size; // size of the field |
| 291 | // Only if field type size is set to 0 |
| 292 | // (it's variable), then the field_size should be |
| 293 | // dynamically calculated while reading the trace |
| 294 | // and put here. Otherwise, the field_size always |
| 295 | // equels the type size. |
| 296 | off_t array_offset; // offset of the beginning of the array (for array |
| 297 | // and sequences) |
| 298 | GArray * dynamic_offsets; // array of offsets calculated dynamically at |
| 299 | // each event for sequences and arrays that |
| 300 | // contain variable length fields. |
| 301 | }; |
| 302 | |
| 303 | struct _LttFacility{ |
| 304 | LttTrace *trace; |
| 305 | GQuark name; |
| 306 | guint32 checksum; //checksum of the facility |
| 307 | guint32 id; //id of the facility |
| 308 | |
| 309 | guint32 int_size; |
| 310 | guint32 long_size; |
| 311 | guint32 pointer_size; |
| 312 | guint32 size_t_size; |
| 313 | guint32 alignment; |
| 314 | |
| 315 | GArray *events; |
| 316 | GData *events_by_name; |
| 317 | // not necessary in LTTV GData *named_types; |
| 318 | |
| 319 | unsigned char exists; /* 0 does not exist, 1 exists */ |
| 320 | }; |
| 321 | |
| 322 | typedef struct _LttBuffer { |
| 323 | void * head; |
| 324 | unsigned int index; |
| 325 | |
| 326 | struct { |
| 327 | LttTime timestamp; |
| 328 | uint64_t cycle_count; |
| 329 | uint64_t freq; /* Frequency in khz */ |
| 330 | } begin; |
| 331 | struct { |
| 332 | LttTime timestamp; |
| 333 | uint64_t cycle_count; |
| 334 | uint64_t freq; /* Frequency in khz */ |
| 335 | } end; |
| 336 | uint32_t lost_size; /* Size unused at the end of the buffer */ |
| 337 | |
| 338 | /* Timekeeping */ |
| 339 | uint64_t tsc; /* Current timestamp counter */ |
| 340 | uint64_t freq; /* Frequency in khz */ |
| 341 | //double nsecs_per_cycle; /* Precalculated from freq */ |
| 342 | guint32 cyc2ns_scale; |
| 343 | } LttBuffer; |
| 344 | |
| 345 | struct _LttTracefile{ |
| 346 | gboolean cpu_online; //is the cpu online ? |
| 347 | GQuark long_name; //tracefile complete filename |
| 348 | GQuark name; //tracefile name |
| 349 | guint cpu_num; //cpu number of the tracefile |
| 350 | guint tid; //Usertrace tid, else 0 |
| 351 | guint pgid; //Usertrace pgid, else 0 |
| 352 | guint64 creation; //Usertrace creation, else 0 |
| 353 | LttTrace * trace; //trace containing the tracefile |
| 354 | int fd; //file descriptor |
| 355 | off_t file_size; //file size |
| 356 | //unsigned block_size; //block_size |
| 357 | guint num_blocks; //number of blocks in the file |
| 358 | gboolean reverse_bo; //must we reverse byte order ? |
| 359 | gboolean float_word_order; //what is the byte order of floats ? |
| 360 | size_t has_alignment; //alignment of events in the tracefile. |
| 361 | // 0 or the architecture size in bytes. |
| 362 | |
| 363 | size_t buffer_header_size; |
| 364 | |
| 365 | /* Current event */ |
| 366 | LttEvent event; //Event currently accessible in the trace |
| 367 | |
| 368 | /* Current block */ |
| 369 | LttBuffer buffer; //current buffer |
| 370 | guint32 buf_size; /* The size of blocks */ |
| 371 | |
| 372 | /* Time flow */ |
| 373 | //unsigned int count; //the number of overflow of cycle count |
| 374 | //double nsec_per_cycle; //Nsec per cycle |
| 375 | //TimeHeartbeat * last_heartbeat; //last heartbeat |
| 376 | |
| 377 | //LttCycleCount cycles_per_nsec_reciprocal; // Optimisation for speed |
| 378 | //void * last_event_pos; |
| 379 | |
| 380 | //LttTime prev_block_end_time; //the end time of previous block |
| 381 | //LttTime prev_event_time; //the time of the previous event |
| 382 | //LttCycleCount pre_cycle_count; //previous cycle count of the event |
| 383 | }; |
| 384 | |
| 385 | struct _LttTrace{ |
| 386 | GQuark pathname; //the pathname of the trace |
| 387 | //LttSystemDescription * system_description;//system description |
| 388 | |
| 389 | GArray *facilities_by_num; /* fac_id as index in array */ |
| 390 | GData *facilities_by_name; /* fac name (GQuark) as index */ |
| 391 | /* Points to array of fac_id of all the |
| 392 | * facilities that has this name. */ |
| 393 | guint num_cpu; |
| 394 | |
| 395 | guint32 arch_type; |
| 396 | guint32 arch_variant; |
| 397 | guint8 arch_size; |
| 398 | guint8 ltt_major_version; |
| 399 | guint8 ltt_minor_version; |
| 400 | guint8 flight_recorder; |
| 401 | guint8 has_heartbeat; |
| 402 | guint32 freq_scale; |
| 403 | uint64_t start_freq; |
| 404 | uint64_t start_tsc; |
| 405 | uint64_t start_monotonic; |
| 406 | LttTime start_time; |
| 407 | LttTime start_time_from_tsc; |
| 408 | |
| 409 | GData *tracefiles; //tracefiles groups |
| 410 | }; |
| 411 | |
| 412 | /* The characteristics of the system on which the trace was obtained |
| 413 | is described in a LttSystemDescription structure. */ |
| 414 | |
| 415 | struct _LttSystemDescription { |
| 416 | gchar *description; |
| 417 | gchar *node_name; |
| 418 | gchar *domain_name; |
| 419 | unsigned nb_cpu; |
| 420 | LttArchSize size; |
| 421 | LttArchEndian endian; |
| 422 | gchar *kernel_name; |
| 423 | gchar *kernel_release; |
| 424 | gchar *kernel_version; |
| 425 | gchar *machine; |
| 426 | gchar *processor; |
| 427 | gchar *hardware_platform; |
| 428 | gchar *operating_system; |
| 429 | LttTime trace_start; |
| 430 | LttTime trace_end; |
| 431 | }; |
| 432 | |
| 433 | /***************************************************************************** |
| 434 | macro for size of some data types |
| 435 | *****************************************************************************/ |
| 436 | // alignment -> dynamic! |
| 437 | |
| 438 | //#define TIMESTAMP_SIZE sizeof(guint32) |
| 439 | //#define EVENT_ID_SIZE sizeof(guint16) |
| 440 | //#define EVENT_HEADER_SIZE (TIMESTAMP_SIZE + EVENT_ID_SIZE) |
| 441 | |
| 442 | |
| 443 | off_t get_alignment(LttField *field); |
| 444 | |
| 445 | /* Calculate the offset needed to align the type. |
| 446 | * If has_alignment is 0, alignment is disactivated. |
| 447 | * else, the function returns the offset needed to |
| 448 | * align align_drift on the has_alignment value (should be |
| 449 | * the size of the architecture). */ |
| 450 | static inline unsigned int ltt_align(size_t align_drift, |
| 451 | size_t size_of_type, |
| 452 | size_t has_alignment) |
| 453 | { |
| 454 | size_t alignment = min(has_alignment, size_of_type); |
| 455 | |
| 456 | if(!has_alignment) return 0; |
| 457 | |
| 458 | g_assert(size_of_type != 0); |
| 459 | return ((alignment - align_drift) & (alignment-1)); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | #endif /* LTT_PRIVATE_H */ |