Move to kernel style SPDX license identifiers
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.c
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 #include <assert.h>
12
13 #include <common/common.h>
14 #include <common/utils.h>
15 #include <urcu/rculist.h>
16
17 #include "ctf-trace.h"
18 #include "lttng-relayd.h"
19 #include "stream.h"
20
21 static uint64_t last_relay_ctf_trace_id;
22 static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
23
24 static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
25 {
26 struct ctf_trace *trace =
27 caa_container_of(rcu_head, struct 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 assert(cds_list_empty(&trace->stream_list));
45 session_put(trace->session);
46 trace->session = NULL;
47 free(trace->path);
48 trace->path = NULL;
49 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
50 }
51
52 static void ctf_trace_release(struct urcu_ref *ref)
53 {
54 struct ctf_trace *trace =
55 caa_container_of(ref, struct ctf_trace, ref);
56 int ret;
57 struct lttng_ht_iter iter;
58
59 iter.iter.node = &trace->node.node;
60 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
61 assert(!ret);
62 ctf_trace_destroy(trace);
63 }
64
65 /*
66 * Should be called with RCU read-side lock held.
67 */
68 bool ctf_trace_get(struct ctf_trace *trace)
69 {
70 return urcu_ref_get_unless_zero(&trace->ref);
71 }
72
73 /*
74 * Create and return an allocated ctf_trace. NULL on error.
75 * There is no "open" and "close" for a ctf_trace, but rather just a
76 * create and refcounting. Whenever all the streams belonging to a trace
77 * put their reference, its refcount drops to 0.
78 */
79 static struct ctf_trace *ctf_trace_create(struct relay_session *session,
80 const char *subpath)
81 {
82 struct ctf_trace *trace;
83
84 trace = zmalloc(sizeof(*trace));
85 if (!trace) {
86 PERROR("Failed to allocate ctf_trace");
87 goto end;
88 }
89 urcu_ref_init(&trace->ref);
90
91 if (!session_get(session)) {
92 ERR("Failed to acquire session reference");
93 goto error;
94 }
95 trace->session = session;
96 trace->path = strdup(subpath);
97 if (!trace->path) {
98 goto error;
99 }
100
101 CDS_INIT_LIST_HEAD(&trace->stream_list);
102
103 pthread_mutex_lock(&last_relay_ctf_trace_id_lock);
104 trace->id = ++last_relay_ctf_trace_id;
105 pthread_mutex_unlock(&last_relay_ctf_trace_id_lock);
106
107 lttng_ht_node_init_str(&trace->node, trace->path);
108 trace->session = session;
109 pthread_mutex_init(&trace->lock, NULL);
110 pthread_mutex_init(&trace->stream_list_lock, NULL);
111 lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
112
113 DBG("Created ctf_trace %" PRIu64 "of session \"%s\" from host \"%s\" with path: %s",
114 trace->id, session->session_name, session->hostname,
115 subpath);
116
117 end:
118 return trace;
119 error:
120 ctf_trace_put(trace);
121 return NULL;
122 }
123
124 /*
125 * Return a ctf_trace if found by id in the given hash table else NULL.
126 * Hold a reference on the ctf_trace, and must be paired with
127 * ctf_trace_put().
128 */
129 struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
130 const char *subpath)
131 {
132 struct lttng_ht_node_str *node;
133 struct lttng_ht_iter iter;
134 struct ctf_trace *trace = NULL;
135
136 rcu_read_lock();
137 lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter);
138 node = lttng_ht_iter_get_node_str(&iter);
139 if (!node) {
140 DBG("CTF Trace path %s not found", subpath);
141 goto end;
142 }
143 trace = caa_container_of(node, struct ctf_trace, node);
144 if (!ctf_trace_get(trace)) {
145 trace = NULL;
146 }
147 end:
148 rcu_read_unlock();
149 if (!trace) {
150 /* Try to create */
151 trace = ctf_trace_create(session, subpath);
152 }
153 return trace;
154 }
155
156 void ctf_trace_put(struct ctf_trace *trace)
157 {
158 rcu_read_lock();
159 urcu_ref_put(&trace->ref, ctf_trace_release);
160 rcu_read_unlock();
161 }
162
163 int ctf_trace_close(struct ctf_trace *trace)
164 {
165 struct relay_stream *stream;
166
167 rcu_read_lock();
168 cds_list_for_each_entry_rcu(stream, &trace->stream_list,
169 stream_node) {
170 /*
171 * Close stream since the connection owning the trace is being
172 * torn down.
173 */
174 try_stream_close(stream);
175 }
176 rcu_read_unlock();
177 /*
178 * Since all references to the trace are held by its streams, we
179 * don't need to do any self-ref put.
180 */
181 return 0;
182 }
183
184 struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace)
185 {
186 struct relay_viewer_stream *vstream;
187
188 rcu_read_lock();
189 vstream = rcu_dereference(trace->viewer_metadata_stream);
190 if (!vstream) {
191 goto end;
192 }
193 if (!viewer_stream_get(vstream)) {
194 vstream = NULL;
195 }
196 end:
197 rcu_read_unlock();
198 return vstream;
199 }
This page took 0.033164 seconds and 5 git commands to generate.