Fix: Relay daemon ownership and reference counting
[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
20#define _GNU_SOURCE
6c1c0768 21#define _LGPL_SOURCE
d3e2ba59
JD
22#include <assert.h>
23
24#include <common/common.h>
25#include <common/utils.h>
7591bab1 26#include <urcu/rculist.h>
d3e2ba59
JD
27
28#include "ctf-trace.h"
2a174661
DG
29#include "lttng-relayd.h"
30#include "stream.h"
d3e2ba59
JD
31
32static uint64_t last_relay_ctf_trace_id;
7591bab1 33static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER;
d3e2ba59 34
7591bab1 35static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head)
2a174661 36{
7591bab1
MD
37 struct ctf_trace *trace =
38 caa_container_of(rcu_head, struct ctf_trace, rcu_node);
2a174661
DG
39
40 free(trace);
41}
42
d3e2ba59 43/*
2a174661
DG
44 * Destroy a ctf trace and all stream contained in it.
45 *
46 * MUST be called with the RCU read side lock.
d3e2ba59 47 */
7591bab1 48void ctf_trace_destroy(struct ctf_trace *trace)
d3e2ba59 49{
2a174661 50 /*
7591bab1
MD
51 * Getting to this point, every stream referenced by that trace
52 * have put back their ref since the've been closed by the
53 * control side.
2a174661 54 */
7591bab1
MD
55 assert(cds_list_empty(&trace->stream_list));
56 session_put(trace->session);
57 trace->session = NULL;
58 call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace);
59}
2a174661 60
7591bab1
MD
61void ctf_trace_release(struct urcu_ref *ref)
62{
63 struct ctf_trace *trace =
64 caa_container_of(ref, struct ctf_trace, ref);
65 int ret;
66 struct lttng_ht_iter iter;
d3e2ba59 67
7591bab1
MD
68 iter.iter.node = &trace->node.node;
69 ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter);
70 assert(!ret);
71 ctf_trace_destroy(trace);
2a174661
DG
72}
73
7591bab1
MD
74/*
75 * Should be called with RCU read-side lock held.
76 */
77bool ctf_trace_get(struct ctf_trace *trace)
2a174661 78{
7591bab1 79 bool has_ref = false;
2a174661 80
7591bab1
MD
81 /* Confirm that the trace refcount has not reached 0. */
82 pthread_mutex_lock(&trace->reflock);
83 if (trace->ref.refcount != 0) {
84 has_ref = true;
85 urcu_ref_get(&trace->ref);
d3e2ba59 86 }
7591bab1
MD
87 pthread_mutex_unlock(&trace->reflock);
88
89 return has_ref;
d3e2ba59
JD
90}
91
92/*
7591bab1
MD
93 * Create and return an allocated ctf_trace. NULL on error.
94 * There is no "open" and "close" for a ctf_trace, but rather just a
95 * create and refcounting. Whenever all the streams belonging to a trace
96 * put their reference, its refcount drops to 0.
d3e2ba59 97 */
7591bab1
MD
98static struct ctf_trace *ctf_trace_create(struct relay_session *session,
99 char *path_name)
d3e2ba59 100{
7591bab1 101 struct ctf_trace *trace;
d3e2ba59 102
7591bab1
MD
103 trace = zmalloc(sizeof(*trace));
104 if (!trace) {
d3e2ba59
JD
105 PERROR("ctf_trace alloc");
106 goto error;
107 }
108
7591bab1
MD
109 if (!session_get(session)) {
110 ERR("Cannot get session");
111 free(trace);
112 trace = NULL;
113 goto error;
114 }
115 trace->session = session;
116
117 CDS_INIT_LIST_HEAD(&trace->stream_list);
118
119 pthread_mutex_lock(&last_relay_ctf_trace_id_lock);
120 trace->id = ++last_relay_ctf_trace_id;
121 pthread_mutex_unlock(&last_relay_ctf_trace_id_lock);
2a174661 122
7591bab1
MD
123 lttng_ht_node_init_str(&trace->node, path_name);
124 trace->session = session;
125 urcu_ref_init(&trace->ref);
126 pthread_mutex_init(&trace->lock, NULL);
127 pthread_mutex_init(&trace->reflock, NULL);
128 pthread_mutex_init(&trace->stream_list_lock, NULL);
129 lttng_ht_add_str(session->ctf_traces_ht, &trace->node);
2a174661 130
7591bab1 131 DBG("Created ctf_trace %" PRIu64 " with path: %s", trace->id, path_name);
d3e2ba59
JD
132
133error:
7591bab1 134 return trace;
d3e2ba59
JD
135}
136
137/*
7591bab1
MD
138 * Return a ctf_trace if found by id in the given hash table else NULL.
139 * Hold a reference on the ctf_trace, and must be paired with
140 * ctf_trace_put().
d3e2ba59 141 */
7591bab1 142struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session,
2a174661 143 char *path_name)
d3e2ba59 144{
2a174661 145 struct lttng_ht_node_str *node;
d3e2ba59 146 struct lttng_ht_iter iter;
2a174661 147 struct ctf_trace *trace = NULL;
d3e2ba59 148
7591bab1
MD
149 rcu_read_lock();
150 lttng_ht_lookup(session->ctf_traces_ht, (void *) path_name, &iter);
2a174661
DG
151 node = lttng_ht_iter_get_node_str(&iter);
152 if (!node) {
153 DBG("CTF Trace path %s not found", path_name);
154 goto end;
d3e2ba59 155 }
2a174661 156 trace = caa_container_of(node, struct ctf_trace, node);
7591bab1
MD
157 if (!ctf_trace_get(trace)) {
158 trace = NULL;
159 }
d3e2ba59 160end:
7591bab1
MD
161 rcu_read_unlock();
162 if (!trace) {
163 /* Try to create */
164 trace = ctf_trace_create(session, path_name);
165 }
2a174661 166 return trace;
d3e2ba59
JD
167}
168
7591bab1 169void ctf_trace_put(struct ctf_trace *trace)
2a174661 170{
7591bab1
MD
171 rcu_read_lock();
172 pthread_mutex_lock(&trace->reflock);
173 urcu_ref_put(&trace->ref, ctf_trace_release);
174 pthread_mutex_unlock(&trace->reflock);
175 rcu_read_unlock();
2a174661
DG
176}
177
7591bab1 178int ctf_trace_close(struct ctf_trace *trace)
2a174661 179{
7591bab1
MD
180 struct relay_stream *stream;
181
182 rcu_read_lock();
183 cds_list_for_each_entry_rcu(stream, &trace->stream_list,
184 stream_node) {
185 /*
186 * Close the stream.
187 */
188 stream_close(stream);
189 }
190 rcu_read_unlock();
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
7591bab1
MD
202 rcu_read_lock();
203 vstream = rcu_dereference(trace->viewer_metadata_stream);
204 if (!vstream) {
205 goto end;
206 }
207 if (!viewer_stream_get(vstream)) {
208 vstream = NULL;
209 }
210end:
211 rcu_read_unlock();
212 return vstream;
2a174661 213}
This page took 0.042616 seconds and 5 git commands to generate.