Skip to contentSkip to conversation

libtracefs: Fix enable_disable_all() return value

PATCH In review Yordan Karadzhov (VMware) 2 participants 6 messages Jun 8, 2021 – Jun 11, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 libtracefs: Fix enable_disable_all() return value

enable_disable_all() is a static method used internally by the tracefs_event_enable/disable() API. In some cases its return value gets propagated as a return of the API function but this value do not obey the description given in the documentation.

Fixes fc94d1a (libtracefs: Add tracefs_event_enable/disable() API)
---
 src/tracefs-events.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/tracefs-events.c +3 -1
index 55316c6..8b2d9ef 100644
@@ -822,8 +822,10 @@ static int enable_disable_all(struct tracefs_instance *instance,
 			      bool enable)
 {
 	const char *str = enable ? "1" : "0";
+	int ret;
 
-	return tracefs_instance_file_write(instance, "events/enable", str);
+	ret = tracefs_instance_file_write(instance, "events/enable", str);
+	return ret < 0 ? ret : 0;
 }
 
 static int event_enable_disable(struct tracefs_instance *instance,
Signature · 1 line
2.27.0
#1 lore raw

PATCH 2/4 libtracefs: Fix event_enable_disable() return value

event_enable_disable() is a static method used internally by the tracefs_event_enable/disable() API. The returned value do not obey the description given in the documentation. Note that "ret" must be set to -1 right before the beginning of the loop.

Fixes fc94d1a (libtracefs: Add tracefs_event_enable/disable() API)
---
 src/tracefs-events.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/tracefs-events.c +2 -1
index 8b2d9ef..0fef64f 100644
@@ -835,7 +835,7 @@ static int event_enable_disable(struct tracefs_instance *instance,
 	regex_t system_re, event_re;
 	char **systems;
 	char **events = NULL;
-	int ret = -1;
+	int ret;
 	int s, e;
 
 	/* Handle all events first */
@@ -860,6 +860,7 @@ static int event_enable_disable(struct tracefs_instance *instance,
 		}
 	}
 
+	ret = -1;
 	for (s = 0; systems[s]; s++) {
 		if (system && !match(systems[s], &system_re))
 			continue;
Signature · 1 line
2.27.0
#2 lore raw

PATCH 3/4 libtracefs: Fix typo in function name

In reply to Yordan Karadzhov (VMware)
Fixes 5c013e7 (libtracefs: New APIs for trace options)
---
 include/tracefs.h   | 2 +-
 src/tracefs-tools.c | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/tracefs.h +1 -1
index 08dda13..e29b550 100644
@@ -159,7 +159,7 @@ bool tracefs_option_is_supported(struct tracefs_instance *instance, enum tracefs
 const struct tracefs_options_mask *tracefs_options_get_enabled(struct tracefs_instance *instance);
 bool tracefs_option_is_enabled(struct tracefs_instance *instance, enum tracefs_option_id id);
 int tracefs_option_enable(struct tracefs_instance *instance, enum tracefs_option_id id);
-int tracefs_option_diasble(struct tracefs_instance *instance, enum tracefs_option_id id);
+int tracefs_option_disable(struct tracefs_instance *instance, enum tracefs_option_id id);
 const char *tracefs_option_name(enum tracefs_option_id id);
 enum tracefs_option_id tracefs_option_id(const char *name);
 
src/tracefs-tools.c +2 -2
index 993fb3c..ca40d4d 100644
@@ -307,13 +307,13 @@ int tracefs_option_enable(struct tracefs_instance *instance, enum tracefs_option
 }
 
 /**
- * tracefs_option_diasble - Disable trace option
+ * tracefs_option_disable - Disable trace option
  * @instance: ftrace instance, can be NULL for the top instance
  * @id: trace option id
  *
  * Returns -1 in case of an error or 0 otherwise
  */
-int tracefs_option_diasble(struct tracefs_instance *instance, enum tracefs_option_id id)
+int tracefs_option_disable(struct tracefs_instance *instance, enum tracefs_option_id id)
 {
 	return trace_config_option(instance, id, false);
 }
Signature · 1 line
2.27.0
#3 lore raw

PATCH 4/4 libtracefs: Propagate the return value of the callback function

In reply to Yordan Karadzhov (VMware)

Currently the return value of the callback function can be used to stop pulling data from the trace buffer, however this return value is lost and the user has no idea if tracefs_iterate_raw_events() terminated because there was no more data or because this was requested from the callback function. If we propagate the return value of the callback, this can be used in cases like the one below:

int callback(struct tep_event *event, struct tep_record *record,
int cpu, void *py_func)

{
	....

	return (something) ? 0 : 1
}

int main()

{
	int ret;

	....

	while(ret == 0)
		ret = tracefs_iterate_raw_events(tep, instance,
		                                 NULL, 0,
		                                 callback, NULL);

	....

Here the user can effectively terminate the pulling the data from inside the callback.

---
 src/tracefs-events.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/tracefs-events.c +2 -1
index 0fef64f..0a87d28 100644
@@ -131,7 +131,8 @@ static int read_cpu_pages(struct tep_handle *tep, struct cpu_iterate *cpus, int
 		}
 		if (j < count) {
 			if (callback(cpus[j].event, &cpus[j].record, cpus[j].cpu, callback_context))
-				break;
+				return -1;
+
 			cpus[j].event = NULL;
 			read_next_record(tep, cpus + j);
 		} else {
Signature · 1 line
2.27.0
#4 lore raw

Re: [PATCH 4/4] libtracefs: Propagate the return value of the callback function

[ Sending again, but this time with "Reply-all" to include the mailing list ]

Quoted 28 lines · On Tue, 8 Jun 2021 16:55:03 +0300 "Yordan Karadzhov (VMware)"…

Currently the return value of the callback function can be used to stop pulling data from the trace buffer, however this return value is lost and the user has no idea if tracefs_iterate_raw_events() terminated because there was no more data or because this was requested from the callback function. If we propagate the return value of the callback, this can be used in cases like the one below:

int callback(struct tep_event *event, struct tep_record *record,
int cpu, void *py_func)

{
	....

	return (something) ? 0 : 1
}

int main()

{
	int ret;

	....

	while(ret == 0)
		ret = tracefs_iterate_raw_events(tep, instance,
		                                 NULL, 0,
		                                 callback, NULL);

	....
Quoted 2 lines

Here the user can effectively terminate the pulling the data from inside the callback.

With this change, the user can't tell if returned -1 due to an error or because the callback ended early, (which is not considered an error).

The proper way for an application to handle this, is to pass in a context structure, and have the callback set a value if it exits early or not. I've done this already for needing the same information.

No change to libtracefs is needed. This functionality is already available with the current design:

struct my_struct {
	bool	stopped;
};

int callback(struct tep_event *event, struct tep_record *record,
int cpu, void *data)

{
	struct my_struct *my_data = data;

	[..]
	if (condition) {
		my_data->stopped = true;
		return 1;
	}
	return 0;
}

int main()

{
	struct my_struct my_data = { .stopped = false };
	int ret = 0;

	while (!ret && !my_data.stopped)
		ret = tracefs_iterate_raw_events(tep, instance, NULL, 0,
					callback, &my_data);
}

Now, if you are using this within python, and you want the python wrapper to pass data as well, you just need to add that to the struct:

struct my_struct {
	bool		stopped;
	func		*python_callback;
	void		*python_data;
}

int callback(struct tep_event *event, struct tep_record *record,
int cpu, void *data)

{
	struct my_struct *my_data = data;
	int ret;

	[..]
	ret = my_data->python_callback(event, record, cpu, data->python_data);

	if (ret) {
		my_data->stopped = true;
		return 1;
	}
	return 0;
}

int python_iterator(pthyon_callback, python_data)

{
	struct my_struct my_data = { .stopped = false };
	int ret = 0;

	my_data.python_data = python_data;
	my_data.python_callback = python_callback;

	while (!ret && !my_data.stopped)
		ret = tracefs_iterate_raw_events(tep, instance, NULL, 0,
					callback, &my_data);
}

So this patch isn't needed.

-- Steve

#5 lore raw

Re: [PATCH 4/4] libtracefs: Propagate the return value of the callback function

Quoted 111 lines · Steven Rostedt

[ Sending again, but this time with "Reply-all" to include the mailing list ]

Quoted 28 lines · On Tue, 8 Jun 2021 16:55:03 +0300 "Yordan Karadzhov (VMware)"…

Currently the return value of the callback function can be used to stop pulling data from the trace buffer, however this return value is lost and the user has no idea if tracefs_iterate_raw_events() terminated because there was no more data or because this was requested from the callback function. If we propagate the return value of the callback, this can be used in cases like the one below:

int callback(struct tep_event *event, struct tep_record *record,
int cpu, void *py_func)

{
	....

	return (something) ? 0 : 1
}

int main()

{
	int ret;

	....

	while(ret == 0)
		ret = tracefs_iterate_raw_events(tep, instance,
		                                 NULL, 0,
		                                 callback, NULL);

	....
Quoted 2 lines

Here the user can effectively terminate the pulling the data from inside the callback.

With this change, the user can't tell if returned -1 due to an error or because the callback ended early, (which is not considered an error).

The proper way for an application to handle this, is to pass in a context structure, and have the callback set a value if it exits early or not. I've done this already for needing the same information.

No change to libtracefs is needed. This functionality is already available with the current design:

struct my_struct {
	bool	stopped;
};

int callback(struct tep_event *event, struct tep_record *record,
int cpu, void *data)

{
	struct my_struct *my_data = data;

	[..]
	if (condition) {
		my_data->stopped = true;
		return 1;
	}
	return 0;
}

int main()

{
	struct my_struct my_data = { .stopped = false };
	int ret = 0;

	while (!ret && !my_data.stopped)
		ret = tracefs_iterate_raw_events(tep, instance, NULL, 0,
					callback, &my_data);
}

Now, if you are using this within python, and you want the python wrapper to pass data as well, you just need to add that to the struct:

struct my_struct {
	bool		stopped;
	func		*python_callback;
	void		*python_data;
}

int callback(struct tep_event *event, struct tep_record *record,
int cpu, void *data)

{
	struct my_struct *my_data = data;
	int ret;

	[..]
	ret = my_data->python_callback(event, record, cpu, data->python_data);

	if (ret) {
		my_data->stopped = true;
		return 1;
	}
	return 0;
}

int python_iterator(pthyon_callback, python_data)

{
	struct my_struct my_data = { .stopped = false };
	int ret = 0;

	my_data.python_data = python_data;
	my_data.python_callback = python_callback;

	while (!ret && !my_data.stopped)
		ret = tracefs_iterate_raw_events(tep, instance, NULL, 0,
					callback, &my_data);
}
Quoted 1 line

So this patch isn't needed.

I agree. Your solution is better.

Thanks!
Yordan

Quoted 3 lines

-- Steve