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