[lttng-dev] [PATCH babeltrace 1/2] python: Add stream event context support

Simon Marchi simon.marchi at polymtl.ca
Sun Jun 19 02:54:51 UTC 2016


This patch adds the possibility to get/set the event context type of a
stream class and set the stream context value of an event from Python.

>From my experience, the value setter (Event.stream_context.setter) is
not so useful, since it looks like the context is already instanciated
when you create the event.  So in order to set the values in it, you
only need to get the existing instance and set its fields.  There is no
need to to re-assign the stream context instance to the event.  I still
put it there for completeness.

I added an example of the usage to the existing CTF writer Python
example.

Signed-off-by: Simon Marchi <simon.marchi at polymtl.ca>
---
 bindings/python/babeltrace/examples/ctf_writer.py |  7 +++
 bindings/python/babeltrace/nativebt.i             |  8 +++
 bindings/python/babeltrace/writer.py              | 61 +++++++++++++++++++++++
 3 files changed, 76 insertions(+)

diff --git a/bindings/python/babeltrace/examples/ctf_writer.py b/bindings/python/babeltrace/examples/ctf_writer.py
index d60eb3d..03e01ea 100644
--- a/bindings/python/babeltrace/examples/ctf_writer.py
+++ b/bindings/python/babeltrace/examples/ctf_writer.py
@@ -71,6 +71,11 @@ for field in packet_context_type.fields:
 packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
 stream_class.packet_context_type = packet_context_type
 
+# Set a stream event context
+stream_event_context_type = btw.StructureFieldDeclaration()
+stream_event_context_type.add_field(int32_type, "field_in_stream_event_context")
+stream_class.event_context_type = stream_event_context_type
+
 # Create a string type
 string_type = btw.StringFieldDeclaration()
 
@@ -135,6 +140,8 @@ for i in range(100):
     integer_field = enumeration_field.container
     enumeration_field.value = i % 10
 
+    event.stream_context.field("field_in_stream_event_context").value = i * 10
+
     stream.append_event(event)
 
 # Populate custom packet context field before flushing
diff --git a/bindings/python/babeltrace/nativebt.i b/bindings/python/babeltrace/nativebt.i
index 15726ba..ad20c13 100644
--- a/bindings/python/babeltrace/nativebt.i
+++ b/bindings/python/babeltrace/nativebt.i
@@ -560,6 +560,8 @@ void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
 %rename("_bt_ctf_event_get_payload_by_index") bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, int index);
 %rename("_bt_ctf_event_get") bt_ctf_event_get(struct bt_ctf_event *event);
 %rename("_bt_ctf_event_put") bt_ctf_event_put(struct bt_ctf_event *event);
+%rename("_bt_ctf_event_get_stream_event_context") bt_ctf_event_get_stream_event_context(struct bt_ctf_event *event);
+%rename("_bt_ctf_event_set_stream_event_context") bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, struct bt_ctf_field *stream_event_context);
 
 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class);
 struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event);
@@ -569,6 +571,8 @@ int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struc
 struct bt_ctf_field *bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, int index);
 void bt_ctf_event_get(struct bt_ctf_event *event);
 void bt_ctf_event_put(struct bt_ctf_event *event);
+struct bt_ctf_field *bt_ctf_event_get_stream_event_context(struct bt_ctf_event *event);
+int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, struct bt_ctf_field *stream_event_context);
 
 
 /* stream-class.h */
@@ -584,6 +588,8 @@ void bt_ctf_event_put(struct bt_ctf_event *event);
 %rename("_bt_ctf_stream_class_get_event_class_by_name") bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
 %rename("_bt_ctf_stream_class_get_packet_context_type") bt_ctf_stream_class_get_packet_context_type(struct bt_ctf_stream_class *stream_class);
 %rename("_bt_ctf_stream_class_set_packet_context_type") bt_ctf_stream_class_set_packet_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *packet_context_type);
+%rename("_bt_ctf_stream_class_get_event_context_type") bt_ctf_stream_class_get_event_context_type(struct bt_ctf_stream_class *stream_class);
+%rename("_bt_ctf_stream_class_set_event_context_type") bt_ctf_stream_class_set_event_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *event_context_type);
 %rename("_bt_ctf_stream_class_get") bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
 %rename("_bt_ctf_stream_class_put") bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
 
@@ -599,6 +605,8 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(struct bt_ctf_str
 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
 struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(struct bt_ctf_stream_class *stream_class);
 int bt_ctf_stream_class_set_packet_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *packet_context_type);
+struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(struct bt_ctf_stream_class *stream_class);
+int bt_ctf_stream_class_set_event_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *event_context_type);
 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
 
diff --git a/bindings/python/babeltrace/writer.py b/bindings/python/babeltrace/writer.py
index dc65b24..cd609af 100644
--- a/bindings/python/babeltrace/writer.py
+++ b/bindings/python/babeltrace/writer.py
@@ -1747,6 +1747,34 @@ class Event:
         if ret < 0:
             raise ValueError("Could not set event field payload.")
 
+    @property
+    def stream_context(self):
+        """
+        Stream event context field (instance of
+        :class:`StructureField`).
+
+        Set this attribute to assign a stream event context field
+        to this stream.
+
+        :exc:`ValueError` is raised on error.
+        """
+
+        native_field = nbt._bt_ctf_event_get_stream_event_context(self._e)
+
+        if native_field is None:
+            raise ValueError("Invalid Stream.")
+
+        return Field._create_field_from_native_instance(native_field)
+
+    @stream_context.setter
+    def stream_context(self, field):
+        if not isinstance(field, StructureField):
+            raise TypeError("Argument field must be of type StructureField")
+
+        ret = nbt._bt_ctf_event_set_stream_event_context(self._e, field._f)
+
+        if ret < 0:
+            raise ValueError("Invalid stream context field.")
 
 class StreamClass:
     """
@@ -1923,6 +1951,39 @@ class StreamClass:
         if ret < 0:
             raise ValueError("Failed to set packet context type.")
 
+    @property
+    def event_context_type(self):
+        """
+        Stream event context declaration.
+
+        Set this attribute to change the stream event context
+        declaration (must be an instance of
+        :class:`StructureFieldDeclaration`).
+
+        :exc:`ValueError` is raised on error.
+
+        """
+
+        field_type_native = nbt._bt_ctf_stream_class_get_event_context_type(self._sc)
+
+        if field_type_native is None:
+            raise ValueError("Invalid StreamClass")
+
+        field_type = FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
+
+        return field_type
+
+    @event_context_type.setter
+    def event_context_type(self, field_type):
+        if not isinstance(field_type, StructureFieldDeclaration):
+            raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
+
+        ret = nbt._bt_ctf_stream_class_set_event_context_type(self._sc,
+                                                              field_type._ft)
+
+        if ret < 0:
+            raise ValueError("Failed to set event context type.")
+
 
 class Stream:
     """
-- 
2.8.3



More information about the lttng-dev mailing list