vscode: Add configurations to run the executables under the debugger
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.cpp
1 /*
2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11
12 #include "ctf-trace.hpp"
13 #include "lttng-relayd.hpp"
14 #include "stream.hpp"
15
16 #include <common/common.hpp>
17 #include <common/urcu.hpp>
18 #include <common/utils.hpp>
19
20 #include <urcu/rculist.h>
21
22 static uint64_t last_relay_ctf_trace_id;
23 static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
24
25 static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
26 {
27 struct ctf_trace *trace = lttng::utils::container_of(rcu_head, &ctf_trace::rcu_node);
28
29 free(trace);
30 }
31
32 /*
33 * Destroy a ctf trace and all stream contained in it.
34 *
35 * MUST be called with the RCU read side lock.
36 */
37 static void ctf_trace_destroy(struct ctf_trace *trace)
38 {
39 /*
40 * Getting to this point, every stream referenced by that trace
41 * have put back their ref since the've been closed by the
42 * control side.
43 */
44 LTTNG_ASSERT(cds_list_empty(&trace->stream_list));
45 ASSERT_RCU_READ_LOCKED();
46
47 session_put(trace->session);
48 trace->session = nullptr;
49 free(trace->path);
50 trace->path = nullptr;
51 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
52 }
53
54 static void ctf_trace_release(struct urcu_ref *ref)
55 {
56 struct ctf_trace *trace = lttng::utils::container_of(ref, &ctf_trace::ref);
57 int ret;
58 struct lttng_ht_iter iter;
59
60 iter.iter.node = &trace->node.node;
61 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
62 LTTNG_ASSERT(!ret);
63 ctf_trace_destroy(trace);
64 }
65
66 /*
67 * The caller must either:
68 * - hold the RCU read side lock, or
69 * - guarantee the existence of the object by already holding a reference to
70 * the object.
71 */
72 bool ctf_trace_get(struct ctf_trace *trace)
73 {
74 const bool ref = urcu_ref_get_unless_zero(&trace->ref);
75
76 if (!ref) {
77 /*
78 * The ref count is already zero. It means the object is being
79 * torn down concurently.
80 * This is only acceptable if we hold the RCU read-side lock,
81 * else it's a logic error.
82 */
83 ASSERT_RCU_READ_LOCKED();
84 }
85
86 return ref;
87 }
88
89 /*
90 * Create and return an allocated ctf_trace. NULL on error.
91 * There is no "open" and "close" for a ctf_trace, but rather just a
92 * create and refcounting. Whenever all the streams belonging to a trace
93 * put their reference, its refcount drops to 0.
94 */
95 static struct ctf_trace *ctf_trace_create(struct relay_session *session, const char *subpath)
96 {
97 struct ctf_trace *trace;
98
99 trace = zmalloc<ctf_trace>();
100 if (!trace) {
101 PERROR("Failed to allocate ctf_trace");
102 goto end;
103 }
104 urcu_ref_init(&trace->ref);
105
106 if (!session_get(session)) {
107 ERR("Failed to acquire session reference");
108 goto error;
109 }
110 trace->session = session;
111 trace->path = strdup(subpath);
112 if (!trace->path) {
113 goto error;
114 }
115
116 CDS_INIT_LIST_HEAD(&trace->stream_list);
117
118 pthread_mutex_lock(&last_relay_ctf_trace_id_lock);
119 trace->id = ++last_relay_ctf_trace_id;
120 pthread_mutex_unlock(&last_relay_ctf_trace_id_lock);
121
122 lttng_ht_node_init_str(&trace->node, trace->path);
123 trace->session = session;
124 pthread_mutex_init(&trace->lock, nullptr);
125 pthread_mutex_init(&trace->stream_list_lock, nullptr);
126 lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
127
128 DBG("Created ctf_trace %" PRIu64 " of session \"%s\" from host \"%s\" with path: %s",
129 trace->id,
130 session->session_name,
131 session->hostname,
132 subpath);
133
134 end:
135 return trace;
136 error:
137 ctf_trace_put(trace);
138 return nullptr;
139 }
140
141 /*
142 * Return a ctf_trace if found by id in the given hash table else NULL.
143 * Hold a reference on the ctf_trace, and must be paired with
144 * ctf_trace_put().
145 */
146 struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
147 const char *subpath)
148 {
149 struct lttng_ht_node_str *node;
150 struct lttng_ht_iter iter;
151 struct ctf_trace *trace = nullptr;
152
153 lttng::urcu::read_lock_guard read_lock;
154 lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter);
155 node = lttng_ht_iter_get_node_str(&iter);
156 if (!node) {
157 DBG("CTF Trace path %s not found", subpath);
158 goto end;
159 }
160 trace = lttng::utils::container_of(node, &ctf_trace::node);
161 if (!ctf_trace_get(trace)) {
162 trace = nullptr;
163 }
164 end:
165 if (!trace) {
166 /* Try to create */
167 trace = ctf_trace_create(session, subpath);
168 }
169 return trace;
170 }
171
172 void ctf_trace_put(struct ctf_trace *trace)
173 {
174 lttng::urcu::read_lock_guard read_lock;
175 urcu_ref_put(&trace->ref, ctf_trace_release);
176 }
177
178 int ctf_trace_close(struct ctf_trace *trace)
179 {
180 struct relay_stream *stream;
181
182 lttng::urcu::read_lock_guard read_lock;
183 cds_list_for_each_entry_rcu(stream, &trace->stream_list, stream_node)
184 {
185 /*
186 * Close stream since the connection owning the trace is being
187 * torn down.
188 */
189 try_stream_close(stream);
190 }
191 /*
192 * Since all references to the trace are held by its streams, we
193 * don't need to do any self-ref put.
194 */
195 return 0;
196 }
197
198 struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace)
199 {
200 struct relay_viewer_stream *vstream;
201
202 lttng::urcu::read_lock_guard read_lock;
203 vstream = rcu_dereference(trace->viewer_metadata_stream);
204 if (!vstream) {
205 goto end;
206 }
207 if (!viewer_stream_get(vstream)) {
208 vstream = nullptr;
209 }
210 end:
211 return vstream;
212 }
This page took 0.03273 seconds and 4 git commands to generate.