liblttng-ctl: use export list to define exported symbols
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.c
1 /*
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>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11
12 #include <common/common.h>
13 #include <common/utils.h>
14 #include <urcu/rculist.h>
15
16 #include "ctf-trace.h"
17 #include "lttng-relayd.h"
18 #include "stream.h"
19
20 static uint64_t last_relay_ctf_trace_id;
21 static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
22
23 static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
24 {
25 struct ctf_trace *trace =
26 caa_container_of(rcu_head, struct ctf_trace, rcu_node);
27
28 free(trace);
29 }
30
31 /*
32 * Destroy a ctf trace and all stream contained in it.
33 *
34 * MUST be called with the RCU read side lock.
35 */
36 static void ctf_trace_destroy(struct ctf_trace *trace)
37 {
38 /*
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.
42 */
43 LTTNG_ASSERT(cds_list_empty(&trace->stream_list));
44 session_put(trace->session);
45 trace->session = NULL;
46 free(trace->path);
47 trace->path = NULL;
48 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
49 }
50
51 static void ctf_trace_release(struct urcu_ref *ref)
52 {
53 struct ctf_trace *trace =
54 caa_container_of(ref, struct ctf_trace, ref);
55 int ret;
56 struct lttng_ht_iter iter;
57
58 iter.iter.node = &trace->node.node;
59 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
60 LTTNG_ASSERT(!ret);
61 ctf_trace_destroy(trace);
62 }
63
64 /*
65 * Should be called with RCU read-side lock held.
66 */
67 bool ctf_trace_get(struct ctf_trace *trace)
68 {
69 return urcu_ref_get_unless_zero(&trace->ref);
70 }
71
72 /*
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.
77 */
78 static struct ctf_trace *ctf_trace_create(struct relay_session *session,
79 const char *subpath)
80 {
81 struct ctf_trace *trace;
82
83 trace = zmalloc(sizeof(*trace));
84 if (!trace) {
85 PERROR("Failed to allocate ctf_trace");
86 goto end;
87 }
88 urcu_ref_init(&trace->ref);
89
90 if (!session_get(session)) {
91 ERR("Failed to acquire session reference");
92 goto error;
93 }
94 trace->session = session;
95 trace->path = strdup(subpath);
96 if (!trace->path) {
97 goto error;
98 }
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);
105
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);
111
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);
115
116 end:
117 return trace;
118 error:
119 ctf_trace_put(trace);
120 return NULL;
121 }
122
123 /*
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().
127 */
128 struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
129 const char *subpath)
130 {
131 struct lttng_ht_node_str *node;
132 struct lttng_ht_iter iter;
133 struct ctf_trace *trace = NULL;
134
135 rcu_read_lock();
136 lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter);
137 node = lttng_ht_iter_get_node_str(&iter);
138 if (!node) {
139 DBG("CTF Trace path %s not found", subpath);
140 goto end;
141 }
142 trace = caa_container_of(node, struct ctf_trace, node);
143 if (!ctf_trace_get(trace)) {
144 trace = NULL;
145 }
146 end:
147 rcu_read_unlock();
148 if (!trace) {
149 /* Try to create */
150 trace = ctf_trace_create(session, subpath);
151 }
152 return trace;
153 }
154
155 void ctf_trace_put(struct ctf_trace *trace)
156 {
157 rcu_read_lock();
158 urcu_ref_put(&trace->ref, ctf_trace_release);
159 rcu_read_unlock();
160 }
161
162 int ctf_trace_close(struct ctf_trace *trace)
163 {
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 /*
170 * Close stream since the connection owning the trace is being
171 * torn down.
172 */
173 try_stream_close(stream);
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 }
182
183 struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace)
184 {
185 struct relay_viewer_stream *vstream;
186
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 }
195 end:
196 rcu_read_unlock();
197 return vstream;
198 }
This page took 0.034318 seconds and 4 git commands to generate.