Commit | Line | Data |
---|---|---|
d0b96690 DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | #define _GNU_SOURCE | |
18 | #include <assert.h> | |
19 | ||
20 | #include <common/common.h> | |
21 | #include "ust-registry.h" | |
22 | ||
23 | /* | |
24 | * Hash table match function for event in the registry. | |
25 | */ | |
26 | static int ht_match_event(struct cds_lfht_node *node, const void *_key) | |
27 | { | |
28 | struct ust_registry_event *event; | |
29 | const struct ust_registry_event *key; | |
30 | ||
31 | assert(node); | |
32 | assert(_key); | |
33 | ||
34 | event = caa_container_of(node, struct ust_registry_event, node.node); | |
35 | assert(event); | |
36 | key = _key; | |
37 | ||
38 | /* It has to be a perfect match. */ | |
39 | if (strncmp(event->name, key->name, sizeof(event->name)) != 0) { | |
40 | goto no_match; | |
41 | } | |
42 | ||
43 | /* It has to be a perfect match. */ | |
44 | if (strncmp(event->signature, key->signature, | |
45 | strlen(event->signature) != 0)) { | |
46 | goto no_match; | |
47 | } | |
48 | ||
49 | /* Match */ | |
50 | return 1; | |
51 | ||
52 | no_match: | |
53 | return 0; | |
54 | } | |
55 | ||
56 | /* | |
57 | * Allocate event and initialize it. This does NOT set a valid event id from a | |
58 | * registry. | |
59 | */ | |
60 | static struct ust_registry_event *alloc_event(int session_objd, | |
61 | int channel_objd, char *name, char *sig, size_t nr_fields, | |
62 | struct ustctl_field *fields, int loglevel, char *model_emf_uri) | |
63 | { | |
64 | struct ust_registry_event *event = NULL; | |
65 | ||
66 | event = zmalloc(sizeof(*event)); | |
67 | if (!event) { | |
68 | PERROR("zmalloc ust registry event"); | |
69 | goto error; | |
70 | } | |
71 | ||
72 | event->session_objd = session_objd; | |
73 | event->channel_objd = channel_objd; | |
74 | /* Allocated by ustctl. */ | |
75 | event->signature = sig; | |
76 | event->nr_fields = nr_fields; | |
77 | event->fields = fields; | |
78 | event->loglevel = loglevel; | |
79 | event->model_emf_uri = model_emf_uri; | |
80 | if (name) { | |
81 | /* Copy event name and force NULL byte. */ | |
82 | strncpy(event->name, name, sizeof(event->name)); | |
83 | event->name[sizeof(event->name) - 1] = '\0'; | |
84 | } | |
85 | lttng_ht_node_init_str(&event->node, event->name); | |
86 | ||
87 | error: | |
88 | return event; | |
89 | } | |
90 | ||
91 | /* | |
92 | * Free event data structure. This does NOT delete it from any hash table. It's | |
93 | * safe to pass a NULL pointer. This shoudl be called inside a call RCU if the | |
94 | * event is previously deleted from a rcu hash table. | |
95 | */ | |
96 | static void destroy_event(struct ust_registry_event *event) | |
97 | { | |
98 | if (!event) { | |
99 | return; | |
100 | } | |
101 | ||
102 | free(event->fields); | |
103 | free(event->model_emf_uri); | |
104 | free(event->signature); | |
105 | free(event); | |
106 | } | |
107 | ||
108 | /* | |
109 | * Destroy event function call of the call RCU. | |
110 | */ | |
111 | static void destroy_event_rcu(struct rcu_head *head) | |
112 | { | |
113 | struct lttng_ht_node_str *node = | |
114 | caa_container_of(head, struct lttng_ht_node_str, head); | |
115 | struct ust_registry_event *event = | |
116 | caa_container_of(node, struct ust_registry_event, node); | |
117 | ||
118 | destroy_event(event); | |
119 | } | |
120 | ||
121 | /* | |
122 | * Find an event using the name and signature in the given registry. RCU read | |
123 | * side lock MUST be acquired before calling this function and as long as the | |
124 | * event reference is kept by the caller. | |
125 | * | |
126 | * On success, the event pointer is returned else NULL. | |
127 | */ | |
128 | struct ust_registry_event *ust_registry_find_event( | |
129 | struct ust_registry_channel *chan, char *name, char *sig) | |
130 | { | |
131 | struct lttng_ht_node_str *node; | |
132 | struct lttng_ht_iter iter; | |
133 | struct ust_registry_event *event = NULL; | |
134 | struct ust_registry_event key; | |
135 | ||
136 | assert(chan); | |
137 | assert(name); | |
138 | assert(sig); | |
139 | ||
140 | /* Setup key for the match function. */ | |
141 | strncpy(key.name, name, sizeof(key.name)); | |
142 | key.name[sizeof(key.name) - 1] = '\0'; | |
143 | key.signature = sig; | |
144 | ||
145 | cds_lfht_lookup(chan->ht->ht, chan->ht->hash_fct(name, lttng_ht_seed), | |
146 | chan->ht->match_fct, &key, &iter.iter); | |
147 | node = lttng_ht_iter_get_node_str(&iter); | |
148 | if (!node) { | |
149 | goto end; | |
150 | } | |
151 | event = caa_container_of(node, struct ust_registry_event, node); | |
152 | ||
153 | end: | |
154 | return event; | |
155 | } | |
156 | ||
157 | /* | |
158 | * Create a ust_registry_event from the given parameters and add it to the | |
159 | * registry hash table. If event_id is valid, it is set with the newly created | |
160 | * event id. | |
161 | * | |
162 | * On success, return 0 else a negative value. The created event MUST be unique | |
163 | * so on duplicate entry -EINVAL is returned. On error, event_id is untouched. | |
164 | * | |
165 | * Should be called with session registry mutex held. | |
166 | */ | |
167 | int ust_registry_create_event(struct ust_registry_session *session, | |
168 | struct ust_registry_channel *chan, | |
169 | int session_objd, int channel_objd, char *name, char *sig, | |
170 | size_t nr_fields, struct ustctl_field *fields, int loglevel, | |
171 | char *model_emf_uri, uint32_t *event_id) | |
172 | { | |
173 | int ret; | |
174 | struct cds_lfht_node *nptr; | |
175 | struct ust_registry_event *event = NULL; | |
176 | ||
177 | assert(session); | |
178 | assert(chan); | |
179 | assert(name); | |
180 | assert(sig); | |
181 | ||
182 | /* | |
183 | * This should not happen but since it comes from the UST tracer, an | |
184 | * external party, don't assert and simply validate values. | |
185 | */ | |
186 | if (session_objd < 0 || channel_objd < 0) { | |
187 | ret = -EINVAL; | |
188 | goto error; | |
189 | } | |
190 | ||
191 | /* Check if we've reached the maximum possible id. */ | |
192 | if (ust_registry_is_max_id(chan->used_event_id)) { | |
193 | ret = -ENOENT; | |
194 | goto error; | |
195 | } | |
196 | ||
197 | event = alloc_event(session_objd, channel_objd, name, sig, nr_fields, | |
198 | fields, loglevel, model_emf_uri); | |
199 | if (!event) { | |
200 | ret = -ENOMEM; | |
201 | goto error; | |
202 | } | |
203 | ||
204 | event->id = ust_registry_get_next_event_id(chan); | |
205 | ||
206 | DBG3("UST registry creating event with event: %s, sig: %s, id: %u, " | |
207 | "chan_objd: %u, sess_objd: %u", event->name, event->signature, | |
208 | event->id, event->channel_objd, event->session_objd); | |
209 | ||
210 | rcu_read_lock(); | |
211 | /* | |
212 | * This is an add unique with a custom match function for event. The node | |
213 | * are matched using the event name and signature. | |
214 | */ | |
215 | nptr = cds_lfht_add_unique(chan->ht->ht, chan->ht->hash_fct(event->node.key, | |
216 | lttng_ht_seed), chan->ht->match_fct, event, &event->node.node); | |
217 | if (nptr != &event->node.node) { | |
218 | ERR("UST registry create event add unique failed for event: %s, " | |
219 | "sig: %s, id: %u, chan_objd: %u, sess_objd: %u", event->name, | |
220 | event->signature, event->id, event->channel_objd, | |
221 | event->session_objd); | |
222 | ret = -EINVAL; | |
223 | goto error_unlock; | |
224 | } | |
225 | ||
226 | /* Set event id if user wants it. */ | |
227 | if (event_id) { | |
228 | *event_id = event->id; | |
229 | } | |
230 | rcu_read_unlock(); | |
231 | ||
232 | /* Append to metadata */ | |
233 | ret = ust_metadata_event_statedump(session, chan, event); | |
234 | if (ret) { | |
235 | ERR("Error appending event metadata (errno = %d)", ret); | |
236 | return ret; | |
237 | } | |
238 | ||
239 | return 0; | |
240 | ||
241 | error_unlock: | |
242 | rcu_read_unlock(); | |
243 | error: | |
244 | destroy_event(event); | |
245 | return ret; | |
246 | } | |
247 | ||
248 | /* | |
249 | * For a given event in a registry, delete the entry and destroy the event. | |
250 | * This MUST be called within a RCU read side lock section. | |
251 | */ | |
252 | void ust_registry_destroy_event(struct ust_registry_channel *chan, | |
253 | struct ust_registry_event *event) | |
254 | { | |
255 | int ret; | |
256 | struct lttng_ht_iter iter; | |
257 | ||
258 | assert(chan); | |
259 | assert(event); | |
260 | ||
261 | /* Delete the node first. */ | |
262 | iter.iter.node = &event->node.node; | |
263 | ret = lttng_ht_del(chan->ht, &iter); | |
264 | assert(!ret); | |
265 | ||
266 | call_rcu(&event->node.head, destroy_event_rcu); | |
267 | ||
268 | return; | |
269 | } | |
270 | ||
271 | /* | |
272 | * Initialize registry with default values. | |
273 | */ | |
274 | void ust_registry_channel_init(struct ust_registry_session *session, | |
275 | struct ust_registry_channel *chan) | |
276 | { | |
277 | assert(chan); | |
278 | ||
279 | memset(chan, 0, sizeof(struct ust_registry_channel)); | |
280 | ||
281 | chan->ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
282 | assert(chan->ht); | |
283 | ||
284 | /* Set custom match function. */ | |
285 | chan->ht->match_fct = ht_match_event; | |
286 | } | |
287 | ||
288 | /* | |
289 | * Destroy every element of the registry and free the memory. This does NOT | |
290 | * free the registry pointer since it might not have been allocated before so | |
291 | * it's the caller responsability. | |
292 | * | |
293 | * This MUST be called within a RCU read side lock section. | |
294 | */ | |
295 | void ust_registry_channel_destroy(struct ust_registry_session *session, | |
296 | struct ust_registry_channel *chan) | |
297 | { | |
298 | struct lttng_ht_iter iter; | |
299 | struct ust_registry_event *event; | |
300 | ||
301 | assert(chan); | |
302 | ||
303 | /* Destroy all event associated with this registry. */ | |
304 | cds_lfht_for_each_entry(chan->ht->ht, &iter.iter, event, node.node) { | |
305 | /* Delete the node from the ht and free it. */ | |
306 | ust_registry_destroy_event(chan, event); | |
307 | } | |
308 | lttng_ht_destroy(chan->ht); | |
309 | } | |
310 | ||
311 | /* | |
312 | * Initialize registry with default values. | |
313 | */ | |
314 | int ust_registry_session_init(struct ust_registry_session *session, | |
315 | struct ust_app *app, | |
316 | uint32_t bits_per_long, | |
317 | uint32_t uint8_t_alignment, | |
318 | uint32_t uint16_t_alignment, | |
319 | uint32_t uint32_t_alignment, | |
320 | uint32_t uint64_t_alignment, | |
321 | uint32_t long_alignment, | |
322 | int byte_order) | |
323 | { | |
324 | int ret; | |
325 | ||
326 | assert(session); | |
327 | ||
328 | memset(session, 0, sizeof(struct ust_registry_session)); | |
329 | ||
330 | pthread_mutex_init(&session->lock, NULL); | |
331 | session->bits_per_long = bits_per_long; | |
332 | session->uint8_t_alignment = uint8_t_alignment; | |
333 | session->uint16_t_alignment = uint16_t_alignment; | |
334 | session->uint32_t_alignment = uint32_t_alignment; | |
335 | session->uint64_t_alignment = uint64_t_alignment; | |
336 | session->long_alignment = long_alignment; | |
337 | session->byte_order = byte_order; | |
338 | ||
339 | ret = lttng_uuid_generate(session->uuid); | |
340 | if (ret) { | |
341 | ERR("Failed to generate UST uuid (errno = %d)", ret); | |
342 | goto error; | |
343 | } | |
344 | ||
345 | pthread_mutex_lock(&session->lock); | |
346 | ret = ust_metadata_session_statedump(session, app); | |
347 | pthread_mutex_unlock(&session->lock); | |
348 | if (ret) { | |
349 | ERR("Failed to generate session metadata (errno = %d)", ret); | |
350 | goto error; | |
351 | } | |
352 | ||
353 | return 0; | |
354 | ||
355 | error: | |
356 | return -1; | |
357 | } | |
358 | ||
359 | /* | |
360 | * Destroy session registry. This does NOT free the given pointer since it | |
361 | * might get passed as a reference. The registry lock should NOT be acquired. | |
362 | */ | |
363 | void ust_registry_session_destroy(struct ust_registry_session *reg) | |
364 | { | |
365 | int ret; | |
366 | ||
367 | /* On error, EBUSY can be returned if lock. Code flow error. */ | |
368 | ret = pthread_mutex_destroy(®->lock); | |
369 | assert(!ret); | |
370 | ||
371 | free(reg->metadata); | |
372 | } |