Commit | Line | Data |
---|---|---|
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 | #include <assert.h> |
12 | ||
13 | #include <common/common.h> | |
14 | #include <common/utils.h> | |
7591bab1 | 15 | #include <urcu/rculist.h> |
d3e2ba59 JD |
16 | |
17 | #include "ctf-trace.h" | |
2a174661 DG |
18 | #include "lttng-relayd.h" |
19 | #include "stream.h" | |
d3e2ba59 JD |
20 | |
21 | static uint64_t last_relay_ctf_trace_id; | |
7591bab1 | 22 | static pthread_mutex_t last_relay_ctf_trace_id_lock = PTHREAD_MUTEX_INITIALIZER; |
d3e2ba59 | 23 | |
7591bab1 | 24 | static void rcu_destroy_ctf_trace(struct rcu_head *rcu_head) |
2a174661 | 25 | { |
7591bab1 MD |
26 | struct ctf_trace *trace = |
27 | caa_container_of(rcu_head, struct ctf_trace, rcu_node); | |
2a174661 DG |
28 | |
29 | free(trace); | |
30 | } | |
31 | ||
d3e2ba59 | 32 | /* |
2a174661 DG |
33 | * Destroy a ctf trace and all stream contained in it. |
34 | * | |
35 | * MUST be called with the RCU read side lock. | |
d3e2ba59 | 36 | */ |
a1075e32 | 37 | static void ctf_trace_destroy(struct ctf_trace *trace) |
d3e2ba59 | 38 | { |
2a174661 | 39 | /* |
7591bab1 MD |
40 | * Getting to this point, every stream referenced by that trace |
41 | * have put back their ref since the've been closed by the | |
42 | * control side. | |
2a174661 | 43 | */ |
7591bab1 MD |
44 | assert(cds_list_empty(&trace->stream_list)); |
45 | session_put(trace->session); | |
46 | trace->session = NULL; | |
348a81dc JG |
47 | free(trace->path); |
48 | trace->path = NULL; | |
7591bab1 MD |
49 | call_rcu(&trace->rcu_node, rcu_destroy_ctf_trace); |
50 | } | |
2a174661 | 51 | |
a1075e32 | 52 | static void ctf_trace_release(struct urcu_ref *ref) |
7591bab1 MD |
53 | { |
54 | struct ctf_trace *trace = | |
55 | caa_container_of(ref, struct ctf_trace, ref); | |
56 | int ret; | |
57 | struct lttng_ht_iter iter; | |
d3e2ba59 | 58 | |
7591bab1 MD |
59 | iter.iter.node = &trace->node.node; |
60 | ret = lttng_ht_del(trace->session->ctf_traces_ht, &iter); | |
61 | assert(!ret); | |
62 | ctf_trace_destroy(trace); | |
2a174661 DG |
63 | } |
64 | ||
7591bab1 MD |
65 | /* |
66 | * Should be called with RCU read-side lock held. | |
67 | */ | |
68 | bool ctf_trace_get(struct ctf_trace *trace) | |
2a174661 | 69 | { |
ce4d4083 | 70 | return urcu_ref_get_unless_zero(&trace->ref); |
d3e2ba59 JD |
71 | } |
72 | ||
73 | /* | |
7591bab1 MD |
74 | * Create and return an allocated ctf_trace. NULL on error. |
75 | * There is no "open" and "close" for a ctf_trace, but rather just a | |
76 | * create and refcounting. Whenever all the streams belonging to a trace | |
77 | * put their reference, its refcount drops to 0. | |
d3e2ba59 | 78 | */ |
7591bab1 | 79 | static struct ctf_trace *ctf_trace_create(struct relay_session *session, |
348a81dc | 80 | const char *subpath) |
d3e2ba59 | 81 | { |
7591bab1 | 82 | struct ctf_trace *trace; |
d3e2ba59 | 83 | |
7591bab1 MD |
84 | trace = zmalloc(sizeof(*trace)); |
85 | if (!trace) { | |
348a81dc JG |
86 | PERROR("Failed to allocate ctf_trace"); |
87 | goto end; | |
d3e2ba59 | 88 | } |
348a81dc | 89 | urcu_ref_init(&trace->ref); |
d3e2ba59 | 90 | |
7591bab1 | 91 | if (!session_get(session)) { |
348a81dc | 92 | ERR("Failed to acquire session reference"); |
7591bab1 MD |
93 | goto error; |
94 | } | |
95 | trace->session = session; | |
348a81dc JG |
96 | trace->path = strdup(subpath); |
97 | if (!trace->path) { | |
98 | goto error; | |
99 | } | |
7591bab1 MD |
100 | |
101 | CDS_INIT_LIST_HEAD(&trace->stream_list); | |
102 | ||
103 | pthread_mutex_lock(&last_relay_ctf_trace_id_lock); | |
104 | trace->id = ++last_relay_ctf_trace_id; | |
105 | pthread_mutex_unlock(&last_relay_ctf_trace_id_lock); | |
2a174661 | 106 | |
348a81dc | 107 | lttng_ht_node_init_str(&trace->node, trace->path); |
7591bab1 | 108 | trace->session = session; |
7591bab1 | 109 | pthread_mutex_init(&trace->lock, NULL); |
7591bab1 MD |
110 | pthread_mutex_init(&trace->stream_list_lock, NULL); |
111 | lttng_ht_add_str(session->ctf_traces_ht, &trace->node); | |
2a174661 | 112 | |
348a81dc JG |
113 | DBG("Created ctf_trace %" PRIu64 "of session \"%s\" from host \"%s\" with path: %s", |
114 | trace->id, session->session_name, session->hostname, | |
115 | subpath); | |
d3e2ba59 | 116 | |
348a81dc | 117 | end: |
7591bab1 | 118 | return trace; |
348a81dc JG |
119 | error: |
120 | ctf_trace_put(trace); | |
121 | return NULL; | |
d3e2ba59 JD |
122 | } |
123 | ||
124 | /* | |
7591bab1 MD |
125 | * Return a ctf_trace if found by id in the given hash table else NULL. |
126 | * Hold a reference on the ctf_trace, and must be paired with | |
127 | * ctf_trace_put(). | |
d3e2ba59 | 128 | */ |
7591bab1 | 129 | struct ctf_trace *ctf_trace_get_by_path_or_create(struct relay_session *session, |
348a81dc | 130 | const char *subpath) |
d3e2ba59 | 131 | { |
2a174661 | 132 | struct lttng_ht_node_str *node; |
d3e2ba59 | 133 | struct lttng_ht_iter iter; |
2a174661 | 134 | struct ctf_trace *trace = NULL; |
d3e2ba59 | 135 | |
7591bab1 | 136 | rcu_read_lock(); |
348a81dc | 137 | lttng_ht_lookup(session->ctf_traces_ht, subpath, &iter); |
2a174661 DG |
138 | node = lttng_ht_iter_get_node_str(&iter); |
139 | if (!node) { | |
348a81dc | 140 | DBG("CTF Trace path %s not found", subpath); |
2a174661 | 141 | goto end; |
d3e2ba59 | 142 | } |
2a174661 | 143 | trace = caa_container_of(node, struct ctf_trace, node); |
7591bab1 MD |
144 | if (!ctf_trace_get(trace)) { |
145 | trace = NULL; | |
146 | } | |
d3e2ba59 | 147 | end: |
7591bab1 MD |
148 | rcu_read_unlock(); |
149 | if (!trace) { | |
150 | /* Try to create */ | |
348a81dc | 151 | trace = ctf_trace_create(session, subpath); |
7591bab1 | 152 | } |
2a174661 | 153 | return trace; |
d3e2ba59 JD |
154 | } |
155 | ||
7591bab1 | 156 | void ctf_trace_put(struct ctf_trace *trace) |
2a174661 | 157 | { |
7591bab1 | 158 | rcu_read_lock(); |
7591bab1 | 159 | urcu_ref_put(&trace->ref, ctf_trace_release); |
7591bab1 | 160 | rcu_read_unlock(); |
2a174661 DG |
161 | } |
162 | ||
7591bab1 | 163 | int ctf_trace_close(struct ctf_trace *trace) |
2a174661 | 164 | { |
7591bab1 MD |
165 | struct relay_stream *stream; |
166 | ||
167 | rcu_read_lock(); | |
168 | cds_list_for_each_entry_rcu(stream, &trace->stream_list, | |
169 | stream_node) { | |
170 | /* | |
bda7c7b9 JG |
171 | * Close stream since the connection owning the trace is being |
172 | * torn down. | |
7591bab1 | 173 | */ |
bda7c7b9 | 174 | try_stream_close(stream); |
7591bab1 MD |
175 | } |
176 | rcu_read_unlock(); | |
177 | /* | |
178 | * Since all references to the trace are held by its streams, we | |
179 | * don't need to do any self-ref put. | |
180 | */ | |
181 | return 0; | |
182 | } | |
2a174661 | 183 | |
7591bab1 MD |
184 | struct relay_viewer_stream *ctf_trace_get_viewer_metadata_stream(struct ctf_trace *trace) |
185 | { | |
186 | struct relay_viewer_stream *vstream; | |
2a174661 | 187 | |
7591bab1 MD |
188 | rcu_read_lock(); |
189 | vstream = rcu_dereference(trace->viewer_metadata_stream); | |
190 | if (!vstream) { | |
191 | goto end; | |
192 | } | |
193 | if (!viewer_stream_get(vstream)) { | |
194 | vstream = NULL; | |
195 | } | |
196 | end: | |
197 | rcu_read_unlock(); | |
198 | return vstream; | |
2a174661 | 199 | } |