Tests: Add test to check shared-memory FD leaks after relayd dies
[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 16#include <common/common.hpp>
56047f5a 17#include <common/urcu.hpp>
28ab034a
JG
18#include <common/utils.hpp>
19
20#include <urcu/rculist.h>
21
d3e2ba59 22static uint64_t last_relay_ctf_trace_id;
7591bab1 23static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
d3e2ba59 24
7591bab1 25static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
2a174661 26{
28ab034a 27 struct ctf_trace *trace = lttng::utils::container_of(rcu_head, &ctf_trace::rcu_node);
2a174661
DG
28
29 free(trace);
30}
31
d3e2ba59 32/*
2a174661
DG
33 * Destroy a ctf trace and all stream contained in it.
34 *
35 * MUST be called with the RCU read side lock.
d3e2ba59 36 */
a1075e32 37static void ctf_trace_destroy(struct ctf_trace *trace)
d3e2ba59 38{
2a174661 39 /*
7591bab1
MD
40 * Getting to this point, every stream referenced by that trace
41 * have put back their ref since the've been closed by the
42 * control side.
2a174661 43 */
a0377dfe 44 LTTNG_ASSERT(cds_list_empty(&trace->stream_list));
48b7cdc2
FD
45 ASSERT_RCU_READ_LOCKED();
46
7591bab1 47 session_put(trace->session);
cd9adb8b 48 trace->session = nullptr;
348a81dc 49 free(trace->path);
cd9adb8b 50 trace->path = nullptr;
7591bab1
MD
51 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
52}
2a174661 53
a1075e32 54static void ctf_trace_release(struct urcu_ref *ref)
7591bab1 55{
28ab034a 56 struct ctf_trace *trace = lttng::utils::container_of(ref, &ctf_trace::ref);
7591bab1
MD
57 int ret;
58 struct lttng_ht_iter iter;
d3e2ba59 59
7591bab1
MD
60 iter.iter.node = &trace->node.node;
61 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
a0377dfe 62 LTTNG_ASSERT(!ret);
7591bab1 63 ctf_trace_destroy(trace);
2a174661
DG
64}
65
7591bab1 66/*
48b7cdc2
FD
67 * The caller must either:
68 * - hold the RCU read side lock, or
69 * - guarantee the existence of the object by already holding a reference to
70 * the object.
7591bab1
MD
71 */
72bool ctf_trace_get(struct ctf_trace *trace)
2a174661 73{
48b7cdc2
FD
74 const bool ref = urcu_ref_get_unless_zero(&trace->ref);
75
76 if (!ref) {
77 /*
78 * The ref count is already zero. It means the object is being
79 * torn down concurently.
80 * This is only acceptable if we hold the RCU read-side lock,
81 * else it's a logic error.
82 */
83 ASSERT_RCU_READ_LOCKED();
84 }
85
86 return ref;
d3e2ba59
JD
87}
88
89/*
7591bab1
MD
90 * Create and return an allocated ctf_trace. NULL on error.
91 * There is no "open" and "close" for a ctf_trace, but rather just a
92 * create and refcounting. Whenever all the streams belonging to a trace
93 * put their reference, its refcount drops to 0.
d3e2ba59 94 */
28ab034a 95static struct ctf_trace *ctf_trace_create(struct relay_session *session, const char *subpath)
d3e2ba59 96{
7591bab1 97 struct ctf_trace *trace;
d3e2ba59 98
64803277 99 trace = zmalloc<ctf_trace>();
7591bab1 100 if (!trace) {
348a81dc
JG
101 PERROR("Failed to allocate ctf_trace");
102 goto end;
d3e2ba59 103 }
348a81dc 104 urcu_ref_init(&trace->ref);
d3e2ba59 105
7591bab1 106 if (!session_get(session)) {
348a81dc 107 ERR("Failed to acquire session reference");
7591bab1
MD
108 goto error;
109 }
110 trace->session = session;
348a81dc
JG
111 trace->path = strdup(subpath);
112 if (!trace->path) {
113 goto error;
114 }
7591bab1
MD
115
116 CDS_INIT_LIST_HEAD(&trace->stream_list);
117
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);
2a174661 121
348a81dc 122 lttng_ht_node_init_str(&trace->node, trace->path);
7591bab1 123 trace->session = session;
cd9adb8b
JG
124 pthread_mutex_init(&trace->lock, nullptr);
125 pthread_mutex_init(&trace->stream_list_lock, nullptr);
7591bab1 126 lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
2a174661 127
9e0e2ff9 128 DBG("Created ctf_trace %" PRIu64 " of session \"%s\" from host \"%s\" with path: %s",
28ab034a
JG
129 trace->id,
130 session->session_name,
131 session->hostname,
132 subpath);
d3e2ba59 133
348a81dc 134end:
7591bab1 135 return trace;
348a81dc
JG
136error:
137 ctf_trace_put(trace);
cd9adb8b 138 return nullptr;
d3e2ba59
JD
139}
140
141/*
7591bab1
MD
142 * Return a ctf_trace if found by id in the given hash table else NULL.
143 * Hold a reference on the ctf_trace, and must be paired with
144 * ctf_trace_put().
d3e2ba59 145 */
7591bab1 146struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
28ab034a 147 const char *subpath)
d3e2ba59 148{
2a174661 149 struct lttng_ht_node_str *node;
d3e2ba59 150 struct lttng_ht_iter iter;
cd9adb8b 151 struct ctf_trace *trace = nullptr;
d3e2ba59 152
56047f5a 153 lttng::urcu::read_lock_guard read_lock;
348a81dc 154 lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter);
2a174661
DG
155 node = lttng_ht_iter_get_node_str(&iter);
156 if (!node) {
348a81dc 157 DBG("CTF Trace path %s not found", subpath);
2a174661 158 goto end;
d3e2ba59 159 }
0114db0e 160 trace = lttng::utils::container_of(node, &ctf_trace::node);
7591bab1 161 if (!ctf_trace_get(trace)) {
cd9adb8b 162 trace = nullptr;
7591bab1 163 }
d3e2ba59 164end:
7591bab1
MD
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{
56047f5a 174 lttng::urcu::read_lock_guard read_lock;
7591bab1 175 urcu_ref_put(&trace->ref, ctf_trace_release);
2a174661
DG
176}
177
7591bab1 178int ctf_trace_close(struct ctf_trace *trace)
2a174661 179{
7591bab1
MD
180 struct relay_stream *stream;
181
56047f5a 182 lttng::urcu::read_lock_guard read_lock;
28ab034a
JG
183 cds_list_for_each_entry_rcu(stream, &trace->stream_list, stream_node)
184 {
7591bab1 185 /*
bda7c7b9
JG
186 * Close stream since the connection owning the trace is being
187 * torn down.
7591bab1 188 */
bda7c7b9 189 try_stream_close(stream);
7591bab1 190 }
7591bab1
MD
191 /*
192 * Since all references to the trace are held by its streams, we
193 * don't need to do any self-ref put.
194 */
195 return 0;
196}
2a174661 197
7591bab1
MD
198struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace)
199{
200 struct relay_viewer_stream *vstream;
2a174661 201
56047f5a 202 lttng::urcu::read_lock_guard read_lock;
7591bab1
MD
203 vstream = rcu_dereference(trace->viewer_metadata_stream);
204 if (!vstream) {
205 goto end;
206 }
207 if (!viewer_stream_get(vstream)) {
cd9adb8b 208 vstream = nullptr;
7591bab1
MD
209 }
210end:
7591bab1 211 return vstream;
2a174661 212}
This page took 0.077515 seconds and 4 git commands to generate.