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>
6 * SPDX-License-Identifier: GPL-2.0-only
13 #include <common/common.h>
14 #include <common/utils.h>
15 #include <urcu/rculist.h>
17 #include "ctf-trace.h"
18 #include "lttng-relayd.h"
21 static uint64_t last_relay_ctf_trace_id
;
22 static pthread_mutex_t last_relay_ctf_trace_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
24 static void rcu_destroy_ctf_trace(struct rcu_head
*rcu_head
)
26 struct ctf_trace
*trace
=
27 caa_container_of(rcu_head
, struct ctf_trace
, rcu_node
);
33 * Destroy a ctf trace and all stream contained in it.
35 * MUST be called with the RCU read side lock.
37 static void ctf_trace_destroy(struct ctf_trace
*trace
)
40 * Getting to this point, every stream referenced by that trace
41 * have put back their ref since the've been closed by the
44 assert(cds_list_empty(&trace
->stream_list
));
45 session_put(trace
->session
);
46 trace
->session
= NULL
;
49 call_rcu(&trace
->rcu_node
, rcu_destroy_ctf_trace
);
52 static void ctf_trace_release(struct urcu_ref
*ref
)
54 struct ctf_trace
*trace
=
55 caa_container_of(ref
, struct ctf_trace
, ref
);
57 struct lttng_ht_iter iter
;
59 iter
.iter
.node
= &trace
->node
.node
;
60 ret
= lttng_ht_del(trace
->session
->ctf_traces_ht
, &iter
);
62 ctf_trace_destroy(trace
);
66 * Should be called with RCU read-side lock held.
68 bool ctf_trace_get(struct ctf_trace
*trace
)
70 return urcu_ref_get_unless_zero(&trace
->ref
);
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.
79 static struct ctf_trace
*ctf_trace_create(struct relay_session
*session
,
82 struct ctf_trace
*trace
;
84 trace
= zmalloc(sizeof(*trace
));
86 PERROR("Failed to allocate ctf_trace");
89 urcu_ref_init(&trace
->ref
);
91 if (!session_get(session
)) {
92 ERR("Failed to acquire session reference");
95 trace
->session
= session
;
96 trace
->path
= strdup(subpath
);
101 CDS_INIT_LIST_HEAD(&trace
->stream_list
);
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
);
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
);
113 DBG("Created ctf_trace %" PRIu64
"of session \"%s\" from host \"%s\" with path: %s",
114 trace
->id
, session
->session_name
, session
->hostname
,
120 ctf_trace_put(trace
);
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
129 struct ctf_trace
*ctf_trace_get_by_path_or_create(struct relay_session
*session
,
132 struct lttng_ht_node_str
*node
;
133 struct lttng_ht_iter iter
;
134 struct ctf_trace
*trace
= NULL
;
137 lttng_ht_lookup(session
->ctf_traces_ht
, subpath
, &iter
);
138 node
= lttng_ht_iter_get_node_str(&iter
);
140 DBG("CTF Trace path %s not found", subpath
);
143 trace
= caa_container_of(node
, struct ctf_trace
, node
);
144 if (!ctf_trace_get(trace
)) {
151 trace
= ctf_trace_create(session
, subpath
);
156 void ctf_trace_put(struct ctf_trace
*trace
)
159 urcu_ref_put(&trace
->ref
, ctf_trace_release
);
163 int ctf_trace_close(struct ctf_trace
*trace
)
165 struct relay_stream
*stream
;
168 cds_list_for_each_entry_rcu(stream
, &trace
->stream_list
,
171 * Close stream since the connection owning the trace is being
174 try_stream_close(stream
);
178 * Since all references to the trace are held by its streams, we
179 * don't need to do any self-ref put.
184 struct relay_viewer_stream
*ctf_trace_get_viewer_metadata_stream(struct ctf_trace
*trace
)
186 struct relay_viewer_stream
*vstream
;
189 vstream
= rcu_dereference(trace
->viewer_metadata_stream
);
193 if (!viewer_stream_get(vstream
)) {