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