6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/context.h>
24 #include <babeltrace/context-internal.h>
25 #include <babeltrace/trace-handle.h>
26 #include <babeltrace/trace-handle-internal.h>
27 #include <babeltrace/trace-collection.h>
28 #include <babeltrace/format.h>
29 #include <babeltrace/babeltrace-internal.h>
35 #include <fcntl.h> /* For O_RDONLY */
39 struct bt_context
*bt_context_create(void)
41 struct bt_context
*ctx
;
43 ctx
= g_new0(struct bt_context
, 1);
45 /* Negative handle id are errors. */
46 ctx
->last_trace_handle_id
= 0;
48 /* Instanciate the trace handle container */
49 ctx
->trace_handles
= g_hash_table_new_full(g_direct_hash
,
51 (GDestroyNotify
) bt_trace_handle_destroy
);
53 ctx
->current_iterator
= NULL
;
54 ctx
->tc
= g_new0(struct trace_collection
, 1);
55 init_trace_collection(ctx
->tc
);
60 int bt_context_add_trace(struct bt_context
*ctx
, const char *path
,
61 const char *format_name
,
62 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
64 struct mmap_stream_list
*stream_list
,
67 struct trace_descriptor
*td
;
69 struct bt_trace_handle
*handle
;
72 if (!ctx
|| !format_name
|| (!path
&& !stream_list
))
75 fmt
= bt_lookup_format(g_quark_from_string(format_name
));
77 fprintf(stderr
, "[error] [Context] Format \"%s\" unknown.\n\n",
83 td
= fmt
->open_trace(path
, O_RDONLY
, packet_seek
, NULL
);
85 fprintf(stderr
, "[warning] [Context] Cannot open_trace of the format %s .\n\n",
91 td
= fmt
->open_mmap_trace(stream_list
, packet_seek
, metadata
);
93 fprintf(stderr
, "[error] [Context] Cannot open_trace of the format %s .\n\n",
100 /* Create an handle for the trace */
101 handle
= bt_trace_handle_create(ctx
);
103 fprintf(stderr
, "[error] [Context] Creating trace handle %s .\n\n",
108 handle
->format
= fmt
;
111 strncpy(handle
->path
, path
, PATH_MAX
);
112 handle
->path
[PATH_MAX
- 1] = '\0';
116 fmt
->set_handle(td
, handle
);
117 if (fmt
->set_context
)
118 fmt
->set_context(td
, ctx
);
120 /* Add new handle to container */
121 g_hash_table_insert(ctx
->trace_handles
,
122 (gpointer
) (unsigned long) handle
->id
,
124 ret
= trace_collection_add(ctx
->tc
, td
);
128 ret
= fmt
->convert_index_timestamp(td
);
132 handle
->real_timestamp_begin
= fmt
->timestamp_begin(td
, handle
, BT_CLOCK_REAL
);
133 handle
->real_timestamp_end
= fmt
->timestamp_end(td
, handle
, BT_CLOCK_REAL
);
134 handle
->cycles_timestamp_begin
= fmt
->timestamp_begin(td
, handle
, BT_CLOCK_CYCLES
);
135 handle
->cycles_timestamp_end
= fmt
->timestamp_end(td
, handle
, BT_CLOCK_CYCLES
);
142 int bt_context_remove_trace(struct bt_context
*ctx
, int handle_id
)
144 struct bt_trace_handle
*handle
;
149 handle
= g_hash_table_lookup(ctx
->trace_handles
,
150 (gpointer
) (unsigned long) handle_id
);
154 /* Remove from containers */
155 trace_collection_remove(ctx
->tc
, handle
->td
);
156 /* Close the trace */
157 handle
->format
->close_trace(handle
->td
);
159 /* Remove and free the handle */
160 g_hash_table_remove(ctx
->trace_handles
,
161 (gpointer
) (unsigned long) handle_id
);
166 void bt_context_destroy(struct bt_context
*ctx
)
169 finalize_trace_collection(ctx
->tc
);
171 /* Remote all traces. The g_hash_table_destroy will call
172 * bt_trace_handle_destroy on each elements.
174 g_hash_table_destroy(ctx
->trace_handles
);
176 /* ctx->tc should always be valid */
177 assert(ctx
->tc
!= NULL
);
182 void bt_context_get(struct bt_context
*ctx
)
188 void bt_context_put(struct bt_context
*ctx
)
192 if (ctx
->refcount
== 0)
193 bt_context_destroy(ctx
);
This page took 0.032246 seconds and 4 git commands to generate.