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 <common/common.h>
13 #include <common/utils.h>
14 #include <urcu/rculist.h>
16 #include "ctf-trace.h"
17 #include "lttng-relayd.h"
20 static uint64_t last_relay_ctf_trace_id
;
21 static pthread_mutex_t last_relay_ctf_trace_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
23 static void rcu_destroy_ctf_trace(struct rcu_head
*rcu_head
)
25 struct ctf_trace
*trace
=
26 caa_container_of(rcu_head
, struct 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 session_put(trace
->session
);
45 trace
->session
= NULL
;
48 call_rcu(&trace
->rcu_node
, rcu_destroy_ctf_trace
);
51 static void ctf_trace_release(struct urcu_ref
*ref
)
53 struct ctf_trace
*trace
=
54 caa_container_of(ref
, struct ctf_trace
, ref
);
56 struct lttng_ht_iter iter
;
58 iter
.iter
.node
= &trace
->node
.node
;
59 ret
= lttng_ht_del(trace
->session
->ctf_traces_ht
, &iter
);
61 ctf_trace_destroy(trace
);
65 * Should be called with RCU read-side lock held.
67 bool ctf_trace_get(struct ctf_trace
*trace
)
69 return urcu_ref_get_unless_zero(&trace
->ref
);
73 * Create and return an allocated ctf_trace. NULL on error.
74 * There is no "open" and "close" for a ctf_trace, but rather just a
75 * create and refcounting. Whenever all the streams belonging to a trace
76 * put their reference, its refcount drops to 0.
78 static struct ctf_trace
*ctf_trace_create(struct relay_session
*session
,
81 struct ctf_trace
*trace
;
83 trace
= zmalloc(sizeof(*trace
));
85 PERROR("Failed to allocate ctf_trace");
88 urcu_ref_init(&trace
->ref
);
90 if (!session_get(session
)) {
91 ERR("Failed to acquire session reference");
94 trace
->session
= session
;
95 trace
->path
= strdup(subpath
);
100 CDS_INIT_LIST_HEAD(&trace
->stream_list
);
102 pthread_mutex_lock(&last_relay_ctf_trace_id_lock
);
103 trace
->id
= ++last_relay_ctf_trace_id
;
104 pthread_mutex_unlock(&last_relay_ctf_trace_id_lock
);
106 lttng_ht_node_init_str(&trace
->node
, trace
->path
);
107 trace
->session
= session
;
108 pthread_mutex_init(&trace
->lock
, NULL
);
109 pthread_mutex_init(&trace
->stream_list_lock
, NULL
);
110 lttng_ht_add_str(session
->ctf_traces_ht
, &trace
->node
);
112 DBG("Created ctf_trace %" PRIu64
"of session \"%s\" from host \"%s\" with path: %s",
113 trace
->id
, session
->session_name
, session
->hostname
,
119 ctf_trace_put(trace
);
124 * Return a ctf_trace if found by id in the given hash table else NULL.
125 * Hold a reference on the ctf_trace, and must be paired with
128 struct ctf_trace
*ctf_trace_get_by_path_or_create(struct relay_session
*session
,
131 struct lttng_ht_node_str
*node
;
132 struct lttng_ht_iter iter
;
133 struct ctf_trace
*trace
= NULL
;
136 lttng_ht_lookup(session
->ctf_traces_ht
, subpath
, &iter
);
137 node
= lttng_ht_iter_get_node_str(&iter
);
139 DBG("CTF Trace path %s not found", subpath
);
142 trace
= caa_container_of(node
, struct ctf_trace
, node
);
143 if (!ctf_trace_get(trace
)) {
150 trace
= ctf_trace_create(session
, subpath
);
155 void ctf_trace_put(struct ctf_trace
*trace
)
158 urcu_ref_put(&trace
->ref
, ctf_trace_release
);
162 int ctf_trace_close(struct ctf_trace
*trace
)
164 struct relay_stream
*stream
;
167 cds_list_for_each_entry_rcu(stream
, &trace
->stream_list
,
170 * Close stream since the connection owning the trace is being
173 try_stream_close(stream
);
177 * Since all references to the trace are held by its streams, we
178 * don't need to do any self-ref put.
183 struct relay_viewer_stream
*ctf_trace_get_viewer_metadata_stream(struct ctf_trace
*trace
)
185 struct relay_viewer_stream
*vstream
;
188 vstream
= rcu_dereference(trace
->viewer_metadata_stream
);
192 if (!viewer_stream_get(vstream
)) {