Skip to content

Commit

Permalink
DAOS-15120 gurt: Misc d_rand fixes
Browse files Browse the repository at this point in the history
  - Remove duplicate d_srand calls.

  - Replace rand calls in libgurt and libpool with d_randn.

Signed-off-by: Li Wei <[email protected]>
Required-githooks: true
  • Loading branch information
liw committed Feb 5, 2024
1 parent e0e9faf commit e02cb24
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 12 deletions.
8 changes: 0 additions & 8 deletions src/cart/crt_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
#include <malloc.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "crt_internal.h"

Expand Down Expand Up @@ -610,8 +609,6 @@ crt_init_opt(crt_group_id_t grpid, uint32_t flags, crt_init_options_t *opt)
char *domain_env = NULL;
char *auth_key;
char *auth_key_env = NULL;
struct timeval now;
unsigned int seed;
char *path;
bool server = flags & CRT_FLAG_BIT_SERVER;
int rc = 0;
Expand Down Expand Up @@ -673,11 +670,6 @@ crt_init_opt(crt_group_id_t grpid, uint32_t flags, crt_init_options_t *opt)

D_RWLOCK_WRLOCK(&crt_gdata.cg_rwlock);
if (crt_gdata.cg_inited == 0) {
/* feed a seed for pseudo-random number generator */
gettimeofday(&now, NULL);
seed = (unsigned int)(now.tv_sec * 1000000 + now.tv_usec);
d_srand(seed);

crt_gdata.cg_server = server;
crt_gdata.cg_auto_swim_disable =
(flags & CRT_FLAG_BIT_AUTO_SWIM_DISABLE) ? 1 : 0;
Expand Down
3 changes: 2 additions & 1 deletion src/gurt/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void
d_srand(long int seedval)
{
srand48_r(seedval, &randBuffer);
D_INFO("d_rand initialized with seed %ld\n", seedval);
}

long int
Expand Down Expand Up @@ -544,7 +545,7 @@ d_rank_list_shuffle(d_rank_list_t *rank_list)
return;

for (i = 0; i < rank_list->rl_nr; i++) {
j = rand() % rank_list->rl_nr;
j = d_randn(rank_list->rl_nr);
tmp = rank_list->rl_ranks[i];
rank_list->rl_ranks[i] = rank_list->rl_ranks[j];
rank_list->rl_ranks[j] = tmp;
Expand Down
4 changes: 1 addition & 3 deletions src/pool/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -1631,9 +1631,7 @@ choose_map_refresh_rank(struct map_refresh_arg *arg)

if (arg->mra_i == -1) {
/* Let i be a random integer in [0, n). */
i = ((double)rand() / RAND_MAX) * n;
if (i == n)
i = 0;
i = d_randn(n);
} else {
/* Continue the round robin. */
i = arg->mra_i;
Expand Down

0 comments on commit e02cb24

Please sign in to comment.