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