97bed28d4ab8abbc5c20496ab64462eaa39f3884
[babeltrace.git] / plugins / utils / counter / counter.c
1 /*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include <babeltrace/babeltrace.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/common-internal.h>
26 #include <plugins-common.h>
27 #include <babeltrace/assert-internal.h>
28 #include <inttypes.h>
29 #include <stdint.h>
30
31 #include "counter.h"
32
33 #define PRINTF_COUNT(_what, _var, args...) \
34 do { \
35 if (counter->count._var != 0 || !counter->hide_zero) { \
36 printf("%15" PRIu64 " %s message%s\n", \
37 counter->count._var, \
38 (_what), \
39 counter->count._var == 1 ? "" : "s"); \
40 } \
41 } while (0)
42
43 static
44 const char * const in_port_name = "in";
45
46 static
47 uint64_t get_total_count(struct counter *counter)
48 {
49 return counter->count.event +
50 counter->count.stream_begin +
51 counter->count.stream_end +
52 counter->count.stream_activity_begin +
53 counter->count.stream_activity_end +
54 counter->count.packet_begin +
55 counter->count.packet_end +
56 counter->count.disc_events +
57 counter->count.disc_packets +
58 counter->count.msg_iter_inactivity +
59 counter->count.other;
60 }
61
62 static
63 void print_count(struct counter *counter)
64 {
65 uint64_t total = get_total_count(counter);
66
67 PRINTF_COUNT("Event", event);
68 PRINTF_COUNT("Stream beginning", stream_begin);
69 PRINTF_COUNT("Stream end", stream_end);
70 PRINTF_COUNT("Stream activity beginning", stream_activity_begin);
71 PRINTF_COUNT("Stream activity end", stream_activity_end);
72 PRINTF_COUNT("Packet beginning", packet_begin);
73 PRINTF_COUNT("Packet end", packet_end);
74 PRINTF_COUNT("Discarded event", disc_events);
75 PRINTF_COUNT("Discarded packet", disc_packets);
76 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity);
77
78 if (counter->count.other > 0) {
79 PRINTF_COUNT("Other (unknown)", other);
80 }
81
82 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
83 bt_common_color_bold(), total, total == 1 ? "" : "s",
84 bt_common_color_reset());
85 counter->last_printed_total = total;
86 }
87
88 static
89 void try_print_count(struct counter *counter, uint64_t msg_count)
90 {
91 if (counter->step == 0) {
92 /* No update */
93 return;
94 }
95
96 counter->at += msg_count;
97
98 if (counter->at >= counter->step) {
99 counter->at = 0;
100 print_count(counter);
101 putchar('\n');
102 }
103 }
104
105 static
106 void try_print_last(struct counter *counter)
107 {
108 const uint64_t total = get_total_count(counter);
109
110 if (total != counter->last_printed_total) {
111 print_count(counter);
112 }
113 }
114
115 void destroy_private_counter_data(struct counter *counter)
116 {
117 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
118 g_free(counter);
119 }
120
121 BT_HIDDEN
122 void counter_finalize(bt_self_component_sink *comp)
123 {
124 struct counter *counter;
125
126 BT_ASSERT(comp);
127 counter = bt_self_component_get_data(
128 bt_self_component_sink_as_self_component(comp));
129 BT_ASSERT(counter);
130 try_print_last(counter);
131 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
132 g_free(counter);
133 }
134
135 BT_HIDDEN
136 bt_self_component_status counter_init(
137 bt_self_component_sink *component,
138 const bt_value *params,
139 UNUSED_VAR void *init_method_data)
140 {
141 bt_self_component_status ret;
142 struct counter *counter = g_new0(struct counter, 1);
143 const bt_value *step = NULL;
144 const bt_value *hide_zero = NULL;
145
146 if (!counter) {
147 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
148 goto error;
149 }
150
151 ret = bt_self_component_sink_add_input_port(component,
152 "in", NULL, NULL);
153 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
154 goto error;
155 }
156
157 counter->last_printed_total = -1ULL;
158 counter->step = 10000;
159 step = bt_value_map_borrow_entry_value_const(params, "step");
160 if (step && bt_value_is_integer(step)) {
161 int64_t val;
162
163 val = bt_value_integer_get(step);
164 if (val >= 0) {
165 counter->step = (uint64_t) val;
166 }
167 }
168
169 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
170 if (hide_zero && bt_value_is_bool(hide_zero)) {
171 bt_bool val;
172
173 val = bt_value_bool_get(hide_zero);
174 counter->hide_zero = (bool) val;
175 }
176
177 bt_self_component_set_data(
178 bt_self_component_sink_as_self_component(component),
179 counter);
180 goto end;
181
182 error:
183 destroy_private_counter_data(counter);
184
185 end:
186 return ret;
187 }
188
189 BT_HIDDEN
190 bt_self_component_status counter_graph_is_configured(
191 bt_self_component_sink *comp)
192 {
193 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
194 struct counter *counter;
195 bt_self_component_port_input_message_iterator *iterator;
196
197 counter = bt_self_component_get_data(
198 bt_self_component_sink_as_self_component(comp));
199 BT_ASSERT(counter);
200 iterator = bt_self_component_port_input_message_iterator_create(
201 bt_self_component_sink_borrow_input_port_by_name(comp,
202 in_port_name));
203 if (!iterator) {
204 status = BT_SELF_COMPONENT_STATUS_NOMEM;
205 goto end;
206 }
207
208 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
209 counter->msg_iter, iterator);
210
211 end:
212 return status;
213 }
214
215 BT_HIDDEN
216 bt_self_component_status counter_consume(
217 bt_self_component_sink *comp)
218 {
219 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
220 struct counter *counter;
221 bt_message_iterator_status it_ret;
222 uint64_t msg_count;
223 bt_message_array_const msgs;
224
225 counter = bt_self_component_get_data(
226 bt_self_component_sink_as_self_component(comp));
227 BT_ASSERT(counter);
228
229 if (unlikely(!counter->msg_iter)) {
230 try_print_last(counter);
231 ret = BT_SELF_COMPONENT_STATUS_END;
232 goto end;
233 }
234
235 /* Consume messages */
236 it_ret = bt_self_component_port_input_message_iterator_next(
237 counter->msg_iter, &msgs, &msg_count);
238 if (it_ret < 0) {
239 ret = BT_SELF_COMPONENT_STATUS_ERROR;
240 goto end;
241 }
242
243 switch (it_ret) {
244 case BT_MESSAGE_ITERATOR_STATUS_OK:
245 {
246 uint64_t i;
247
248 for (i = 0; i < msg_count; i++) {
249 const bt_message *msg = msgs[i];
250
251 BT_ASSERT(msg);
252 switch (bt_message_get_type(msg)) {
253 case BT_MESSAGE_TYPE_EVENT:
254 counter->count.event++;
255 break;
256 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
257 counter->count.packet_begin++;
258 break;
259 case BT_MESSAGE_TYPE_PACKET_END:
260 counter->count.packet_end++;
261 break;
262 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
263 counter->count.msg_iter_inactivity++;
264 break;
265 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
266 counter->count.stream_begin++;
267 break;
268 case BT_MESSAGE_TYPE_STREAM_END:
269 counter->count.stream_end++;
270 break;
271 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
272 counter->count.stream_activity_begin++;
273 break;
274 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
275 counter->count.stream_activity_end++;
276 break;
277 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
278 counter->count.disc_events++;
279 break;
280 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
281 counter->count.disc_packets++;
282 break;
283 default:
284 counter->count.other++;
285 }
286
287 bt_message_put_ref(msg);
288 }
289
290 ret = BT_SELF_COMPONENT_STATUS_OK;
291 break;
292 }
293 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
294 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
295 goto end;
296 case BT_MESSAGE_ITERATOR_STATUS_END:
297 try_print_last(counter);
298 ret = BT_SELF_COMPONENT_STATUS_END;
299 goto end;
300 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
301 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
302 goto end;
303 default:
304 break;
305 }
306
307 try_print_count(counter, msg_count);
308
309 end:
310 return ret;
311 }
This page took 0.034595 seconds and 3 git commands to generate.