From 98a0490342a1b8f8841ef52aaaef2cef11782ff1 Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Tue, 27 Mar 2012 09:12:46 -0400 Subject: [PATCH] API Fix : give access to trace_handle and context When reading multiple traces, we need to know to which context or trace_handle an event belongs to. Setting the context and trace_handle is format agnostic, but reading it is not, so the reading part is only implemented for CTF. Signed-off-by: Julien Desfossez Signed-off-by: Mathieu Desnoyers --- formats/ctf/ctf.c | 30 +++++++++++++++++++++ formats/ctf/events.c | 40 ++++++++++++++++++++++++++++ include/babeltrace/context.h | 8 ++++++ include/babeltrace/ctf-ir/metadata.h | 3 +++ include/babeltrace/ctf/metadata.h | 2 ++ include/babeltrace/format.h | 6 +++++ include/babeltrace/trace-handle.h | 8 ++++++ lib/context.c | 5 ++++ 8 files changed, 102 insertions(+) diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 8608b5a1..2965f496 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -76,6 +78,12 @@ struct trace_descriptor *ctf_open_mmap_trace( void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp); +static +void ctf_set_context(struct trace_descriptor *descriptor, + struct bt_context *ctx); +static +void ctf_set_handle(struct trace_descriptor *descriptor, + struct bt_trace_handle *handle); static void ctf_close_trace(struct trace_descriptor *descriptor); @@ -109,6 +117,8 @@ struct format ctf_format = { .open_trace = ctf_open_trace, .open_mmap_trace = ctf_open_mmap_trace, .close_trace = ctf_close_trace, + .set_context = ctf_set_context, + .set_handle = ctf_set_handle, }; /* @@ -1610,6 +1620,26 @@ void ctf_close_trace(struct trace_descriptor *tdp) g_free(td); } +static +void ctf_set_context(struct trace_descriptor *descriptor, + struct bt_context *ctx) +{ + struct ctf_trace *td = container_of(descriptor, struct ctf_trace, + parent); + + td->ctx = ctx; +} + +static +void ctf_set_handle(struct trace_descriptor *descriptor, + struct bt_trace_handle *handle) +{ + struct ctf_trace *td = container_of(descriptor, struct ctf_trace, + parent); + + td->handle = handle; +} + void __attribute__((constructor)) ctf_init(void) { int ret; diff --git a/formats/ctf/events.c b/formats/ctf/events.c index ebc4f1b0..be90d841 100644 --- a/formats/ctf/events.c +++ b/formats/ctf/events.c @@ -251,6 +251,46 @@ error: return -1; } +struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event) +{ + struct bt_context *ret = NULL; + struct ctf_file_stream *cfs; + struct ctf_stream *stream; + struct ctf_stream_class *stream_class; + struct ctf_trace *trace; + + cfs = container_of(event->stream, struct ctf_file_stream, + parent); + stream = &cfs->parent; + stream_class = stream->stream_class; + trace = stream_class->trace; + + if (trace->ctx) + ret = trace->ctx; + + return ret; +} + +int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event) +{ + int ret = -1; + struct ctf_file_stream *cfs; + struct ctf_stream *stream; + struct ctf_stream_class *stream_class; + struct ctf_trace *trace; + + cfs = container_of(event->stream, struct ctf_file_stream, + parent); + stream = &cfs->parent; + stream_class = stream->stream_class; + trace = stream_class->trace; + + if (trace->handle) + ret = trace->handle->id; + + return ret; +} + uint64_t bt_ctf_get_timestamp_raw(const struct bt_ctf_event *event) { if (event && event->stream->has_timestamp) diff --git a/include/babeltrace/context.h b/include/babeltrace/context.h index 591c9cad..4a85ff99 100644 --- a/include/babeltrace/context.h +++ b/include/babeltrace/context.h @@ -29,6 +29,7 @@ /* struct bt_context is opaque to the user */ struct bt_context; struct stream_pos; +struct bt_ctf_event; /* * bt_context_create : create a Babeltrace context @@ -96,4 +97,11 @@ void bt_context_remove_trace(struct bt_context *ctx, int trace_id); void bt_context_get(struct bt_context *ctx); void bt_context_put(struct bt_context *ctx); +/* + * bt_ctf_get_context : get the context associated with an event + * + * Returns NULL on error + */ +struct bt_context *bt_ctf_event_get_context(const struct bt_ctf_event *event); + #endif /* _BABELTRACE_CONTEXT_H */ diff --git a/include/babeltrace/ctf-ir/metadata.h b/include/babeltrace/ctf-ir/metadata.h index a36dc89a..024b50f1 100644 --- a/include/babeltrace/ctf-ir/metadata.h +++ b/include/babeltrace/ctf-ir/metadata.h @@ -166,6 +166,9 @@ struct ctf_trace { /* Heap of streams, ordered to always get the lowest timestam */ struct ptr_heap *stream_heap; char path[PATH_MAX]; + + struct bt_context *ctx; + struct bt_trace_handle *handle; }; #define CTF_STREAM_SET_FIELD(ctf_stream, field) \ diff --git a/include/babeltrace/ctf/metadata.h b/include/babeltrace/ctf/metadata.h index de8a1a8e..82b01708 100644 --- a/include/babeltrace/ctf/metadata.h +++ b/include/babeltrace/ctf/metadata.h @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/include/babeltrace/format.h b/include/babeltrace/format.h index 8eae42c0..b67dd5dc 100644 --- a/include/babeltrace/format.h +++ b/include/babeltrace/format.h @@ -29,6 +29,8 @@ typedef int bt_intern_str; /* forward declaration */ struct stream_pos; +struct bt_context; +struct bt_trace_handle; /* Parent trace descriptor */ struct trace_descriptor { @@ -56,6 +58,10 @@ struct format { size_t index, int whence), FILE *metadata_fp); void (*close_trace)(struct trace_descriptor *descriptor); + void (*set_context)(struct trace_descriptor *descriptor, + struct bt_context *ctx); + void (*set_handle)(struct trace_descriptor *descriptor, + struct bt_trace_handle *handle); }; extern struct format *bt_lookup_format(bt_intern_str qname); diff --git a/include/babeltrace/trace-handle.h b/include/babeltrace/trace-handle.h index d188cb70..31877ee1 100644 --- a/include/babeltrace/trace-handle.h +++ b/include/babeltrace/trace-handle.h @@ -31,6 +31,7 @@ * It is a unique identifier representing a trace file. */ struct bt_trace_handle; +struct bt_ctf_event; /* * bt_trace_handle_get_path : returns the path of a trace_handle. @@ -49,4 +50,11 @@ uint64_t bt_trace_handle_get_timestamp_begin(struct bt_context *ctx, int handle_ */ uint64_t bt_trace_handle_get_timestamp_end(struct bt_context *ctx, int handle_id); +/* + * bt_ctf_event_get_handle_id : get the handle id associated with an event + * + * Returns -1 on error + */ +int bt_ctf_event_get_handle_id(const struct bt_ctf_event *event); + #endif /* _BABELTRACE_TRACE_HANDLE_H */ diff --git a/lib/context.c b/lib/context.c index e9a35c1d..dbab3375 100644 --- a/lib/context.c +++ b/lib/context.c @@ -107,6 +107,11 @@ int bt_context_add_trace(struct bt_context *ctx, const char *path, strncpy(handle->path, path, PATH_MAX); handle->path[PATH_MAX - 1] = '\0'; + if (fmt->set_handle) + fmt->set_handle(td, handle); + if (fmt->set_context) + fmt->set_context(td, ctx); + /* Add new handle to container */ g_hash_table_insert(ctx->trace_handles, (gpointer) (unsigned long) handle->id, -- 2.34.1