Get rid of clock-raw and use real clock
[babeltrace.git] / lib / context.c
1 /*
2 * context.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
10 *
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:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 */
21
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>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include <fts.h>
35 #include <fcntl.h> /* For O_RDONLY */
36
37 #include <glib.h>
38
39 struct bt_context *bt_context_create(void)
40 {
41 struct bt_context *ctx;
42
43 ctx = g_new0(struct bt_context, 1);
44 ctx->refcount = 1;
45 /* Negative handle id are errors. */
46 ctx->last_trace_handle_id = 0;
47
48 /* Instanciate the trace handle container */
49 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
50 g_direct_equal, NULL,
51 (GDestroyNotify) bt_trace_handle_destroy);
52
53 ctx->current_iterator = NULL;
54 ctx->tc = g_new0(struct trace_collection, 1);
55 init_trace_collection(ctx->tc);
56
57 return ctx;
58 }
59
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,
63 int whence),
64 struct mmap_stream_list *stream_list,
65 FILE *metadata)
66 {
67 struct trace_descriptor *td;
68 struct format *fmt;
69 struct bt_trace_handle *handle;
70 int ret;
71
72 fmt = bt_lookup_format(g_quark_from_string(format_name));
73 if (!fmt) {
74 fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
75 format_name);
76 ret = -1;
77 goto end;
78 }
79 if (path) {
80 td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
81 if (!td) {
82 fprintf(stderr, "[warning] [Context] Cannot open_trace of the format %s .\n\n",
83 path);
84 ret = -1;
85 goto end;
86 }
87 } else {
88 td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
89 if (!td) {
90 fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n",
91 path);
92 ret = -1;
93 goto end;
94 }
95 }
96
97 /* Create an handle for the trace */
98 handle = bt_trace_handle_create(ctx);
99 if (handle < 0) {
100 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
101 path);
102 ret = -1;
103 goto end;
104 }
105 handle->format = fmt;
106 handle->td = td;
107 strncpy(handle->path, path, PATH_MAX);
108 handle->path[PATH_MAX - 1] = '\0';
109
110 if (fmt->set_handle)
111 fmt->set_handle(td, handle);
112 if (fmt->set_context)
113 fmt->set_context(td, ctx);
114
115 /* Add new handle to container */
116 g_hash_table_insert(ctx->trace_handles,
117 (gpointer) (unsigned long) handle->id,
118 handle);
119 ret = trace_collection_add(ctx->tc, td);
120 if (ret != 0)
121 goto end;
122
123 ret = fmt->convert_index_timestamp(td);
124 if (ret < 0)
125 goto end;
126
127 handle->real_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL);
128 handle->real_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_REAL);
129 handle->cycles_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES);
130 handle->cycles_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES);
131
132 return handle->id;
133 end:
134 return ret;
135 }
136
137 void bt_context_remove_trace(struct bt_context *ctx, int handle_id)
138 {
139 struct bt_trace_handle *handle;
140
141 handle = g_hash_table_lookup(ctx->trace_handles,
142 (gpointer) (unsigned long) handle_id);
143 assert(handle != NULL);
144
145 /* Remove from containers */
146 trace_collection_remove(ctx->tc, handle->td);
147 /* Close the trace */
148 handle->format->close_trace(handle->td);
149
150 /* Remove and free the handle */
151 g_hash_table_remove(ctx->trace_handles,
152 (gpointer) (unsigned long) handle_id);
153
154 }
155
156 static
157 void bt_context_destroy(struct bt_context *ctx)
158 {
159 finalize_trace_collection(ctx->tc);
160
161 /* Remote all traces. The g_hash_table_destroy will call
162 * bt_trace_handle_destroy on each elements.
163 */
164 g_hash_table_destroy(ctx->trace_handles);
165
166 /* ctx->tc should always be valid */
167 assert(ctx->tc != NULL);
168 g_free(ctx->tc);
169 g_free(ctx);
170 }
171
172 void bt_context_get(struct bt_context *ctx)
173 {
174 ctx->refcount++;
175 }
176
177 void bt_context_put(struct bt_context *ctx)
178 {
179 ctx->refcount--;
180 if (ctx->refcount == 0)
181 bt_context_destroy(ctx);
182 }
This page took 0.032782 seconds and 4 git commands to generate.