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
12 #include "ctf-trace.hpp"
13 #include "lttng-relayd.hpp"
16 #include <common/common.hpp>
17 #include <common/utils.hpp>
19 #include <urcu/rculist.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
= lttng::utils::container_of(rcu_head
, &ctf_trace::rcu_node
);
32 * Destroy a ctf trace and all stream contained in it.
34 * MUST be called with the RCU read side lock.
36 static void ctf_trace_destroy(struct ctf_trace
*trace
)
39 * Getting to this point, every stream referenced by that trace
40 * have put back their ref since the've been closed by the
43 LTTNG_ASSERT(cds_list_empty(&trace
->stream_list
));
44 ASSERT_RCU_READ_LOCKED();
46 session_put(trace
->session
);
47 trace
->session
= nullptr;
49 trace
->path
= nullptr;
50 call_rcu(&trace
->rcu_node
, rcu_destroy_ctf_trace
);
53 static void ctf_trace_release(struct urcu_ref
*ref
)
55 struct ctf_trace
*trace
= lttng::utils::container_of(ref
, &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 * The caller must either:
67 * - hold the RCU read side lock, or
68 * - guarantee the existence of the object by already holding a reference to
71 bool ctf_trace_get(struct ctf_trace
*trace
)
73 const bool ref
= urcu_ref_get_unless_zero(&trace
->ref
);
77 * The ref count is already zero. It means the object is being
78 * torn down concurently.
79 * This is only acceptable if we hold the RCU read-side lock,
80 * else it's a logic error.
82 ASSERT_RCU_READ_LOCKED();
89 * Create and return an allocated ctf_trace. NULL on error.
90 * There is no "open" and "close" for a ctf_trace, but rather just a
91 * create and refcounting. Whenever all the streams belonging to a trace
92 * put their reference, its refcount drops to 0.
94 static struct ctf_trace
*ctf_trace_create(struct relay_session
*session
, const char *subpath
)
96 struct ctf_trace
*trace
;
98 trace
= zmalloc
<ctf_trace
>();
100 PERROR("Failed to allocate ctf_trace");
103 urcu_ref_init(&trace
->ref
);
105 if (!session_get(session
)) {
106 ERR("Failed to acquire session reference");
109 trace
->session
= session
;
110 trace
->path
= strdup(subpath
);
115 CDS_INIT_LIST_HEAD(&trace
->stream_list
);
117 pthread_mutex_lock(&last_relay_ctf_trace_id_lock
);
118 trace
->id
= ++last_relay_ctf_trace_id
;
119 pthread_mutex_unlock(&last_relay_ctf_trace_id_lock
);
121 lttng_ht_node_init_str(&trace
->node
, trace
->path
);
122 trace
->session
= session
;
123 pthread_mutex_init(&trace
->lock
, nullptr);
124 pthread_mutex_init(&trace
->stream_list_lock
, nullptr);
125 lttng_ht_add_str(session
->ctf_traces_ht
, &trace
->node
);
127 DBG("Created ctf_trace %" PRIu64
" of session \"%s\" from host \"%s\" with path: %s",
129 session
->session_name
,
136 ctf_trace_put(trace
);
141 * Return a ctf_trace if found by id in the given hash table else NULL.
142 * Hold a reference on the ctf_trace, and must be paired with
145 struct ctf_trace
*ctf_trace_get_by_path_or_create(struct relay_session
*session
,
148 struct lttng_ht_node_str
*node
;
149 struct lttng_ht_iter iter
;
150 struct ctf_trace
*trace
= nullptr;
153 lttng_ht_lookup(session
->ctf_traces_ht
, subpath
, &iter
);
154 node
= lttng_ht_iter_get_node_str(&iter
);
156 DBG("CTF Trace path %s not found", subpath
);
159 trace
= lttng::utils::container_of(node
, &ctf_trace::node
);
160 if (!ctf_trace_get(trace
)) {
167 trace
= ctf_trace_create(session
, subpath
);
172 void ctf_trace_put(struct ctf_trace
*trace
)
175 urcu_ref_put(&trace
->ref
, ctf_trace_release
);
179 int ctf_trace_close(struct ctf_trace
*trace
)
181 struct relay_stream
*stream
;
184 cds_list_for_each_entry_rcu(stream
, &trace
->stream_list
, stream_node
)
187 * Close stream since the connection owning the trace is being
190 try_stream_close(stream
);
194 * Since all references to the trace are held by its streams, we
195 * don't need to do any self-ref put.
200 struct relay_viewer_stream
*ctf_trace_get_viewer_metadata_stream(struct ctf_trace
*trace
)
202 struct relay_viewer_stream
*vstream
;
205 vstream
= rcu_dereference(trace
->viewer_metadata_stream
);
209 if (!viewer_stream_get(vstream
)) {