Babeltrace 2 C API
2.0.0
Open-source trace manipulation framework
|
This example shows a basic filter component class packaged as a shared object plugin.
The name of the plugin is distill
and the name of the filter component class is theone
. Therefore the component class is identified in the babeltrace2
command-line tool as filter.distill.theone
.
A filter.distill.theone
component removes specific event messages from a stream based on their event class's name.
A filter.distill.theone
component accepts a single initialization parameter, names
, which is an array value of string values. The array value contains the names of the classes of the events to discard.
A filter.distill.theone
component creates a single input port named in
and a single output port named out
.
To simplify this example, a filter.distill.theone
component is not resilient and needs a valid input and valid initialization parameters. The code also doesn't check the return status codes of API functions for simplicity, but you must check them in production code.
The filter component class implementation and the shared object plugin macros are in the same file, distill.c
:
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdbool.h>
#include <babeltrace2/babeltrace.h>
struct distill {
};
static
const bt_value *params,
void *initialize_method_data)
{
struct distill *distill = malloc(sizeof(*distill));
distill->names_value =
distill);
"in", NULL, &distill->in_port);
"out", NULL, NULL);
}
static
{
free(distill);
}
struct distill_message_iterator {
struct distill *distill;
};
static
distill_message_iterator_initialize(
{
struct distill_message_iterator *distill_iter =
malloc(sizeof(*distill_iter));
distill_iter->distill = distill;
distill->in_port, &distill_iter->message_iterator);
}
static
void distill_message_iterator_finalize(
{
struct distill_message_iterator *distill_iter =
free(distill_iter);
}
static
bool message_passes(struct distill_message_iterator *distill_iter,
{
bool passes = true;
passes = false;
goto end;
}
distill_iter->distill->names_value); i++) {
distill_iter->distill->names_value, i));
if (strcmp(name, discard_name) == 0) {
passes = false;
goto end;
}
}
end:
return passes;
}
static
uint64_t *count)
{
struct distill_message_iterator *distill_iter =
uint64_t upstream_message_count;
consume_upstream_messages:
&upstream_messages, &upstream_message_count);
switch (next_status) {
goto end;
goto end;
goto end;
goto end;
default:
break;
}
uint64_t i = 0;
for (uint64_t upstream_i = 0; upstream_i < upstream_message_count;
upstream_i++) {
const bt_message *upstream_message = upstream_messages[upstream_i];
if (message_passes(distill_iter, upstream_message)) {
messages[i] = upstream_message;
i++;
continue;
}
}
if (i == 0) {
goto consume_upstream_messages;
}
*count = i;
end:
return status;
}
theone, distill_message_iterator_initialize);
distill_message_iterator_finalize);
As per the Compile and link a Babeltrace 2 shared object plugin guide, you can build the shared object plugin as such:
$ cc distill.c -fPIC -c $(pkg-config --cflags babeltrace2)
$ ld distill.o -o distill.so -shared $(pkg-config --libs babeltrace2)
With the babeltrace2
tool, you can use a filter.distill.theone
component, reading a CTF trace (see babeltrace2-source.ctf.fs(7)
) for example:
$ babeltrace2 --plugin-path=. /path/to/ctf/trace \
--component=filter.distill.theone \
--params='names=["sched_switch", "rcu_utilization", "kmem_kfree"]'
Success.
Definition: message-iterator-class.h:736
struct bt_value bt_value
Value.
Definition: types.h:107
struct bt_self_message_iterator bt_self_message_iterator
Self message iterator.
Definition: types.h:100
#define BT_PLUGIN_FILTER_COMPONENT_CLASS(_name, _message_iterator_class_next_method)
Alias of BT_PLUGIN_FILTER_COMPONENT_CLASS_WITH_ID() with the _plugin_id parameter set to auto,...
Definition: plugin-dev.h:839
bt_self_component_add_port_status bt_self_component_filter_add_output_port(bt_self_component_filter *self_component, const char *name, void *user_data, bt_self_component_port_output **self_component_port)
Adds an output port named name and having the user data user_data to the filter component self_compon...
void bt_self_component_set_data(bt_self_component *self_component, void *user_data)
Sets the user data of the component self_component to data.
uint64_t bt_value_array_get_length(const bt_value *value)
Returns the length of the array value value.
Try again.
Definition: message-iterator.h:400
void * bt_self_component_get_data(const bt_self_component *self_component)
Returns the user data of the component self_component.
bt_message_iterator_class_initialize_method_status
Status codes for bt_message_iterator_class_initialize_method.
Definition: message-iterator-class.h:674
bt_component_class_initialize_method_status
Status codes for bt_component_class_source_initialize_method, bt_component_class_filter_initialize_me...
Definition: component-class-dev.h:881
#define BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD(_name, _method)
Alias of BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_WITH_ID() with the...
Definition: plugin-dev.h:1923
struct bt_self_component_filter_configuration bt_self_component_filter_configuration
Self filter component configuration.
Definition: types.h:91
void bt_value_get_ref(const bt_value *value)
Increments the reference count of the value value.
struct bt_message_iterator bt_message_iterator
Message iterator.
Definition: types.h:72
#define BT_PLUGIN_MODULE()
Defines a plugin module.
Definition: plugin-dev.h:338
Try again.
Definition: message-iterator-class.h:748
End of iteration.
Definition: message-iterator-class.h:742
struct bt_self_component_port_output bt_self_component_port_output
Self component output port.
Definition: types.h:95
bt_message_iterator_create_from_message_iterator_status bt_message_iterator_create_from_message_iterator(bt_self_message_iterator *self_message_iterator, bt_self_component_port_input *port, bt_message_iterator **message_iterator)
Creates a message iterator on the input port port from another message iterator self_message_iterator...
static bt_self_component * bt_self_component_filter_as_self_component(bt_self_component_filter *self_component)
Upcasts the self filter component self_component to the common bt_self_component type.
Definition: self-component.h:846
Event message.
Definition: message.h:997
const bt_message ** bt_message_array_const
Array of constant messages.
Definition: types.h:196
Out of memory.
Definition: message-iterator.h:406
const bt_value * bt_value_array_borrow_element_by_index_const(const bt_value *value, uint64_t index)
Borrows the element at index index from the array value value (const version).
#define BT_PLUGIN_FILTER_COMPONENT_CLASS_INITIALIZE_METHOD(_name, _method)
Alias of BT_PLUGIN_FILTER_COMPONENT_CLASS_INITIALIZE_METHOD_WITH_ID() with the _plugin_id parameter s...
Definition: plugin-dev.h:1785
bt_message_iterator_next_status bt_message_iterator_next(bt_message_iterator *message_iterator, bt_message_array_const *messages, uint64_t *count)
Returns the next messages of the message iterator message_iterator into the *messages array of size *...
void bt_message_iterator_put_ref(const bt_message_iterator *message_iterator)
Decrements the reference count of the message iterator message_iterator.
const char * bt_event_class_get_name(const bt_event_class *event_class)
Returns the name of the event class event_class.
#define BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD(_name, _method)
Alias of BT_PLUGIN_FILTER_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID() with the _plugin_id parameter set...
Definition: plugin-dev.h:1694
const bt_value * bt_value_map_borrow_entry_value_const(const bt_value *value, const char *key)
Borrows the value of the entry with the key key in the map value value (const version).
const bt_event_class * bt_event_borrow_class_const(const bt_event *event)
Borrows the class of the event event (const version).
Out of memory.
Definition: message-iterator-class.h:754
struct bt_event bt_event
Event.
Definition: types.h:50
const bt_event * bt_message_event_borrow_event_const(const bt_message *message)
Borrows the event of the event message message (const version).
bt_self_component * bt_self_message_iterator_borrow_component(bt_self_message_iterator *self_message_iterator)
Borrows the component which provides the message iterator self_message_iterator.
Success.
Definition: component-class-dev.h:886
void * bt_self_message_iterator_get_data(const bt_self_message_iterator *self_message_iterator)
Returns the user data of the message iterator self_message_iterator.
struct bt_event_class bt_event_class
Event class.
Definition: types.h:51
bt_message_iterator_next_status
Status code for bt_message_iterator_next().
Definition: message-iterator.h:383
Other error.
Definition: message-iterator.h:412
#define BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_FINALIZE_METHOD(_name, _method)
Alias of BT_PLUGIN_FILTER_COMPONENT_CLASS_MESSAGE_ITERATOR_CLASS_FINALIZE_METHOD_WITH_ID() with the _...
Definition: plugin-dev.h:1877
struct bt_self_component_filter bt_self_component_filter
Self filter component.
Definition: types.h:90
void bt_message_put_ref(const bt_message *message)
Decrements the reference count of the message message.
Success.
Definition: message-iterator-class.h:679
End of iteration.
Definition: message-iterator.h:394
#define BT_PLUGIN(_name)
Alias of BT_PLUGIN_WITH_ID() with the _id parameter set to auto.
Definition: plugin-dev.h:426
bt_message_type bt_message_get_type(const bt_message *message)
Returns the type enumerator of the message message.
const char * bt_value_string_get(const bt_value *value)
Returns the raw value of the string value value.
struct bt_self_component_port_input bt_self_component_port_input
Self component input port.
Definition: types.h:93
struct bt_message bt_message
Message.
Definition: types.h:71
bt_message_iterator_class_next_method_status
Status codes for bt_message_iterator_class_next_method.
Definition: message-iterator-class.h:731
void bt_self_message_iterator_set_data(bt_self_message_iterator *self_message_iterator, void *user_data)
Sets the user data of the message iterator self_message_iterator to data.
User error.
Definition: message-iterator-class.h:760
void bt_value_put_ref(const bt_value *value)
Decrements the reference count of the value value.
struct bt_self_message_iterator_configuration bt_self_message_iterator_configuration
Self message iterator configuration.
Definition: types.h:101
bt_self_component_add_port_status bt_self_component_filter_add_input_port(bt_self_component_filter *self_component, const char *name, void *user_data, bt_self_component_port_input **self_component_port)
Adds an input port named name and having the user data user_data to the filter component self_compone...