lib: graph: add "self" and some "private" APIs
[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 uint64_t get_total_count(struct counter *counter)
44 {
45 return counter->count.event +
46 counter->count.stream_begin +
47 counter->count.stream_end +
48 counter->count.packet_begin +
49 counter->count.packet_end +
50 counter->count.inactivity +
51 counter->count.other;
52 }
53
54 static
55 void print_count(struct counter *counter)
56 {
57 uint64_t total = get_total_count(counter);
58
59 PRINTF_COUNT("event", "events", event);
60 PRINTF_COUNT("stream beginning", "stream beginnings", stream_begin);
61 PRINTF_COUNT("stream end", "stream ends", stream_end);
62 PRINTF_COUNT("packet beginning", "packet beginnings", packet_begin);
63 PRINTF_COUNT("packet end", "packet ends", packet_end);
64 PRINTF_COUNT("inactivity", "inactivities", inactivity);
65
66 if (counter->count.other > 0) {
67 PRINTF_COUNT(" other (unknown) notification",
68 " other (unknown) notifications", other);
69 }
70
71 printf("%s%15" PRIu64 " notification%s (TOTAL)%s\n",
72 bt_common_color_bold(), total, total == 1 ? "" : "s",
73 bt_common_color_reset());
74 counter->last_printed_total = total;
75 }
76
77 static
78 void try_print_count(struct counter *counter, uint64_t notif_count)
79 {
80 if (counter->step == 0) {
81 /* No update */
82 return;
83 }
84
85 counter->at += notif_count;
86
87 if (counter->at >= counter->step) {
88 counter->at = 0;
89 print_count(counter);
90 putchar('\n');
91 }
92 }
93
94 static
95 void try_print_last(struct counter *counter)
96 {
97 const uint64_t total = get_total_count(counter);
98
99 if (total != counter->last_printed_total) {
100 print_count(counter);
101 }
102 }
103
104 void destroy_private_counter_data(struct counter *counter)
105 {
106 bt_object_put_ref(counter->notif_iter);
107 g_free(counter);
108 }
109
110 BT_HIDDEN
111 void counter_finalize(struct bt_self_component_sink *comp)
112 {
113 struct counter *counter;
114
115 BT_ASSERT(comp);
116 counter = bt_self_component_get_data(
117 bt_self_component_sink_borrow_self_component(comp));
118 BT_ASSERT(counter);
119 try_print_last(counter);
120 bt_object_put_ref(counter->notif_iter);
121 g_free(counter);
122 }
123
124 BT_HIDDEN
125 enum bt_self_component_status counter_init(
126 struct bt_self_component_sink *component,
127 struct bt_value *params, UNUSED_VAR void *init_method_data)
128 {
129 enum bt_self_component_status ret;
130 struct counter *counter = g_new0(struct counter, 1);
131 struct bt_value *step = NULL;
132 struct bt_value *hide_zero = NULL;
133
134 if (!counter) {
135 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
136 goto error;
137 }
138
139 ret = bt_self_component_sink_add_input_port(component,
140 "in", NULL, NULL);
141 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
142 goto error;
143 }
144
145 counter->last_printed_total = -1ULL;
146 counter->step = 1000;
147 step = bt_value_map_borrow_entry_value(params, "step");
148 if (step && bt_value_is_integer(step)) {
149 int64_t val;
150
151 val = bt_value_integer_get(step);
152 if (val >= 0) {
153 counter->step = (uint64_t) val;
154 }
155 }
156
157 hide_zero = bt_value_map_borrow_entry_value(params, "hide-zero");
158 if (hide_zero && bt_value_is_bool(hide_zero)) {
159 bt_bool val;
160
161 val = bt_value_bool_get(hide_zero);
162 counter->hide_zero = (bool) val;
163 }
164
165 bt_self_component_set_data(
166 bt_self_component_sink_borrow_self_component(component),
167 counter);
168 goto end;
169
170 error:
171 destroy_private_counter_data(counter);
172
173 end:
174 return ret;
175 }
176
177 BT_HIDDEN
178 enum bt_self_component_status counter_port_connected(
179 struct bt_self_component_sink *comp,
180 struct bt_self_component_port_input *self_port,
181 struct bt_port_output *other_port)
182 {
183 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
184 struct counter *counter;
185 struct bt_self_component_port_input_notification_iterator *iterator;
186
187 counter = bt_self_component_get_data(
188 bt_self_component_sink_borrow_self_component(comp));
189 BT_ASSERT(counter);
190 iterator = bt_self_component_port_input_notification_iterator_create(
191 self_port);
192 if (!iterator) {
193 status = BT_SELF_COMPONENT_STATUS_NOMEM;
194 goto end;
195 }
196
197 BT_OBJECT_MOVE_REF(counter->notif_iter, iterator);
198
199 end:
200 return status;
201 }
202
203 BT_HIDDEN
204 enum bt_self_component_status counter_consume(
205 struct bt_self_component_sink *comp)
206 {
207 enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
208 struct counter *counter;
209 enum bt_notification_iterator_status it_ret;
210 uint64_t notif_count;
211 bt_notification_array notifs;
212
213 counter = bt_self_component_get_data(
214 bt_self_component_sink_borrow_self_component(comp));
215 BT_ASSERT(counter);
216
217 if (unlikely(!counter->notif_iter)) {
218 try_print_last(counter);
219 ret = BT_SELF_COMPONENT_STATUS_END;
220 goto end;
221 }
222
223 /* Consume notifications */
224 it_ret = bt_self_component_port_input_notification_iterator_next(
225 counter->notif_iter, &notifs, &notif_count);
226 if (it_ret < 0) {
227 ret = BT_SELF_COMPONENT_STATUS_ERROR;
228 goto end;
229 }
230
231 switch (it_ret) {
232 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
233 {
234 uint64_t i;
235
236 for (i = 0; i < notif_count; i++) {
237 struct bt_notification *notif = notifs[i];
238
239 BT_ASSERT(notif);
240 switch (bt_notification_get_type(notif)) {
241 case BT_NOTIFICATION_TYPE_EVENT:
242 counter->count.event++;
243 break;
244 case BT_NOTIFICATION_TYPE_INACTIVITY:
245 counter->count.inactivity++;
246 break;
247 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
248 counter->count.stream_begin++;
249 break;
250 case BT_NOTIFICATION_TYPE_STREAM_END:
251 counter->count.stream_end++;
252 break;
253 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
254 counter->count.packet_begin++;
255 break;
256 case BT_NOTIFICATION_TYPE_PACKET_END:
257 counter->count.packet_end++;
258 break;
259 default:
260 counter->count.other++;
261 }
262
263 bt_object_put_ref(notif);
264 }
265
266 ret = BT_SELF_COMPONENT_STATUS_OK;
267 break;
268 }
269 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
270 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
271 goto end;
272 case BT_NOTIFICATION_ITERATOR_STATUS_END:
273 try_print_last(counter);
274 ret = BT_SELF_COMPONENT_STATUS_END;
275 goto end;
276 case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM:
277 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
278 goto end;
279 default:
280 break;
281 }
282
283 try_print_count(counter, notif_count);
284
285 end:
286 return ret;
287 }
This page took 0.042858 seconds and 4 git commands to generate.