2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef LTTNG_UST_REGISTRY_H
9 #define LTTNG_UST_REGISTRY_H
14 #include <common/hashtable/hashtable.h>
15 #include <common/uuid.h>
17 #include "lttng-ust-ctl.h"
19 #define CTF_SPEC_MAJOR 1
20 #define CTF_SPEC_MINOR 8
24 struct ust_registry_session
{
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.
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
;
38 /* Next map ID available for a newly registered map. */
40 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
42 /* Next enumeration ID available. */
43 uint64_t next_enum_id
;
44 /* Universal unique identifier used by the tracer. */
45 unsigned char uuid
[LTTNG_UUID_LEN
];
47 /* session ABI description */
49 /* Size of long, in bits */
50 unsigned int bits_per_long
;
51 /* Alignment, in bits */
52 unsigned int uint8_t_alignment
,
58 int byte_order
; /* BIG_ENDIAN or LITTLE_ENDIAN */
60 /* Generated metadata. */
61 char *metadata
; /* NOT null-terminated ! Use memcpy. */
62 size_t metadata_len
, metadata_alloc_len
;
63 /* Length of bytes sent to the consumer. */
64 size_t metadata_len_sent
;
65 /* Current version of the metadata. */
66 uint64_t metadata_version
;
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.
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
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
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
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.
91 * metadata_fd is a file descriptor that points to the file at
94 char root_shm_path
[PATH_MAX
];
95 char shm_path
[PATH_MAX
];
96 char metadata_path
[PATH_MAX
];
97 int metadata_fd
; /* file-backed metadata FD */
100 * Hash table containing channels sent by the UST tracer. MUST
101 * be accessed with a RCU read side lock acquired.
103 struct lttng_ht
*channels
;
106 * Hash table containing maps sent by the UST tracer. MUST
107 * be accessed with a RCU read side lock acquired.
109 struct lttng_ht
*maps
;
111 * Unique key to identify the metadata on the consumer side.
113 uint64_t metadata_key
;
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.
119 unsigned int metadata_closed
;
121 /* User and group owning the session. */
125 /* Enumerations table. */
126 struct lttng_ht
*enums
;
129 * Copy of the tracer version when the first app is registered.
130 * It is used if we need to regenerate the metadata.
135 /* The id of the parent session */
140 struct ust_registry_channel
{
142 uint64_t consumer_key
;
143 /* Id set when replying to a register channel. */
145 enum ustctl_channel_header header_type
;
148 * Flag for this channel if the metadata was dumped once during
149 * registration. 0 means no, 1 yes.
151 unsigned int metadata_dumped
;
152 /* Indicates if this channel registry has already been registered. */
153 unsigned int register_done
;
156 * Hash table containing events sent by the UST tracer. MUST be accessed
157 * with a RCU read side lock acquired.
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
;
165 * Context fields of the registry. Context are per channel. Allocated by a
166 * register channel notification from the UST tracer.
168 size_t nr_ctx_fields
;
169 struct ustctl_field
*ctx_fields
;
170 struct lttng_ht_node_u64 node
;
171 /* For delayed reclaim */
172 struct rcu_head rcu_head
;
175 struct ust_registry_map_key_ht_entry
{
176 struct lttng_map_key
*key
;
177 struct lttng_ht_node_u64 node
;
180 struct ust_registry_map_index_ht_entry
{
183 struct lttng_ht_node_str node
;
186 struct ust_registry_map
{
188 /* Id set when replying to a register map. */
191 /* Indicates if this map registry has already been registered. */
192 unsigned int register_done
;
195 * Hash table containing events sent by the UST tracer. MUST be accessed
196 * with a RCU read side lock acquired.
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
;
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
;
209 struct lttng_ht_node_u64 node
;
210 /* For delayed reclaim */
211 struct rcu_head rcu_head
;
215 * Event registered from a UST tracer sent to the session daemon. This is
216 * indexed and matched by <event_name/signature>.
218 struct ust_registry_event
{
220 /* Both objd are set by the tracer. */
223 /* Name of the event returned by the tracer. */
224 char name
[LTTNG_UST_SYM_NAME_LEN
];
228 struct ustctl_field
*fields
;
231 * Flag for this channel if the metadata was dumped once during
232 * registration. 0 means no, 1 yes.
234 unsigned int metadata_dumped
;
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.
239 struct lttng_ht_node_u64 node
;
242 struct ust_registry_enum
{
243 char name
[LTTNG_UST_SYM_NAME_LEN
];
244 struct ustctl_enum_entry
*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
;
254 * Validate that the id has reached the maximum allowed or not.
256 * Return 0 if NOT else 1.
258 static inline int ust_registry_is_max_id(uint32_t id
)
260 return (id
== UINT32_MAX
) ? 1 : 0;
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
269 * Return a unique channel ID. If max is reached, the used_event_id counter is
272 static inline uint32_t ust_registry_channel_get_next_event_id(
273 struct ust_registry_channel
*r
)
275 if (ust_registry_is_max_id(r
->used_event_id
)) {
276 return r
->used_event_id
;
280 return r
->next_event_id
++;
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
289 * Return a unique map ID. If max is reached, the used_event_id counter is
292 static inline uint32_t ust_registry_map_get_next_event_id(
293 struct ust_registry_map
*r
)
295 if (ust_registry_is_max_id(r
->used_event_id
)) {
296 return r
->used_event_id
;
300 return r
->next_event_id
++;
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
309 * Return a unique channel ID. If max is reached, the used_channel_id counter
312 static inline uint32_t ust_registry_get_next_chan_id(
313 struct ust_registry_session
*r
)
315 if (ust_registry_is_max_id(r
->used_channel_id
)) {
316 return r
->used_channel_id
;
319 r
->used_channel_id
++;
320 return r
->next_channel_id
++;
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
329 * Return a unique map ID. If max is reached, the used_map_id counter
332 static inline uint32_t ust_registry_get_next_map_id(
333 struct ust_registry_session
*r
)
335 if (ust_registry_is_max_id(r
->used_map_id
)) {
336 return r
->used_map_id
;
340 return r
->next_map_id
++;
344 * Return registry event count. This is read atomically.
346 static inline uint32_t ust_registry_get_event_count(
347 struct ust_registry_channel
*r
)
349 return (uint32_t) uatomic_read(&r
->used_event_id
);
352 #ifdef HAVE_LIBLTTNG_UST_CTL
355 void ust_registry_channel_destroy(struct ust_registry_session
*session
,
356 struct ust_registry_channel
*chan
);
357 struct ust_registry_channel
*ust_registry_channel_find(
358 struct ust_registry_session
*session
, uint64_t key
);
359 int ust_registry_channel_add(struct ust_registry_session
*session
,
361 void ust_registry_channel_del_free(struct ust_registry_session
*session
,
362 uint64_t key
, bool notif
);
365 void ust_registry_map_destroy(struct ust_registry_session
*session
,
366 struct ust_registry_map
*map
);
367 struct ust_registry_map
*ust_registry_map_find(
368 struct ust_registry_session
*session
, uint64_t key
);
369 int ust_registry_map_add(struct ust_registry_session
*session
,
371 void ust_registry_map_del_free(struct ust_registry_session
*session
,
373 int 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
);
377 int ust_registry_session_init(struct ust_registry_session
**sessionp
,
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
,
388 const char *root_shm_path
,
389 const char *shm_path
,
394 void ust_registry_session_destroy(struct ust_registry_session
*session
);
396 int ust_registry_chan_create_event(struct ust_registry_session
*session
,
397 uint64_t chan_key
, int session_objd
, int channel_objd
, char *name
,
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
);
401 struct ust_registry_event
*ust_registry_chan_find_event(
402 struct ust_registry_channel
*chan
, char *name
, char *sig
);
403 void ust_registry_chan_destroy_event(struct ust_registry_channel
*chan
,
404 struct ust_registry_event
*event
);
406 int 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
);
411 struct ust_registry_event
*ust_registry_map_find_event(
412 struct ust_registry_map
*map
, char *name
, char *sig
);
413 void ust_registry_map_destroy_event(struct ust_registry_map
*map
,
414 struct ust_registry_event
*event
);
416 /* app can be NULL for registry shared across applications. */
417 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
418 struct ust_app
*app
, uint32_t major
, uint32_t minor
);
419 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
420 struct ust_registry_channel
*chan
);
421 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
422 struct ust_registry_channel
*chan
,
423 struct ust_registry_event
*event
);
424 int 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
,
428 struct ust_registry_enum
*
429 ust_registry_lookup_enum_by_id(struct ust_registry_session
*session
,
430 const char *name
, uint64_t id
);
432 #else /* HAVE_LIBLTTNG_UST_CTL */
435 void ust_registry_channel_destroy(struct ust_registry_session
*session
,
436 struct ust_registry_channel
*chan
)
439 struct ust_registry_channel
*ust_registry_channel_find(
440 struct ust_registry_session
*session
, uint64_t key
)
445 int ust_registry_channel_add(struct ust_registry_session
*session
,
451 void ust_registry_channel_del_free(struct ust_registry_session
*session
,
452 uint64_t key
, bool notif
)
455 int ust_registry_session_init(struct ust_registry_session
**sessionp
,
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
,
466 const char *root_shm_path
,
467 const char *shm_path
,
476 void ust_registry_session_destroy(struct ust_registry_session
*session
)
479 int ust_registry_create_event(struct ust_registry_session
*session
,
480 uint64_t chan_key
, int session_objd
, int channel_objd
, char *name
,
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
)
488 struct ust_registry_event
*ust_registry_find_event(
489 struct ust_registry_channel
*chan
, char *name
, char *sig
)
494 void ust_registry_destroy_event(struct ust_registry_channel
*chan
,
495 struct ust_registry_event
*event
)
498 /* The app object can be NULL for registry shared across applications. */
500 int ust_metadata_session_statedump(struct ust_registry_session
*session
,
501 struct ust_app
*app
, uint32_t major
, uint32_t minor
)
506 int ust_metadata_channel_statedump(struct ust_registry_session
*session
,
507 struct ust_registry_channel
*chan
)
512 int ust_metadata_event_statedump(struct ust_registry_session
*session
,
513 struct ust_registry_channel
*chan
,
514 struct ust_registry_event
*event
)
519 int 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
,
527 struct ust_registry_enum
*
528 ust_registry_lookup_enum_by_id(struct ust_registry_session
*session
,
529 const char *name
, uint64_t id
)
534 #endif /* HAVE_LIBLTTNG_UST_CTL */
536 #endif /* LTTNG_UST_REGISTRY_H */