Cleanup: remove plugin-common.h
[babeltrace.git] / src / plugins / utils / counter / counter.c
CommitLineData
14b7ef9e
PP
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
68d9d039
PP
23#define BT_LOG_TAG "PLUGIN-UTILS-COUNTER-FLT"
24#include "logging.h"
25
71c5da58 26#include <babeltrace2/babeltrace.h>
85e7137b 27#include "common/macros.h"
57952005 28#include "common/common.h"
57952005 29#include "common/assert.h"
14b7ef9e
PP
30#include <inttypes.h>
31#include <stdint.h>
32
33#include "counter.h"
34
168baad4 35#define PRINTF_COUNT(_what, _var, args...) \
14b7ef9e
PP
36 do { \
37 if (counter->count._var != 0 || !counter->hide_zero) { \
168baad4 38 printf("%15" PRIu64 " %s message%s\n", \
14b7ef9e 39 counter->count._var, \
168baad4
PP
40 (_what), \
41 counter->count._var == 1 ? "" : "s"); \
14b7ef9e
PP
42 } \
43 } while (0)
44
1043fdea
PP
45static
46const char * const in_port_name = "in";
47
14b7ef9e
PP
48static
49uint64_t get_total_count(struct counter *counter)
50{
51 return counter->count.event +
52 counter->count.stream_begin +
53 counter->count.stream_end +
168baad4
PP
54 counter->count.stream_activity_begin +
55 counter->count.stream_activity_end +
14b7ef9e
PP
56 counter->count.packet_begin +
57 counter->count.packet_end +
168baad4
PP
58 counter->count.disc_events +
59 counter->count.disc_packets +
40bf6fd0 60 counter->count.msg_iter_inactivity +
14b7ef9e
PP
61 counter->count.other;
62}
63
64static
3fd7b79d 65void print_count(struct counter *counter)
14b7ef9e 66{
3fd7b79d
PP
67 uint64_t total = get_total_count(counter);
68
168baad4
PP
69 PRINTF_COUNT("Event", event);
70 PRINTF_COUNT("Stream beginning", stream_begin);
71 PRINTF_COUNT("Stream end", stream_end);
72 PRINTF_COUNT("Stream activity beginning", stream_activity_begin);
73 PRINTF_COUNT("Stream activity end", stream_activity_end);
74 PRINTF_COUNT("Packet beginning", packet_begin);
75 PRINTF_COUNT("Packet end", packet_end);
76 PRINTF_COUNT("Discarded event", disc_events);
77 PRINTF_COUNT("Discarded packet", disc_packets);
78 PRINTF_COUNT("Message iterator inactivity", msg_iter_inactivity);
14b7ef9e
PP
79
80 if (counter->count.other > 0) {
168baad4 81 PRINTF_COUNT("Other (unknown)", other);
14b7ef9e
PP
82 }
83
b09a5592 84 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
14b7ef9e
PP
85 bt_common_color_bold(), total, total == 1 ? "" : "s",
86 bt_common_color_reset());
87 counter->last_printed_total = total;
88}
89
90static
b09a5592 91void try_print_count(struct counter *counter, uint64_t msg_count)
14b7ef9e 92{
14b7ef9e
PP
93 if (counter->step == 0) {
94 /* No update */
95 return;
96 }
97
b09a5592 98 counter->at += msg_count;
14b7ef9e 99
3fd7b79d
PP
100 if (counter->at >= counter->step) {
101 counter->at = 0;
102 print_count(counter);
14b7ef9e
PP
103 putchar('\n');
104 }
105}
106
107static
108void try_print_last(struct counter *counter)
109{
110 const uint64_t total = get_total_count(counter);
111
112 if (total != counter->last_printed_total) {
3fd7b79d 113 print_count(counter);
14b7ef9e
PP
114 }
115}
116
117void destroy_private_counter_data(struct counter *counter)
118{
b09a5592 119 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
14b7ef9e
PP
120 g_free(counter);
121}
122
834e9996 123BT_HIDDEN
8eee8ea2 124void counter_finalize(bt_self_component_sink *comp)
14b7ef9e
PP
125{
126 struct counter *counter;
127
834e9996
PP
128 BT_ASSERT(comp);
129 counter = bt_self_component_get_data(
bb61965b 130 bt_self_component_sink_as_self_component(comp));
8b45963b 131 BT_ASSERT(counter);
14b7ef9e 132 try_print_last(counter);
b09a5592 133 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
14b7ef9e
PP
134 g_free(counter);
135}
136
834e9996 137BT_HIDDEN
ee78f405 138bt_self_component_status counter_init(
8eee8ea2
PP
139 bt_self_component_sink *component,
140 const bt_value *params,
f807570c 141 __attribute__((unused)) void *init_method_data)
14b7ef9e 142{
ee78f405 143 bt_self_component_status ret;
14b7ef9e 144 struct counter *counter = g_new0(struct counter, 1);
8eee8ea2
PP
145 const bt_value *step = NULL;
146 const bt_value *hide_zero = NULL;
14b7ef9e
PP
147
148 if (!counter) {
834e9996
PP
149 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
150 goto error;
14b7ef9e
PP
151 }
152
834e9996 153 ret = bt_self_component_sink_add_input_port(component,
14b7ef9e 154 "in", NULL, NULL);
834e9996
PP
155 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
156 goto error;
14b7ef9e
PP
157 }
158
159 counter->last_printed_total = -1ULL;
a79f976c 160 counter->step = 10000;
ce141536 161 step = bt_value_map_borrow_entry_value_const(params, "step");
68d9d039
PP
162 if (step) {
163 if (!bt_value_is_unsigned_integer(step)) {
164 BT_LOGE("`step` parameter: expecting an unsigned integer value: "
165 "type=%s", bt_common_value_type_string(
166 bt_value_get_type(step)));
167 goto error;
14b7ef9e 168 }
68d9d039
PP
169
170 counter->step = bt_value_unsigned_integer_get(step);
14b7ef9e
PP
171 }
172
ce141536 173 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
68d9d039
PP
174 if (hide_zero) {
175 if (!bt_value_is_bool(hide_zero)) {
176 BT_LOGE("`hide-zero` parameter: expecting a boolean value: "
177 "type=%s", bt_common_value_type_string(
178 bt_value_get_type(hide_zero)));
179 goto error;
180 }
14b7ef9e 181
68d9d039 182 counter->hide_zero = (bool) bt_value_bool_get(hide_zero);
14b7ef9e
PP
183 }
184
834e9996 185 bt_self_component_set_data(
bb61965b 186 bt_self_component_sink_as_self_component(component),
834e9996 187 counter);
14b7ef9e
PP
188 goto end;
189
190error:
191 destroy_private_counter_data(counter);
68d9d039 192 ret = BT_SELF_COMPONENT_STATUS_ERROR;
14b7ef9e
PP
193
194end:
14b7ef9e
PP
195 return ret;
196}
197
834e9996 198BT_HIDDEN
1043fdea
PP
199bt_self_component_status counter_graph_is_configured(
200 bt_self_component_sink *comp)
14b7ef9e 201{
ee78f405 202 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
14b7ef9e 203 struct counter *counter;
b09a5592 204 bt_self_component_port_input_message_iterator *iterator;
14b7ef9e 205
834e9996 206 counter = bt_self_component_get_data(
bb61965b 207 bt_self_component_sink_as_self_component(comp));
8b45963b 208 BT_ASSERT(counter);
b09a5592 209 iterator = bt_self_component_port_input_message_iterator_create(
1043fdea
PP
210 bt_self_component_sink_borrow_input_port_by_name(comp,
211 in_port_name));
834e9996
PP
212 if (!iterator) {
213 status = BT_SELF_COMPONENT_STATUS_NOMEM;
14b7ef9e
PP
214 goto end;
215 }
216
b09a5592
PP
217 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
218 counter->msg_iter, iterator);
14b7ef9e
PP
219
220end:
634f394c 221 return status;
14b7ef9e
PP
222}
223
834e9996 224BT_HIDDEN
ee78f405 225bt_self_component_status counter_consume(
8eee8ea2 226 bt_self_component_sink *comp)
14b7ef9e 227{
ee78f405 228 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
14b7ef9e 229 struct counter *counter;
ee78f405 230 bt_message_iterator_status it_ret;
b09a5592
PP
231 uint64_t msg_count;
232 bt_message_array_const msgs;
14b7ef9e 233
834e9996 234 counter = bt_self_component_get_data(
bb61965b 235 bt_self_component_sink_as_self_component(comp));
8b45963b 236 BT_ASSERT(counter);
14b7ef9e 237
85e7137b 238 if (G_UNLIKELY(!counter->msg_iter)) {
14b7ef9e 239 try_print_last(counter);
834e9996 240 ret = BT_SELF_COMPONENT_STATUS_END;
14b7ef9e
PP
241 goto end;
242 }
243
b09a5592
PP
244 /* Consume messages */
245 it_ret = bt_self_component_port_input_message_iterator_next(
246 counter->msg_iter, &msgs, &msg_count);
14b7ef9e 247 if (it_ret < 0) {
834e9996 248 ret = BT_SELF_COMPONENT_STATUS_ERROR;
14b7ef9e
PP
249 goto end;
250 }
251
252 switch (it_ret) {
b09a5592 253 case BT_MESSAGE_ITERATOR_STATUS_OK:
14b7ef9e 254 {
3fd7b79d
PP
255 uint64_t i;
256
b09a5592
PP
257 for (i = 0; i < msg_count; i++) {
258 const bt_message *msg = msgs[i];
3fd7b79d 259
b09a5592
PP
260 BT_ASSERT(msg);
261 switch (bt_message_get_type(msg)) {
262 case BT_MESSAGE_TYPE_EVENT:
3fd7b79d
PP
263 counter->count.event++;
264 break;
168baad4
PP
265 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
266 counter->count.packet_begin++;
267 break;
268 case BT_MESSAGE_TYPE_PACKET_END:
269 counter->count.packet_end++;
270 break;
40bf6fd0
FD
271 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
272 counter->count.msg_iter_inactivity++;
3fd7b79d 273 break;
b09a5592 274 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
3fd7b79d
PP
275 counter->count.stream_begin++;
276 break;
b09a5592 277 case BT_MESSAGE_TYPE_STREAM_END:
3fd7b79d
PP
278 counter->count.stream_end++;
279 break;
168baad4
PP
280 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
281 counter->count.stream_activity_begin++;
3fd7b79d 282 break;
168baad4
PP
283 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
284 counter->count.stream_activity_end++;
285 break;
286 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
287 counter->count.disc_events++;
288 break;
289 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
290 counter->count.disc_packets++;
3fd7b79d 291 break;
3fd7b79d
PP
292 default:
293 counter->count.other++;
14b7ef9e 294 }
3fd7b79d 295
b09a5592 296 bt_message_put_ref(msg);
14b7ef9e 297 }
834e9996
PP
298
299 ret = BT_SELF_COMPONENT_STATUS_OK;
300 break;
14b7ef9e 301 }
b09a5592 302 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
834e9996
PP
303 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
304 goto end;
b09a5592 305 case BT_MESSAGE_ITERATOR_STATUS_END:
834e9996
PP
306 try_print_last(counter);
307 ret = BT_SELF_COMPONENT_STATUS_END;
308 goto end;
b09a5592 309 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
834e9996
PP
310 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
311 goto end;
14b7ef9e
PP
312 default:
313 break;
314 }
315
b09a5592 316 try_print_count(counter, msg_count);
14b7ef9e
PP
317
318end:
14b7ef9e
PP
319 return ret;
320}
This page took 0.048595 seconds and 4 git commands to generate.