Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.c
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
JD
11
12#include <common/common.h>
13#include <common/utils.h>
7591bab1 14#include <urcu/rculist.h>
d3e2ba59
JD
15
16#include "ctf-trace.h"
2a174661
DG
17#include "lttng-relayd.h"
18#include "stream.h"
d3e2ba59
JD
19
20static uint64_t last_relay_ctf_trace_id;
7591bab1 21static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
d3e2ba59 22
7591bab1 23static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
2a174661 24{
7591bab1
MD
25 struct ctf_trace *trace =
26 caa_container_of(rcu_head, struct 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));
7591bab1
MD
44 session_put(trace->session);
45 trace->session = NULL;
348a81dc
JG
46 free(trace->path);
47 trace->path = NULL;
7591bab1
MD
48 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
49}
2a174661 50
a1075e32 51static void ctf_trace_release(struct urcu_ref *ref)
7591bab1
MD
52{
53 struct ctf_trace *trace =
54 caa_container_of(ref, struct ctf_trace, ref);
55 int ret;
56 struct lttng_ht_iter iter;
d3e2ba59 57
7591bab1
MD
58 iter.iter.node = &trace->node.node;
59 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
a0377dfe 60 LTTNG_ASSERT(!ret);
7591bab1 61 ctf_trace_destroy(trace);
2a174661
DG
62}
63
7591bab1
MD
64/*
65 * Should be called with RCU read-side lock held.
66 */
67bool ctf_trace_get(struct ctf_trace *trace)
2a174661 68{
ce4d4083 69 return urcu_ref_get_unless_zero(&trace->ref);
d3e2ba59
JD
70}
71
72/*
7591bab1
MD
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.
d3e2ba59 77 */
7591bab1 78static struct ctf_trace *ctf_trace_create(struct relay_session *session,
348a81dc 79 const char *subpath)
d3e2ba59 80{
7591bab1 81 struct ctf_trace *trace;
d3e2ba59 82
7591bab1
MD
83 trace = zmalloc(sizeof(*trace));
84 if (!trace) {
348a81dc
JG
85 PERROR("Failed to allocate ctf_trace");
86 goto end;
d3e2ba59 87 }
348a81dc 88 urcu_ref_init(&trace->ref);
d3e2ba59 89
7591bab1 90 if (!session_get(session)) {
348a81dc 91 ERR("Failed to acquire session reference");
7591bab1
MD
92 goto error;
93 }
94 trace->session = session;
348a81dc
JG
95 trace->path = strdup(subpath);
96 if (!trace->path) {
97 goto error;
98 }
7591bab1
MD
99
100 CDS_INIT_LIST_HEAD(&trace->stream_list);
101
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);
2a174661 105
348a81dc 106 lttng_ht_node_init_str(&trace->node, trace->path);
7591bab1 107 trace->session = session;
7591bab1 108 pthread_mutex_init(&trace->lock, NULL);
7591bab1
MD
109 pthread_mutex_init(&trace->stream_list_lock, NULL);
110 lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
2a174661 111
348a81dc
JG
112 DBG("Created ctf_trace %" PRIu64 "of session \"%s\" from host \"%s\" with path: %s",
113 trace->id, session->session_name, session->hostname,
114 subpath);
d3e2ba59 115
348a81dc 116end:
7591bab1 117 return trace;
348a81dc
JG
118error:
119 ctf_trace_put(trace);
120 return NULL;
d3e2ba59
JD
121}
122
123/*
7591bab1
MD
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
126 * ctf_trace_put().
d3e2ba59 127 */
7591bab1 128struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
348a81dc 129 const char *subpath)
d3e2ba59 130{
2a174661 131 struct lttng_ht_node_str *node;
d3e2ba59 132 struct lttng_ht_iter iter;
2a174661 133 struct ctf_trace *trace = NULL;
d3e2ba59 134
7591bab1 135 rcu_read_lock();
348a81dc 136 lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter);
2a174661
DG
137 node = lttng_ht_iter_get_node_str(&iter);
138 if (!node) {
348a81dc 139 DBG("CTF Trace path %s not found", subpath);
2a174661 140 goto end;
d3e2ba59 141 }
2a174661 142 trace = caa_container_of(node, struct ctf_trace, node);
7591bab1
MD
143 if (!ctf_trace_get(trace)) {
144 trace = NULL;
145 }
d3e2ba59 146end:
7591bab1
MD
147 rcu_read_unlock();
148 if (!trace) {
149 /* Try to create */
348a81dc 150 trace = ctf_trace_create(session, subpath);
7591bab1 151 }
2a174661 152 return trace;
d3e2ba59
JD
153}
154
7591bab1 155void ctf_trace_put(struct ctf_trace *trace)
2a174661 156{
7591bab1 157 rcu_read_lock();
7591bab1 158 urcu_ref_put(&trace->ref, ctf_trace_release);
7591bab1 159 rcu_read_unlock();
2a174661
DG
160}
161
7591bab1 162int ctf_trace_close(struct ctf_trace *trace)
2a174661 163{
7591bab1
MD
164 struct relay_stream *stream;
165
166 rcu_read_lock();
167 cds_list_for_each_entry_rcu(stream, &trace->stream_list,
168 stream_node) {
169 /*
bda7c7b9
JG
170 * Close stream since the connection owning the trace is being
171 * torn down.
7591bab1 172 */
bda7c7b9 173 try_stream_close(stream);
7591bab1
MD
174 }
175 rcu_read_unlock();
176 /*
177 * Since all references to the trace are held by its streams, we
178 * don't need to do any self-ref put.
179 */
180 return 0;
181}
2a174661 182
7591bab1
MD
183struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace)
184{
185 struct relay_viewer_stream *vstream;
2a174661 186
7591bab1
MD
187 rcu_read_lock();
188 vstream = rcu_dereference(trace->viewer_metadata_stream);
189 if (!vstream) {
190 goto end;
191 }
192 if (!viewer_stream_get(vstream)) {
193 vstream = NULL;
194 }
195end:
196 rcu_read_unlock();
197 return vstream;
2a174661 198}
This page took 0.055577 seconds and 4 git commands to generate.