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