6 #include <babeltrace2/babeltrace.h>
8 /* Source component's private data */
10 /* Input file path parameter value (owned by this) */
11 const bt_value
*path_value
;
13 /* Stream (owned by this) */
16 /* Event classes for each type of event (owned by this) */
17 bt_event_class
*send_msg_event_class
;
18 bt_event_class
*recv_msg_event_class
;
22 * Creates an event class within `stream_class` named `name`.
25 bt_event_class
*create_event_class(bt_stream_class
*stream_class
,
28 /* Borrow trace class from stream class */
29 bt_trace_class
*trace_class
=
30 bt_stream_class_borrow_trace_class(stream_class
);
32 /* Create a default event class */
33 bt_event_class
*event_class
= bt_event_class_create(stream_class
);
35 /* Name the event class */
36 bt_event_class_set_name(event_class
, name
);
39 * Create an empty structure field class to be used as the
40 * event class's payload field class.
42 bt_field_class
*payload_field_class
=
43 bt_field_class_structure_create(trace_class
);
46 * Create a string field class to be used as the payload field
47 * class's `msg` member.
49 bt_field_class
*msg_field_class
=
50 bt_field_class_string_create(trace_class
);
53 * Append the string field class to the structure field class as the
56 bt_field_class_structure_append_member(payload_field_class
, "msg",
59 /* Set the event class's payload field class */
60 bt_event_class_set_payload_field_class(event_class
, payload_field_class
);
62 /* Put the references we don't need anymore */
63 bt_field_class_put_ref(payload_field_class
);
64 bt_field_class_put_ref(msg_field_class
);
70 * Creates the source component's metadata and stream objects.
73 void create_metadata_and_stream(bt_self_component
*self_component
,
74 struct dust_in
*dust_in
)
76 /* Create a default trace class */
77 bt_trace_class
*trace_class
= bt_trace_class_create(self_component
);
79 /* Create a stream trace class within `trace_class` */
80 bt_stream_class
*stream_class
= bt_stream_class_create(trace_class
);
82 /* Create a default clock class (1 GHz frequency) */
83 bt_clock_class
*clock_class
= bt_clock_class_create(self_component
);
86 * Set `clock_class` as the default clock class of `stream_class`.
88 * This means all the streams created from `stream_class` have a
89 * conceptual default clock which is an instance of `clock_class`.
90 * Any event message created for such a stream has a snapshot of the
91 * stream's default clock.
93 bt_stream_class_set_default_clock_class(stream_class
, clock_class
);
95 /* Create the two event classes we need */
96 dust_in
->send_msg_event_class
= create_event_class(stream_class
,
98 dust_in
->recv_msg_event_class
= create_event_class(stream_class
,
101 /* Create a default trace from (instance of `trace_class`) */
102 bt_trace
*trace
= bt_trace_create(trace_class
);
105 * Create the source component's stream (instance of `stream_class`
108 dust_in
->stream
= bt_stream_create(stream_class
, trace
);
110 /* Put the references we don't need anymore */
111 bt_trace_put_ref(trace
);
112 bt_clock_class_put_ref(clock_class
);
113 bt_stream_class_put_ref(stream_class
);
114 bt_trace_class_put_ref(trace_class
);
118 * Initializes the source component.
121 bt_component_class_initialize_method_status
dust_in_initialize(
122 bt_self_component_source
*self_component_source
,
123 bt_self_component_source_configuration
*configuration
,
124 const bt_value
*params
, void *initialize_method_data
)
126 /* Allocate a private data structure */
127 struct dust_in
*dust_in
= malloc(sizeof(*dust_in
));
130 * Keep a reference of the `path` string value parameter so that the
131 * initialization method of a message iterator can read its string
132 * value to open the file.
134 dust_in
->path_value
=
135 bt_value_map_borrow_entry_value_const(params
, "path");
136 bt_value_get_ref(dust_in
->path_value
);
138 /* Upcast `self_component_source` to the `bt_self_component` type */
139 bt_self_component
*self_component
=
140 bt_self_component_source_as_self_component(self_component_source
);
142 /* Create the source component's metadata and stream objects */
143 create_metadata_and_stream(self_component
, dust_in
);
145 /* Set the component's user data to our private data structure */
146 bt_self_component_set_data(self_component
, dust_in
);
149 * Add an output port named `out` to the source component.
151 * This is needed so that this source component can be connected to
152 * a filter or a sink component. Once a downstream component is
153 * connected, it can create our message iterator.
155 bt_self_component_source_add_output_port(self_component_source
,
158 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
162 * Finalizes the source component.
165 void dust_in_finalize(bt_self_component_source
*self_component_source
)
167 /* Retrieve our private data from the component's user data */
168 struct dust_in
*dust_in
= bt_self_component_get_data(
169 bt_self_component_source_as_self_component(self_component_source
));
171 /* Put all references */
172 bt_value_put_ref(dust_in
->path_value
);
173 bt_event_class_put_ref(dust_in
->send_msg_event_class
);
174 bt_event_class_put_ref(dust_in
->recv_msg_event_class
);
175 bt_stream_put_ref(dust_in
->stream
);
177 /* Free the allocated structure */
181 /* State of a message iterator */
182 enum dust_in_message_iterator_state
{
183 /* Emit a stream beginning message */
184 DUST_IN_MESSAGE_ITERATOR_STATE_STREAM_BEGINNING
,
186 /* Emit an event message */
187 DUST_IN_MESSAGE_ITERATOR_STATE_EVENT
,
189 /* Message iterator is ended */
190 DUST_IN_MESSAGE_ITERATOR_STATE_ENDED
,
193 /* Message iterator's private data */
194 struct dust_in_message_iterator
{
195 /* (Weak) link to the component's private data */
196 struct dust_in
*dust_in
;
198 /* Current message iterator's state */
199 enum dust_in_message_iterator_state state
;
204 /* Buffers to read data from the input file */
205 char name_buffer
[32];
206 char msg_buffer
[1024];
210 * Initializes the message iterator.
213 bt_message_iterator_class_initialize_method_status
214 dust_in_message_iterator_initialize(
215 bt_self_message_iterator
*self_message_iterator
,
216 bt_self_message_iterator_configuration
*configuration
,
217 bt_self_component_port_output
*self_port
)
219 /* Allocate a private data structure */
220 struct dust_in_message_iterator
*dust_in_iter
=
221 malloc(sizeof(*dust_in_iter
));
223 /* Retrieve the component's private data from its user data */
224 struct dust_in
*dust_in
= bt_self_component_get_data(
225 bt_self_message_iterator_borrow_component(self_message_iterator
));
227 /* Keep a link to the component's private data */
228 dust_in_iter
->dust_in
= dust_in
;
230 /* Set the message iterator's initial state */
231 dust_in_iter
->state
= DUST_IN_MESSAGE_ITERATOR_STATE_STREAM_BEGINNING
;
233 /* Get the raw value of the input file path string value */
234 const char *path
= bt_value_string_get(dust_in
->path_value
);
236 /* Open the input file in text mode */
237 dust_in_iter
->file
= fopen(path
, "r");
239 /* Set the message iterator's user data to our private data structure */
240 bt_self_message_iterator_set_data(self_message_iterator
, dust_in_iter
);
242 return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
246 * Finalizes the message iterator.
249 void dust_in_message_iterator_finalize(
250 bt_self_message_iterator
*self_message_iterator
)
252 /* Retrieve our private data from the message iterator's user data */
253 struct dust_in_message_iterator
*dust_in_iter
=
254 bt_self_message_iterator_get_data(self_message_iterator
);
256 /* Close the input file */
257 fclose(dust_in_iter
->file
);
259 /* Free the allocated structure */
264 * Creates a message from the message iterator's input file's current
267 * If there's a line to process, this function creates an event message.
268 * Otherwise it creates a stream end message and sets the message
269 * iterator's state accordingly.
272 bt_message
*create_message_from_line(
273 struct dust_in_message_iterator
*dust_in_iter
,
274 bt_self_message_iterator
*self_message_iterator
)
280 /* Try to read a line from the input file into individual tokens */
281 int count
= fscanf(dust_in_iter
->file
, "%" PRIu64
" %" PRIu64
" %s %[^\n]",
282 ×tamp
, &extra_us
, &dust_in_iter
->name_buffer
[0],
283 &dust_in_iter
->msg_buffer
[0]);
285 /* Reached the end of the file? */
286 if (count
== EOF
|| feof(dust_in_iter
->file
)) {
288 * Reached the end of the file: create a stream end message and
289 * set the message iterator's state to
290 * `DUST_IN_MESSAGE_ITERATOR_STATE_ENDED` so that the next call
291 * to dust_in_message_iterator_next() returns
292 * `BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END` (end of
295 message
= bt_message_stream_end_create(self_message_iterator
,
296 dust_in_iter
->dust_in
->stream
);
297 dust_in_iter
->state
= DUST_IN_MESSAGE_ITERATOR_STATE_ENDED
;
301 /* Choose the correct event class, depending on the event name token */
302 bt_event_class
*event_class
;
304 if (strcmp(dust_in_iter
->name_buffer
, "send-msg") == 0) {
305 event_class
= dust_in_iter
->dust_in
->send_msg_event_class
;
307 event_class
= dust_in_iter
->dust_in
->recv_msg_event_class
;
311 * At this point `timestamp` contains seconds since the Unix epoch.
312 * Multiply it by 1,000,000,000 to get nanoseconds since the Unix
313 * epoch because the stream's clock's frequency is 1 GHz.
315 timestamp
*= UINT64_C(1000000000);
317 /* Add the extra microseconds (as nanoseconds) to `timestamp` */
318 timestamp
+= extra_us
* UINT64_C(1000);
320 /* Create the event message */
321 message
= bt_message_event_create_with_default_clock_snapshot(
322 self_message_iterator
, event_class
, dust_in_iter
->dust_in
->stream
,
326 * At this point `message` is an event message which contains
327 * an empty event object.
329 * We need to fill its fields.
331 * The only field to fill is the payload field's `msg` field
332 * which is the event record's message.
334 * All the references below are borrowed references, therefore we
335 * don't need to put them.
337 bt_event
*event
= bt_message_event_borrow_event(message
);
338 bt_field
*payload_field
= bt_event_borrow_payload_field(event
);
339 bt_field
*msg_field
= bt_field_structure_borrow_member_field_by_index(
342 bt_field_string_set_value(msg_field
, dust_in_iter
->msg_buffer
);
349 * Returns the next message to the message iterator's user.
351 * This method can fill the `messages` array with up to `capacity`
354 * To keep this example simple, we put a single message into `messages`
355 * and set `*count` to 1 (if the message iterator is not ended).
358 bt_message_iterator_class_next_method_status
dust_in_message_iterator_next(
359 bt_self_message_iterator
*self_message_iterator
,
360 bt_message_array_const messages
, uint64_t capacity
,
363 /* Retrieve our private data from the message iterator's user data */
364 struct dust_in_message_iterator
*dust_in_iter
=
365 bt_self_message_iterator_get_data(self_message_iterator
);
368 * This is the message to return (by moving it to the `messages`
371 * We initialize it to `NULL`. If it's not `NULL` after the
372 * processing below, then we move it to the message array.
374 bt_message
*message
= NULL
;
376 /* Initialize the return status to a success */
377 bt_message_iterator_class_next_method_status status
=
378 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
380 switch (dust_in_iter
->state
) {
381 case DUST_IN_MESSAGE_ITERATOR_STATE_STREAM_BEGINNING
:
382 /* Create a stream beginning message */
383 message
= bt_message_stream_beginning_create(self_message_iterator
,
384 dust_in_iter
->dust_in
->stream
);
386 /* Next state: an event message */
387 dust_in_iter
->state
= DUST_IN_MESSAGE_ITERATOR_STATE_EVENT
;
389 case DUST_IN_MESSAGE_ITERATOR_STATE_EVENT
:
391 * Create an event or a stream end message from the message
392 * iterator's input file's current line.
394 * This function also updates the message iterator's state if
397 message
= create_message_from_line(dust_in_iter
,
398 self_message_iterator
);
400 case DUST_IN_MESSAGE_ITERATOR_STATE_ENDED
:
401 /* Message iterator is ended: return the corresponding status */
403 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
409 * We created a message above: move it to the beginning of the
410 * `messages` array, setting `*count` to 1 to indicate that the
411 * array contains a single message.
413 messages
[0] = message
;
424 /* Define the `dust` plugin */
427 /* Define the `input` source component class */
428 BT_PLUGIN_SOURCE_COMPONENT_CLASS(input
, dust_in_message_iterator_next
);
430 /* Set some of the `input` source component class's optional methods */
431 BT_PLUGIN_SOURCE_COMPONENT_CLASS_INITIALIZE_METHOD(input
, dust_in_initialize
);
432 BT_PLUGIN_SOURCE_COMPONENT_CLASS_FINALIZE_METHOD(input
, dust_in_finalize
);
433 BT_PLUGIN_SOURCE_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD(input
,
434 dust_in_message_iterator_initialize
);
435 BT_PLUGIN_SOURCE_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_FINALIZE_METHOD(input
,
436 dust_in_message_iterator_finalize
);