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