lib: add sink component class's "graph is configured" method
[babeltrace.git] / 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
e0831b38 23#include <babeltrace/babeltrace.h>
14b7ef9e
PP
24#include <babeltrace/babeltrace-internal.h>
25#include <babeltrace/common-internal.h>
14b7ef9e 26#include <plugins-common.h>
8b45963b 27#include <babeltrace/assert-internal.h>
14b7ef9e
PP
28#include <inttypes.h>
29#include <stdint.h>
30
31#include "counter.h"
32
33#define PRINTF_COUNT(_what_sing, _what_plur, _var, args...) \
34 do { \
35 if (counter->count._var != 0 || !counter->hide_zero) { \
36 printf("%15" PRIu64 " %s\n", \
37 counter->count._var, \
38 counter->count._var == 1 ? _what_sing : _what_plur); \
39 } \
40 } while (0)
41
1043fdea
PP
42static
43const char * const in_port_name = "in";
44
14b7ef9e
PP
45static
46uint64_t get_total_count(struct counter *counter)
47{
48 return counter->count.event +
49 counter->count.stream_begin +
50 counter->count.stream_end +
51 counter->count.packet_begin +
52 counter->count.packet_end +
53 counter->count.inactivity +
14b7ef9e
PP
54 counter->count.other;
55}
56
57static
3fd7b79d 58void print_count(struct counter *counter)
14b7ef9e 59{
3fd7b79d
PP
60 uint64_t total = get_total_count(counter);
61
14b7ef9e
PP
62 PRINTF_COUNT("event", "events", event);
63 PRINTF_COUNT("stream beginning", "stream beginnings", stream_begin);
64 PRINTF_COUNT("stream end", "stream ends", stream_end);
65 PRINTF_COUNT("packet beginning", "packet beginnings", packet_begin);
66 PRINTF_COUNT("packet end", "packet ends", packet_end);
67 PRINTF_COUNT("inactivity", "inactivities", inactivity);
14b7ef9e
PP
68
69 if (counter->count.other > 0) {
b09a5592
PP
70 PRINTF_COUNT(" other (unknown) message",
71 " other (unknown) messages", other);
14b7ef9e
PP
72 }
73
b09a5592 74 printf("%s%15" PRIu64 " message%s (TOTAL)%s\n",
14b7ef9e
PP
75 bt_common_color_bold(), total, total == 1 ? "" : "s",
76 bt_common_color_reset());
77 counter->last_printed_total = total;
78}
79
80static
b09a5592 81void try_print_count(struct counter *counter, uint64_t msg_count)
14b7ef9e 82{
14b7ef9e
PP
83 if (counter->step == 0) {
84 /* No update */
85 return;
86 }
87
b09a5592 88 counter->at += msg_count;
14b7ef9e 89
3fd7b79d
PP
90 if (counter->at >= counter->step) {
91 counter->at = 0;
92 print_count(counter);
14b7ef9e
PP
93 putchar('\n');
94 }
95}
96
97static
98void try_print_last(struct counter *counter)
99{
100 const uint64_t total = get_total_count(counter);
101
102 if (total != counter->last_printed_total) {
3fd7b79d 103 print_count(counter);
14b7ef9e
PP
104 }
105}
106
107void destroy_private_counter_data(struct counter *counter)
108{
b09a5592 109 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
14b7ef9e
PP
110 g_free(counter);
111}
112
834e9996 113BT_HIDDEN
8eee8ea2 114void counter_finalize(bt_self_component_sink *comp)
14b7ef9e
PP
115{
116 struct counter *counter;
117
834e9996
PP
118 BT_ASSERT(comp);
119 counter = bt_self_component_get_data(
bb61965b 120 bt_self_component_sink_as_self_component(comp));
8b45963b 121 BT_ASSERT(counter);
14b7ef9e 122 try_print_last(counter);
b09a5592 123 bt_self_component_port_input_message_iterator_put_ref(counter->msg_iter);
14b7ef9e
PP
124 g_free(counter);
125}
126
834e9996 127BT_HIDDEN
ee78f405 128bt_self_component_status counter_init(
8eee8ea2
PP
129 bt_self_component_sink *component,
130 const bt_value *params,
ce141536 131 UNUSED_VAR void *init_method_data)
14b7ef9e 132{
ee78f405 133 bt_self_component_status ret;
14b7ef9e 134 struct counter *counter = g_new0(struct counter, 1);
8eee8ea2
PP
135 const bt_value *step = NULL;
136 const bt_value *hide_zero = NULL;
14b7ef9e
PP
137
138 if (!counter) {
834e9996
PP
139 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
140 goto error;
14b7ef9e
PP
141 }
142
834e9996 143 ret = bt_self_component_sink_add_input_port(component,
14b7ef9e 144 "in", NULL, NULL);
834e9996
PP
145 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
146 goto error;
14b7ef9e
PP
147 }
148
149 counter->last_printed_total = -1ULL;
150 counter->step = 1000;
ce141536 151 step = bt_value_map_borrow_entry_value_const(params, "step");
8b45963b 152 if (step && bt_value_is_integer(step)) {
14b7ef9e 153 int64_t val;
14b7ef9e 154
b5cdc106 155 val = bt_value_integer_get(step);
14b7ef9e
PP
156 if (val >= 0) {
157 counter->step = (uint64_t) val;
158 }
159 }
160
ce141536 161 hide_zero = bt_value_map_borrow_entry_value_const(params, "hide-zero");
8b45963b 162 if (hide_zero && bt_value_is_bool(hide_zero)) {
14b7ef9e 163 bt_bool val;
14b7ef9e 164
b5cdc106 165 val = bt_value_bool_get(hide_zero);
14b7ef9e
PP
166 counter->hide_zero = (bool) val;
167 }
168
834e9996 169 bt_self_component_set_data(
bb61965b 170 bt_self_component_sink_as_self_component(component),
834e9996 171 counter);
14b7ef9e
PP
172 goto end;
173
174error:
175 destroy_private_counter_data(counter);
176
177end:
14b7ef9e
PP
178 return ret;
179}
180
834e9996 181BT_HIDDEN
1043fdea
PP
182bt_self_component_status counter_graph_is_configured(
183 bt_self_component_sink *comp)
14b7ef9e 184{
ee78f405 185 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
14b7ef9e 186 struct counter *counter;
b09a5592 187 bt_self_component_port_input_message_iterator *iterator;
14b7ef9e 188
834e9996 189 counter = bt_self_component_get_data(
bb61965b 190 bt_self_component_sink_as_self_component(comp));
8b45963b 191 BT_ASSERT(counter);
b09a5592 192 iterator = bt_self_component_port_input_message_iterator_create(
1043fdea
PP
193 bt_self_component_sink_borrow_input_port_by_name(comp,
194 in_port_name));
834e9996
PP
195 if (!iterator) {
196 status = BT_SELF_COMPONENT_STATUS_NOMEM;
14b7ef9e
PP
197 goto end;
198 }
199
b09a5592
PP
200 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
201 counter->msg_iter, iterator);
14b7ef9e
PP
202
203end:
634f394c 204 return status;
14b7ef9e
PP
205}
206
834e9996 207BT_HIDDEN
ee78f405 208bt_self_component_status counter_consume(
8eee8ea2 209 bt_self_component_sink *comp)
14b7ef9e 210{
ee78f405 211 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
14b7ef9e 212 struct counter *counter;
ee78f405 213 bt_message_iterator_status it_ret;
b09a5592
PP
214 uint64_t msg_count;
215 bt_message_array_const msgs;
14b7ef9e 216
834e9996 217 counter = bt_self_component_get_data(
bb61965b 218 bt_self_component_sink_as_self_component(comp));
8b45963b 219 BT_ASSERT(counter);
14b7ef9e 220
b09a5592 221 if (unlikely(!counter->msg_iter)) {
14b7ef9e 222 try_print_last(counter);
834e9996 223 ret = BT_SELF_COMPONENT_STATUS_END;
14b7ef9e
PP
224 goto end;
225 }
226
b09a5592
PP
227 /* Consume messages */
228 it_ret = bt_self_component_port_input_message_iterator_next(
229 counter->msg_iter, &msgs, &msg_count);
14b7ef9e 230 if (it_ret < 0) {
834e9996 231 ret = BT_SELF_COMPONENT_STATUS_ERROR;
14b7ef9e
PP
232 goto end;
233 }
234
235 switch (it_ret) {
b09a5592 236 case BT_MESSAGE_ITERATOR_STATUS_OK:
14b7ef9e 237 {
3fd7b79d
PP
238 uint64_t i;
239
b09a5592
PP
240 for (i = 0; i < msg_count; i++) {
241 const bt_message *msg = msgs[i];
3fd7b79d 242
b09a5592
PP
243 BT_ASSERT(msg);
244 switch (bt_message_get_type(msg)) {
245 case BT_MESSAGE_TYPE_EVENT:
3fd7b79d
PP
246 counter->count.event++;
247 break;
b09a5592 248 case BT_MESSAGE_TYPE_INACTIVITY:
3fd7b79d
PP
249 counter->count.inactivity++;
250 break;
b09a5592 251 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
3fd7b79d
PP
252 counter->count.stream_begin++;
253 break;
b09a5592 254 case BT_MESSAGE_TYPE_STREAM_END:
3fd7b79d
PP
255 counter->count.stream_end++;
256 break;
b09a5592 257 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
3fd7b79d
PP
258 counter->count.packet_begin++;
259 break;
b09a5592 260 case BT_MESSAGE_TYPE_PACKET_END:
3fd7b79d
PP
261 counter->count.packet_end++;
262 break;
3fd7b79d
PP
263 default:
264 counter->count.other++;
14b7ef9e 265 }
3fd7b79d 266
b09a5592 267 bt_message_put_ref(msg);
14b7ef9e 268 }
834e9996
PP
269
270 ret = BT_SELF_COMPONENT_STATUS_OK;
271 break;
14b7ef9e 272 }
b09a5592 273 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
834e9996
PP
274 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
275 goto end;
b09a5592 276 case BT_MESSAGE_ITERATOR_STATUS_END:
834e9996
PP
277 try_print_last(counter);
278 ret = BT_SELF_COMPONENT_STATUS_END;
279 goto end;
b09a5592 280 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
834e9996
PP
281 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
282 goto end;
14b7ef9e
PP
283 default:
284 break;
285 }
286
b09a5592 287 try_print_count(counter, msg_count);
14b7ef9e
PP
288
289end:
14b7ef9e
PP
290 return ret;
291}
This page took 0.042437 seconds and 4 git commands to generate.