Commit | Line | Data |
---|---|---|
55478183 PP |
1 | /* |
2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
55478183 PP |
23 | #include <babeltrace2/babeltrace.h> |
24 | ||
25 | #include "common/common.h" | |
26 | #include "common/assert.h" | |
27 | #include "compat/glib.h" | |
28 | ||
29 | #include "details.h" | |
30 | #include "write.h" | |
31 | #include "obj-lifetime-mgmt.h" | |
32 | ||
33 | static | |
34 | void trace_class_destruction_listener(const bt_trace_class *tc, void *data) | |
35 | { | |
36 | struct details_comp *details_comp = data; | |
37 | ||
38 | BT_ASSERT(details_comp); | |
39 | BT_ASSERT(details_comp->meta); | |
40 | ||
41 | /* Remove from hash table, which also destroys the value */ | |
42 | g_hash_table_remove(details_comp->meta, tc); | |
43 | } | |
44 | ||
45 | static | |
46 | struct details_trace_class_meta *borrow_trace_class_meta( | |
47 | struct details_write_ctx *ctx, const bt_trace_class *tc) | |
48 | { | |
49 | struct details_trace_class_meta *details_tc_meta; | |
50 | ||
51 | BT_ASSERT(ctx->details_comp->cfg.with_meta); | |
52 | BT_ASSERT(ctx->details_comp->meta); | |
53 | details_tc_meta = g_hash_table_lookup(ctx->details_comp->meta, tc); | |
54 | if (!details_tc_meta) { | |
55 | /* Not found: create one */ | |
56 | details_tc_meta = details_create_details_trace_class_meta(); | |
57 | if (!details_tc_meta) { | |
58 | goto error; | |
59 | } | |
60 | ||
61 | /* Register trace class destruction listener */ | |
62 | if (bt_trace_class_add_destruction_listener(tc, | |
63 | trace_class_destruction_listener, | |
64 | ctx->details_comp, | |
65 | &details_tc_meta->tc_destruction_listener_id)) { | |
66 | goto error; | |
67 | } | |
68 | ||
69 | /* Insert into hash table (becomes the owner) */ | |
70 | g_hash_table_insert(ctx->details_comp->meta, (void *) tc, | |
71 | details_tc_meta); | |
72 | } | |
73 | ||
74 | goto end; | |
75 | ||
76 | error: | |
77 | details_destroy_details_trace_class_meta(details_tc_meta); | |
78 | details_tc_meta = NULL; | |
79 | ||
80 | end: | |
81 | return details_tc_meta; | |
82 | } | |
83 | ||
84 | BT_HIDDEN | |
85 | bool details_need_to_write_meta_object(struct details_write_ctx *ctx, | |
86 | const bt_trace_class *tc, const void *obj) | |
87 | { | |
88 | bool need_to_write; | |
89 | struct details_trace_class_meta *details_tc_meta; | |
90 | ||
91 | if (!ctx->details_comp->cfg.with_meta) { | |
92 | need_to_write = false; | |
93 | goto end; | |
94 | } | |
95 | ||
96 | BT_ASSERT(ctx->details_comp->meta); | |
97 | details_tc_meta = g_hash_table_lookup(ctx->details_comp->meta, tc); | |
98 | BT_ASSERT(details_tc_meta); | |
99 | need_to_write = | |
100 | g_hash_table_lookup(details_tc_meta->objects, obj) == NULL; | |
101 | ||
102 | end: | |
103 | return need_to_write; | |
104 | } | |
105 | ||
106 | BT_HIDDEN | |
107 | void details_did_write_meta_object(struct details_write_ctx *ctx, | |
108 | const bt_trace_class *tc, const void *obj) | |
109 | { | |
110 | struct details_trace_class_meta *details_tc_meta; | |
111 | ||
112 | BT_ASSERT(ctx->details_comp->cfg.with_meta); | |
113 | details_tc_meta = borrow_trace_class_meta(ctx, tc); | |
114 | BT_ASSERT(details_tc_meta); | |
115 | g_hash_table_insert(details_tc_meta->objects, (gpointer) obj, | |
116 | GUINT_TO_POINTER(1)); | |
117 | } | |
118 | ||
119 | BT_HIDDEN | |
120 | bool details_need_to_write_trace_class(struct details_write_ctx *ctx, | |
121 | const bt_trace_class *tc) | |
122 | { | |
123 | struct details_trace_class_meta *details_tc_meta; | |
124 | bool need_to_write; | |
125 | ||
126 | if (!ctx->details_comp->cfg.with_meta) { | |
127 | need_to_write = false; | |
128 | goto end; | |
129 | } | |
130 | ||
131 | BT_ASSERT(ctx->details_comp->meta); | |
132 | details_tc_meta = g_hash_table_lookup(ctx->details_comp->meta, tc); | |
133 | need_to_write = details_tc_meta == NULL; | |
134 | ||
135 | end: | |
136 | return need_to_write; | |
137 | } | |
138 | ||
139 | BT_HIDDEN | |
140 | int details_did_write_trace_class(struct details_write_ctx *ctx, | |
141 | const bt_trace_class *tc) | |
142 | { | |
143 | int ret = 0; | |
144 | struct details_trace_class_meta *details_tc_meta; | |
145 | ||
146 | BT_ASSERT(ctx->details_comp->cfg.with_meta); | |
147 | ||
148 | /* borrow_trace_class_meta() creates an entry if none exists */ | |
149 | details_tc_meta = borrow_trace_class_meta(ctx, tc); | |
150 | if (!details_tc_meta) { | |
151 | ret = -1; | |
152 | } | |
153 | ||
154 | return ret; | |
155 | } | |
156 | ||
157 | static | |
158 | void trace_destruction_listener(const bt_trace *trace, void *data) | |
159 | { | |
160 | struct details_comp *details_comp = data; | |
161 | ||
162 | BT_ASSERT(details_comp); | |
163 | BT_ASSERT(details_comp->traces); | |
164 | ||
165 | /* Remove from hash table, which also destroys the value */ | |
166 | g_hash_table_remove(details_comp->traces, trace); | |
167 | } | |
168 | ||
169 | static | |
170 | struct details_trace *create_details_trace(uint64_t unique_id) | |
171 | { | |
172 | struct details_trace *details_trace = g_new0(struct details_trace, 1); | |
173 | ||
174 | if (!details_trace) { | |
175 | goto end; | |
176 | } | |
177 | ||
178 | details_trace->unique_id = unique_id; | |
179 | details_trace->trace_destruction_listener_id = UINT64_C(-1); | |
180 | ||
181 | end: | |
182 | return details_trace; | |
183 | } | |
184 | ||
185 | ||
186 | BT_HIDDEN | |
187 | int details_trace_unique_id(struct details_write_ctx *ctx, | |
188 | const bt_trace *trace, uint64_t *unique_id) | |
189 | { | |
190 | int ret = 0; | |
191 | struct details_trace *details_trace = NULL; | |
192 | ||
193 | BT_ASSERT(unique_id); | |
194 | BT_ASSERT(ctx->details_comp->traces); | |
195 | if (!bt_g_hash_table_contains(ctx->details_comp->traces, | |
196 | trace)) { | |
197 | /* Not found: create one */ | |
198 | *unique_id = ctx->details_comp->next_unique_trace_id; | |
199 | details_trace = create_details_trace(*unique_id); | |
200 | if (!details_trace) { | |
201 | goto error; | |
202 | } | |
203 | ||
204 | ctx->details_comp->next_unique_trace_id++; | |
205 | ||
206 | /* Register trace destruction listener if there's none */ | |
207 | if (bt_trace_add_destruction_listener(trace, | |
208 | trace_destruction_listener, | |
209 | ctx->details_comp, | |
210 | &details_trace->trace_destruction_listener_id)) { | |
211 | goto error; | |
212 | } | |
213 | ||
214 | BT_ASSERT(details_trace->trace_destruction_listener_id != | |
215 | UINT64_C(-1)); | |
216 | ||
217 | /* Move to hash table */ | |
218 | g_hash_table_insert(ctx->details_comp->traces, (gpointer) trace, | |
219 | details_trace); | |
220 | details_trace = NULL; | |
221 | } else { | |
222 | /* Found */ | |
223 | details_trace = g_hash_table_lookup( | |
224 | ctx->details_comp->traces, trace); | |
225 | *unique_id = details_trace->unique_id; | |
226 | details_trace = NULL; | |
227 | } | |
228 | ||
229 | goto end; | |
230 | ||
231 | error: | |
232 | ret = -1; | |
233 | ||
234 | end: | |
235 | if (details_trace) { | |
236 | g_free(details_trace); | |
237 | } | |
238 | ||
239 | return ret; | |
240 | } |