Use the trace format type for ust metadata generation
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry-session.hpp
1 /*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef LTTNG_UST_REGISTRY_SESSION_H
9 #define LTTNG_UST_REGISTRY_SESSION_H
10
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"
16 #include "ust-registry.hpp"
17
18 #include <common/make-unique-wrapper.hpp>
19 #include <lttng/trace-format-descriptor-internal.hpp>
20
21 #include <cstdint>
22 #include <ctime>
23 #include <lttng/lttng.h>
24 #include <string>
25 #include <unistd.h>
26
27 namespace lttng {
28 namespace sessiond {
29 namespace ust {
30
31 class registry_enum;
32 class registry_session;
33
34 namespace details {
35 void locked_registry_session_release(registry_session *session);
36 } /* namespace details */
37
38 class registry_session : public lttng::sessiond::trace::trace_class {
39 public:
40 using locked_ptr = std::unique_ptr<registry_session,
41 lttng::details::create_unique_class<registry_session,
42 details::locked_registry_session_release>::
43 deleter>;
44
45 virtual lttng_buffer_type get_buffering_scheme() const noexcept = 0;
46 locked_ptr lock() noexcept;
47
48 void add_channel(uint64_t channel_key);
49
50 /* A channel is protected by its parent registry session's lock. */
51 lttng::sessiond::ust::registry_channel& get_channel(uint64_t channel_key) const;
52
53 void remove_channel(uint64_t channel_key, bool notify);
54
55 void create_or_find_enum(int session_objd,
56 const char *enum_name,
57 struct lttng_ust_ctl_enum_entry *raw_entries,
58 size_t nr_entries,
59 uint64_t *enum_id);
60 registry_enum::const_rcu_protected_reference get_enumeration(
61 const char *enum_name, uint64_t enum_id) const;
62
63 void regenerate_metadata();
64 virtual ~registry_session();
65
66 virtual const lttng::sessiond::trace::type *get_packet_header() const noexcept override;
67
68 /*
69 * With multiple writers and readers, use this lock to access
70 * the registry. Can nest within the ust app session lock.
71 * Also acts as a registry serialization lock. Used by registry
72 * readers to serialize the registry information sent from the
73 * sessiond to the consumerd.
74 *
75 * The consumer socket lock nests within this lock.
76 */
77 mutable pthread_mutex_t _lock;
78
79 /* Generated metadata, not null-terminated. */
80 char *_metadata = nullptr; /* */
81 size_t _metadata_len = 0;
82 /* Length of bytes sent to the consumer. */
83 size_t _metadata_len_sent = 0;
84 /* Current version of the metadata. */
85 uint64_t _metadata_version = 0;
86
87 /*
88 * Unique key to identify the metadata on the consumer side.
89 */
90 uint64_t _metadata_key = 0;
91 /*
92 * Indicates if the metadata is closed on the consumer side. This is to
93 * avoid double close of metadata when an application unregisters AND
94 * deletes its sessions.
95 */
96 bool _metadata_closed = false;
97
98 protected:
99 /* Prevent instanciation of this base class. */
100 registry_session(const struct lttng::sessiond::trace::abi& abi,
101 unsigned int app_tracer_version_major,
102 unsigned int app_tracer_version_minor,
103 const char *root_shm_path,
104 const char *shm_path,
105 uid_t euid,
106 gid_t egid,
107 uint64_t tracing_id,
108 const lttng::trace_format_descriptor& trace_format);
109 virtual void accept(
110 trace::trace_class_environment_visitor& environment_visitor) const override;
111 void _generate_metadata();
112
113 private:
114 uint32_t _get_next_channel_id();
115 void _increase_metadata_size(size_t reservation_length);
116 void _append_metadata_fragment(const std::string& fragment);
117 void _reset_metadata();
118 void _destroy_enum(registry_enum *reg_enum) noexcept;
119 registry_enum *_lookup_enum(const registry_enum *target_enum) const;
120 lttng::sessiond::trace::type::cuptr _create_packet_header() const;
121 lttng::sessiond::trace::trace_class_visitor::cuptr trace_class_visitor_factory(
122 const struct lttng::sessiond::trace::abi& trace_abi,
123 const lttng::trace_format_descriptor& trace_format);
124
125 virtual void _accept_on_clock_classes(
126 lttng::sessiond::trace::trace_class_visitor& trace_class_visitor)
127 const override final;
128 virtual void _accept_on_stream_classes(
129 lttng::sessiond::trace::trace_class_visitor& trace_class_visitor)
130 const override final;
131
132 /* Next channel ID available for a newly registered channel. */
133 uint32_t _next_channel_id = 0;
134
135 /* Once this value reaches UINT32_MAX, no more id can be allocated. */
136 uint32_t _used_channel_id = 0;
137
138 /* Next enumeration ID available. */
139 uint64_t _next_enum_id = 0;
140
141 size_t _metadata_alloc_len = 0;
142
143 /*
144 * Those fields are only used when a session is created with
145 * the --shm-path option. In this case, the metadata is output
146 * twice: once to the consumer, as ususal, but a second time
147 * also in the shm path directly. This is done so that a copy
148 * of the metadata that is as fresh as possible is available
149 * on the event of a crash.
150 *
151 * root_shm_path contains the shm-path provided by the user, along with
152 * the session's name and timestamp:
153 * e.g. /tmp/my_shm/my_session-20180612-135822
154 *
155 * shm_path contains the full path of the memory buffers:
156 * e.g. /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit
157 *
158 * metadata_path contains the full path to the metadata file that
159 * is kept for the "crash buffer" extraction:
160 * e.g.
161 * /tmp/my_shm/my_session-20180612-135822/ust/uid/1000/64-bit/metadata
162 *
163 * Note that this is not the trace's final metadata file. It is
164 * only meant to be used to read the contents of the ring buffers
165 * in the event of a crash.
166 *
167 * metadata_fd is a file descriptor that points to the file at
168 * 'metadata_path'.
169 */
170 const std::string _root_shm_path;
171 const std::string _shm_path;
172 const std::string _metadata_path;
173
174 /* File-backed metadata FD */
175 int _metadata_fd = -1;
176
177 /*
178 * Hash table containing channels sent by the UST tracer. MUST
179 * be accessed with a RCU read side lock acquired.
180 */
181 lttng_ht::uptr _channels;
182
183 /* Enumerations table. */
184 lttng_ht::uptr _enums;
185
186 /* User and group owning the session. */
187 const uid_t _uid;
188 const gid_t _gid;
189
190 /*
191 * Copy of the tracer version when the first app is registered.
192 * It is used if we need to regenerate the metadata.
193 */
194 const struct {
195 uint32_t major, minor;
196 } _app_tracer_version;
197
198 /* The id of the parent session. */
199 const ltt_session::id_t _tracing_id;
200
201 lttng::sessiond::ust::clock_class _clock;
202 const lttng::sessiond::trace::trace_class_visitor::cuptr _metadata_generating_visitor;
203 lttng::sessiond::trace::type::cuptr _packet_header;
204 };
205
206 } /* namespace ust */
207 } /* namespace sessiond */
208 } /* namespace lttng */
209
210 #endif /* LTTNG_UST_REGISTRY_SESSION_H */
This page took 0.03519 seconds and 5 git commands to generate.