SoW-2020-0003: Trace Hit Counters
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry.h
CommitLineData
d0b96690 1/*
ab5be9fa 2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
d0b96690 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
d0b96690 5 *
d0b96690
DG
6 */
7
8#ifndef LTTNG_UST_REGISTRY_H
9#define LTTNG_UST_REGISTRY_H
10
11#include <pthread.h>
12#include <stdint.h>
d0b96690
DG
13
14#include <common/hashtable/hashtable.h>
c70636a7 15#include <common/uuid.h>
d0b96690 16
75018ab6 17#include "lttng-ust-ctl.h"
7972aab2 18
d0b96690
DG
19#define CTF_SPEC_MAJOR 1
20#define CTF_SPEC_MINOR 8
21
22struct ust_app;
23
24struct ust_registry_session {
25 /*
dc2bbdae
MD
26 * With multiple writers and readers, use this lock to access
27 * the registry. Can nest within the ust app session lock.
28 * Also acts as a registry serialization lock. Used by registry
29 * readers to serialize the registry information sent from the
30 * sessiond to the consumerd.
31 * The consumer socket lock nests within this lock.
d0b96690
DG
32 */
33 pthread_mutex_t lock;
34 /* Next channel ID available for a newly registered channel. */
35 uint32_t next_channel_id;
36 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
37 uint32_t used_channel_id;
ebdb334b
JR
38 /* Next map ID available for a newly registered map. */
39 uint32_t next_map_id;
40 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
41 uint32_t used_map_id;
10b56aef
MD
42 /* Next enumeration ID available. */
43 uint64_t next_enum_id;
d0b96690 44 /* Universal unique identifier used by the tracer. */
c70636a7 45 unsigned char uuid[LTTNG_UUID_LEN];
d0b96690
DG
46
47 /* session ABI description */
48
49 /* Size of long, in bits */
50 unsigned int bits_per_long;
51 /* Alignment, in bits */
52 unsigned int uint8_t_alignment,
53 uint16_t_alignment,
54 uint32_t_alignment,
55 uint64_t_alignment,
56 long_alignment;
57 /* endianness */
58 int byte_order; /* BIG_ENDIAN or LITTLE_ENDIAN */
59
60 /* Generated metadata. */
61 char *metadata; /* NOT null-terminated ! Use memcpy. */
62 size_t metadata_len, metadata_alloc_len;
d88aee68
DG
63 /* Length of bytes sent to the consumer. */
64 size_t metadata_len_sent;
93ec662e
JD
65 /* Current version of the metadata. */
66 uint64_t metadata_version;
d7ba1388 67
9aed4377
JG
68 /*
69 * Those fields are only used when a session is created with
70 * the --shm-path option. In this case, the metadata is output
71 * twice: once to the consumer, as ususal, but a second time
72 * also in the shm path directly. This is done so that a copy
73 * of the metadata that is as fresh as possible is available
74 * on the event of a crash.
75 *
76 * root_shm_path contains the shm-path provided by the user, along with
77 * the session's name and timestamp:
78 * e.g. /tmp/my_shm/my_session-20180612-135822
79 *
80 * shm_path contains the full path of the memory buffers:
81 * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit
82 *
83 * metadata_path contains the full path to the metadata file that
84 * is kept for the "crash buffer" extraction:
85 * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit/metadata
86 *
87 * Note that this is not the trace's final metadata file. It is
88 * only meant to be used to read the contents of the ring buffers
89 * in the event of a crash.
90 *
91 * metadata_fd is a file descriptor that points to the file at
92 * 'metadata_path'.
93 */
3d071855 94 char root_shm_path[PATH_MAX];
d7ba1388
MD
95 char shm_path[PATH_MAX];
96 char metadata_path[PATH_MAX];
97 int metadata_fd; /* file-backed metadata FD */
98
45893984 99 /*
dc2bbdae
MD
100 * Hash table containing channels sent by the UST tracer. MUST
101 * be accessed with a RCU read side lock acquired.
45893984
DG
102 */
103 struct lttng_ht *channels;
ebdb334b
JR
104
105 /*
106 * Hash table containing maps sent by the UST tracer. MUST
107 * be accessed with a RCU read side lock acquired.
108 */
109 struct lttng_ht *maps;
dc2bbdae
MD
110 /*
111 * Unique key to identify the metadata on the consumer side.
112 */
7972aab2
DG
113 uint64_t metadata_key;
114 /*
115 * Indicates if the metadata is closed on the consumer side. This is to
116 * avoid double close of metadata when an application unregisters AND
117 * deletes its sessions.
118 */
119 unsigned int metadata_closed;
4628484a
MD
120
121 /* User and group owning the session. */
122 uid_t uid;
123 gid_t gid;
10b56aef
MD
124
125 /* Enumerations table. */
126 struct lttng_ht *enums;
7062f070
JD
127
128 /*
129 * Copy of the tracer version when the first app is registered.
130 * It is used if we need to regenerate the metadata.
131 */
132 uint32_t major;
133 uint32_t minor;
8de88061
JR
134
135 /* The id of the parent session */
136 uint64_t tracing_id;
137 uid_t tracing_uid;
d0b96690
DG
138};
139
140struct ust_registry_channel {
45893984 141 uint64_t key;
e9404c27 142 uint64_t consumer_key;
d0b96690
DG
143 /* Id set when replying to a register channel. */
144 uint32_t chan_id;
145 enum ustctl_channel_header header_type;
146
7972aab2
DG
147 /*
148 * Flag for this channel if the metadata was dumped once during
149 * registration. 0 means no, 1 yes.
150 */
151 unsigned int metadata_dumped;
152 /* Indicates if this channel registry has already been registered. */
153 unsigned int register_done;
154
d0b96690
DG
155 /*
156 * Hash table containing events sent by the UST tracer. MUST be accessed
157 * with a RCU read side lock acquired.
158 */
159 struct lttng_ht *ht;
160 /* Next event ID available for a newly registered event. */
161 uint32_t next_event_id;
162 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
163 uint32_t used_event_id;
164 /*
165 * Context fields of the registry. Context are per channel. Allocated by a
166 * register channel notification from the UST tracer.
167 */
168 size_t nr_ctx_fields;
169 struct ustctl_field *ctx_fields;
45893984 170 struct lttng_ht_node_u64 node;
36b588ed
MD
171 /* For delayed reclaim */
172 struct rcu_head rcu_head;
d0b96690
DG
173};
174
ebdb334b
JR
175struct ust_registry_map_key_ht_entry {
176 struct lttng_map_key *key;
177 struct lttng_ht_node_u64 node;
178};
179
180struct ust_registry_map_index_ht_entry {
181 uint64_t index;
182 char *formated_key;
183 struct lttng_ht_node_str node;
184};
185
186struct ust_registry_map {
187 uint64_t key;
188 /* Id set when replying to a register map. */
189 uint32_t map_id;
190
191 /* Indicates if this map registry has already been registered. */
192 unsigned int register_done;
193
194 /*
195 * Hash table containing events sent by the UST tracer. MUST be accessed
196 * with a RCU read side lock acquired.
197 */
198 struct lttng_ht *events_ht;
199 /* Next event ID available for a newly registered event. */
200 uint32_t next_event_id;
201 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
202 uint32_t used_event_id;
203
204 /* tracer_token -> ust_registry_map_key_ht_entry */
205 struct lttng_ht *tracer_token_to_map_key_ht;
206 /* format key -> ust_registry_map_index_ht_entry */
207 struct lttng_ht *key_string_to_bucket_index_ht;
208
209 struct lttng_ht_node_u64 node;
210 /* For delayed reclaim */
211 struct rcu_head rcu_head;
212};
213
d0b96690
DG
214/*
215 * Event registered from a UST tracer sent to the session daemon. This is
216 * indexed and matched by <event_name/signature>.
217 */
218struct ust_registry_event {
219 int id;
220 /* Both objd are set by the tracer. */
221 int session_objd;
ebdb334b 222 int container_objd;
d0b96690
DG
223 /* Name of the event returned by the tracer. */
224 char name[LTTNG_UST_SYM_NAME_LEN];
225 char *signature;
2106efa0 226 int loglevel_value;
d0b96690
DG
227 size_t nr_fields;
228 struct ustctl_field *fields;
229 char *model_emf_uri;
7972aab2
DG
230 /*
231 * Flag for this channel if the metadata was dumped once during
232 * registration. 0 means no, 1 yes.
233 */
234 unsigned int metadata_dumped;
d0b96690
DG
235 /*
236 * Node in the ust-registry hash table. The event name is used to
237 * initialize the node and the event_name/signature for the match function.
238 */
7972aab2 239 struct lttng_ht_node_u64 node;
d0b96690
DG
240};
241
10b56aef
MD
242struct ust_registry_enum {
243 char name[LTTNG_UST_SYM_NAME_LEN];
244 struct ustctl_enum_entry *entries;
245 size_t nr_entries;
246 uint64_t id; /* enum id in session */
247 /* Enumeration node in session hash table. */
248 struct lttng_ht_node_str node;
249 /* For delayed reclaim. */
250 struct rcu_head rcu_head;
251};
252
d0b96690
DG
253/*
254 * Validate that the id has reached the maximum allowed or not.
255 *
256 * Return 0 if NOT else 1.
257 */
258static inline int ust_registry_is_max_id(uint32_t id)
259{
260 return (id == UINT32_MAX) ? 1 : 0;
261}
262
263/*
264 * Return next available event id and increment the used counter. The
265 * ust_registry_is_max_id function MUST be called before in order to validate
266 * if the maximum number of IDs have been reached. If not, it is safe to call
267 * this function.
268 *
269 * Return a unique channel ID. If max is reached, the used_event_id counter is
270 * returned.
271 */
ebdb334b 272static inline uint32_t ust_registry_channel_get_next_event_id(
d0b96690
DG
273 struct ust_registry_channel *r)
274{
275 if (ust_registry_is_max_id(r->used_event_id)) {
276 return r->used_event_id;
277 }
278
279 r->used_event_id++;
280 return r->next_event_id++;
281}
282
ebdb334b
JR
283/*
284 * Return next available event id and increment the used counter. The
285 * ust_registry_is_max_id function MUST be called before in order to validate
286 * if the maximum number of IDs have been reached. If not, it is safe to call
287 * this function.
288 *
289 * Return a unique map ID. If max is reached, the used_event_id counter is
290 * returned.
291 */
292static inline uint32_t ust_registry_map_get_next_event_id(
293 struct ust_registry_map *r)
294{
295 if (ust_registry_is_max_id(r->used_event_id)) {
296 return r->used_event_id;
297 }
298
299 r->used_event_id++;
300 return r->next_event_id++;
301}
302
d0b96690
DG
303/*
304 * Return next available channel id and increment the used counter. The
305 * ust_registry_is_max_id function MUST be called before in order to validate
306 * if the maximum number of IDs have been reached. If not, it is safe to call
307 * this function.
308 *
309 * Return a unique channel ID. If max is reached, the used_channel_id counter
310 * is returned.
311 */
312static inline uint32_t ust_registry_get_next_chan_id(
313 struct ust_registry_session *r)
314{
315 if (ust_registry_is_max_id(r->used_channel_id)) {
316 return r->used_channel_id;
317 }
318
319 r->used_channel_id++;
320 return r->next_channel_id++;
321}
322
ebdb334b
JR
323/*
324 * Return next available map id and increment the used counter. The
325 * ust_registry_is_max_id function MUST be called before in order to validate
326 * if the maximum number of IDs have been reached. If not, it is safe to call
327 * this function.
328 *
329 * Return a unique map ID. If max is reached, the used_map_id counter
330 * is returned.
331 */
332static inline uint32_t ust_registry_get_next_map_id(
333 struct ust_registry_session *r)
334{
335 if (ust_registry_is_max_id(r->used_map_id)) {
336 return r->used_map_id;
337 }
338
339 r->used_map_id++;
340 return r->next_map_id++;
341}
342
d0b96690
DG
343/*
344 * Return registry event count. This is read atomically.
345 */
346static inline uint32_t ust_registry_get_event_count(
347 struct ust_registry_channel *r)
348{
349 return (uint32_t) uatomic_read(&r->used_event_id);
350}
351
7972aab2
DG
352#ifdef HAVE_LIBLTTNG_UST_CTL
353
ebdb334b 354/* Channels */
d0b96690
DG
355void ust_registry_channel_destroy(struct ust_registry_session *session,
356 struct ust_registry_channel *chan);
45893984
DG
357struct ust_registry_channel *ust_registry_channel_find(
358 struct ust_registry_session *session, uint64_t key);
359int ust_registry_channel_add(struct ust_registry_session *session,
360 uint64_t key);
361void ust_registry_channel_del_free(struct ust_registry_session *session,
e9404c27 362 uint64_t key, bool notif);
45893984 363
ebdb334b
JR
364/* Maps */
365void ust_registry_map_destroy(struct ust_registry_session *session,
366 struct ust_registry_map *map);
367struct ust_registry_map *ust_registry_map_find(
368 struct ust_registry_session *session, uint64_t key);
369int ust_registry_map_add(struct ust_registry_session *session,
370 uint64_t key);
371void ust_registry_map_del_free(struct ust_registry_session *session,
372 uint64_t key);
373int ust_registry_map_add_token_key_mapping(struct ust_registry_session *session,
374 uint64_t map_key, uint64_t tracer_token,
375 struct lttng_map_key *key);
376
45893984 377int ust_registry_session_init(struct ust_registry_session **sessionp,
d0b96690
DG
378 struct ust_app *app,
379 uint32_t bits_per_long,
380 uint32_t uint8_t_alignment,
381 uint32_t uint16_t_alignment,
382 uint32_t uint32_t_alignment,
383 uint32_t uint64_t_alignment,
384 uint32_t long_alignment,
af6142cf
MD
385 int byte_order,
386 uint32_t major,
d7ba1388 387 uint32_t minor,
3d071855 388 const char *root_shm_path,
d7ba1388
MD
389 const char *shm_path,
390 uid_t euid,
8de88061
JR
391 gid_t egid,
392 uint64_t tracing_id,
393 uid_t tracing_uid);
d0b96690
DG
394void ust_registry_session_destroy(struct ust_registry_session *session);
395
ebdb334b 396int ust_registry_chan_create_event(struct ust_registry_session *session,
45893984 397 uint64_t chan_key, int session_objd, int channel_objd, char *name,
2106efa0
PP
398 char *sig, size_t nr_fields, struct ustctl_field *fields,
399 int loglevel_value, char *model_emf_uri, int buffer_type,
400 uint32_t *event_id_p, struct ust_app *app);
ebdb334b 401struct ust_registry_event *ust_registry_chan_find_event(
d0b96690 402 struct ust_registry_channel *chan, char *name, char *sig);
ebdb334b
JR
403void ust_registry_chan_destroy_event(struct ust_registry_channel *chan,
404 struct ust_registry_event *event);
405
406int ust_registry_map_create_event(struct ust_registry_session *session,
407 uint64_t map_key, int session_objd, int map_objd, char *name,
408 char *sig, size_t nr_fields, struct ustctl_field *fields,
409 int loglevel_value, char *model_emf_uri, int buffer_type,
410 uint64_t tracer_token, uint64_t *counter_index_p, struct ust_app *app);
411struct ust_registry_event *ust_registry_map_find_event(
412 struct ust_registry_map *map, char *name, char *sig);
413void ust_registry_map_destroy_event(struct ust_registry_map *map,
d0b96690
DG
414 struct ust_registry_event *event);
415
416/* app can be NULL for registry shared across applications. */
417int ust_metadata_session_statedump(struct ust_registry_session *session,
af6142cf 418 struct ust_app *app, uint32_t major, uint32_t minor);
d0b96690
DG
419int ust_metadata_channel_statedump(struct ust_registry_session *session,
420 struct ust_registry_channel *chan);
421int ust_metadata_event_statedump(struct ust_registry_session *session,
422 struct ust_registry_channel *chan,
423 struct ust_registry_event *event);
10b56aef
MD
424int ust_registry_create_or_find_enum(struct ust_registry_session *session,
425 int session_objd, char *name,
426 struct ustctl_enum_entry *entries, size_t nr_entries,
427 uint64_t *enum_id);
428struct ust_registry_enum *
429 ust_registry_lookup_enum_by_id(struct ust_registry_session *session,
430 const char *name, uint64_t id);
d0b96690 431
7972aab2
DG
432#else /* HAVE_LIBLTTNG_UST_CTL */
433
434static inline
435void ust_registry_channel_destroy(struct ust_registry_session *session,
436 struct ust_registry_channel *chan)
437{}
438static inline
439struct ust_registry_channel *ust_registry_channel_find(
440 struct ust_registry_session *session, uint64_t key)
441{
442 return NULL;
443}
444static inline
445int ust_registry_channel_add(struct ust_registry_session *session,
446 uint64_t key)
447{
448 return 0;
449}
450static inline
451void ust_registry_channel_del_free(struct ust_registry_session *session,
e9404c27 452 uint64_t key, bool notif)
7972aab2
DG
453{}
454static inline
455int ust_registry_session_init(struct ust_registry_session **sessionp,
456 struct ust_app *app,
457 uint32_t bits_per_long,
458 uint32_t uint8_t_alignment,
459 uint32_t uint16_t_alignment,
460 uint32_t uint32_t_alignment,
461 uint32_t uint64_t_alignment,
462 uint32_t long_alignment,
8de88061
JR
463 int byte_order,
464 uint32_t major,
465 uint32_t minor,
466 const char *root_shm_path,
467 const char *shm_path,
468 uid_t euid,
469 gid_t egid,
470 uint64_t tracing_id,
16ea9370 471 uid_t tracing_uid)
7972aab2
DG
472{
473 return 0;
474}
475static inline
476void ust_registry_session_destroy(struct ust_registry_session *session)
477{}
478static inline
479int ust_registry_create_event(struct ust_registry_session *session,
480 uint64_t chan_key, int session_objd, int channel_objd, char *name,
2106efa0
PP
481 char *sig, size_t nr_fields, struct ustctl_field *fields,
482 int loglevel_value, char *model_emf_uri, int buffer_type,
483 uint32_t *event_id_p)
7972aab2
DG
484{
485 return 0;
486}
487static inline
488struct ust_registry_event *ust_registry_find_event(
489 struct ust_registry_channel *chan, char *name, char *sig)
490{
491 return NULL;
492}
493static inline
494void ust_registry_destroy_event(struct ust_registry_channel *chan,
495 struct ust_registry_event *event)
496{}
497
498/* The app object can be NULL for registry shared across applications. */
499static inline
500int ust_metadata_session_statedump(struct ust_registry_session *session,
9804676e 501 struct ust_app *app, uint32_t major, uint32_t minor)
7972aab2
DG
502{
503 return 0;
504}
505static inline
506int ust_metadata_channel_statedump(struct ust_registry_session *session,
507 struct ust_registry_channel *chan)
508{
509 return 0;
510}
511static inline
512int ust_metadata_event_statedump(struct ust_registry_session *session,
513 struct ust_registry_channel *chan,
514 struct ust_registry_event *event)
515{
516 return 0;
517}
10b56aef
MD
518static inline
519int ust_registry_create_or_find_enum(struct ust_registry_session *session,
520 int session_objd, char *name,
521 struct ustctl_enum_entry *entries, size_t nr_entries,
522 uint64_t *enum_id)
523{
524 return 0;
525}
526static inline
527struct ust_registry_enum *
528 ust_registry_lookup_enum_by_id(struct ust_registry_session *session,
529 const char *name, uint64_t id)
530{
531 return NULL;
532}
7972aab2
DG
533
534#endif /* HAVE_LIBLTTNG_UST_CTL */
535
d0b96690 536#endif /* LTTNG_UST_REGISTRY_H */
This page took 0.096412 seconds and 5 git commands to generate.