Commit | Line | Data |
---|---|---|
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 | |
31 | static uint64_t last_relay_ctf_trace_id; | |
7591bab1 | 32 | static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER; |
d3e2ba59 | 33 | |
7591bab1 | 34 | static 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 | 47 | void 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; | |
57 | call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace); | |
58 | } | |
2a174661 | 59 | |
7591bab1 MD |
60 | void ctf_trace_release(struct urcu_ref *ref) |
61 | { | |
62 | struct ctf_trace *trace = | |
63 | caa_container_of(ref, struct ctf_trace, ref); | |
64 | int ret; | |
65 | struct lttng_ht_iter iter; | |
d3e2ba59 | 66 | |
7591bab1 MD |
67 | iter.iter.node = &trace->node.node; |
68 | ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter); | |
69 | assert(!ret); | |
70 | ctf_trace_destroy(trace); | |
2a174661 DG |
71 | } |
72 | ||
7591bab1 MD |
73 | /* |
74 | * Should be called with RCU read-side lock held. | |
75 | */ | |
76 | bool ctf_trace_get(struct ctf_trace *trace) | |
2a174661 | 77 | { |
ce4d4083 | 78 | return urcu_ref_get_unless_zero(&trace->ref); |
d3e2ba59 JD |
79 | } |
80 | ||
81 | /* | |
7591bab1 MD |
82 | * Create and return an allocated ctf_trace. NULL on error. |
83 | * There is no "open" and "close" for a ctf_trace, but rather just a | |
84 | * create and refcounting. Whenever all the streams belonging to a trace | |
85 | * put their reference, its refcount drops to 0. | |
d3e2ba59 | 86 | */ |
7591bab1 MD |
87 | static struct ctf_trace *ctf_trace_create(struct relay_session *session, |
88 | char *path_name) | |
d3e2ba59 | 89 | { |
7591bab1 | 90 | struct ctf_trace *trace; |
d3e2ba59 | 91 | |
7591bab1 MD |
92 | trace = zmalloc(sizeof(*trace)); |
93 | if (!trace) { | |
d3e2ba59 JD |
94 | PERROR("ctf_trace alloc"); |
95 | goto error; | |
96 | } | |
97 | ||
7591bab1 MD |
98 | if (!session_get(session)) { |
99 | ERR("Cannot get session"); | |
100 | free(trace); | |
101 | trace = NULL; | |
102 | goto error; | |
103 | } | |
104 | trace->session = session; | |
105 | ||
106 | CDS_INIT_LIST_HEAD(&trace->stream_list); | |
107 | ||
108 | pthread_mutex_lock(&last_relay_ctf_trace_id_lock); | |
109 | trace->id = ++last_relay_ctf_trace_id; | |
110 | pthread_mutex_unlock(&last_relay_ctf_trace_id_lock); | |
2a174661 | 111 | |
7591bab1 MD |
112 | lttng_ht_node_init_str(&trace->node, path_name); |
113 | trace->session = session; | |
114 | urcu_ref_init(&trace->ref); | |
115 | pthread_mutex_init(&trace->lock, NULL); | |
7591bab1 MD |
116 | pthread_mutex_init(&trace->stream_list_lock, NULL); |
117 | lttng_ht_add_str(session->ctf_traces_ht, &trace->node); | |
2a174661 | 118 | |
7591bab1 | 119 | DBG("Created ctf_trace %" PRIu64 " with path: %s", trace->id, path_name); |
d3e2ba59 JD |
120 | |
121 | error: | |
7591bab1 | 122 | return trace; |
d3e2ba59 JD |
123 | } |
124 | ||
125 | /* | |
7591bab1 MD |
126 | * Return a ctf_trace if found by id in the given hash table else NULL. |
127 | * Hold a reference on the ctf_trace, and must be paired with | |
128 | * ctf_trace_put(). | |
d3e2ba59 | 129 | */ |
7591bab1 | 130 | struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session, |
2a174661 | 131 | char *path_name) |
d3e2ba59 | 132 | { |
2a174661 | 133 | struct lttng_ht_node_str *node; |
d3e2ba59 | 134 | struct lttng_ht_iter iter; |
2a174661 | 135 | struct ctf_trace *trace = NULL; |
d3e2ba59 | 136 | |
7591bab1 MD |
137 | rcu_read_lock(); |
138 | lttng_ht_lookup(session->ctf_traces_ht, (void *) path_name, &iter); | |
2a174661 DG |
139 | node = lttng_ht_iter_get_node_str(&iter); |
140 | if (!node) { | |
141 | DBG("CTF Trace path %s not found", path_name); | |
142 | goto end; | |
d3e2ba59 | 143 | } |
2a174661 | 144 | trace = caa_container_of(node, struct ctf_trace, node); |
7591bab1 MD |
145 | if (!ctf_trace_get(trace)) { |
146 | trace = NULL; | |
147 | } | |
d3e2ba59 | 148 | end: |
7591bab1 MD |
149 | rcu_read_unlock(); |
150 | if (!trace) { | |
151 | /* Try to create */ | |
152 | trace = ctf_trace_create(session, path_name); | |
153 | } | |
2a174661 | 154 | return trace; |
d3e2ba59 JD |
155 | } |
156 | ||
7591bab1 | 157 | void ctf_trace_put(struct ctf_trace *trace) |
2a174661 | 158 | { |
7591bab1 | 159 | rcu_read_lock(); |
7591bab1 | 160 | urcu_ref_put(&trace->ref, ctf_trace_release); |
7591bab1 | 161 | rcu_read_unlock(); |
2a174661 DG |
162 | } |
163 | ||
7591bab1 | 164 | int ctf_trace_close(struct ctf_trace *trace) |
2a174661 | 165 | { |
7591bab1 MD |
166 | struct relay_stream *stream; |
167 | ||
168 | rcu_read_lock(); | |
169 | cds_list_for_each_entry_rcu(stream, &trace->stream_list, | |
170 | stream_node) { | |
171 | /* | |
bda7c7b9 JG |
172 | * Close stream since the connection owning the trace is being |
173 | * torn down. | |
7591bab1 | 174 | */ |
bda7c7b9 | 175 | try_stream_close(stream); |
7591bab1 MD |
176 | } |
177 | rcu_read_unlock(); | |
178 | /* | |
179 | * Since all references to the trace are held by its streams, we | |
180 | * don't need to do any self-ref put. | |
181 | */ | |
182 | return 0; | |
183 | } | |
2a174661 | 184 | |
7591bab1 MD |
185 | struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace) |
186 | { | |
187 | struct relay_viewer_stream *vstream; | |
2a174661 | 188 | |
7591bab1 MD |
189 | rcu_read_lock(); |
190 | vstream = rcu_dereference(trace->viewer_metadata_stream); | |
191 | if (!vstream) { | |
192 | goto end; | |
193 | } | |
194 | if (!viewer_stream_get(vstream)) { | |
195 | vstream = NULL; | |
196 | } | |
197 | end: | |
198 | rcu_read_unlock(); | |
199 | return vstream; | |
2a174661 | 200 | } |