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