2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
21 #include <common/common.h>
22 #include <urcu/rculist.h>
24 #include "lttng-relayd.h"
25 #include "ctf-trace.h"
28 #include "sessiond-trace-chunks.h"
30 /* Global session id used in the session creation. */
31 static uint64_t last_relay_session_id
;
32 static pthread_mutex_t last_relay_session_id_lock
= PTHREAD_MUTEX_INITIALIZER
;
35 * Create a new session by assigning a new session ID.
37 * Return allocated session or else NULL.
39 struct relay_session
*session_create(const char *session_name
,
40 const char *hostname
, uint32_t live_timer
,
41 bool snapshot
, const lttng_uuid sessiond_uuid
,
42 uint32_t major
, uint32_t minor
)
45 struct relay_session
*session
;
47 session
= zmalloc(sizeof(*session
));
49 PERROR("relay session zmalloc");
52 if (lttng_strncpy(session
->session_name
, session_name
,
53 sizeof(session
->session_name
))) {
56 if (lttng_strncpy(session
->hostname
, hostname
,
57 sizeof(session
->hostname
))) {
60 session
->ctf_traces_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
61 if (!session
->ctf_traces_ht
) {
65 pthread_mutex_lock(&last_relay_session_id_lock
);
66 session
->id
= ++last_relay_session_id
;
67 pthread_mutex_unlock(&last_relay_session_id_lock
);
69 session
->major
= major
;
70 session
->minor
= minor
;
71 lttng_ht_node_init_u64(&session
->session_n
, session
->id
);
72 urcu_ref_init(&session
->ref
);
73 CDS_INIT_LIST_HEAD(&session
->recv_list
);
74 pthread_mutex_init(&session
->lock
, NULL
);
75 pthread_mutex_init(&session
->recv_list_lock
, NULL
);
77 session
->live_timer
= live_timer
;
78 session
->snapshot
= snapshot
;
79 lttng_uuid_copy(session
->sessiond_uuid
, sessiond_uuid
);
81 ret
= sessiond_trace_chunk_registry_session_created(
82 sessiond_trace_chunk_registry
, sessiond_uuid
);
87 lttng_ht_add_unique_u64(sessions_ht
, &session
->session_n
);
95 /* Should be called with RCU read-side lock held. */
96 bool session_get(struct relay_session
*session
)
98 return urcu_ref_get_unless_zero(&session
->ref
);
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.
106 * Return session or NULL if not found.
108 struct relay_session
*session_get_by_id(uint64_t id
)
110 struct relay_session
*session
= NULL
;
111 struct lttng_ht_node_u64
*node
;
112 struct lttng_ht_iter iter
;
115 lttng_ht_lookup(sessions_ht
, &id
, &iter
);
116 node
= lttng_ht_iter_get_node_u64(&iter
);
118 DBG("Session find by ID %" PRIu64
" id NOT found", id
);
121 session
= caa_container_of(node
, struct relay_session
, session_n
);
122 DBG("Session find by ID %" PRIu64
" id found", id
);
123 if (!session_get(session
)) {
131 static void rcu_destroy_session(struct rcu_head
*rcu_head
)
133 struct relay_session
*session
=
134 caa_container_of(rcu_head
, struct relay_session
,
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.
143 lttng_ht_destroy(session
->ctf_traces_ht
);
148 * Delete session from the given hash table.
150 * Return lttng ht del error code being 0 on success and 1 on failure.
152 static int session_delete(struct relay_session
*session
)
154 struct lttng_ht_iter iter
;
156 iter
.iter
.node
= &session
->session_n
.node
;
157 return lttng_ht_del(sessions_ht
, &iter
);
161 static void destroy_session(struct relay_session
*session
)
165 ret
= session_delete(session
);
167 lttng_trace_chunk_put(session
->current_trace_chunk
);
168 ret
= sessiond_trace_chunk_registry_session_destroyed(
169 sessiond_trace_chunk_registry
, session
->sessiond_uuid
);
171 lttng_trace_chunk_put(session
->current_trace_chunk
);
172 session
->current_trace_chunk
= NULL
;
173 call_rcu(&session
->rcu_node
, rcu_destroy_session
);
176 void session_release(struct urcu_ref
*ref
)
178 struct relay_session
*session
=
179 caa_container_of(ref
, struct relay_session
, ref
);
181 destroy_session(session
);
184 void session_put(struct relay_session
*session
)
187 urcu_ref_put(&session
->ref
, session_release
);
191 int session_close(struct relay_session
*session
)
194 struct ctf_trace
*trace
;
195 struct lttng_ht_iter iter
;
196 struct relay_stream
*stream
;
198 pthread_mutex_lock(&session
->lock
);
199 DBG("closing session %" PRIu64
": is conn already closed %d",
200 session
->id
, session
->connection_closed
);
201 session
->connection_closed
= true;
202 pthread_mutex_unlock(&session
->lock
);
205 cds_lfht_for_each_entry(session
->ctf_traces_ht
->ht
,
206 &iter
.iter
, trace
, node
.node
) {
207 ret
= ctf_trace_close(trace
);
212 cds_list_for_each_entry_rcu(stream
, &session
->recv_list
,
214 /* Close streams which have not been published yet. */
215 try_stream_close(stream
);
222 /* Put self-reference from create. */
223 session_put(session
);
227 int session_abort(struct relay_session
*session
)
235 pthread_mutex_lock(&session
->lock
);
236 DBG("aborting session %" PRIu64
, session
->id
);
237 session
->aborted
= true;
238 pthread_mutex_unlock(&session
->lock
);
242 void print_sessions(void)
244 struct lttng_ht_iter iter
;
245 struct relay_session
*session
;
252 cds_lfht_for_each_entry(sessions_ht
->ht
, &iter
.iter
, session
,
254 if (!session_get(session
)) {
257 DBG("session %p refcount %ld session %" PRIu64
,
259 session
->ref
.refcount
,
261 session_put(session
);