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