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