7fffea8af4d29145e97cb320786cc0265531106b
[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
50 session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
51 if (!session->ctf_traces_ht) {
52 free(session);
53 session = NULL;
54 goto error;
55 }
56
57 pthread_mutex_lock(&last_relay_session_id_lock);
58 session->id = ++last_relay_session_id;
59 pthread_mutex_unlock(&last_relay_session_id_lock);
60
61 session->major = major;
62 session->minor = minor;
63 lttng_ht_node_init_u64(&session->session_n, session->id);
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);
78
79 error:
80 return session;
81 }
82
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
98 /*
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.
102 *
103 * Return session or NULL if not found.
104 */
105 struct relay_session *session_get_by_id(uint64_t id)
106 {
107 struct relay_session *session = NULL;
108 struct lttng_ht_node_u64 *node;
109 struct lttng_ht_iter iter;
110
111 rcu_read_lock();
112 lttng_ht_lookup(sessions_ht, &id, &iter);
113 node = lttng_ht_iter_get_node_u64(&iter);
114 if (!node) {
115 DBG("Session find by ID %" PRIu64 " id NOT found", id);
116 goto end;
117 }
118 session = caa_container_of(node, struct relay_session, session_n);
119 DBG("Session find by ID %" PRIu64 " id found", id);
120 if (!session_get(session)) {
121 session = NULL;
122 }
123 end:
124 rcu_read_unlock();
125 return session;
126 }
127
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 * Since each trace has a reference on the session, it means
135 * that if we are at the point where we teardown the session, no
136 * trace belonging to that session exist at this point.
137 * Calling lttng_ht_destroy in call_rcu worker thread so we
138 * don't hold the RCU read-side lock while calling it.
139 */
140 lttng_ht_destroy(session->ctf_traces_ht);
141 free(session);
142 }
143
144 /*
145 * Delete session from the given hash table.
146 *
147 * Return lttng ht del error code being 0 on success and 1 on failure.
148 */
149 static int session_delete(struct relay_session *session)
150 {
151 struct lttng_ht_iter iter;
152
153 iter.iter.node = &session->session_n.node;
154 return lttng_ht_del(sessions_ht, &iter);
155 }
156
157
158 static void destroy_session(struct relay_session *session)
159 {
160 int ret;
161
162 ret = session_delete(session);
163 assert(!ret);
164 call_rcu(&session->rcu_node, rcu_destroy_session);
165 }
166
167 void session_release(struct urcu_ref *ref)
168 {
169 struct relay_session *session =
170 caa_container_of(ref, struct relay_session, ref);
171
172 destroy_session(session);
173 }
174
175 void session_put(struct relay_session *session)
176 {
177 rcu_read_lock();
178 pthread_mutex_lock(&session->reflock);
179 urcu_ref_put(&session->ref, session_release);
180 pthread_mutex_unlock(&session->reflock);
181 rcu_read_unlock();
182 }
183
184 int session_close(struct relay_session *session)
185 {
186 int ret = 0;
187 struct ctf_trace *trace;
188 struct lttng_ht_iter iter;
189 struct relay_stream *stream;
190
191 pthread_mutex_lock(&session->lock);
192 DBG("closing session %" PRIu64 ": is conn already closed %d",
193 session->id, session->connection_closed);
194 if (session->connection_closed) {
195 ret = -1;
196 goto unlock;
197 }
198 session->connection_closed = true;
199 unlock:
200 pthread_mutex_unlock(&session->lock);
201 if (ret) {
202 return ret;
203 }
204
205 rcu_read_lock();
206 cds_lfht_for_each_entry(session->ctf_traces_ht->ht,
207 &iter.iter, trace, node.node) {
208 ret = ctf_trace_close(trace);
209 if (ret) {
210 goto rcu_unlock;
211 }
212 }
213 cds_list_for_each_entry_rcu(stream, &session->recv_list,
214 recv_node) {
215 /* Close streams which have not been published yet. */
216 try_stream_close(stream);
217 }
218 rcu_unlock:
219 rcu_read_unlock();
220 if (ret) {
221 return ret;
222 }
223 /* Put self-reference from create. */
224 session_put(session);
225 return ret;
226 }
227
228 void print_sessions(void)
229 {
230 struct lttng_ht_iter iter;
231 struct relay_session *session;
232
233 rcu_read_lock();
234 cds_lfht_for_each_entry(sessions_ht->ht, &iter.iter, session,
235 session_n.node) {
236 if (!session_get(session)) {
237 continue;
238 }
239 DBG("session %p refcount %ld session %" PRIu64,
240 session,
241 session->ref.refcount,
242 session->id);
243 session_put(session);
244 }
245 rcu_read_unlock();
246 }
This page took 0.033316 seconds and 3 git commands to generate.