Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Quanah Gibson-Mount
OpenLDAP
Commits
8968d131
Commit
8968d131
authored
Sep 23, 2021
by
Ondřej Kuzník
Committed by
Quanah Gibson-Mount
Oct 05, 2021
Browse files
ITS#9599 Switch to xorshift for random numbers
parent
3c07544b
Changes
1
Hide whitespace changes
Inline
Side-by-side
servers/lloadd/tier_bestof.c
View file @
8968d131
...
...
@@ -30,26 +30,29 @@ static LloadTierSelect bestof_select;
struct
lload_tier_type
bestof_tier
;
/*
*
Linear Congruential Generator -
we don't
need
*
high quality randomness, and we don't want to
*
interfere with anyone else's use of srand()
.
*
xorshift - we don't need high quality randomness, and
we don't
want to
*
interfere with anyone else's use of srand() but we still want something with
*
little bias
.
*
* The PRNG here cycles thru
941,955
numbers.
* The PRNG here cycles thru
2^64−1
numbers.
*/
static
floa
t
bestof_seed
;
static
uint64_
t
bestof_seed
;
static
void
bestof_srand
(
int
seed
)
{
bestof_seed
=
(
float
)
seed
/
(
float
)
RAND_MAX
;
bestof_seed
=
seed
;
}
static
floa
t
static
uint64_
t
bestof_rand
()
{
float
val
=
9821
.
0
*
bestof_seed
+
.
211327
;
bestof_seed
=
val
-
(
int
)
val
;
return
bestof_seed
;
uint64_t
val
=
bestof_seed
;
val
^=
val
<<
13
;
val
^=
val
>>
7
;
val
^=
val
<<
17
;
bestof_seed
=
val
;
return
val
;
}
static
int
...
...
@@ -84,6 +87,7 @@ LloadTier *
bestof_init
(
void
)
{
LloadTier
*
tier
;
int
seed
;
tier
=
ch_calloc
(
1
,
sizeof
(
LloadTier
)
);
...
...
@@ -91,7 +95,11 @@ bestof_init( void )
ldap_pvt_thread_mutex_init
(
&
tier
->
t_mutex
);
LDAP_CIRCLEQ_INIT
(
&
tier
->
t_backends
);
bestof_srand
(
rand
()
);
/* Make sure we don't pass 0 as a seed */
do
{
seed
=
rand
();
}
while
(
!
seed
);
bestof_srand
(
seed
);
return
tier
;
}
...
...
@@ -227,8 +235,8 @@ bestof_select(
}
/* Pick two backend indices at random */
i0
=
bestof_rand
()
*
n
;
i1
=
bestof_rand
()
*
(
n
-
1
);
i0
=
bestof_rand
()
%
n
;
i1
=
bestof_rand
()
%
(
n
-
1
);
if
(
i1
>=
i0
)
{
i1
+=
1
;
}
else
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment