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