clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.cpp
CommitLineData
d3e2ba59 1/*
ab5be9fa
MJ
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>
d3e2ba59 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
d3e2ba59 7 *
d3e2ba59
JD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
d3e2ba59 11
c9e313bc
SM
12#include "ctf-trace.hpp"
13#include "lttng-relayd.hpp"
14#include "stream.hpp"
d3e2ba59 15
28ab034a
JG
16#include <common/common.hpp>
17#include <common/utils.hpp>
18
19#include <urcu/rculist.h>
20
d3e2ba59 21static uint64_t last_relay_ctf_trace_id;
7591bab1 22static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
d3e2ba59 23
7591bab1 24static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
2a174661 25{
28ab034a 26 struct ctf_trace *trace = lttng::utils::container_of(rcu_head, &ctf_trace::rcu_node);
2a174661
DG
27
28 free(trace);
29}
30
d3e2ba59 31/*
2a174661
DG
32 * Destroy a ctf trace and all stream contained in it.
33 *
34 * MUST be called with the RCU read side lock.
d3e2ba59 35 */
a1075e32 36static void ctf_trace_destroy(struct ctf_trace *trace)
d3e2ba59 37{
2a174661 38 /*
7591bab1
MD
39 * Getting to this point, every stream referenced by that trace
40 * have put back their ref since the've been closed by the
41 * control side.
2a174661 42 */
a0377dfe 43 LTTNG_ASSERT(cds_list_empty(&trace->stream_list));
48b7cdc2
FD
44 ASSERT_RCU_READ_LOCKED();
45
7591bab1 46 session_put(trace->session);
cd9adb8b 47 trace->session = nullptr;
348a81dc 48 free(trace->path);
cd9adb8b 49 trace->path = nullptr;
7591bab1
MD
50 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
51}
2a174661 52
a1075e32 53static void ctf_trace_release(struct urcu_ref *ref)
7591bab1 54{
28ab034a 55 struct ctf_trace *trace = lttng::utils::container_of(ref, &ctf_trace::ref);
7591bab1
MD
56 int ret;
57 struct lttng_ht_iter iter;
d3e2ba59 58
7591bab1
MD
59 iter.iter.node = &trace->node.node;
60 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
a0377dfe 61 LTTNG_ASSERT(!ret);
7591bab1 62 ctf_trace_destroy(trace);
2a174661
DG
63}
64
7591bab1 65/*
48b7cdc2
FD
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
69 * the object.
7591bab1
MD
70 */
71bool ctf_trace_get(struct ctf_trace *trace)
2a174661 72{
48b7cdc2
FD
73 const bool ref = urcu_ref_get_unless_zero(&trace->ref);
74
75 if (!ref) {
76 /*
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.
81 */
82 ASSERT_RCU_READ_LOCKED();
83 }
84
85 return ref;
d3e2ba59
JD
86}
87
88/*
7591bab1
MD
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.
d3e2ba59 93 */
28ab034a 94static struct ctf_trace *ctf_trace_create(struct relay_session *session, const char *subpath)
d3e2ba59 95{
7591bab1 96 struct ctf_trace *trace;
d3e2ba59 97
64803277 98 trace = zmalloc<ctf_trace>();
7591bab1 99 if (!trace) {
348a81dc
JG
100 PERROR("Failed to allocate ctf_trace");
101 goto end;
d3e2ba59 102 }
348a81dc 103 urcu_ref_init(&trace->ref);
d3e2ba59 104
7591bab1 105 if (!session_get(session)) {
348a81dc 106 ERR("Failed to acquire session reference");
7591bab1
MD
107 goto error;
108 }
109 trace->session = session;
348a81dc
JG
110 trace->path = strdup(subpath);
111 if (!trace->path) {
112 goto error;
113 }
7591bab1
MD
114
115 CDS_INIT_LIST_HEAD(&trace->stream_list);
116
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);
2a174661 120
348a81dc 121 lttng_ht_node_init_str(&trace->node, trace->path);
7591bab1 122 trace->session = session;
cd9adb8b
JG
123 pthread_mutex_init(&trace->lock, nullptr);
124 pthread_mutex_init(&trace->stream_list_lock, nullptr);
7591bab1 125 lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
2a174661 126
9e0e2ff9 127 DBG("Created ctf_trace %" PRIu64 " of session \"%s\" from host \"%s\" with path: %s",
28ab034a
JG
128 trace->id,
129 session->session_name,
130 session->hostname,
131 subpath);
d3e2ba59 132
348a81dc 133end:
7591bab1 134 return trace;
348a81dc
JG
135error:
136 ctf_trace_put(trace);
cd9adb8b 137 return nullptr;
d3e2ba59
JD
138}
139
140/*
7591bab1
MD
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
143 * ctf_trace_put().
d3e2ba59 144 */
7591bab1 145struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
28ab034a 146 const char *subpath)
d3e2ba59 147{
2a174661 148 struct lttng_ht_node_str *node;
d3e2ba59 149 struct lttng_ht_iter iter;
cd9adb8b 150 struct ctf_trace *trace = nullptr;
d3e2ba59 151
7591bab1 152 rcu_read_lock();
348a81dc 153 lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter);
2a174661
DG
154 node = lttng_ht_iter_get_node_str(&iter);
155 if (!node) {
348a81dc 156 DBG("CTF Trace path %s not found", subpath);
2a174661 157 goto end;
d3e2ba59 158 }
0114db0e 159 trace = lttng::utils::container_of(node, &ctf_trace::node);
7591bab1 160 if (!ctf_trace_get(trace)) {
cd9adb8b 161 trace = nullptr;
7591bab1 162 }
d3e2ba59 163end:
7591bab1
MD
164 rcu_read_unlock();
165 if (!trace) {
166 /* Try to create */
348a81dc 167 trace = ctf_trace_create(session, subpath);
7591bab1 168 }
2a174661 169 return trace;
d3e2ba59
JD
170}
171
7591bab1 172void ctf_trace_put(struct ctf_trace *trace)
2a174661 173{
7591bab1 174 rcu_read_lock();
7591bab1 175 urcu_ref_put(&trace->ref, ctf_trace_release);
7591bab1 176 rcu_read_unlock();
2a174661
DG
177}
178
7591bab1 179int ctf_trace_close(struct ctf_trace *trace)
2a174661 180{
7591bab1
MD
181 struct relay_stream *stream;
182
183 rcu_read_lock();
28ab034a
JG
184 cds_list_for_each_entry_rcu(stream, &trace->stream_list, stream_node)
185 {
7591bab1 186 /*
bda7c7b9
JG
187 * Close stream since the connection owning the trace is being
188 * torn down.
7591bab1 189 */
bda7c7b9 190 try_stream_close(stream);
7591bab1
MD
191 }
192 rcu_read_unlock();
193 /*
194 * Since all references to the trace are held by its streams, we
195 * don't need to do any self-ref put.
196 */
197 return 0;
198}
2a174661 199
7591bab1
MD
200struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace)
201{
202 struct relay_viewer_stream *vstream;
2a174661 203
7591bab1
MD
204 rcu_read_lock();
205 vstream = rcu_dereference(trace->viewer_metadata_stream);
206 if (!vstream) {
207 goto end;
208 }
209 if (!viewer_stream_get(vstream)) {
cd9adb8b 210 vstream = nullptr;
7591bab1
MD
211 }
212end:
213 rcu_read_unlock();
214 return vstream;
2a174661 215}
This page took 0.068043 seconds and 4 git commands to generate.