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