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 /* The existence of session must be guaranteed by the caller. */
45 enum lttng_viewer_attach_return_code
viewer_session_attach(
46 struct relay_viewer_session
*vsession
,
47 struct relay_session
*session
)
49 enum lttng_viewer_attach_return_code viewer_attach_status
=
50 LTTNG_VIEWER_ATTACH_OK
;
52 ASSERT_LOCKED(session
->lock
);
54 /* Will not fail, as per the ownership guarantee. */
55 if (!session_get(session
)) {
56 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
59 if (session
->viewer_attached
) {
60 viewer_attach_status
= LTTNG_VIEWER_ATTACH_ALREADY
;
64 assert(session
->current_trace_chunk
);
65 assert(!vsession
->current_trace_chunk
);
66 session
->viewer_attached
= true;
68 ret
= viewer_session_set_trace_chunk(vsession
,
69 session
->current_trace_chunk
);
72 * The live protocol does not define a generic error
73 * value for the "attach" command. The "unknown"
74 * status is used so that the viewer may handle this
75 * failure as if the session didn't exist anymore.
77 DBG("Failed to create a viewer trace chunk from the current trace chunk of session \"%s\", returning LTTNG_VIEWER_ATTACH_UNK",
78 session
->session_name
);
79 viewer_attach_status
= LTTNG_VIEWER_ATTACH_UNK
;
83 if (viewer_attach_status
== LTTNG_VIEWER_ATTACH_OK
) {
84 pthread_mutex_lock(&vsession
->session_list_lock
);
85 /* Ownership is transfered to the list. */
86 cds_list_add_rcu(&session
->viewer_session_node
,
87 &vsession
->session_list
);
88 pthread_mutex_unlock(&vsession
->session_list_lock
);
90 /* Put our local ref. */
94 return viewer_attach_status
;
97 /* The existence of session must be guaranteed by the caller. */
98 static int viewer_session_detach(struct relay_viewer_session
*vsession
,
99 struct relay_session
*session
)
103 pthread_mutex_lock(&session
->lock
);
104 if (!session
->viewer_attached
) {
107 session
->viewer_attached
= false;
111 pthread_mutex_lock(&vsession
->session_list_lock
);
112 cds_list_del_rcu(&session
->viewer_session_node
);
113 pthread_mutex_unlock(&vsession
->session_list_lock
);
114 /* Release reference held by the list. */
115 session_put(session
);
117 /* Safe since we know the session exists. */
118 pthread_mutex_unlock(&session
->lock
);
122 void viewer_session_destroy(struct relay_viewer_session
*vsession
)
124 lttng_trace_chunk_put(vsession
->current_trace_chunk
);
129 * Release ownership of all the streams of one session and detach the viewer.
131 void viewer_session_close_one_session(struct relay_viewer_session
*vsession
,
132 struct relay_session
*session
)
134 struct lttng_ht_iter iter
;
135 struct relay_viewer_stream
*vstream
;
138 * TODO: improvement: create more efficient list of
139 * vstream per session.
141 cds_lfht_for_each_entry(viewer_streams_ht
->ht
, &iter
.iter
,
142 vstream
, stream_n
.node
) {
143 if (!viewer_stream_get(vstream
)) {
146 if (vstream
->stream
->trace
->session
!= session
) {
147 viewer_stream_put(vstream
);
150 /* Put local reference. */
151 viewer_stream_put(vstream
);
153 * We have reached one of the viewer stream's lifetime
154 * end condition. This "put" will cause the proper
155 * teardown of the viewer stream.
157 viewer_stream_put(vstream
);
159 lttng_trace_chunk_put(vsession
->current_trace_chunk
);
160 vsession
->current_trace_chunk
= NULL
;
161 viewer_session_detach(vsession
, session
);
164 void viewer_session_close(struct relay_viewer_session
*vsession
)
166 struct relay_session
*session
;
169 cds_list_for_each_entry_rcu(session
,
170 &vsession
->session_list
, viewer_session_node
) {
171 viewer_session_close_one_session(vsession
, session
);
177 * Check if a connection is attached to a session.
178 * Return 1 if attached, 0 if not attached, a negative value on error.
180 int viewer_session_is_attached(struct relay_viewer_session
*vsession
,
181 struct relay_session
*session
)
183 struct relay_session
*iter
;
186 pthread_mutex_lock(&session
->lock
);
190 if (!session
->viewer_attached
) {
194 cds_list_for_each_entry_rcu(iter
,
195 &vsession
->session_list
,
196 viewer_session_node
) {
197 if (session
== iter
) {
205 pthread_mutex_unlock(&session
->lock
);
209 int viewer_session_set_trace_chunk(struct relay_viewer_session
*vsession
,
210 struct lttng_trace_chunk
*relay_session_trace_chunk
)
213 struct lttng_trace_chunk
*viewer_chunk
;
215 assert(relay_session_trace_chunk
);
216 assert(!vsession
->current_trace_chunk
);
218 DBG("Copying relay session's current trace chunk to the viewer session");
219 viewer_chunk
= lttng_trace_chunk_copy(relay_session_trace_chunk
);
221 ERR("Failed to create a viewer trace chunk from the relay session's current chunk");
226 vsession
->current_trace_chunk
= viewer_chunk
;