lkml.org 
[lkml]   [2021]   [Aug]   [12]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH 1/2] libtracefs: Add random number to keep synthetic variables unique
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

\
 
 \ /
  Last update: 2021-08-12 16:01    [W:0.021 / U:0.416 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site