X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=lttv%2Flttv%2Ftraceset.c;h=b10564c5dce8d040a7fafb44bfb2d97ec8f9981e;hb=9a366873e0ed6ecc7f23e2d3a959ecddd4f2f91d;hp=16487a117ff90b86bf7e3675e7d93762e3a480f6;hpb=7a4bdb546094fea3343bfc80e3744ea26a08dd22;p=lttv.git diff --git a/lttv/lttv/traceset.c b/lttv/lttv/traceset.c index 16487a11..b10564c5 100644 --- a/lttv/lttv/traceset.c +++ b/lttv/lttv/traceset.c @@ -23,11 +23,23 @@ #include #include #include +#include #include #include +#include #include -#include +#include #include + +/* To traverse a tree recursively */ +#include +#include +/* For the use of realpath*/ +#include +#include +/* For strcpy*/ +#include + /* A trace is a sequence of events gathered in the same tracing session. The events may be stored in several tracefiles in the same directory. A trace set is defined when several traces are to be analyzed together, @@ -37,29 +49,27 @@ LttvTraceset *lttv_traceset_new(void) { - LttvTraceset *s; - struct bt_iter_pos begin_pos; - - s = g_new(LttvTraceset, 1); - s->filename = NULL; - s->traces = g_ptr_array_new(); - s->context = bt_context_create(); - s->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); + LttvTraceset *ts; + struct bt_iter_pos begin_pos; + + ts = g_new(LttvTraceset, 1); + ts->filename = NULL; + ts->traces = g_ptr_array_new(); + ts->context = bt_context_create(); + ts->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); //TODO remove this when we have really mecanism //s->tmpState = g_new(LttvTraceState *, 1); //lttv_trace_state_init(s->tmpState,0); - begin_pos.type = BT_SEEK_BEGIN; - - //s->iter = bt_ctf_iter_create(lttv_traceset_get_context(s), - // &begin_pos, - // NULL); - s->iter = 0; - s->event_hooks = lttv_hooks_new(); + /*Initialize iterator to the beginning of the traces*/ + begin_pos.type = BT_SEEK_BEGIN; + ts->iter = bt_ctf_iter_create(ts->context, &begin_pos, NULL); + ts->event_hooks = lttv_hooks_new(); + ts->state_trace_handle_index = g_ptr_array_new(); - return s; + return ts; } char * lttv_traceset_name(LttvTraceset * s) @@ -79,6 +89,30 @@ LttvTrace *lttv_trace_new(LttTrace *t) return new_trace; } #endif +/* + * get_absolute_pathname : Return the unique pathname in the system + * + * pathname is the relative path. + * + * abs_pathname is being set to the absolute path. + * + */ +void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname) +{ + abs_pathname[0] = '\0'; + + if (realpath(pathname, abs_pathname) != NULL) + return; + else + { + /* error, return the original path unmodified */ + strcpy(abs_pathname, pathname); + return; + } + return; +} + + /* * lttv_trace_create : Create a trace from a path @@ -94,27 +128,31 @@ LttvTrace *lttv_trace_new(LttTrace *t) */ static LttvTrace *lttv_trace_create(LttvTraceset *ts, const char *path) { - int id = bt_context_add_trace(lttv_traceset_get_context(ts), - path, - "ctf", - NULL, - NULL, - NULL); - if (id < 0) { - return NULL; - } - // Create the trace and save the trace handle id returned by babeltrace - LttvTrace *new_trace; + int id = bt_context_add_trace(lttv_traceset_get_context(ts), + path, + "ctf", + NULL, + NULL, + NULL); + if (id < 0) { + return NULL; + } + // Create the trace and save the trace handle id returned by babeltrace + LttvTrace *new_trace; + + new_trace = g_new(LttvTrace, 1); + new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); + new_trace->id = id; + new_trace->ref_count = 0; + new_trace->traceset = ts; + new_trace->state = g_new(LttvTraceState,1); + lttv_trace_state_init(new_trace->state,new_trace); + + /* Add the state to the trace_handle to state index */ + g_ptr_array_set_size(ts->state_trace_handle_index,id+1); + g_ptr_array_index(ts->state_trace_handle_index,id) = new_trace->state; - new_trace = g_new(LttvTrace, 1); - new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL); - new_trace->id = id; - new_trace->ref_count = 0; - new_trace->traceset = ts; - new_trace->state = g_new(LttvTraceState,1); - lttv_trace_state_init(new_trace->state,new_trace); - ts->tmpState = new_trace->state; - return new_trace; + return new_trace; } /* @@ -128,12 +166,12 @@ static LttvTrace *lttv_trace_create(LttvTraceset *ts, const char *path) */ static int lttv_traceset_create_trace(LttvTraceset *ts, const char *path) { - LttvTrace *trace = lttv_trace_create(ts, path); - if (trace == NULL) { - return -1; - } - lttv_traceset_add(ts, trace); - return 0; + LttvTrace *trace = lttv_trace_create(ts, path); + if (trace == NULL) { + return -1; + } + lttv_traceset_add(ts, trace); + return 0; } LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig) @@ -145,6 +183,7 @@ LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig) s = g_new(LttvTraceset, 1); s->filename = NULL; s->traces = g_ptr_array_new(); + s->state_trace_handle_index = g_ptr_array_new(); for(i=0;itraces->len;i++) { trace = g_ptr_array_index(s_orig->traces, i); @@ -152,6 +191,10 @@ LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig) /* WARNING: this is an alias, not a copy. */ g_ptr_array_add(s->traces, trace); + + g_ptr_array_set_size(s->state_trace_handle_index,trace->id+1); + g_ptr_array_index(s->state_trace_handle_index,trace->id) = trace->state; + } s->context = s_orig->context; bt_context_get(s->context); @@ -225,20 +268,88 @@ void lttv_trace_destroy(LttvTrace *t) g_free(t); } - void lttv_traceset_add(LttvTraceset *s, LttvTrace *t) { t->ref_count++; g_ptr_array_add(s->traces, t); } -int lttv_traceset_add_path(LttvTraceset *ts, const char *trace_path) +int lttv_traceset_add_path(LttvTraceset *ts, char *trace_path) { - // todo mdenis 2012-03-27: add trace recursively and update comment - int ret = lttv_traceset_create_trace(ts, trace_path); - return ret; + FTS *tree; + FTSENT *node; + char * const paths[2] = { trace_path, NULL }; + int ret = -1; + + gboolean metaFileFound = FALSE; + + tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0); + if (tree == NULL) { + g_warning("Cannot traverse \"%s\" for reading.\n", + trace_path); + return ret; + } + + int dirfd, metafd; + while ((node = fts_read(tree))) { + + if (!(node->fts_info & FTS_D)) + continue; + + dirfd = open(node->fts_accpath, 0); + if (dirfd < 0) { + g_warning("Unable to open trace " + "directory file descriptor : %s.", node->fts_accpath); + ret = dirfd; + goto error; + } + + // Check if a metadata file exists in the current directory + metafd = openat(dirfd, "metadata", O_RDONLY); + if (metafd < 0) { + ret = close(dirfd); + if (ret < 0) { + g_warning("Unable to open metadata " + "file descriptor : %s.", node->fts_accpath); + goto error; + } + } else { + ret = close(metafd); + if (ret < 0) { + g_warning("Unable to close metadata " + "file descriptor : %s.", node->fts_accpath); + goto error; + } + ret = close(dirfd); + if (ret < 0) { + g_warning("Unable to close trace " + "directory file descriptor : %s.", node->fts_accpath); + goto error; + } + + ret = lttv_traceset_create_trace(ts, node->fts_accpath); + if (ret < 0) { + g_warning("Opening trace \"%s\" from %s " + "for reading.", node->fts_accpath, trace_path); + goto error; + } + metaFileFound = TRUE; + } + } + +error: + ret = fts_close(tree); + if (ret < 0) { + g_warning("Unable to close tree " + "file descriptor : %s.", trace_path); + } + if(metaFileFound) + return ret; + else + return -1; } + unsigned lttv_traceset_number(LttvTraceset *s) { return s->traces->len; @@ -277,16 +388,10 @@ LttvAttribute *lttv_trace_attribute(LttvTrace *t) return t->a; } -#ifdef BABEL_CLEANUP -LttTrace *lttv_trace(LttvTrace *t) -{ - return t->t; -} -#endif gint lttv_trace_get_id(LttvTrace *t) { - return t->id; + return t->id; } guint lttv_trace_get_ref_number(LttvTrace * t) @@ -318,24 +423,54 @@ guint lttv_trace_get_num_cpu(LttvTrace *t) LttvTracesetPosition *lttv_traceset_create_position(LttvTraceset *traceset) { -#warning "TODO" - return NULL; + LttvTracesetPosition *traceset_pos; + + traceset_pos = g_new(LttvTracesetPosition, 1); + + /* Check if the new passed */ + if(traceset_pos == NULL) { + return NULL; + } + + traceset_pos->iter = traceset->iter; + traceset_pos->bt_pos = bt_iter_get_pos(bt_ctf_get_iter(traceset->iter)); + + return traceset_pos; +} + +LttvTracesetPosition *lttv_traceset_create_time_position(LttvTraceset *traceset, + LttTime timestamp) +{ + LttvTracesetPosition *traceset_pos; + + traceset_pos = g_new(LttvTracesetPosition, 1); + + /* Check if the new passed */ + if(traceset_pos == NULL) { + return NULL; + } + + traceset_pos->iter = traceset->iter; + traceset_pos->bt_pos = bt_iter_create_time_pos( + bt_ctf_get_iter(traceset_pos->iter), + ltt_time_to_uint64(timestamp)); + + return traceset_pos; } void lttv_traceset_destroy_position(LttvTracesetPosition *traceset_pos) { -#warning "TODO" - return NULL; + bt_iter_free_pos(traceset_pos->bt_pos); + g_free(traceset_pos); } -void lttv_traceset_seek_to_position(LttvTracesetPosition *traceset_pos) +void lttv_traceset_seek_to_position(const LttvTracesetPosition *traceset_pos) { -#warning "TODO" + bt_iter_set_pos(bt_ctf_get_iter(traceset_pos->iter), traceset_pos->bt_pos); } guint lttv_traceset_get_cpuid_from_event(LttvEvent *event) { - struct definition *scope; unsigned long timestamp; unsigned int cpu_id; @@ -344,7 +479,7 @@ guint lttv_traceset_get_cpuid_from_event(LttvEvent *event) if (timestamp == -1ULL) { return 0; } - scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT); + const struct definition *scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT); if (bt_ctf_field_get_error()) { return 0; } @@ -356,7 +491,166 @@ guint lttv_traceset_get_cpuid_from_event(LttvEvent *event) } } +guint64 lttv_traceset_get_timestamp_first_event(LttvTraceset *ts) +{ + LttvTracesetPosition begin_position; + struct bt_iter_pos pos; + begin_position.bt_pos = &pos; + + /* Assign iterator to the beginning of the traces */ + begin_position.bt_pos->type = BT_SEEK_BEGIN; + begin_position.iter = ts->iter; + + return lttv_traceset_position_get_timestamp(&begin_position); +} + +/* + * lttv_traceset_get_timestamp_begin : returns the minimum timestamp of + * all the traces in the traceset. + * + */ +guint64 lttv_traceset_get_timestamp_begin(LttvTraceset *traceset) +{ + struct bt_context *bt_ctx; + bt_ctx = lttv_traceset_get_context(traceset); + guint64 timestamp_min, timestamp_cur = 0; + int i; + int trace_count; + LttvTrace *currentTrace; + trace_count = traceset->traces->len; + if(trace_count == 0) + timestamp_min = 0; + else{ + timestamp_min = G_MAXUINT64; + for(i = 0; i < trace_count;i++) + { + currentTrace = g_ptr_array_index(traceset->traces,i); + timestamp_cur = bt_trace_handle_get_timestamp_begin(bt_ctx, + currentTrace->id); + if(timestamp_cur < timestamp_min) + timestamp_min = timestamp_cur; + } + } + return timestamp_min; +} + +/* + * lttv_traceset_get_timestamp_end: returns the maximum timestamp of + * all the traces in the traceset. + * + */ +guint64 lttv_traceset_get_timestamp_end(LttvTraceset *traceset) +{ + struct bt_context *bt_ctx; + bt_ctx = lttv_traceset_get_context(traceset); + guint64 timestamp_max, timestamp_cur = 0; + int i; + int trace_count; + LttvTrace *currentTrace; + trace_count = traceset->traces->len; + + if(trace_count == 0){ + timestamp_max = 1; + } + else{ + timestamp_max = 0; + for(i =0; i < trace_count;i++) + { + currentTrace = g_ptr_array_index(traceset->traces,i); + timestamp_cur = bt_trace_handle_get_timestamp_end(bt_ctx, + currentTrace->id); + if(timestamp_cur > timestamp_max){ + timestamp_max = timestamp_cur; + } + } + } + return timestamp_max; +} +/* + * lttv_traceset_get_time_span_real : return a TimeInterval representing the + * minimum timestamp dans le maximum timestamp of the traceset. + * + */ +TimeInterval lttv_traceset_get_time_span_real(LttvTraceset *ts) +{ + TimeInterval time_span; + time_span.start_time =ltt_time_from_uint64(lttv_traceset_get_timestamp_first_event(ts)); + time_span.end_time = ltt_time_from_uint64(lttv_traceset_get_timestamp_end(ts)); + return time_span; +} + +/* + * lttv_traceset_get_time_span : return a TimeInterval representing the + * minimum timestamp dans le maximum timestamp of the traceset. + * + */ +TimeInterval lttv_traceset_get_time_span(LttvTraceset *ts) +{ + TimeInterval time_span; + time_span.start_time =ltt_time_from_uint64(lttv_traceset_get_timestamp_begin(ts)); + time_span.end_time = ltt_time_from_uint64(lttv_traceset_get_timestamp_end(ts)); + return time_span; +} + const char *lttv_traceset_get_name_from_event(LttvEvent *event) { return bt_ctf_event_name(event->bt_event); } + +guint64 lttv_traceset_position_get_timestamp(const LttvTracesetPosition *pos) +{ + guint64 timestamp = 0; + /*We save the current iterator,so we can reassign it after the seek*/ + LttvTracesetPosition previous_pos; + previous_pos.iter = pos->iter; + previous_pos.bt_pos = bt_iter_get_pos(bt_ctf_get_iter(pos->iter)); + /* Seek to the new desired position */ + lttv_traceset_seek_to_position(pos); + /*Read the event*/ + struct bt_ctf_event *event = bt_ctf_iter_read_event(pos->iter); + + if(event != NULL){ + timestamp = bt_ctf_get_timestamp_raw(event); + } + /* Reassign the previously saved position */ + lttv_traceset_seek_to_position(&previous_pos); + return timestamp; +} + +LttTime lttv_traceset_position_get_time(const LttvTracesetPosition *pos) +{ + return ltt_time_from_uint64(lttv_traceset_position_get_timestamp(pos)); +} + +int lttv_traceset_position_compare(const LttvTracesetPosition *pos1, const LttvTracesetPosition *pos2) +{ +#warning " TODO :Rename for lttv_traceset_position_equals && Must return COMPARAISON OF THE 2 POSITION && verify if it is the best way to compare position" + if(pos1 == NULL || pos2 == NULL){ + return -1; + } + + guint64 timeStampPos1,timeStampPos2; + guint cpuId1, cpuId2; + LttvEvent event1, event2; + int ret; + + timeStampPos1 = lttv_traceset_position_get_timestamp(pos1); + timeStampPos2 = lttv_traceset_position_get_timestamp(pos2); + + event1.bt_event = bt_ctf_iter_read_event(pos1->iter); + event2.bt_event = bt_ctf_iter_read_event(pos2->iter); + + if(event1.bt_event == NULL || event2.bt_event == NULL){ + return -1; + } + + cpuId1 = lttv_traceset_get_cpuid_from_event(&event1); + cpuId2 = lttv_traceset_get_cpuid_from_event(&event2); + + if(timeStampPos1 == timeStampPos2 && cpuId1 == cpuId2){ + return 0; + } + else{ + return 1; + } +}