Automatically generate discarded packets/events notifications
[babeltrace.git] / include / babeltrace / graph / notification-iterator-internal.h
CommitLineData
33b34c43
PP
1#ifndef BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H
2#define BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H
47e5a032
JG
3
4/*
5 * BabelTrace - Notification Iterator Internal
6 *
7 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3230ee6b 8 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
47e5a032
JG
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <babeltrace/babeltrace-internal.h>
890882ef 30#include <babeltrace/object-internal.h>
fb2dcc52 31#include <babeltrace/ref-internal.h>
bd14d768 32#include <babeltrace/graph/connection.h>
b2e0c907
PP
33#include <babeltrace/graph/notification.h>
34#include <babeltrace/graph/notification-iterator.h>
35#include <babeltrace/graph/private-notification-iterator.h>
c55a9f58 36#include <babeltrace/types.h>
47e5a032 37
3230ee6b
PP
38struct bt_port;
39
fa054faf
PP
40enum bt_notification_iterator_notif_type {
41 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT = (1U << 0),
42 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY = (1U << 1),
43 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN = (1U << 2),
44 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END = (1U << 3),
45 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN = (1U << 4),
46 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END = (1U << 5),
2ec84d26
PP
47 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS = (1U << 6),
48 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS = (1U << 7),
fa054faf
PP
49};
50
bd14d768
PP
51enum bt_notification_iterator_state {
52 /* Iterator is active, not at the end yet, and not finalized. */
53 BT_NOTIFICATION_ITERATOR_STATE_ACTIVE,
54
55 /*
56 * Iterator is ended, not finalized yet: the "next" method
57 * returns BT_NOTIFICATION_ITERATOR_STATUS_END.
58 */
59 BT_NOTIFICATION_ITERATOR_STATE_ENDED,
60
61 /*
62 * Iterator is finalized, but not at the end yet. This means
63 * that the "next" method can still return queued notifications
64 * before returning the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
65 * status.
66 */
67 BT_NOTIFICATION_ITERATOR_STATE_FINALIZED,
68
69 /*
70 * Iterator is finalized and ended: the "next" method always
71 * returns BT_NOTIFICATION_ITERATOR_STATUS_CANCELED.
72 */
73 BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED,
74};
75
47e5a032 76struct bt_notification_iterator {
b8a06801 77 struct bt_object base;
bd14d768
PP
78 struct bt_component *upstream_component; /* Weak */
79 struct bt_port *upstream_port; /* Weak */
80 struct bt_connection *connection; /* Weak */
3230ee6b
PP
81 struct bt_notification *current_notification; /* owned by this */
82 GQueue *queue; /* struct bt_notification * (owned by this) */
83
84 /*
85 * This hash table keeps the state of a stream as viewed by
86 * this notification iterator. This is used to:
87 *
88 * * Automatically enqueue "stream begin", "packet begin",
89 * "packet end", and "stream end" notifications depending
90 * on the stream's state and on the next notification returned
91 * by the upstream component.
92 *
93 * * Make sure that, once the notification iterator has seen
94 * a "stream end" notification for a given stream, that no
95 * other notifications which refer to this stream can be
96 * delivered by this iterator.
97 *
98 * The key (struct bt_ctf_stream *) is not owned by this. The
99 * value is an allocated state structure.
100 */
101 GHashTable *stream_states;
102
103 /*
104 * This is an array of actions which can be rolled back. It's
105 * similar to the memento pattern, but it's not exactly that. It
106 * is allocated once and reset for each notification to process.
107 * More details near the implementation.
108 */
109 GArray *actions;
110
fa054faf
PP
111 /*
112 * This is a mask of notifications to which the user of this
113 * iterator is subscribed
114 * (see enum bt_notification_iterator_notif_type above).
115 */
116 uint32_t subscription_mask;
117
bd14d768 118 enum bt_notification_iterator_state state;
47e5a032
JG
119 void *user_data;
120};
121
890882ef
PP
122static inline
123struct bt_notification_iterator *bt_notification_iterator_from_private(
124 struct bt_private_notification_iterator *private_notification_iterator)
125{
126 return (void *) private_notification_iterator;
127}
128
129static inline
130struct bt_private_notification_iterator *
131bt_private_notification_iterator_from_notification_iterator(
132 struct bt_notification_iterator *notification_iterator)
133{
134 return (void *) notification_iterator;
135}
136
47e5a032 137BT_HIDDEN
73d5c1ad
PP
138enum bt_connection_status bt_notification_iterator_create(
139 struct bt_component *upstream_comp,
fa054faf 140 struct bt_port *upstream_port,
bd14d768 141 const enum bt_notification_type *notification_types,
73d5c1ad
PP
142 struct bt_connection *connection,
143 struct bt_notification_iterator **iterator);
47e5a032 144
bd14d768
PP
145BT_HIDDEN
146void bt_notification_iterator_finalize(
147 struct bt_notification_iterator *iterator);
148
149BT_HIDDEN
150void bt_notification_iterator_set_connection(
151 struct bt_notification_iterator *iterator,
152 struct bt_connection *connection);
153
a36bfb16
PP
154static inline
155const char *bt_notification_iterator_status_string(
156 enum bt_notification_iterator_status status)
157{
158 switch (status) {
159 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
160 return "BT_NOTIFICATION_ITERATOR_STATUS_CANCELED";
161 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
162 return "BT_NOTIFICATION_ITERATOR_STATUS_AGAIN";
163 case BT_NOTIFICATION_ITERATOR_STATUS_END:
164 return "BT_NOTIFICATION_ITERATOR_STATUS_END";
165 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
166 return "BT_NOTIFICATION_ITERATOR_STATUS_OK";
167 case BT_NOTIFICATION_ITERATOR_STATUS_INVALID:
168 return "BT_NOTIFICATION_ITERATOR_STATUS_INVALID";
169 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR:
170 return "BT_NOTIFICATION_ITERATOR_STATUS_ERROR";
171 case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM:
172 return "BT_NOTIFICATION_ITERATOR_STATUS_NOMEM";
173 case BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED:
174 return "BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED";
175 default:
176 return "(unknown)";
177 }
178};
179
8f9d7550
PP
180static inline
181const char *bt_notification_iterator_state_string(
182 enum bt_notification_iterator_state state)
183{
184 switch (state) {
185 case BT_NOTIFICATION_ITERATOR_STATE_ACTIVE:
186 return "BT_NOTIFICATION_ITERATOR_STATE_ACTIVE";
187 case BT_NOTIFICATION_ITERATOR_STATE_ENDED:
188 return "BT_NOTIFICATION_ITERATOR_STATE_ENDED";
189 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED:
190 return "BT_NOTIFICATION_ITERATOR_STATE_FINALIZED";
191 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
192 return "BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED";
193 default:
194 return "(unknown)";
195 }
196};
197
33b34c43 198#endif /* BABELTRACE_COMPONENT_NOTIFICATION_ITERATOR_INTERNAL_H */
This page took 0.042558 seconds and 4 git commands to generate.