/* This file is part of the Linux Trace Toolkit viewer
* Copyright (C) 2003-2004 Xiangxiu Yang
+ * 2006 Mathieu Desnoyers
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License Version 2 as
- * published by the Free Software Foundation;
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License Version 2.1 as published by the Free Software Foundation.
*
- * This program is distributed in the hope that it will be useful,
+ * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
- * MA 02111-1307, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
*/
#ifndef LTT_PRIVATE_H
#include <glib.h>
#include <sys/types.h>
#include <ltt/ltt.h>
+#include <endian.h>
+
+
+#ifndef max
+#define max(a,b) ((a)>(b)?(a):(b))
+#endif
+
+#ifndef min
+#define min(a,b) ((a)<(b)?(a):(b))
+#endif
+
+
+
+#define LTT_MAGIC_NUMBER 0x00D6B7ED
+#define LTT_REV_MAGIC_NUMBER 0xEDB7D600
+
+#define NSEC_PER_USEC 1000
#define LTT_PACKED_STRUCT __attribute__ ((packed))
+/* Hardcoded facilities */
+#define LTT_FACILITY_CORE 0
+
+/* Byte ordering */
+#define LTT_GET_BO(t) ((t)->reverse_bo)
+
+#define LTT_HAS_FLOAT(t) ((t)->float_word_order!=0)
+#define LTT_GET_FLOAT_BO(t) \
+ (((t)->float_word_order==__BYTE_ORDER)?0:1)
+
+#define SEQUENCE_AVG_ELEMENTS 1000
+
+/* Hardcoded core events */
+enum ltt_core_events {
+ LTT_EVENT_FACILITY_LOAD,
+ LTT_EVENT_FACILITY_UNLOAD,
+ LTT_EVENT_HEARTBEAT,
+ LTT_EVENT_STATE_DUMP_FACILITY_LOAD
+};
+
+
+#if 0
/* enumeration definition */
typedef enum _BuildinEvent{
/* structure definition */
typedef struct _FacilityLoad{
- char * name;
+ gchar * name;
LttChecksum checksum;
guint32 base_code;
} LTT_PACKED_STRUCT FacilityLoad;
typedef struct _BlockStart {
- LttTime time; //Time stamp of this block
+ LttTime time; //Time stamp of this block
LttCycleCount cycle_count; //cycle count of the event
guint32 block_id; //block id
} LTT_PACKED_STRUCT BlockStart;
typedef struct _BlockEnd {
- LttTime time; //Time stamp of this block
+ LttTime time; //Time stamp of this block
LttCycleCount cycle_count; //cycle count of the event
guint32 block_id; //block id
} LTT_PACKED_STRUCT BlockEnd;
+#endif //0
+
+
+typedef guint8 uint8_t;
+typedef guint16 uint16_t;
+typedef guint32 uint32_t;
+typedef guint64 uint64_t;
+
+/* Hardcoded facility load event : this plus an preceding "name" string */
+struct LttFacilityLoad {
+ guint32 checksum;
+ guint32 id;
+ guint32 int_size;
+ guint32 long_size;
+ guint32 pointer_size;
+ guint32 size_t_size;
+ guint32 has_alignment;
+} LTT_PACKED_STRUCT;
+
+struct LttFacilityUnload {
+ guint32 id;
+} LTT_PACKED_STRUCT;
+
+struct LttStateDumpFacilityLoad {
+ guint32 checksum;
+ guint32 id;
+ guint32 int_size;
+ guint32 long_size;
+ guint32 pointer_size;
+ guint32 size_t_size;
+ guint32 has_alignment;
+} LTT_PACKED_STRUCT;
typedef struct _TimeHeartbeat {
- LttTime time; //Time stamp of this block
- LttCycleCount cycle_count; //cycle count of the event
+ LttTime time; //Time stamp of this block
+ uint64_t cycle_count; //cycle count of the event
} LTT_PACKED_STRUCT TimeHeartbeat;
+struct ltt_event_header_hb {
+ uint32_t timestamp;
+ unsigned char facility_id;
+ unsigned char event_id;
+ uint16_t event_size;
+} LTT_PACKED_STRUCT;
+
+struct ltt_event_header_nohb {
+ uint64_t timestamp;
+ unsigned char facility_id;
+ unsigned char event_id;
+ uint16_t event_size;
+} LTT_PACKED_STRUCT;
+
+
+/* Block and trace headers */
+
+struct ltt_trace_header_any {
+ uint32_t magic_number;
+ uint32_t arch_type;
+ uint32_t arch_variant;
+ uint32_t float_word_order;
+ uint8_t arch_size;
+ uint8_t major_version;
+ uint8_t minor_version;
+ uint8_t flight_recorder;
+ uint8_t has_heartbeat;
+ uint8_t has_alignment; /* Event header alignment */
+ uint8_t has_tsc;
+} LTT_PACKED_STRUCT;
+
+
+/* For version 0.3 */
+
+struct ltt_trace_header_0_3 {
+ uint32_t magic_number;
+ uint32_t arch_type;
+ uint32_t arch_variant;
+ uint32_t float_word_order;
+ uint8_t arch_size;
+ uint8_t major_version;
+ uint8_t minor_version;
+ uint8_t flight_recorder;
+ uint8_t has_heartbeat;
+ uint8_t has_alignment; /* Event header alignment */
+ uint8_t has_tsc;
+} LTT_PACKED_STRUCT;
+
+/* For version 0.6 */
+
+struct ltt_trace_header_0_6 {
+ uint32_t magic_number;
+ uint32_t arch_type;
+ uint32_t arch_variant;
+ uint32_t float_word_order;
+ uint8_t arch_size;
+ uint8_t major_version;
+ uint8_t minor_version;
+ uint8_t flight_recorder;
+ uint8_t has_heartbeat;
+ uint8_t has_alignment; /* Event header alignment */
+ uint8_t has_tsc;
+ uint64_t start_freq;
+ uint64_t start_tsc;
+ uint64_t start_monotonic;
+ //struct timespec start_time; // not portable
+ uint64_t start_time_sec;
+ uint64_t start_time_usec;
+} LTT_PACKED_STRUCT;
+
+
+struct ltt_block_start_header {
+ struct {
+ //struct timeval timestamp;
+ uint64_t timestamp_sec;
+ uint64_t timestamp_usec;
+ uint64_t cycle_count;
+ uint64_t freq;
+ } begin;
+ struct {
+ //struct timeval timestamp;
+ uint64_t timestamp_sec;
+ uint64_t timestamp_usec;
+ uint64_t cycle_count;
+ uint64_t freq;
+ } end;
+ uint32_t lost_size; /* Size unused at the end of the buffer */
+ uint32_t buf_size; /* The size of this sub-buffer */
+ struct ltt_trace_header_any trace[0];
+} LTT_PACKED_STRUCT;
+
struct _LttType{
- char * type_name; //type name if it is a named type
- char * element_name; //elements name of the struct
- char * fmt;
- unsigned int size;
+// LTTV does not care about type names. Everything is a field.
+// GQuark type_name; //type name if it is a named type
+ gchar * fmt;
+ guint size;
LttTypeEnum type_class; //which type
- char ** enum_strings; //for enum labels
- struct _LttType ** element_type; //for array, sequence and struct
- unsigned element_number; //the number of elements
- //for enum, array, sequence and structure
+ GHashTable *enum_map; //maps enum labels to numbers.
+ GArray *fields; // Array of LttFields, for array, sequence, union, struct.
+ GData *fields_by_name;
};
struct _LttEventType{
- char * name;
- char * description;
- int index; //id of the event type within the facility
+ GQuark name;
+ gchar * description;
+ guint index; //id of the event type within the facility
LttFacility * facility; //the facility that contains the event type
- LttField * root_field; //root field
- unsigned int latest_block; //the latest block using the event type
- unsigned int latest_event; //the latest event using the event type
+ GArray * fields; //event's fields (LttField)
+ GData *fields_by_name;
};
+/* Structure LttEvent and LttEventPosition must begin with the _exact_ same
+ * fields in the exact same order. LttEventPosition is a parent of LttEvent. */
struct _LttEvent{
- guint16 event_id;
- guint32 time_delta;
+
+ /* Begin of LttEventPosition fields */
+ LttTracefile *tracefile;
+ unsigned int block;
+ unsigned int offset;
+
+ /* Timekeeping */
+ uint64_t tsc; /* Current timestamp counter */
+
+ /* End of LttEventPosition fields */
+
+ union { /* choice by trace has_tsc */
+ guint32 timestamp; /* truncated timestamp */
+ LttTime delta;
+ } time;
+
+ unsigned char facility_id; /* facility ID are never reused. */
+ unsigned char event_id;
+
LttTime event_time;
- LttCycleCount event_cycle_count;
- LttTracefile * tracefile;
+
void * data; //event data
- unsigned int which_block; //the current block of the event
- unsigned int which_event; //the position of the event
- /* This is a workaround for fast position seek */
- void * last_event_pos;
-
- LttTime prev_block_end_time; //the end time of previous block
- LttTime prev_event_time; //the time of the previous event
- LttCycleCount pre_cycle_count; //previous cycle count of the event
+ guint data_size;
+ guint event_size; //event_size field of the header :
+ //used to verify data_size from facility.
+
int count; //the number of overflow of cycle count
gint64 overflow_nsec; //precalculated nsec for overflows
- TimeHeartbeat * last_heartbeat; //last heartbeat
+};
- /* end of workaround */
+struct _LttEventPosition{
+ LttTracefile *tracefile;
+ unsigned int block;
+ unsigned int offset;
+
+ /* Timekeeping */
+ uint64_t tsc; /* Current timestamp counter */
};
+enum field_status { FIELD_UNKNOWN, FIELD_VARIABLE, FIELD_FIXED };
+
struct _LttField{
- unsigned field_pos; //field position within its parent
- LttType * field_type; //field type, if it is root field
- //then it must be struct type
-
- off_t offset_root; //offset from the root, -1:uninitialized
- short fixed_root; //offset fixed according to the root
- //-1:uninitialized, 0:unfixed, 1:fixed
- off_t offset_parent; //offset from the parent,-1:uninitialized
- short fixed_parent; //offset fixed according to its parent
- //-1:uninitialized, 0:unfixed, 1:fixed
- // void * base_address; //base address of the field ????
-
- int field_size; //>0: size of the field,
- //0 : uncertain
- //-1: uninitialize
- int sequ_number_size; //the size of unsigned used to save the
- //number of elements in the sequence
-
- int element_size; //the element size of the sequence
- int field_fixed; //0: field has string or sequence
- //1: field has no string or sequenc
- //-1: uninitialize
-
- struct _LttField * parent;
- struct _LttField ** child; //for array, sequence and struct:
- //list of fields, it may have only one
- //field if the element is not a struct
- unsigned current_element; //which element is currently processed
+ GQuark name;
+ gchar *description;
+ LttType field_type; //field type
+
+ off_t offset_root; //offset from the root
+ enum field_status fixed_root; //offset fixed according to the root
+
+ guint field_size; // size of the field
+ // Only if field type size is set to 0
+ // (it's variable), then the field_size should be
+ // dynamically calculated while reading the trace
+ // and put here. Otherwise, the field_size always
+ // equels the type size.
+ off_t array_offset; // offset of the beginning of the array (for array
+ // and sequences)
+ GArray * dynamic_offsets; // array of offsets calculated dynamically at
+ // each event for sequences and arrays that
+ // contain variable length fields.
};
-
struct _LttFacility{
- char * name; //facility name
- unsigned int event_number; //number of events in the facility
- LttChecksum checksum; //checksum of the facility
- guint32 base_id; //base id of the facility
- LttEventType ** events; //array of event types
- LttType ** named_types;
- unsigned int named_types_number;
+ LttTrace *trace;
+ GQuark name;
+ guint32 checksum; //checksum of the facility
+ guint32 id; //id of the facility
+
+ guint32 int_size;
+ guint32 long_size;
+ guint32 pointer_size;
+ guint32 size_t_size;
+ guint32 alignment;
+
+ GArray *events;
+ GData *events_by_name;
+ // not necessary in LTTV GData *named_types;
+
+ unsigned char exists; /* 0 does not exist, 1 exists */
};
+typedef struct _LttBuffer {
+ void * head;
+ unsigned int index;
+
+ struct {
+ LttTime timestamp;
+ uint64_t cycle_count;
+ uint64_t freq; /* Frequency in khz */
+ } begin;
+ struct {
+ LttTime timestamp;
+ uint64_t cycle_count;
+ uint64_t freq; /* Frequency in khz */
+ } end;
+ uint32_t lost_size; /* Size unused at the end of the buffer */
+
+ /* Timekeeping */
+ uint64_t tsc; /* Current timestamp counter */
+ uint64_t freq; /* Frequency in khz */
+ //double nsecs_per_cycle; /* Precalculated from freq */
+ guint32 cyc2ns_scale;
+} LttBuffer;
+
struct _LttTracefile{
- char * name; //tracefile name
+ gboolean cpu_online; //is the cpu online ?
+ GQuark long_name; //tracefile complete filename
+ GQuark name; //tracefile name
+ guint cpu_num; //cpu number of the tracefile
LttTrace * trace; //trace containing the tracefile
int fd; //file descriptor
off_t file_size; //file size
- unsigned block_size; //block_size
- unsigned int block_number; //number of blocks in the file
- unsigned int which_block; //which block the current block is
- unsigned int which_event; //which event of the current block
- //is currently processed
- LttTime current_event_time; //time of the current event
- BlockStart * a_block_start; //block start of the block- trace endian
- BlockEnd * a_block_end; //block end of the block - trace endian
- TimeHeartbeat * last_heartbeat; //last heartbeat
- void * cur_event_pos; //the position of the current event
- void * buffer; //the buffer containing the block
- double nsec_per_cycle; //Nsec per cycle
- guint64 one_overflow_nsec; //nsec for one overflow
- gint64 overflow_nsec; //precalculated nsec for overflows
- //can be negative to include value
- //of block start cycle count.
- //incremented at each overflow while
- //reading.
+ //unsigned block_size; //block_size
+ guint num_blocks; //number of blocks in the file
+ gboolean reverse_bo; //must we reverse byte order ?
+ gboolean float_word_order; //what is the byte order of floats ?
+ size_t has_alignment; //alignment of events in the tracefile.
+ // 0 or the architecture size in bytes.
+
+ size_t buffer_header_size;
+
+ /* Current event */
+ LttEvent event; //Event currently accessible in the trace
+
+ /* Current block */
+ LttBuffer buffer; //current buffer
+ guint32 buf_size; /* The size of blocks */
+
+ /* Time flow */
+ //unsigned int count; //the number of overflow of cycle count
+ //double nsec_per_cycle; //Nsec per cycle
+ //TimeHeartbeat * last_heartbeat; //last heartbeat
+
//LttCycleCount cycles_per_nsec_reciprocal; // Optimisation for speed
- unsigned cur_heart_beat_number; //current number of heart beat in the buf
- LttCycleCount cur_cycle_count; //current cycle count of the event
- void * last_event_pos;
-
- LttTime prev_block_end_time; //the end time of previous block
- LttTime prev_event_time; //the time of the previous event
- LttCycleCount pre_cycle_count; //previous cycle count of the event
- unsigned int count; //the number of overflow of cycle count
-};
+ //void * last_event_pos;
-struct _LttTrace{
- char * pathname; //the pathname of the trace
- guint facility_number; //the number of facilities
- guint control_tracefile_number; //the number of control files
- guint per_cpu_tracefile_number; //the number of per cpu files
- LttSystemDescription * system_description;//system description
-
- GPtrArray *control_tracefiles; //array of control tracefiles
- GPtrArray *per_cpu_tracefiles; //array of per cpu tracefiles
- GPtrArray *facilities; //array of facilities
- gboolean reverse_byte_order; //must we reverse BO ?
+ //LttTime prev_block_end_time; //the end time of previous block
+ //LttTime prev_event_time; //the time of the previous event
+ //LttCycleCount pre_cycle_count; //previous cycle count of the event
};
-struct _LttEventPosition{
- unsigned block_num; //block which contains the event
- unsigned event_num; //event index in the block
- unsigned event_offset; //event position in the block
- LttTime event_time; //the time of the event
- LttCycleCount event_cycle_count; //the cycle count of the event
- unsigned heart_beat_number; //current number of heart beats
- LttTracefile *tf; //tracefile containing the event
- gboolean old_position; //flag to show if it is the position
- //being remembered
- /* This is a workaround for fast position seek */
- void * last_event_pos;
-
- LttTime prev_block_end_time; //the end time of previous block
- LttTime prev_event_time; //the time of the previous event
- LttCycleCount pre_cycle_count; //previous cycle count of the event
- int count; //the number of overflow of cycle count
- gint64 overflow_nsec; //precalculated nsec for overflows
- TimeHeartbeat * last_heartbeat; //last heartbeat
- /* end of workaround */
+struct _LttTrace{
+ GQuark pathname; //the pathname of the trace
+ //LttSystemDescription * system_description;//system description
+
+ GArray *facilities_by_num; /* fac_id as index in array */
+ GData *facilities_by_name; /* fac name (GQuark) as index */
+ /* Points to array of fac_id of all the
+ * facilities that has this name. */
+ guint num_cpu;
+
+ guint32 arch_type;
+ guint32 arch_variant;
+ guint8 arch_size;
+ guint8 ltt_major_version;
+ guint8 ltt_minor_version;
+ guint8 flight_recorder;
+ guint8 has_heartbeat;
+ guint8 has_tsc;
+ uint64_t start_freq;
+ uint64_t start_tsc;
+ uint64_t start_monotonic;
+ LttTime start_time;
+ LttTime start_time_from_tsc;
+
+ GData *tracefiles; //tracefiles groups
};
/* The characteristics of the system on which the trace was obtained
is described in a LttSystemDescription structure. */
struct _LttSystemDescription {
- char *description;
- char *node_name;
- char *domain_name;
+ gchar *description;
+ gchar *node_name;
+ gchar *domain_name;
unsigned nb_cpu;
LttArchSize size;
LttArchEndian endian;
- char *kernel_name;
- char *kernel_release;
- char *kernel_version;
- char *machine;
- char *processor;
- char *hardware_platform;
- char *operating_system;
- unsigned ltt_major_version;
- unsigned ltt_minor_version;
- unsigned ltt_block_size;
+ gchar *kernel_name;
+ gchar *kernel_release;
+ gchar *kernel_version;
+ gchar *machine;
+ gchar *processor;
+ gchar *hardware_platform;
+ gchar *operating_system;
LttTime trace_start;
LttTime trace_end;
};
/*****************************************************************************
macro for size of some data types
*****************************************************************************/
-#define EVENT_ID_SIZE sizeof(guint16)
-#define TIME_DELTA_SIZE sizeof(guint32)
-#define EVENT_HEADER_SIZE (EVENT_ID_SIZE + TIME_DELTA_SIZE)
+// alignment -> dynamic!
+
+//#define TIMESTAMP_SIZE sizeof(guint32)
+//#define EVENT_ID_SIZE sizeof(guint16)
+//#define EVENT_HEADER_SIZE (TIMESTAMP_SIZE + EVENT_ID_SIZE)
+
+
+/* Calculate the offset needed to align the type.
+ * If has_alignment is 0, alignment is disactivated.
+ * else, the function returns the offset needed to
+ * align align_drift on the has_alignment value (should be
+ * the size of the architecture). */
+static inline unsigned int ltt_align(size_t align_drift,
+ size_t size_of_type,
+ size_t has_alignment)
+{
+ size_t alignment = min(has_alignment, size_of_type);
+
+ if(!has_alignment) return 0;
+
+ return ((alignment - align_drift) & (alignment-1));
+}
#endif /* LTT_PRIVATE_H */