Fix trace-collection.h: No such file or directory that build code with libbabeltrace
[babeltrace.git] / lib / context.c
CommitLineData
b469d2dd
JD
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>
08c22d05 24#include <babeltrace/context-internal.h>
6cba487f
MD
25#include <babeltrace/trace-handle.h>
26#include <babeltrace/trace-handle-internal.h>
27#include <babeltrace/trace-collection.h>
28#include <babeltrace/format.h>
e1d01c39 29#include <babeltrace/babeltrace-internal.h>
b469d2dd 30#include <stdlib.h>
e1d01c39
MD
31#include <string.h>
32#include <assert.h>
7f89ddce 33#include <errno.h>
b469d2dd 34
6cba487f
MD
35#include <fts.h>
36#include <fcntl.h> /* For O_RDONLY */
37
6cba487f
MD
38#include <glib.h>
39
40struct bt_context *bt_context_create(void)
b469d2dd
JD
41{
42 struct bt_context *ctx;
43
6cba487f 44 ctx = g_new0(struct bt_context, 1);
b469d2dd 45 ctx->refcount = 1;
6cba487f 46 /* Negative handle id are errors. */
842c2b97 47 ctx->last_trace_handle_id = 0;
b469d2dd 48
6cba487f
MD
49 /* Instanciate the trace handle container */
50 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
51 g_direct_equal, NULL,
52 (GDestroyNotify) bt_trace_handle_destroy);
53
e003e871 54 ctx->current_iterator = NULL;
6cba487f
MD
55 ctx->tc = g_new0(struct trace_collection, 1);
56 init_trace_collection(ctx->tc);
57
b469d2dd 58 return ctx;
6cba487f 59}
b469d2dd 60
6cba487f 61int bt_context_add_trace(struct bt_context *ctx, const char *path,
613f532b 62 const char *format_name,
20d0dcf9 63 void (*packet_seek)(struct stream_pos *pos, size_t index,
0d4c669f
MD
64 int whence),
65 struct mmap_stream_list *stream_list,
66 FILE *metadata)
6cba487f
MD
67{
68 struct trace_descriptor *td;
69 struct format *fmt;
70 struct bt_trace_handle *handle;
1059a2bf 71 int ret;
6cba487f 72
7f89ddce
MD
73 if (!ctx || !format_name || (!path && !stream_list))
74 return -EINVAL;
75
282e1952
MD
76 fmt = bt_lookup_format(g_quark_from_string(format_name));
77 if (!fmt) {
78 fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
79 format_name);
80 ret = -1;
81 goto end;
82 }
0d4c669f
MD
83 if (path) {
84 td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
85 if (!td) {
ca718275 86 fprintf(stderr, "[warning] [Context] Cannot open_trace of the format %s .\n\n",
0d4c669f
MD
87 path);
88 ret = -1;
89 goto end;
90 }
91 } else {
92 td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
93 if (!td) {
94 fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n",
95 path);
96 ret = -1;
97 goto end;
98 }
6cba487f
MD
99 }
100
101 /* Create an handle for the trace */
102 handle = bt_trace_handle_create(ctx);
103 if (handle < 0) {
02dc4610 104 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
6cba487f 105 path);
1059a2bf
JD
106 ret = -1;
107 goto end;
6cba487f
MD
108 }
109 handle->format = fmt;
110 handle->td = td;
afe9cd4a
JD
111 if (path) {
112 strncpy(handle->path, path, PATH_MAX);
113 handle->path[PATH_MAX - 1] = '\0';
114 }
6cba487f 115
98a04903
JD
116 if (fmt->set_handle)
117 fmt->set_handle(td, handle);
118 if (fmt->set_context)
119 fmt->set_context(td, ctx);
120
6cba487f
MD
121 /* Add new handle to container */
122 g_hash_table_insert(ctx->trace_handles,
123 (gpointer) (unsigned long) handle->id,
124 handle);
1059a2bf 125 ret = trace_collection_add(ctx->tc, td);
03798a93
JD
126 if (ret != 0)
127 goto end;
128
129 ret = fmt->convert_index_timestamp(td);
130 if (ret < 0)
131 goto end;
132
133 handle->real_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL);
134 handle->real_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_REAL);
135 handle->cycles_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES);
136 handle->cycles_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES);
137
138 return handle->id;
1059a2bf
JD
139end:
140 return ret;
b469d2dd
JD
141}
142
7f89ddce 143int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
b469d2dd 144{
6cba487f
MD
145 struct bt_trace_handle *handle;
146
7f89ddce
MD
147 if (!ctx)
148 return -EINVAL;
149
6cba487f
MD
150 handle = g_hash_table_lookup(ctx->trace_handles,
151 (gpointer) (unsigned long) handle_id);
7f89ddce
MD
152 if (!handle)
153 return -ENOENT;
6cba487f
MD
154
155 /* Remove from containers */
156 trace_collection_remove(ctx->tc, handle->td);
6cba487f
MD
157 /* Close the trace */
158 handle->format->close_trace(handle->td);
159
188e72bf
JD
160 /* Remove and free the handle */
161 g_hash_table_remove(ctx->trace_handles,
162 (gpointer) (unsigned long) handle_id);
7f89ddce 163 return 0;
6cba487f
MD
164}
165
166static
167void bt_context_destroy(struct bt_context *ctx)
168{
7f89ddce 169 assert(ctx);
6cba487f
MD
170 finalize_trace_collection(ctx->tc);
171
172 /* Remote all traces. The g_hash_table_destroy will call
173 * bt_trace_handle_destroy on each elements.
174 */
175 g_hash_table_destroy(ctx->trace_handles);
176
177 /* ctx->tc should always be valid */
178 assert(ctx->tc != NULL);
179 g_free(ctx->tc);
180 g_free(ctx);
b469d2dd
JD
181}
182
6cba487f 183void bt_context_get(struct bt_context *ctx)
b469d2dd 184{
7f89ddce 185 assert(ctx);
6cba487f
MD
186 ctx->refcount++;
187}
b469d2dd 188
6cba487f
MD
189void bt_context_put(struct bt_context *ctx)
190{
7f89ddce 191 assert(ctx);
b469d2dd
JD
192 ctx->refcount--;
193 if (ctx->refcount == 0)
6cba487f 194 bt_context_destroy(ctx);
b469d2dd 195}
This page took 0.04784 seconds and 4 git commands to generate.