2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
23 #include <common/common.h>
24 #include <common/utils.h>
25 #include <urcu/rculist.h>
27 #include "ctf-trace.h"
28 #include "lttng-relayd.h"
31 static uint64_t last_relay_ctf_trace_id
;
32 static pthread_mutex_t last_relay_ctf_trace_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
34 static void rcu_destroy_ctf_trace(struct rcu_head
*rcu_head
)
36 struct ctf_trace
*trace
=
37 caa_container_of(rcu_head
, struct ctf_trace
, rcu_node
);
43 * Destroy a ctf trace and all stream contained in it.
45 * MUST be called with the RCU read side lock.
47 void ctf_trace_destroy(struct ctf_trace
*trace
)
50 * Getting to this point, every stream referenced by that trace
51 * have put back their ref since the've been closed by the
54 assert(cds_list_empty(&trace
->stream_list
));
55 session_put(trace
->session
);
56 trace
->session
= NULL
;
57 call_rcu(&trace
->rcu_node
, rcu_destroy_ctf_trace
);
60 void ctf_trace_release(struct urcu_ref
*ref
)
62 struct ctf_trace
*trace
=
63 caa_container_of(ref
, struct ctf_trace
, ref
);
65 struct lttng_ht_iter iter
;
67 iter
.iter
.node
= &trace
->node
.node
;
68 ret
= lttng_ht_del(trace
->session
->ctf_traces_ht
, &iter
);
70 ctf_trace_destroy(trace
);
74 * Should be called with RCU read-side lock held.
76 bool ctf_trace_get(struct ctf_trace
*trace
)
80 /* Confirm that the trace refcount has not reached 0. */
81 pthread_mutex_lock(&trace
->reflock
);
82 if (trace
->ref
.refcount
!= 0) {
84 urcu_ref_get(&trace
->ref
);
86 pthread_mutex_unlock(&trace
->reflock
);
92 * Create and return an allocated ctf_trace. NULL on error.
93 * There is no "open" and "close" for a ctf_trace, but rather just a
94 * create and refcounting. Whenever all the streams belonging to a trace
95 * put their reference, its refcount drops to 0.
97 static struct ctf_trace
*ctf_trace_create(struct relay_session
*session
,
100 struct ctf_trace
*trace
;
102 trace
= zmalloc(sizeof(*trace
));
104 PERROR("ctf_trace alloc");
108 if (!session_get(session
)) {
109 ERR("Cannot get session");
114 trace
->session
= session
;
116 CDS_INIT_LIST_HEAD(&trace
->stream_list
);
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
);
122 lttng_ht_node_init_str(&trace
->node
, path_name
);
123 trace
->session
= session
;
124 urcu_ref_init(&trace
->ref
);
125 pthread_mutex_init(&trace
->lock
, NULL
);
126 pthread_mutex_init(&trace
->reflock
, NULL
);
127 pthread_mutex_init(&trace
->stream_list_lock
, NULL
);
128 lttng_ht_add_str(session
->ctf_traces_ht
, &trace
->node
);
130 DBG("Created ctf_trace %" PRIu64
" with path: %s", trace
->id
, path_name
);
137 * Return a ctf_trace if found by id in the given hash table else NULL.
138 * Hold a reference on the ctf_trace, and must be paired with
141 struct ctf_trace
*ctf_trace_get_by_path_or_create(struct relay_session
*session
,
144 struct lttng_ht_node_str
*node
;
145 struct lttng_ht_iter iter
;
146 struct ctf_trace
*trace
= NULL
;
149 lttng_ht_lookup(session
->ctf_traces_ht
, (void *) path_name
, &iter
);
150 node
= lttng_ht_iter_get_node_str(&iter
);
152 DBG("CTF Trace path %s not found", path_name
);
155 trace
= caa_container_of(node
, struct ctf_trace
, node
);
156 if (!ctf_trace_get(trace
)) {
163 trace
= ctf_trace_create(session
, path_name
);
168 void ctf_trace_put(struct ctf_trace
*trace
)
171 pthread_mutex_lock(&trace
->reflock
);
172 urcu_ref_put(&trace
->ref
, ctf_trace_release
);
173 pthread_mutex_unlock(&trace
->reflock
);
177 int ctf_trace_close(struct ctf_trace
*trace
)
179 struct relay_stream
*stream
;
182 cds_list_for_each_entry_rcu(stream
, &trace
->stream_list
,
185 * Close stream since the connection owning the trace is being
188 try_stream_close(stream
);
192 * Since all references to the trace are held by its streams, we
193 * don't need to do any self-ref put.
198 struct relay_viewer_stream
*ctf_trace_get_viewer_metadata_stream(struct ctf_trace
*trace
)
200 struct relay_viewer_stream
*vstream
;
203 vstream
= rcu_dereference(trace
->viewer_metadata_stream
);
207 if (!viewer_stream_get(vstream
)) {