2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #ifndef LTTNG_UST_REGISTRY_SESSION_H
9 #define LTTNG_UST_REGISTRY_SESSION_H
11 #include "clock-class.hpp"
12 #include "session.hpp"
13 #include "trace-class.hpp"
14 #include "ust-clock-class.hpp"
15 #include "ust-registry-channel.hpp"
17 #include <common/make-unique-wrapper.hpp>
21 #include <lttng/lttng.h>
29 class registry_session;
32 void locked_registry_session_release(registry_session *session);
33 } /* namespace details */
35 class registry_session : public lttng::sessiond::trace::trace_class {
37 using locked_ptr = std::unique_ptr<registry_session,
38 lttng::details::create_unique_class<registry_session,
39 details::locked_registry_session_release>::
42 virtual lttng_buffer_type get_buffering_scheme() const noexcept = 0;
45 void add_channel(uint64_t channel_key);
46 lttng::sessiond::ust::registry_channel& get_channel(uint64_t channel_key) const;
47 void remove_channel(uint64_t channel_key, bool notify);
49 void regenerate_metadata();
50 virtual ~registry_session();
53 * With multiple writers and readers, use this lock to access
54 * the registry. Can nest within the ust app session lock.
55 * Also acts as a registry serialization lock. Used by registry
56 * readers to serialize the registry information sent from the
57 * sessiond to the consumerd.
58 * The consumer socket lock nests within this lock.
60 mutable pthread_mutex_t _lock;
61 /* Next channel ID available for a newly registered channel. */
62 uint32_t _next_channel_id = 0;
63 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
64 uint32_t _used_channel_id = 0;
65 /* Next enumeration ID available. */
66 uint64_t _next_enum_id = 0;
68 /* Generated metadata. */
69 char *_metadata = nullptr; /* NOT null-terminated ! Use memcpy. */
70 size_t _metadata_len = 0, _metadata_alloc_len = 0;
71 /* Length of bytes sent to the consumer. */
72 size_t _metadata_len_sent = 0;
73 /* Current version of the metadata. */
74 uint64_t _metadata_version = 0;
77 * Those fields are only used when a session is created with
78 * the --shm-path option. In this case, the metadata is output
79 * twice: once to the consumer, as ususal, but a second time
80 * also in the shm path directly. This is done so that a copy
81 * of the metadata that is as fresh as possible is available
82 * on the event of a crash.
84 * root_shm_path contains the shm-path provided by the user, along with
85 * the session's name and timestamp:
86 * e.g. /tmp/my_shm/my_session-20180612-135822
88 * shm_path contains the full path of the memory buffers:
89 * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit
91 * metadata_path contains the full path to the metadata file that
92 * is kept for the "crash buffer" extraction:
94 * /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit/metadata
96 * Note that this is not the trace's final metadata file. It is
97 * only meant to be used to read the contents of the ring buffers
98 * in the event of a crash.
100 * metadata_fd is a file descriptor that points to the file at
103 char _root_shm_path[PATH_MAX] = {};
104 char _shm_path[PATH_MAX] = {};
105 char _metadata_path[PATH_MAX] = {};
106 /* File-backed metadata FD */
107 int _metadata_fd = -1;
110 * Hash table containing channels sent by the UST tracer. MUST
111 * be accessed with a RCU read side lock acquired.
113 lttng_ht::uptr _channels;
116 * Unique key to identify the metadata on the consumer side.
118 uint64_t _metadata_key = 0;
120 * Indicates if the metadata is closed on the consumer side. This is to
121 * avoid double close of metadata when an application unregisters AND
122 * deletes its sessions.
124 bool _metadata_closed = false;
126 /* User and group owning the session. */
130 /* Enumerations table. */
131 lttng_ht::uptr _enums;
134 * Copy of the tracer version when the first app is registered.
135 * It is used if we need to regenerate the metadata.
137 uint32_t _app_tracer_version_major = 0;
138 uint32_t _app_tracer_version_minor = 0;
140 /* The id of the parent session */
141 ltt_session::id_t _tracing_id = -1ULL;
144 /* Prevent instanciation of this base class. */
145 registry_session(const struct lttng::sessiond::trace::abi& abi,
146 unsigned int app_tracer_version_major,
147 unsigned int app_tracer_version_minor,
148 const char *root_shm_path,
149 const char *shm_path,
152 uint64_t tracing_id);
153 virtual void _visit_environment(
154 lttng::sessiond::trace::trace_class_visitor& trace_class_visitor)
156 void _generate_metadata();
159 uint32_t _get_next_channel_id();
160 void _increase_metadata_size(size_t reservation_length);
161 void _append_metadata_fragment(const std::string& fragment);
162 void _reset_metadata();
164 virtual void _accept_on_clock_classes(
165 lttng::sessiond::trace::trace_class_visitor& trace_class_visitor)
166 const override final;
167 virtual void _accept_on_stream_classes(
168 lttng::sessiond::trace::trace_class_visitor& trace_class_visitor)
169 const override final;
171 lttng::sessiond::ust::clock_class _clock;
172 const lttng::sessiond::trace::trace_class_visitor::cuptr _metadata_generating_visitor;
175 } /* namespace ust */
176 } /* namespace sessiond */
177 } /* namespace lttng */
179 #endif /* LTTNG_UST_REGISTRY_SESSION_H */