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"
27 #include "viewer-session.h"
28 #include "viewer-stream.h"
31 struct relay_viewer_session
*viewer_session_create(void)
33 struct relay_viewer_session
*vsession
;
35 vsession
= zmalloc(sizeof(*vsession
));
39 CDS_INIT_LIST_HEAD(&vsession
->session_list
);
44 int viewer_session_set_trace_chunk_copy(struct relay_viewer_session
*vsession
,
45 struct lttng_trace_chunk
*relay_session_trace_chunk
)
48 struct lttng_trace_chunk
*viewer_chunk
;
50 assert(relay_session_trace_chunk
);
51 assert(!vsession
->current_trace_chunk
);
53 DBG("Copying relay session's current trace chunk to the viewer session");
54 viewer_chunk
= lttng_trace_chunk_copy(relay_session_trace_chunk
);
56 ERR("Failed to create a viewer trace chunk from the relay session's current chunk");
61 vsession
->current_trace_chunk
= viewer_chunk
;
66 /* The existence of session must be guaranteed by the caller. */
67 enum lttng_viewer_attach_return_code
viewer_session_attach(
68 struct relay_viewer_session
*vsession
,
69 struct relay_session
*session
)
71 enum lttng_viewer_attach_return_code viewer_attach_status
=
72 LTTNG_VIEWER_ATTACH_OK
;
74 ASSERT_LOCKED(session
->lock
);
76 /* Will not fail, as per the ownership guarantee. */
77 if (!session_get(session
)) {
78 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
81 if (session
->viewer_attached
) {
82 viewer_attach_status
= LTTNG_VIEWER_ATTACH_ALREADY
;
86 assert(session
->current_trace_chunk
);
87 assert(!vsession
->current_trace_chunk
);
88 session
->viewer_attached
= true;
90 ret
= viewer_session_set_trace_chunk_copy(vsession
,
91 session
->current_trace_chunk
);
94 * The live protocol does not define a generic error
95 * value for the "attach" command. The "unknown"
96 * status is used so that the viewer may handle this
97 * failure as if the session didn't exist anymore.
99 DBG("Failed to create a viewer trace chunk from the current trace chunk of session \"%s\", returning LTTNG_VIEWER_ATTACH_UNK",
100 session
->session_name
);
101 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
105 if (viewer_attach_status
== LTTNG_VIEWER_ATTACH_OK
) {
106 pthread_mutex_lock(&vsession
->session_list_lock
);
107 /* Ownership is transfered to the list. */
108 cds_list_add_rcu(&session
->viewer_session_node
,
109 &vsession
->session_list
);
110 pthread_mutex_unlock(&vsession
->session_list_lock
);
112 /* Put our local ref. */
113 session_put(session
);
116 return viewer_attach_status
;
119 /* The existence of session must be guaranteed by the caller. */
120 static int viewer_session_detach(struct relay_viewer_session
*vsession
,
121 struct relay_session
*session
)
125 pthread_mutex_lock(&session
->lock
);
126 if (!session
->viewer_attached
) {
129 session
->viewer_attached
= false;
133 pthread_mutex_lock(&vsession
->session_list_lock
);
134 cds_list_del_rcu(&session
->viewer_session_node
);
135 pthread_mutex_unlock(&vsession
->session_list_lock
);
136 /* Release reference held by the list. */
137 session_put(session
);
139 /* Safe since we know the session exists. */
140 pthread_mutex_unlock(&session
->lock
);
144 void viewer_session_destroy(struct relay_viewer_session
*vsession
)
146 lttng_trace_chunk_put(vsession
->current_trace_chunk
);
151 * Release ownership of all the streams of one session and detach the viewer.
153 void viewer_session_close_one_session(struct relay_viewer_session
*vsession
,
154 struct relay_session
*session
)
156 struct lttng_ht_iter iter
;
157 struct relay_viewer_stream
*vstream
;
160 * TODO: improvement: create more efficient list of
161 * vstream per session.
163 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
,
164 vstream
, stream_n
.node
) {
165 if (!viewer_stream_get(vstream
)) {
168 if (vstream
->stream
->trace
->session
!= session
) {
169 viewer_stream_put(vstream
);
172 /* Put local reference. */
173 viewer_stream_put(vstream
);
175 * We have reached one of the viewer stream's lifetime
176 * end condition. This "put" will cause the proper
177 * teardown of the viewer stream.
179 viewer_stream_put(vstream
);
181 lttng_trace_chunk_put(vsession
->current_trace_chunk
);
182 vsession
->current_trace_chunk
= NULL
;
183 viewer_session_detach(vsession
, session
);
186 void viewer_session_close(struct relay_viewer_session
*vsession
)
188 struct relay_session
*session
;
191 cds_list_for_each_entry_rcu(session
,
192 &vsession
->session_list
, viewer_session_node
) {
193 viewer_session_close_one_session(vsession
, session
);
199 * Check if a connection is attached to a session.
200 * Return 1 if attached, 0 if not attached, a negative value on error.
202 int viewer_session_is_attached(struct relay_viewer_session
*vsession
,
203 struct relay_session
*session
)
205 struct relay_session
*iter
;
208 pthread_mutex_lock(&session
->lock
);
212 if (!session
->viewer_attached
) {
216 cds_list_for_each_entry_rcu(iter
,
217 &vsession
->session_list
,
218 viewer_session_node
) {
219 if (session
== iter
) {
227 pthread_mutex_unlock(&session
->lock
);