CTF writer: Add function to add an integer environment field value
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 2/2 CTF writer: Add function to add an integer environment field value
From the Python API, it's only possible to set an environment field value as a string (whatever you pass, it gets stringified). This patch adds a function to the CTF writer to allow setting an integer (int64_t) environment field, an then exposes it in the Python interface.
The Python method Writer.add_environment_field now uses the type of the passed value to determine which underlying C function to call (string or integer). Any other type is rejected. This causes a behavior change, since passing an integer value to add_environment_field used to produce a string version of the value, whereas it will now produce an integer version. However, I think it will now behave more closely to the expectation of a lambda user.
Example:
w.add_environment_field("foo", 2)
Result before:
env {
foo = "2";
};Result after:
env {
foo = 2;
};--- bindings/python/babeltrace/nativebt.i | 2 ++ bindings/python/babeltrace/writer.py | 18 +++++++++++++++--- formats/ctf/writer/writer.c | 16 ++++++++++++++++ include/babeltrace/ctf-writer/writer.h | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-)
bindings/python/babeltrace/nativebt.i +2 -0
@@ -635,6 +635,7 @@ void bt_ctf_stream_put(struct bt_ctf_stream *stream); %rename("_bt_ctf_writer_create") bt_ctf_writer_create(const char *path); %rename("_bt_ctf_writer_create_stream") bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class); %rename("_bt_ctf_writer_add_environment_field") bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value); +%rename("_bt_ctf_writer_add_environment_field_int64") bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, const char *name, int64_t value); %rename("_bt_ctf_writer_add_clock") bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock); %newobject bt_ctf_writer_get_metadata_string; %rename("_bt_ctf_writer_get_metadata_string") bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
@@ -646,6 +647,7 @@ void bt_ctf_stream_put(struct bt_ctf_stream *stream); struct bt_ctf_writer *bt_ctf_writer_create(const char *path); struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class); int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value); +int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, const char *name, int64_t value); int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock); char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer); void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);
bindings/python/babeltrace/writer.py +15 -3
@@ -2141,11 +2141,23 @@ class Writer: Sets the CTF environment variable named *name* to value *value* (converted to a string). - :exc:`ValueError` is raised on error. + :exc:`ValueError` or `TypeError` is raised on error. """ - ret = nbt._bt_ctf_writer_add_environment_field(self._w, str(name), - str(value)) + if type(name) != str: + raise TypeError("Field name must be a string.") + + t = type(value) + + if t == str: + ret = nbt._bt_ctf_writer_add_environment_field(self._w, name, + value) + elif t == int: + ret = nbt._bt_ctf_writer_add_environment_field_int64(self._w, + name, + value) + else: + raise TypeError("Value type is not supported.") if ret < 0: raise ValueError("Could not add environment field to trace.")
formats/ctf/writer/writer.c +16 -0
@@ -209,6 +209,22 @@ end:return ret; } +int bt_ctf_writer_add_environment_field_int64 (struct bt_ctf_writer *writer, +const char *name, +int64_t value) +{ +int ret =-1 ; + +if (!writer || !name) { + goto end; + } + + ret =bt_ctf_trace_set_environment_field_integer (writer->trace, name, + value); +end: +return ret; +} +int bt_ctf_writer_add_clock (struct bt_ctf_writer *writer,struct bt_ctf_clock *clock) {
include/babeltrace/ctf-writer/writer.h +17 -0
@@ -96,6 +96,23 @@ extern int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *value);/* +* bt_ctf_writer_add_environment_field_int64: add an environment field to the trace. +* +* Add an environment field to the trace. The name and value parameters are +* copied. +* +* @param writer Writer instance. +* @param name Name of the environment field (will be copied). +* @param value Value of the environment field. +* +* Returns 0 on success, a negative value on error. +*/ +extern int bt_ctf_writer_add_environment_field_int64 ( +struct bt_ctf_writer *writer, +const char *name, +int64_t value); + +/* * bt_ctf_writer_add_clock: add a clock to the trace. * * Add a clock to the trace. Clocks assigned to stream classes must be
Signature · 6 lines
2.8.3 _______________________________________________ lttng-dev mailing list [email protected] https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 6 lines · Simon Marchi
From the Python API, it's only possible to set an environment field value as a string (whatever you pass, it gets stringified). This patch adds a function to the CTF writer to allow setting an integer (
int64_t) environment field, an then exposes it in the Python interface.
Quoted 7 lines
The Python method Writer.add_environment_field now uses the type of the passed value to determine which underlying C function to call (string or integer). Any other type is rejected. This causes a behavior change, since passing an integer value to
add_environment_fieldused to produce a string version of the value, whereas it will now produce an integer version. However, I think it will now behave more closely to the expectation of a lambda user.
Thanks for this, it's a great little addition.
Quoted 4 lines
Example:
w.add_environment_field("foo", 2)
Not directly related, but an interface like os.environ would be nice here I think:
w.env["foo"] = 2
Quoted 46 lines
Result before:
env { foo = "2"; };Result after:
env { foo = 2; };Signed-off-by Simon Marchi <[email protected]>--- bindings/python/babeltrace/nativebt.i | 2 ++ bindings/python/babeltrace/writer.py | 18 +++++++++++++++--- formats/ctf/writer/writer.c | 16 ++++++++++++++++ include/babeltrace/ctf-writer/writer.h | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-)bindings/python/babeltrace/nativebt.i +2
-0@@ -635,6 +635,7 @@ void bt_ctf_stream_put(struct bt_ctf_stream *stream); %rename("_bt_ctf_writer_create") bt_ctf_writer_create(const char *path); %rename("_bt_ctf_writer_create_stream") bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class); %rename("_bt_ctf_writer_add_environment_field") bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value); +%rename("_bt_ctf_writer_add_environment_field_int64") bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, const char *name, int64_t value); %rename("_bt_ctf_writer_add_clock") bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock); %newobject bt_ctf_writer_get_metadata_string; %rename("_bt_ctf_writer_get_metadata_string") bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);@@ -646,6 +647,7 @@ void bt_ctf_stream_put(struct bt_ctf_stream *stream); struct bt_ctf_writer *bt_ctf_writer_create(const char *path); struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class); int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value); +int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, const char *name, int64_t value); int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock); char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer); void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);bindings/python/babeltrace/writer.py +0
-0
bindings/python/babeltrace/writer.py class Writer: · line 2144
Quoted 5 lines
bindings/python/babeltrace/writer.py +1
-1@@ -2141,11 +2141,23 @@ class Writer: Sets the CTF environment variable named *name* to value *value* (converted to a string). - :exc:`ValueError` is raised on error. + :exc:`ValueError` or `TypeError` is raised on error.
Use :exc:TypeError.
bindings/python/babeltrace/writer.py class Writer: · line 2147
Quoted 5 lines
bindings/python/babeltrace/writer.py +1
-2""" - ret = nbt._bt_ctf_writer_add_environment_field(self._w, str(name), - str(value)) + if type(name) != str:
Use is operator.
bindings/python/babeltrace/writer.py class Writer: · line 2150
Quoted 3 lines
bindings/python/babeltrace/writer.py +3
-0+ raise TypeError("Field name must be a string.") + + t = type(value)
value_type might be a better variable name.
bindings/python/babeltrace/writer.py class Writer: · line 2152
Quoted 2 lines
bindings/python/babeltrace/writer.py +2
-0+ + if t == str:
Use is operator.
bindings/python/babeltrace/writer.py class Writer: · line 2155
Quoted 3 lines
bindings/python/babeltrace/writer.py +3
-0+ ret = nbt._bt_ctf_writer_add_environment_field(self._w, name, + value) + elif t == int:
Use is operator.
bindings/python/babeltrace/writer.py class Writer: · line 2158
Quoted 3 lines
bindings/python/babeltrace/writer.py +3
-0+ ret = nbt._bt_ctf_writer_add_environment_field_int64(self._w, + name, + value)
Could you check that value's value is in the range of the int64_t type, so that we catch an illegal operation earlier? Otherwise it's just going to "work" an write the wrong value, I guess.
Thanks again,
Phil
Quoted 63 lines
bindings/python/babeltrace/writer.py +2
-0+ else: + raise TypeError("Value type is not supported.") if ret < 0: raise ValueError("Could not add environment field to trace.")formats/ctf/writer/writer.c +16
-0@@ -209,6 +209,22 @@ end:return ret; } +int bt_ctf_writer_add_environment_field_int64 (struct bt_ctf_writer *writer, +const char *name, +int64_t value) +{ +int ret =-1 ; + +if (!writer || !name) { + goto end; + } + + ret =bt_ctf_trace_set_environment_field_integer (writer->trace, name, + value); +end: +return ret; +} +int bt_ctf_writer_add_clock (struct bt_ctf_writer *writer,struct bt_ctf_clock *clock) {include/babeltrace/ctf-writer/writer.h +17
-1@@ -96,6 +96,23 @@ extern int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *value);/* +* bt_ctf_writer_add_environment_field_int64: add an environment field to the trace. +* +* Add an environment field to the trace. The name and value parameters are +* copied. +* +* @param writer Writer instance. +* @param name Name of the environment field (will be copied). +* @param value Value of the environment field. +* +* Returns 0 on success, a negative value on error. +*/ +extern int bt_ctf_writer_add_environment_field_int64 ( +struct bt_ctf_writer *writer, +const char *name, +int64_t value); + +/* * bt_ctf_writer_add_clock: add a clock to the trace. * * Add a clock to the trace. Clocks assigned to stream classes must be -- 2.8.3
Quoted 4 lines
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
A message that is not in the archive (sent off-list or not yet received): CAB4xu_3st_MLrg2nkJoMvs6Hmow-hOD2QknBKa6qt0PyAm1h-A@mail.gmail.com
Quoted 1 line · Philippe Proulx
Thanks for this, it's a great little addition.
Thanks for the feedback!
Quoted 8 lines
Quoted 3 lines
Example:
w.add_environment_field("foo", 2)Not directly related, but an interface like
os.environwould be nice here I think:w.env["foo"] = 2
Sure, not today though :).
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +1
-1- :exc:`ValueError` is raised on error. + :exc:`ValueError` or `TypeError` is raised on error.Use :exc:
TypeError.
Applied locally.
Quoted 3 lines
Quoted 1 line
+ if type(name) != str:
Use
isoperator.
Applied locally:
if type(name) is not str:
Quoted 3 lines
Quoted 1 line
+ t = type(value)
value_typemight be a better variable name.
It doesn't provide much to use a variable in the first place. I embedded type(value) in the two expressions.
Quoted 3 lines
Quoted 1 line
+ if t == str:
Use
isoperator.
Done.
Quoted 3 lines
Quoted 1 line
+ elif t == int:
Use
isoperator.
Done.
Quoted 10 lines
Quoted 6 lines
+ ret =
nbt._bt_ctf_writer_add_environment_field_int64(self._w, +
name,
+
value)Could you check that
value's value is in the range of theint64_ttype, so that we catch an illegal operation earlier? Otherwise it's just going to "work" an write the wrong value, I guess.
It seems like SWIG does some validation. Passing an out-of-range value throws:
Traceback (most recent call last):
File "babeltrace/examples/ctf_writer.py", line 52, in <module>
writer.add_environment_field("age_of_the_captain",
9223372036854775807+1)
File
"/tmp/babeltrace/lib/python3.5/site-packages/babeltrace/writer.py", line 2158, in add_environment_field
value)
OverflowError: in method
'_bt_ctf_writer_add_environment_field_int64', argument 3 of type 'int64_t'
So I think we are covered, unless you'd like if the error message was more precise?
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 93 lines · Simon Marchi
Quoted 2 lines · Philippe Proulx
Thanks for this, it's a great little addition.
Thanks for the feedback!
Quoted 10 lines
Quoted 3 lines
Example:
w.add_environment_field("foo", 2)Not directly related, but an interface like
os.environwould be nice here
I think:w.env["foo"] = 2Sure, not today though :).
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +1
-1- :exc:`ValueError` is raised on error. + :exc:`ValueError` or `TypeError` is raised on error.Use :exc:
TypeError.Applied locally.
Quoted 4 lines
Quoted 1 line
+ if type(name) != str:
Use
isoperator.Applied locally:
if type(name) is not str:
Quoted 4 lines
Quoted 1 line
+ t = type(value)
value_typemight be a better variable name.It doesn't provide much to use a variable in the first place. I embedded type(value) in the two expressions.
Quoted 4 lines
Quoted 1 line
+ if t == str:
Use
isoperator.Done.
Quoted 4 lines
Quoted 1 line
+ elif t == int:
Use
isoperator.Done.
Quoted 10 lines
Review
Quoted 4 lines
+ ret =
nbt._bt_ctf_writer_add_environment_field_int64(self._w,(quoted diff) +2
-0+ name, + value)Could you check that
value's value is in the range of theint64_ttype, so that we catch an illegal operation earlier? Otherwise it's just going to
"work" an write the wrong value, I guess.It seems like SWIG does some validation. Passing an out-of-range value throws:
Traceback (most recent call last):
File "babeltrace/examples/ctf_writer.py", line 52, in <module>
writer.add_environment_field("age_of_the_captain",
9223372036854775807+1)
File
"/tmp/babeltrace/lib/python3.5/site-packages/babeltrace/writer.py", line 2158, inadd_environment_field
value)
OverflowError: in method '_bt_ctf_writer_add_environment_field_int64',
argument 3 of type 'int64_t'
Quoted 2 lines
So I think we are covered, unless you'd like if the error message was more precise?
Given this rare (I think) condition, I think that's enough, at least to catch the overflow in Python. Jérémie will confirm.
Phil
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Re: [PATCH babeltrace 1/2] python: Add stream event context support
In reply to a message not in the archiveBoth patches were merged in master.
Thanks!
Jérémie
Quoted 171 lines · Simon Marchi
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 <[email protected]>--- 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(+)bindings/python/babeltrace/examples/ctf_writer.py +7
-0@@ -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 flushingbindings/python/babeltrace/nativebt.i +8
-0@@ -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);bindings/python/babeltrace/writer.py +61
-1@@ -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
_______________________________________________
lttng-dev mailing list
[email protected]
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Signature · 7 lines
Jérémie Galarneau EfficiOS Inc. http://www.efficios.com _______________________________________________ lttng-dev mailing list [email protected] https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev