Commit | Line | Data |
---|---|---|
2f8f53af DG |
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> |
2f8f53af DG |
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 |
2a174661 | 21 | #include <common/common.h> |
7591bab1 | 22 | #include <urcu/rculist.h> |
2a174661 | 23 | |
7591bab1 | 24 | #include "lttng-relayd.h" |
2a174661 | 25 | #include "ctf-trace.h" |
2f8f53af | 26 | #include "session.h" |
2a174661 | 27 | #include "stream.h" |
23c8ff50 | 28 | #include "sessiond-trace-chunks.h" |
2a174661 DG |
29 | |
30 | /* Global session id used in the session creation. */ | |
31 | static uint64_t last_relay_session_id; | |
7591bab1 | 32 | static pthread_mutex_t last_relay_session_id_lock = PTHREAD_MUTEX_INITIALIZER; |
2a174661 DG |
33 | |
34 | /* | |
35 | * Create a new session by assigning a new session ID. | |
36 | * | |
37 | * Return allocated session or else NULL. | |
38 | */ | |
7591bab1 MD |
39 | struct relay_session *session_create(const char *session_name, |
40 | const char *hostname, uint32_t live_timer, | |
23c8ff50 JG |
41 | bool snapshot, const lttng_uuid sessiond_uuid, |
42 | uint32_t major, uint32_t minor) | |
2a174661 | 43 | { |
23c8ff50 | 44 | int ret; |
2a174661 DG |
45 | struct relay_session *session; |
46 | ||
47 | session = zmalloc(sizeof(*session)); | |
48 | if (!session) { | |
49 | PERROR("relay session zmalloc"); | |
50 | goto error; | |
51 | } | |
bb5d54e7 MD |
52 | if (lttng_strncpy(session->session_name, session_name, |
53 | sizeof(session->session_name))) { | |
54 | goto error; | |
55 | } | |
56 | if (lttng_strncpy(session->hostname, hostname, | |
57 | sizeof(session->hostname))) { | |
58 | goto error; | |
59 | } | |
2a174661 DG |
60 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
61 | if (!session->ctf_traces_ht) { | |
2a174661 DG |
62 | goto error; |
63 | } | |
64 | ||
7591bab1 | 65 | pthread_mutex_lock(&last_relay_session_id_lock); |
2a174661 | 66 | session->id = ++last_relay_session_id; |
7591bab1 MD |
67 | pthread_mutex_unlock(&last_relay_session_id_lock); |
68 | ||
69 | session->major = major; | |
70 | session->minor = minor; | |
2a174661 | 71 | lttng_ht_node_init_u64(&session->session_n, session->id); |
7591bab1 MD |
72 | urcu_ref_init(&session->ref); |
73 | CDS_INIT_LIST_HEAD(&session->recv_list); | |
74 | pthread_mutex_init(&session->lock, NULL); | |
7591bab1 MD |
75 | pthread_mutex_init(&session->recv_list_lock, NULL); |
76 | ||
7591bab1 MD |
77 | session->live_timer = live_timer; |
78 | session->snapshot = snapshot; | |
23c8ff50 JG |
79 | lttng_uuid_copy(session->sessiond_uuid, sessiond_uuid); |
80 | ||
81 | ret = sessiond_trace_chunk_registry_session_created( | |
82 | sessiond_trace_chunk_registry, sessiond_uuid); | |
83 | if (ret) { | |
84 | goto error; | |
85 | } | |
7591bab1 MD |
86 | |
87 | lttng_ht_add_unique_u64(sessions_ht, &session->session_n); | |
bb5d54e7 | 88 | return session; |
2a174661 DG |
89 | |
90 | error: | |
bb5d54e7 MD |
91 | free(session); |
92 | return NULL; | |
2a174661 | 93 | } |
2f8f53af | 94 | |
7591bab1 MD |
95 | /* Should be called with RCU read-side lock held. */ |
96 | bool session_get(struct relay_session *session) | |
97 | { | |
ce4d4083 | 98 | return urcu_ref_get_unless_zero(&session->ref); |
7591bab1 MD |
99 | } |
100 | ||
2f8f53af | 101 | /* |
7591bab1 MD |
102 | * Lookup a session within the session hash table using the session id |
103 | * as key. A session reference is taken when a session is returned. | |
104 | * session_put() must be called on that session. | |
2f8f53af DG |
105 | * |
106 | * Return session or NULL if not found. | |
107 | */ | |
7591bab1 | 108 | struct relay_session *session_get_by_id(uint64_t id) |
2f8f53af DG |
109 | { |
110 | struct relay_session *session = NULL; | |
2a174661 | 111 | struct lttng_ht_node_u64 *node; |
2f8f53af DG |
112 | struct lttng_ht_iter iter; |
113 | ||
7591bab1 MD |
114 | rcu_read_lock(); |
115 | lttng_ht_lookup(sessions_ht, &id, &iter); | |
2a174661 | 116 | node = lttng_ht_iter_get_node_u64(&iter); |
2f8f53af | 117 | if (!node) { |
2a174661 | 118 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
2f8f53af DG |
119 | goto end; |
120 | } | |
121 | session = caa_container_of(node, struct relay_session, session_n); | |
2a174661 | 122 | DBG("Session find by ID %" PRIu64 " id found", id); |
7591bab1 MD |
123 | if (!session_get(session)) { |
124 | session = NULL; | |
125 | } | |
2f8f53af | 126 | end: |
7591bab1 | 127 | rcu_read_unlock(); |
2f8f53af DG |
128 | return session; |
129 | } | |
2a174661 | 130 | |
7591bab1 MD |
131 | static void rcu_destroy_session(struct rcu_head *rcu_head) |
132 | { | |
133 | struct relay_session *session = | |
134 | caa_container_of(rcu_head, struct relay_session, | |
135 | rcu_node); | |
49e614cb MD |
136 | /* |
137 | * Since each trace has a reference on the session, it means | |
138 | * that if we are at the point where we teardown the session, no | |
139 | * trace belonging to that session exist at this point. | |
140 | * Calling lttng_ht_destroy in call_rcu worker thread so we | |
141 | * don't hold the RCU read-side lock while calling it. | |
142 | */ | |
143 | lttng_ht_destroy(session->ctf_traces_ht); | |
7591bab1 MD |
144 | free(session); |
145 | } | |
146 | ||
2a174661 DG |
147 | /* |
148 | * Delete session from the given hash table. | |
149 | * | |
150 | * Return lttng ht del error code being 0 on success and 1 on failure. | |
151 | */ | |
7591bab1 | 152 | static int session_delete(struct relay_session *session) |
2a174661 DG |
153 | { |
154 | struct lttng_ht_iter iter; | |
155 | ||
2a174661 | 156 | iter.iter.node = &session->session_n.node; |
7591bab1 | 157 | return lttng_ht_del(sessions_ht, &iter); |
2a174661 DG |
158 | } |
159 | ||
7591bab1 MD |
160 | |
161 | static void destroy_session(struct relay_session *session) | |
162 | { | |
163 | int ret; | |
164 | ||
165 | ret = session_delete(session); | |
166 | assert(!ret); | |
23c8ff50 JG |
167 | ret = sessiond_trace_chunk_registry_session_destroyed( |
168 | sessiond_trace_chunk_registry, session->sessiond_uuid); | |
169 | assert(!ret); | |
7591bab1 MD |
170 | call_rcu(&session->rcu_node, rcu_destroy_session); |
171 | } | |
172 | ||
173 | void session_release(struct urcu_ref *ref) | |
2a174661 | 174 | { |
7591bab1 MD |
175 | struct relay_session *session = |
176 | caa_container_of(ref, struct relay_session, ref); | |
2a174661 | 177 | |
7591bab1 MD |
178 | destroy_session(session); |
179 | } | |
2a174661 | 180 | |
7591bab1 MD |
181 | void session_put(struct relay_session *session) |
182 | { | |
183 | rcu_read_lock(); | |
7591bab1 | 184 | urcu_ref_put(&session->ref, session_release); |
7591bab1 | 185 | rcu_read_unlock(); |
2a174661 DG |
186 | } |
187 | ||
7591bab1 | 188 | int session_close(struct relay_session *session) |
2a174661 DG |
189 | { |
190 | int ret = 0; | |
7591bab1 MD |
191 | struct ctf_trace *trace; |
192 | struct lttng_ht_iter iter; | |
193 | struct relay_stream *stream; | |
194 | ||
195 | pthread_mutex_lock(&session->lock); | |
196 | DBG("closing session %" PRIu64 ": is conn already closed %d", | |
197 | session->id, session->connection_closed); | |
7591bab1 | 198 | session->connection_closed = true; |
7591bab1 | 199 | pthread_mutex_unlock(&session->lock); |
2a174661 | 200 | |
7591bab1 MD |
201 | rcu_read_lock(); |
202 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, | |
203 | &iter.iter, trace, node.node) { | |
204 | ret = ctf_trace_close(trace); | |
205 | if (ret) { | |
206 | goto rcu_unlock; | |
2a174661 DG |
207 | } |
208 | } | |
7591bab1 MD |
209 | cds_list_for_each_entry_rcu(stream, &session->recv_list, |
210 | recv_node) { | |
bda7c7b9 JG |
211 | /* Close streams which have not been published yet. */ |
212 | try_stream_close(stream); | |
7591bab1 MD |
213 | } |
214 | rcu_unlock: | |
215 | rcu_read_unlock(); | |
216 | if (ret) { | |
217 | return ret; | |
218 | } | |
219 | /* Put self-reference from create. */ | |
220 | session_put(session); | |
221 | return ret; | |
2a174661 DG |
222 | } |
223 | ||
98ba050e JR |
224 | int session_abort(struct relay_session *session) |
225 | { | |
226 | int ret = 0; | |
227 | ||
228 | if (!session) { | |
229 | return 0; | |
230 | } | |
231 | ||
232 | pthread_mutex_lock(&session->lock); | |
233 | DBG("aborting session %" PRIu64, session->id); | |
98ba050e | 234 | session->aborted = true; |
98ba050e JR |
235 | pthread_mutex_unlock(&session->lock); |
236 | return ret; | |
237 | } | |
238 | ||
7591bab1 | 239 | void print_sessions(void) |
2a174661 | 240 | { |
2a174661 | 241 | struct lttng_ht_iter iter; |
7591bab1 | 242 | struct relay_session *session; |
2a174661 | 243 | |
ce3f3ba3 JG |
244 | if (!sessions_ht) { |
245 | return; | |
246 | } | |
247 | ||
2a174661 | 248 | rcu_read_lock(); |
7591bab1 MD |
249 | cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session, |
250 | session_n.node) { | |
251 | if (!session_get(session)) { | |
252 | continue; | |
253 | } | |
254 | DBG("session %p refcount %ld session %" PRIu64, | |
255 | session, | |
256 | session->ref.refcount, | |
257 | session->id); | |
258 | session_put(session); | |
2a174661 | 259 | } |
2a174661 | 260 | rcu_read_unlock(); |
2a174661 | 261 | } |