LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 0/2] libtracefs: Make hist variable names unique
@ 2021-08-12  0:55 Steven Rostedt
  2021-08-12  0:55 ` [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique Steven Rostedt
  2021-08-12  0:55 ` [PATCH 2/2] libtracefs: Have end event variables not be the end event field name Steven Rostedt
  0 siblings, 2 replies; 5+ messages in thread
From: Steven Rostedt @ 2021-08-12  0:55 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: linux-kernel, Tom Zanussi, Daniel Bristot de Oliveira,
	Masami Hiramatsu, Steven Rostedt (VMware)

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

In order to be able to have API that can do onmax() and onchange() as well
as include the .save() and .snapshot() actions, we need to make sure that
the variable names are unique even between different events.

The first adds a "randomness" to the name, and the second patch fixes an
issue where the "end" event fields are saved in the same name as they are
called, then the ".save" action can not be used on them.

Steven Rostedt (VMware) (2):
  libtracefs: Add random number to keep synthetic variables unique
  libtracefs: Have end event variables not be the end event field name

 src/tracefs-hist.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique
  2021-08-12  0:55 [PATCH 0/2] libtracefs: Make hist variable names unique Steven Rostedt
@ 2021-08-12  0:55 ` Steven Rostedt
  2021-08-12  8:34   ` Yordan Karadzhov
  2021-08-12  0:55 ` [PATCH 2/2] libtracefs: Have end event variables not be the end event field name Steven Rostedt
  1 sibling, 1 reply; 5+ messages in thread
From: Steven Rostedt @ 2021-08-12  0:55 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: linux-kernel, Tom Zanussi, Daniel Bristot de Oliveira,
	Masami Hiramatsu, Steven Rostedt (VMware)

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

The 'hist' triggers expect that all variables are unique. If two synthetic
events are created, it is possible that they will use the same variable
names, and this can break the logic for synthetic events. Add a random
number to the argument names that will help prevent that from happening.
There's no guarantee that there wont be collisions, but the chances of
that happening is 1 in 65535. If this is a problem, we could possibly look
for variables that are already in use.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 src/tracefs-hist.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/tracefs-hist.c b/src/tracefs-hist.c
index 7f9cf3820611..8783ef4364bd 100644
--- a/src/tracefs-hist.c
+++ b/src/tracefs-hist.c
@@ -13,6 +13,8 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
+#include <time.h>
+#include <sys/types.h>
 
 #include "tracefs.h"
 #include "tracefs-local.h"
@@ -558,6 +560,7 @@ struct tracefs_synth {
 	unsigned int		end_parens;
 	unsigned int		end_state;
 	int			*start_type;
+	char			arg_name[16];
 	int			arg_cnt;
 };
 
@@ -957,7 +960,15 @@ static char *new_arg(struct tracefs_synth *synth)
 	char *arg;
 	int ret;
 
-	ret = asprintf(&arg, "__arg__%d", cnt);
+	/* Create a unique argument name */
+	if (!synth->arg_name[0]) {
+		srand(time(NULL));
+		ret = rand() + gettid();
+		/* Truncate so that ret is at most 65535 (total 13 bytes) */
+		ret &= (1 << 16) - 1;
+		sprintf(synth->arg_name, "__arg_%d_", ret);
+	}
+	ret = asprintf(&arg, "%s%d", synth->arg_name, cnt);
 	if (ret < 0)
 		return NULL;
 
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] libtracefs: Have end event variables not be the end event field name
  2021-08-12  0:55 [PATCH 0/2] libtracefs: Make hist variable names unique Steven Rostedt
  2021-08-12  0:55 ` [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique Steven Rostedt
@ 2021-08-12  0:55 ` Steven Rostedt
  1 sibling, 0 replies; 5+ messages in thread
From: Steven Rostedt @ 2021-08-12  0:55 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: linux-kernel, Tom Zanussi, Daniel Bristot de Oliveira,
	Masami Hiramatsu, Steven Rostedt (VMware)

From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

Currently we have:

 # sqlhist -n wakeup 'select end.next_pid, (end.TIMESTAMP - start.TIMESTAMP) as lat
   from sched_waking as start join sched_switch as end on start.pid = end.next_pid'

produces:

 echo 'wakeup s32 next_pid; u64 lat;' > /sys/kernel/tracing/synthetic_events
 echo 'hist:keys=pid:__arg_18871_1=common_timestamp' > /sys/kernel/tracing/events/sched/sched_waking/trigger
 echo 'hist:keys=next_pid:next_pid=next_pid,lat=common_timestamp-$__arg_18871_1:onmatch(sched.sched_waking).trace(wakeup,$next_pid,$lat)' > /sys/kernel/tracing/events/sched/sched_switch/trigger

The issue is that we have "next_pid=next_pid" where if we want to change
the above to use the "save" action:

 hist:keys=next_pid:next_pid=next_pid,lat=common_timestamp-$__arg_18871_1:onmax($lat).save(next_pid)

It fails with:

   hist:sched:sched_switch: error: Couldn't find field
    Command: hist:keys=next_pid:next_pid=next_pid,lat=common_timestamp-$__arg_18871_1:onmax($lat).save(next_pid)
                                                                       ^

But by having the end vars be unique, then the above "save" works.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 src/tracefs-hist.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/tracefs-hist.c b/src/tracefs-hist.c
index 8783ef4364bd..1e6bde7880f2 100644
--- a/src/tracefs-hist.c
+++ b/src/tracefs-hist.c
@@ -851,16 +851,19 @@ struct tracefs_synth *tracefs_synth_init(struct tep_handle *tep,
 
 static int add_synth_fields(struct tracefs_synth *synth,
 			    const struct tep_format_field *field,
-			    const char *name)
+			    const char *name, const char *var)
 {
 	char **list;
 	char *str;
 	int ret;
 
-	str = add_synth_field(field, name);
+	str = add_synth_field(field, name ? : field->name);
 	if (!str)
 		return -1;
 
+	if (!name)
+		name = var;
+
 	list = tracefs_list_add(synth->synthetic_fields, str);
 	free(str);
 	if (!list)
@@ -942,7 +945,7 @@ int tracefs_synth_add_match_field(struct tracefs_synth *synth,
 	if (ret < 0)
 		goto pop_lists;
 
-	ret = add_synth_fields(synth, key_field, name);
+	ret = add_synth_fields(synth, key_field, name, NULL);
 	if (ret < 0)
 		goto pop_lists;
 
@@ -1061,7 +1064,7 @@ int tracefs_synth_add_compare_field(struct tracefs_synth *synth,
 	if (ret < 0)
 		goto out_free;
 
-	ret = add_synth_fields(synth, start_field, name);
+	ret = add_synth_fields(synth, start_field, name, NULL);
 	if (ret < 0)
 		goto out_free;
 
@@ -1106,7 +1109,7 @@ __hidden int synth_add_start_field(struct tracefs_synth *synth,
 	if (ret)
 		goto out_free;
 
-	ret = add_synth_fields(synth, field, name);
+	ret = add_synth_fields(synth, field, name, NULL);
 	if (ret)
 		goto out_free;
 
@@ -1178,6 +1181,7 @@ int tracefs_synth_add_end_field(struct tracefs_synth *synth,
 				const char *name)
 {
 	const struct tep_format_field *field;
+	char *tmp_var = NULL;
 	int ret;
 
 	if (!synth || !end_field) {
@@ -1186,17 +1190,17 @@ int tracefs_synth_add_end_field(struct tracefs_synth *synth,
 	}
 
 	if (!name)
-		name = end_field;
+		tmp_var = new_arg(synth);
 
 	if (!trace_verify_event_field(synth->end_event, end_field, &field))
 		return -1;
 
-	ret = add_var(&synth->end_vars, name, end_field, false);
+	ret = add_var(&synth->end_vars, name ? : tmp_var, end_field, false);
 	if (ret)
 		goto out;
 
-	ret = add_synth_fields(synth, field, name);
-
+	ret = add_synth_fields(synth, field, name, tmp_var);
+	free(tmp_var);
  out:
 	return ret;
 }
-- 
2.30.2


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique
  2021-08-12  0:55 ` [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique Steven Rostedt
@ 2021-08-12  8:34   ` Yordan Karadzhov
  2021-08-12 14:00     ` Steven Rostedt
  0 siblings, 1 reply; 5+ messages in thread
From: Yordan Karadzhov @ 2021-08-12  8:34 UTC (permalink / raw)
  To: Steven Rostedt, linux-trace-devel
  Cc: linux-kernel, Tom Zanussi, Daniel Bristot de Oliveira, Masami Hiramatsu



On 12.08.21 г. 3:55, Steven Rostedt wrote:
> From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>
> 
> The 'hist' triggers expect that all variables are unique. If two synthetic
> events are created, it is possible that they will use the same variable
> names, and this can break the logic for synthetic events. Add a random
> number to the argument names that will help prevent that from happening.
> There's no guarantee that there wont be collisions, but the chances of
> that happening is 1 in 65535. If this is a problem, we could possibly look
> for variables that are already in use.
> 
> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
> ---
>   src/tracefs-hist.c | 13 ++++++++++++-
>   1 file changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/src/tracefs-hist.c b/src/tracefs-hist.c
> index 7f9cf3820611..8783ef4364bd 100644
> --- a/src/tracefs-hist.c
> +++ b/src/tracefs-hist.c
> @@ -13,6 +13,8 @@
>   #include <errno.h>
>   #include <fcntl.h>
>   #include <limits.h>
> +#include <time.h>
> +#include <sys/types.h>
>   
>   #include "tracefs.h"
>   #include "tracefs-local.h"
> @@ -558,6 +560,7 @@ struct tracefs_synth {
>   	unsigned int		end_parens;
>   	unsigned int		end_state;
>   	int			*start_type;
> +	char			arg_name[16];
>   	int			arg_cnt;
>   };
>   
> @@ -957,7 +960,15 @@ static char *new_arg(struct tracefs_synth *synth)
>   	char *arg;
>   	int ret;
>   
> -	ret = asprintf(&arg, "__arg__%d", cnt);
> +	/* Create a unique argument name */
> +	if (!synth->arg_name[0]) {
> +		srand(time(NULL));

Nit: Have in mind that time(NULL) has 1 second resolution. Fast consecutive calls (within a second) of this function can 
generate identical random numbers.
This can be mitigated if we do something like this:

		struct timeval now;

		gettimeofday(&now, NULL);
		srand(now.tv_usec);


-- Yordan

> +		ret = rand() + gettid();
> +		/* Truncate so that ret is at most 65535 (total 13 bytes) */
> +		ret &= (1 << 16) - 1;
> +		sprintf(synth->arg_name, "__arg_%d_", ret);
> +	}
> +	ret = asprintf(&arg, "%s%d", synth->arg_name, cnt);
>   	if (ret < 0)
>   		return NULL;
>   
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique
  2021-08-12  8:34   ` Yordan Karadzhov
@ 2021-08-12 14:00     ` Steven Rostedt
  0 siblings, 0 replies; 5+ messages in thread
From: Steven Rostedt @ 2021-08-12 14:00 UTC (permalink / raw)
  To: Yordan Karadzhov
  Cc: linux-trace-devel, linux-kernel, Tom Zanussi,
	Daniel Bristot de Oliveira, Masami Hiramatsu

On Thu, 12 Aug 2021 11:34:57 +0300
Yordan Karadzhov <y.karadz@gmail.com> wrote:

> > @@ -957,7 +960,15 @@ static char *new_arg(struct tracefs_synth *synth)
> >   	char *arg;
> >   	int ret;
> >   
> > -	ret = asprintf(&arg, "__arg__%d", cnt);
> > +	/* Create a unique argument name */
> > +	if (!synth->arg_name[0]) {
> > +		srand(time(NULL));  
> 
> Nit: Have in mind that time(NULL) has 1 second resolution. Fast consecutive calls (within a second) of this function can 
> generate identical random numbers.
> This can be mitigated if we do something like this:
> 
> 		struct timeval now;
> 
> 		gettimeofday(&now, NULL);
> 		srand(now.tv_usec);

So you are saying that if one thread created two synthetic events
within a second, then this could give the same value. Yeah, I can see
that could happen. I was hoping to avoid the declaring the "now" and
calling gettimeofday().

Also, looking more into this, I see that rand() is not safe in thread
context (it may not be a problem, but there's no guarantee), and
perhaps we should just open code it, to be on the safe side.

Thanks for the review.

-- Steve

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-12 14:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-12  0:55 [PATCH 0/2] libtracefs: Make hist variable names unique Steven Rostedt
2021-08-12  0:55 ` [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique Steven Rostedt
2021-08-12  8:34   ` Yordan Karadzhov
2021-08-12 14:00     ` Steven Rostedt
2021-08-12  0:55 ` [PATCH 2/2] libtracefs: Have end event variables not be the end event field name Steven Rostedt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).