Re: [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique

From: Steven Rostedt
Date: Thu Aug 12 2021 - 10:00:18 EST


On Thu, 12 Aug 2021 11:34:57 +0300
Yordan Karadzhov <y.karadz@xxxxxxxxx> 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