Fix: relayd: session trace chunk is copied too late
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.c
CommitLineData
d3e2ba59
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
7591bab1 4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
d3e2ba59
JD
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
6c1c0768 20#define _LGPL_SOURCE
d3e2ba59
JD
21#include <assert.h>
22
23#include <common/common.h>
24#include <common/utils.h>
7591bab1 25#include <urcu/rculist.h>
d3e2ba59
JD
26
27#include "ctf-trace.h"
2a174661
DG
28#include "lttng-relayd.h"
29#include "stream.h"
d3e2ba59
JD
30
31static uint64_t last_relay_ctf_trace_id;
7591bab1 32static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
d3e2ba59 33
7591bab1 34static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
2a174661 35{
7591bab1
MD
36 struct ctf_trace *trace =
37 caa_container_of(rcu_head, struct ctf_trace, rcu_node);
2a174661
DG
38
39 free(trace);
40}
41
d3e2ba59 42/*
2a174661
DG
43 * Destroy a ctf trace and all stream contained in it.
44 *
45 * MUST be called with the RCU read side lock.
d3e2ba59 46 */
7591bab1 47void ctf_trace_destroy(struct ctf_trace *trace)
d3e2ba59 48{
2a174661 49 /*
7591bab1
MD
50 * Getting to this point, every stream referenced by that trace
51 * have put back their ref since the've been closed by the
52 * control side.
2a174661 53 */
7591bab1
MD
54 assert(cds_list_empty(&trace->stream_list));
55 session_put(trace->session);
56 trace->session = NULL;
348a81dc
JG
57 free(trace->path);
58 trace->path = NULL;
7591bab1
MD
59 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
60}
2a174661 61
7591bab1
MD
62void ctf_trace_release(struct urcu_ref *ref)
63{
64 struct ctf_trace *trace =
65 caa_container_of(ref, struct ctf_trace, ref);
66 int ret;
67 struct lttng_ht_iter iter;
d3e2ba59 68
7591bab1
MD
69 iter.iter.node = &trace->node.node;
70 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
71 assert(!ret);
72 ctf_trace_destroy(trace);
2a174661
DG
73}
74
7591bab1
MD
75/*
76 * Should be called with RCU read-side lock held.
77 */
78bool ctf_trace_get(struct ctf_trace *trace)
2a174661 79{
ce4d4083 80 return urcu_ref_get_unless_zero(&trace->ref);
d3e2ba59
JD
81}
82
83/*
7591bab1
MD
84 * Create and return an allocated ctf_trace. NULL on error.
85 * There is no "open" and "close" for a ctf_trace, but rather just a
86 * create and refcounting. Whenever all the streams belonging to a trace
87 * put their reference, its refcount drops to 0.
d3e2ba59 88 */
7591bab1 89static struct ctf_trace *ctf_trace_create(struct relay_session *session,
348a81dc 90 const char *subpath)
d3e2ba59 91{
7591bab1 92 struct ctf_trace *trace;
d3e2ba59 93
7591bab1
MD
94 trace = zmalloc(sizeof(*trace));
95 if (!trace) {
348a81dc
JG
96 PERROR("Failed to allocate ctf_trace");
97 goto end;
d3e2ba59 98 }
348a81dc 99 urcu_ref_init(&trace->ref);
d3e2ba59 100
7591bab1 101 if (!session_get(session)) {
348a81dc 102 ERR("Failed to acquire session reference");
7591bab1
MD
103 goto error;
104 }
105 trace->session = session;
348a81dc
JG
106 trace->path = strdup(subpath);
107 if (!trace->path) {
108 goto error;
109 }
7591bab1
MD
110
111 CDS_INIT_LIST_HEAD(&trace->stream_list);
112
113 pthread_mutex_lock(&last_relay_ctf_trace_id_lock);
114 trace->id = ++last_relay_ctf_trace_id;
115 pthread_mutex_unlock(&last_relay_ctf_trace_id_lock);
2a174661 116
348a81dc 117 lttng_ht_node_init_str(&trace->node, trace->path);
7591bab1 118 trace->session = session;
7591bab1 119 pthread_mutex_init(&trace->lock, NULL);
7591bab1
MD
120 pthread_mutex_init(&trace->stream_list_lock, NULL);
121 lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
2a174661 122
348a81dc
JG
123 DBG("Created ctf_trace %" PRIu64 "of session \"%s\" from host \"%s\" with path: %s",
124 trace->id, session->session_name, session->hostname,
125 subpath);
d3e2ba59 126
348a81dc 127end:
7591bab1 128 return trace;
348a81dc
JG
129error:
130 ctf_trace_put(trace);
131 return NULL;
d3e2ba59
JD
132}
133
134/*
7591bab1
MD
135 * Return a ctf_trace if found by id in the given hash table else NULL.
136 * Hold a reference on the ctf_trace, and must be paired with
137 * ctf_trace_put().
d3e2ba59 138 */
7591bab1 139struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
348a81dc 140 const char *subpath)
d3e2ba59 141{
2a174661 142 struct lttng_ht_node_str *node;
d3e2ba59 143 struct lttng_ht_iter iter;
2a174661 144 struct ctf_trace *trace = NULL;
d3e2ba59 145
7591bab1 146 rcu_read_lock();
348a81dc 147 lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter);
2a174661
DG
148 node = lttng_ht_iter_get_node_str(&iter);
149 if (!node) {
348a81dc 150 DBG("CTF Trace path %s not found", subpath);
2a174661 151 goto end;
d3e2ba59 152 }
2a174661 153 trace = caa_container_of(node, struct ctf_trace, node);
7591bab1
MD
154 if (!ctf_trace_get(trace)) {
155 trace = NULL;
156 }
d3e2ba59 157end:
7591bab1
MD
158 rcu_read_unlock();
159 if (!trace) {
160 /* Try to create */
348a81dc 161 trace = ctf_trace_create(session, subpath);
7591bab1 162 }
2a174661 163 return trace;
d3e2ba59
JD
164}
165
7591bab1 166void ctf_trace_put(struct ctf_trace *trace)
2a174661 167{
7591bab1 168 rcu_read_lock();
7591bab1 169 urcu_ref_put(&trace->ref, ctf_trace_release);
7591bab1 170 rcu_read_unlock();
2a174661
DG
171}
172
7591bab1 173int ctf_trace_close(struct ctf_trace *trace)
2a174661 174{
7591bab1
MD
175 struct relay_stream *stream;
176
177 rcu_read_lock();
178 cds_list_for_each_entry_rcu(stream, &trace->stream_list,
179 stream_node) {
180 /*
bda7c7b9
JG
181 * Close stream since the connection owning the trace is being
182 * torn down.
7591bab1 183 */
bda7c7b9 184 try_stream_close(stream);
7591bab1
MD
185 }
186 rcu_read_unlock();
187 /*
188 * Since all references to the trace are held by its streams, we
189 * don't need to do any self-ref put.
190 */
191 return 0;
192}
2a174661 193
7591bab1
MD
194struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace)
195{
196 struct relay_viewer_stream *vstream;
2a174661 197
7591bab1
MD
198 rcu_read_lock();
199 vstream = rcu_dereference(trace->viewer_metadata_stream);
200 if (!vstream) {
201 goto end;
202 }
203 if (!viewer_stream_get(vstream)) {
204 vstream = NULL;
205 }
206end:
207 rcu_read_unlock();
208 return vstream;
2a174661 209}
This page took 0.048559 seconds and 4 git commands to generate.