#include <ltt/trace.h>
#include <ltt/ltt-types.h>
+
+
+void compute_fields_offsets(LttTracefile *tf, LttField *field, off_t *offset,
+ void *root);
+
+
+
+
LttEvent *ltt_event_new()
{
return g_new(LttEvent, 1);
return &g_array_index(facility->events, LttEventType, e->event_id);
}
-/*****************************************************************************
- *Function name
- * ltt_event_field : get the root field of the event
- *Input params
- * e : an instance of an event type
- *Return value
- * LttField * : the root field of the event
- ****************************************************************************/
-
-LttField *ltt_event_field(LttEvent *e)
-{
- LttField * field;
- LttEventType * event_type = ltt_event_eventtype(e);
- if(unlikely(!event_type)) return NULL;
- field = event_type->root_field;
- if(unlikely(!field)) return NULL;
-
- get_field_type_size(e->tracefile, event_type, 0, 0,
- field, e->data);
-
- return field;
-}
/*****************************************************************************
*Function name
****************************************************************************/
guint64 ltt_event_field_element_number(LttEvent *e, LttField *f)
{
- if(f->field_type->type_class != LTT_ARRAY &&
- f->field_type->type_class != LTT_SEQUENCE)
+ if(f->field_type.type_class != LTT_ARRAY &&
+ f->field_type.type_class != LTT_SEQUENCE)
return 0;
- if(f->field_type->type_class == LTT_ARRAY)
- return f->field_type->element_number;
- return ltt_get_uint(LTT_GET_BO(e->tracefile), f->sequ_number_size,
- e + f->offset_root);
+ if(f->field_type.type_class == LTT_ARRAY)
+ return f->field_type.size;
+ return ltt_event_get_long_unsigned(e, &g_array_index(f->field_type.fields,
+ LttField, 0));
}
/*****************************************************************************
* ltt_event_field_element_select
* : Set the currently selected element for a sequence or
* array field
- * O(1) if fields are of fixed size, else O(n) if fields are
- * of variable size.
+ * O(1) because of offset array.
*Input params
* e : an instance of an event type
* f : a field of the instance
* i : the ith element (0, ...)
+ *returns : the child field, at the right index, updated.
****************************************************************************/
-void ltt_event_field_element_select(LttEvent *e, LttField *f, unsigned i)
+LttField *ltt_event_field_element_select(LttEvent *e, LttField *f, gulong i)
{
- unsigned element_number;
+ gulong element_number;
LttField *field;
unsigned int k;
size_t size;
LttEventType *event_type;
+ off_t new_offset;
- if(f->field_type->type_class != LTT_ARRAY &&
- f->field_type->type_class != LTT_SEQUENCE)
+ if(f->field_type.type_class != LTT_ARRAY &&
+ f->field_type.type_class != LTT_SEQUENCE)
return ;
element_number = ltt_event_field_element_number(e,f);
/* Sanity check for i : 0..n-1 only, and must be lower or equal element_number
*/
if(i >= element_number) return;
-
- field = f->child[0];
-
- if(f->field_type->type_class == LTT_SEQUENCE)
- size = f->sequ_number_size;
- else
- size = 0;
-
- if(field->fixed_size == FIELD_FIXED) {
- size += field->field_size * i;
-
- get_field_type_size(e->tracefile, event_type,
- f->offset_root+size, size, field, e->data);
+
+ if(f->field_type.type_class == LTT_ARRAY) {
+ field = &g_array_index(f->field_type.fields, LttField, 0);
+ } else {
+ field = &g_array_index(f->field_type.fields, LttField, 1);
+ }
+ if(field->field_size != 0) {
+ if(f->array_offset + (i * field->field_size) == field->offset_root)
+ return; /* fixed length child, already at the right offset */
+ else
+ new_offset = f->array_offset + (i * field->field_size);
} else {
- for(k=0;k<=i;k++){
- size += get_field_type_size(e->tracefile, event_type,
- f->offset_root+size, size, field, e->data);
- }
+ /* Var. len. child */
+ new_offset = g_array_index(f->dynamic_offsets, off_t, i);
}
- f->current_element = i;
+ compute_fields_offsets(e->tracefile, field, &new_offset, e->data);
+
+ return field;
}
/*****************************************************************************
{
gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
- LttTypeEnum t = f->field_type->type_class;
-
- g_assert(t == LTT_UINT || t == LTT_ENUM);
+ LttTypeEnum t = f->field_type.type_class;
if(f->field_size == 1){
guint8 x = *(guint8 *)(e->data + f->offset_root);
{
gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
- g_assert(f->field_type->type_class == LTT_INT);
-
if(f->field_size == 1){
gint8 x = *(gint8 *)(e->data + f->offset_root);
return (gint32) x;
{
gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
- LttTypeEnum t = f->field_type->type_class;
-
- g_assert(t == LTT_UINT || t == LTT_ENUM
- || t == LTT_ULONG || LTT_SIZE_T || LTT_OFF_T || LTT_POINTER);
+ LttTypeEnum t = f->field_type.type_class;
if(f->field_size == 1){
guint8 x = *(guint8 *)(e->data + f->offset_root);
// e->tracefile->trace->system_description->endian ? 0:1;
gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
- g_assert( f->field_type->type_class == LTT_INT
- || f->field_type->type_class == LTT_LONG
- || f->field_type->type_class == LTT_SSIZE_T);
-
if(f->field_size == 1){
gint8 x = *(gint8 *)(e->data + f->offset_root);
return (gint64) x;
float ltt_event_get_float(LttEvent *e, LttField *f)
{
- gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
+ g_assert(LTT_HAS_FLOAT(e->tracefile));
+ gboolean reverse_byte_order = LTT_GET_FLOAT_BO(e->tracefile);
- g_assert(f->field_type->type_class == LTT_FLOAT && f->field_size == 4);
+ g_assert(f->field_type.type_class == LTT_FLOAT && f->field_size == 4);
if(reverse_byte_order == 0) return *(float *)(e->data + f->offset_root);
else{
-// guint32 aInt;
void *ptr = e->data + f->offset_root;
-// memcpy((void*)&aInt, e->data + f->offset_root, 4);
-// aInt = bswap_32();
-// return ((float)aInt);
- return *(float*)bswap_32(*(guint32*)ptr);
+ guint32 value = bswap_32(*(guint32*)ptr);
+ return *(float*)&value;
}
}
double ltt_event_get_double(LttEvent *e, LttField *f)
{
- gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
+ g_assert(LTT_HAS_FLOAT(e->tracefile));
+ gboolean reverse_byte_order = LTT_GET_FLOAT_BO(e->tracefile);
- g_assert(f->field_type->type_class == LTT_FLOAT && f->field_size == 8);
+ if(f->field_size == 4)
+ return ltt_event_get_float(e, f);
+
+ g_assert(f->field_type.type_class == LTT_FLOAT && f->field_size == 8);
if(reverse_byte_order == 0) return *(double *)(e->data + f->offset_root);
- else{
+ else {
void *ptr = e->data + f->offset_root;
- return *(double*)bswap_64(*(guint64*)ptr);
- //guint64 aInt;
- //memcpy((void*)&aInt, e->data + f->offset_root, 8);
- //aInt = ___swab64(aInt);
- //return ((double)aInt);
+ guint64 value = bswap_64(*(guint64*)ptr);
+ return *(double*)&value;
}
}
****************************************************************************/
char *ltt_event_get_string(LttEvent *e, LttField *f)
{
- g_assert(f->field_type->type_class == LTT_STRING);
+ g_assert(f->field_type.type_class == LTT_STRING);
return (gchar*)g_strdup((gchar*)(e->data + f->offset_root));
}
* data : a pointer to the event data.
*Returns the field type size.
****************************************************************************/
+ // TODO
+// Change this function so it uses a *to offset value incrementation, just like
+// genevent-new instead of returning a size. What is of interest here is the
+// offset needed to read each field.
+//
+// Precomputed ones can be returned directly. Otherwise, the field is flagged
+// "VARIABLE OFFSET" and must be computed dynamically. The dynamic processing
+// of an offset takes the last known fixed offset, and then dynamically
+// calculates all variable field offsets from it.
+//
+// After a VARIABLE SIZE element, all fields have a variable offset.
+// Also, is an array or a sequence has variable length child, we must pass
+// through all of them, saving the offsets in the dynamic_offsets array.
+
+#if 0
size_t get_field_type_size(LttTracefile *tf, LttEventType *event_type,
off_t offset_root, off_t offset_parent,
LttField *field, void *data)
size_t size = 0;
guint i;
LttType *type;
+ off_t align;
g_assert(field->fixed_root != FIELD_UNKNOWN);
g_assert(field->fixed_parent != FIELD_UNKNOWN);
case LTT_SSIZE_T:
case LTT_OFF_T:
g_assert(field->fixed_size == FIELD_FIXED);
- size = field->field_size;
+ size = field->field_size;
+ align = ltt_align(field->offset_root,
+ size, event_type->facility->has_alignment);
+ field->offset_root += align;
+ field->offset_parent += align;
+ size += align;
break;
case LTT_SEQUENCE:
{
+ /* FIXME : check the type of sequence identifier */
gint seqnum = ltt_get_uint(LTT_GET_BO(tf),
field->sequ_number_size,
data + offset_root);
return size;
}
+#endif //0
+
+
+
+
+
+/*****************************************************************************
+ *Function name
+ * compute_fields_offsets : set the precomputable offset of the fields
+ *Input params
+ * tf : tracefile
+ * field : the field
+ * offset : pointer to the current offset, must be incremented
+ ****************************************************************************/
+
+
+void compute_fields_offsets(LttTracefile *tf, LttField *field, off_t *offset,
+ void *root)
+{
+ LttType *type = &field->field_type;
+
+ switch(type->type_class) {
+ case LTT_INT_FIXED:
+ case LTT_UINT_FIXED:
+ case LTT_POINTER:
+ case LTT_CHAR:
+ case LTT_UCHAR:
+ case LTT_SHORT:
+ case LTT_USHORT:
+ case LTT_INT:
+ case LTT_UINT:
+ case LTT_LONG:
+ case LTT_ULONG:
+ case LTT_SIZE_T:
+ case LTT_SSIZE_T:
+ case LTT_OFF_T:
+ case LTT_FLOAT:
+ case LTT_ENUM:
+ if(field->fixed_root == FIELD_VARIABLE) {
+ /* Align offset on type size */
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ /* Increment offset */
+ *offset += field->field_size;
+ }
+ /* None of these types has variable size, so we are sure that if
+ * this element has a fixed_root, then the following one will have
+ * a fixed root too, so it does not need the *offset at all.
+ */
+ break;
+ case LTT_STRING:
+ if(field->fixed_root == FIELD_VARIABLE) {
+ field->offset_root = *offset;
+ }
+ *offset += strlen((gchar*)(root+*offset)) + 1;
+ break;
+ case LTT_ARRAY:
+ g_assert(type->fields->len == 1);
+ {
+ off_t local_offset;
+ LttField *child = &g_array_index(type->fields, LttField, 0);
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ field->array_offset = *offset;
+ }
+
+ if(field->field_size != 0) {
+ /* Increment offset */
+ /* field_size is the array size in bytes */
+ *offset = field->offset_root + field->field_size;
+ } else {
+ guint i;
+ *offset = field->array_offset;
+ field->dynamic_offsets = g_array_set_size(field->dynamic_offsets,
+ 0);
+ for(i=0; i<type->size; i++) {
+ g_array_append_val(field->dynamic_offsets, *offset);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ }
+ // local_offset = field->array_offset;
+ // /* Set the offset at position 0 */
+ // compute_fields_offsets(tf, child, &local_offset, root);
+ }
+ break;
+ case LTT_SEQUENCE:
+ g_assert(type->fields->len == 2);
+ {
+ off_t local_offset;
+ LttField *child;
+ guint i;
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+
+ child = &g_array_index(type->fields, LttField, 0);
+ compute_fields_offsets(tf, child, offset, root);
+ child = &g_array_index(type->fields, LttField, 1);
+ *offset += ltt_align(*offset, get_alignment(tf, child),
+ tf->has_alignment);
+ field->array_offset = *offset;
+
+ } else {
+ child = &g_array_index(type->fields, LttField, 1);
+ }
+ *offset = field->array_offset;
+ field->dynamic_offsets = g_array_set_size(field->dynamic_offsets,
+ 0);
+ for(i=0; i<ltt_event_field_element_number(&tf->event, field); i++) {
+ g_array_append_val(field->dynamic_offsets, *offset);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ // local_offset = field->array_offset;
+ // /* Set the offset at position 0 */
+ // compute_fields_offsets(tf, child, &local_offset, root);
+ }
+ break;
+ case LTT_STRUCT:
+ {
+ LttField *child;
+ guint i;
+ gint ret=0;
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ } else {
+ *offset = field->offset_root;
+ }
+ for(i=0; i<type->fields->len; i++) {
+ child = &g_array_index(type->fields, LttField, i);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ }
+ break;
+ case LTT_UNION:
+ {
+ LttField *child;
+ guint i;
+ gint ret=0;
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ }
+ for(i=0; i<type->fields->len; i++) {
+ *offset = field->offset_root;
+ child = &g_array_index(type->fields, LttField, i);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ *offset = field->offset_root + field->field_size;
+ }
+ break;
+ case LTT_NONE:
+ default:
+ g_error("compute_fields_offsets : unknown type");
+ }
+
+}
+
+/*****************************************************************************
+ *Function name
+ * compute_offsets : set the dynamically computable offsets of an event type
+ *Input params
+ * tf : tracefile
+ * event : event type
+ *
+ ****************************************************************************/
+void compute_offsets(LttTracefile *tf, LttEventType *event, off_t *offset,
+ void *root)
+{
+ guint i;
+ gint ret;
+
+ /* compute all variable offsets */
+ for(i=0; i<event->fields->len; i++) {
+ LttField *field = &g_array_index(event->fields, LttField, i);
+ compute_fields_offsets(tf, field, offset, root);
+ if(ret) break;
+ }
+
+}