Commit | Line | Data |
---|---|---|
7cdc2bab MD |
1 | /* |
2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> | |
3 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
4 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
020bc26f PP |
25 | #define BT_LOG_TAG "PLUGIN-CTF-LTTNG-LIVE-SRC-DS" |
26 | #include "logging.h" | |
27 | ||
7cdc2bab MD |
28 | #include <stdio.h> |
29 | #include <stdint.h> | |
30 | #include <stdlib.h> | |
31 | #include <stdbool.h> | |
32 | #include <glib.h> | |
33 | #include <inttypes.h> | |
34 | #include <sys/mman.h> | |
35 | #include <babeltrace/ctf-ir/stream.h> | |
36 | #include "../common/notif-iter/notif-iter.h" | |
37 | #include <assert.h> | |
7cdc2bab | 38 | |
087bc060 | 39 | #include "data-stream.h" |
7cdc2bab MD |
40 | |
41 | static | |
42 | enum bt_ctf_notif_iter_medium_status medop_request_bytes( | |
43 | size_t request_sz, uint8_t **buffer_addr, | |
44 | size_t *buffer_sz, void *data) | |
45 | { | |
46 | enum bt_ctf_notif_iter_medium_status status = | |
47 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
48 | struct lttng_live_stream_iterator *stream = data; | |
49 | struct lttng_live_trace *trace = stream->trace; | |
50 | struct lttng_live_session *session = trace->session; | |
51 | struct lttng_live_component *lttng_live = session->lttng_live; | |
52 | uint64_t recv_len = 0; | |
53 | uint64_t len_left; | |
54 | uint64_t read_len; | |
55 | //int i; | |
56 | ||
57 | len_left = stream->base_offset + stream->len - stream->offset; | |
58 | if (!len_left) { | |
59 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA; | |
60 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN; | |
61 | return status; | |
62 | } | |
63 | read_len = MIN(request_sz, stream->buflen); | |
64 | read_len = MIN(read_len, len_left); | |
65 | status = lttng_live_get_stream_bytes(lttng_live, | |
66 | stream, stream->buf, stream->offset, | |
67 | read_len, &recv_len); | |
087bc060 | 68 | BT_LOGV_MEM(stream->buf, recv_len, "Live receive payload"); |
7cdc2bab MD |
69 | *buffer_addr = stream->buf; |
70 | *buffer_sz = recv_len; | |
71 | stream->offset += recv_len; | |
72 | return status; | |
73 | } | |
74 | ||
75 | static | |
76 | struct bt_ctf_stream *medop_get_stream( | |
77 | struct bt_ctf_stream_class *stream_class, void *data) | |
78 | { | |
79 | struct lttng_live_stream_iterator *lttng_live_stream = data; | |
7cdc2bab MD |
80 | |
81 | if (!lttng_live_stream->stream) { | |
82 | int64_t id = bt_ctf_stream_class_get_id(stream_class); | |
83 | ||
087bc060 | 84 | BT_LOGD("Creating stream %s out of stream class %" PRId64, |
7cdc2bab MD |
85 | lttng_live_stream->name, id); |
86 | lttng_live_stream->stream = bt_ctf_stream_create(stream_class, | |
87 | lttng_live_stream->name); | |
88 | if (!lttng_live_stream->stream) { | |
087bc060 | 89 | BT_LOGE("Cannot create stream %s (stream class %" PRId64 ")", |
7cdc2bab MD |
90 | lttng_live_stream->name, id); |
91 | } | |
92 | } | |
93 | ||
94 | return lttng_live_stream->stream; | |
95 | } | |
96 | ||
97 | static struct bt_ctf_notif_iter_medium_ops medops = { | |
98 | .request_bytes = medop_request_bytes, | |
99 | .get_stream = medop_get_stream, | |
100 | }; | |
101 | ||
102 | BT_HIDDEN | |
103 | enum bt_ctf_lttng_live_iterator_status lttng_live_lazy_notif_init( | |
104 | struct lttng_live_session *session) | |
105 | { | |
106 | struct lttng_live_component *lttng_live = session->lttng_live; | |
107 | struct lttng_live_trace *trace; | |
108 | ||
109 | if (!session->lazy_stream_notif_init) { | |
110 | return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
111 | } | |
112 | ||
113 | bt_list_for_each_entry(trace, &session->traces, node) { | |
114 | struct lttng_live_stream_iterator *stream; | |
115 | ||
116 | bt_list_for_each_entry(stream, &trace->streams, node) { | |
117 | if (stream->notif_iter) { | |
118 | continue; | |
119 | } | |
120 | stream->notif_iter = bt_ctf_notif_iter_create(trace->trace, | |
121 | lttng_live->max_query_size, medops, | |
087bc060 | 122 | stream, stderr); |
7cdc2bab MD |
123 | if (!stream->notif_iter) { |
124 | goto error; | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
129 | session->lazy_stream_notif_init = false; | |
130 | ||
131 | return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
132 | ||
133 | error: | |
134 | return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
135 | } | |
136 | ||
137 | BT_HIDDEN | |
138 | struct lttng_live_stream_iterator *lttng_live_stream_iterator_create( | |
139 | struct lttng_live_session *session, | |
140 | uint64_t ctf_trace_id, | |
141 | uint64_t stream_id) | |
142 | { | |
143 | struct lttng_live_component *lttng_live = session->lttng_live; | |
144 | struct lttng_live_stream_iterator *stream = | |
145 | g_new0(struct lttng_live_stream_iterator, 1); | |
146 | struct lttng_live_trace *trace; | |
147 | int ret; | |
148 | ||
149 | trace = lttng_live_ref_trace(session, ctf_trace_id); | |
150 | if (!trace) { | |
151 | goto error; | |
152 | } | |
153 | ||
154 | stream->p.type = LIVE_STREAM_TYPE_STREAM; | |
155 | stream->trace = trace; | |
156 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA; | |
157 | stream->viewer_stream_id = stream_id; | |
158 | stream->ctf_stream_class_id = -1ULL; | |
159 | stream->last_returned_inactivity_timestamp = INT64_MIN; | |
160 | ||
161 | if (trace->trace) { | |
162 | stream->notif_iter = bt_ctf_notif_iter_create(trace->trace, | |
163 | lttng_live->max_query_size, medops, | |
087bc060 | 164 | stream, stderr); |
7cdc2bab MD |
165 | if (!stream->notif_iter) { |
166 | goto error; | |
167 | } | |
168 | } | |
169 | stream->buf = g_new0(uint8_t, session->lttng_live->max_query_size); | |
170 | stream->buflen = session->lttng_live->max_query_size; | |
171 | ||
172 | ret = lttng_live_add_port(lttng_live, stream); | |
173 | assert(!ret); | |
174 | ||
175 | bt_list_add(&stream->node, &trace->streams); | |
176 | ||
177 | goto end; | |
178 | error: | |
179 | /* Do not touch "borrowed" file. */ | |
180 | lttng_live_stream_iterator_destroy(stream); | |
181 | stream = NULL; | |
182 | end: | |
183 | return stream; | |
184 | } | |
185 | ||
186 | BT_HIDDEN | |
187 | void lttng_live_stream_iterator_destroy(struct lttng_live_stream_iterator *stream) | |
188 | { | |
189 | struct lttng_live_component *lttng_live; | |
190 | int ret; | |
191 | ||
192 | if (!stream) { | |
193 | return; | |
194 | } | |
195 | ||
196 | lttng_live = stream->trace->session->lttng_live; | |
197 | ret = lttng_live_remove_port(lttng_live, stream->port); | |
198 | assert(!ret); | |
199 | ||
200 | if (stream->stream) { | |
201 | BT_PUT(stream->stream); | |
202 | } | |
203 | ||
204 | if (stream->notif_iter) { | |
205 | bt_ctf_notif_iter_destroy(stream->notif_iter); | |
206 | } | |
207 | g_free(stream->buf); | |
208 | BT_PUT(stream->packet_end_notif_queue); | |
209 | bt_list_del(&stream->node); | |
210 | /* | |
211 | * Ensure we poke the trace metadata in the future, which is | |
212 | * required to release the metadata reference on the trace. | |
213 | */ | |
214 | stream->trace->new_metadata_needed = true; | |
215 | lttng_live_unref_trace(stream->trace); | |
216 | g_free(stream); | |
217 | } |