1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +03:00

Replace random(), pg_erand48(), etc with a better PRNG API and algorithm.

Standardize on xoroshiro128** as our basic PRNG algorithm, eliminating
a bunch of platform dependencies as well as fundamentally-obsolete PRNG
code.  In addition, this API replacement will ease replacing the
algorithm again in future, should that become necessary.

xoroshiro128** is a few percent slower than the drand48 family,
but it can produce full-width 64-bit random values not only 48-bit,
and it should be much more trustworthy.  It's likely to be noticeably
faster than the platform's random(), depending on which platform you
are thinking about; and we can have non-global state vectors easily,
unlike with random().  It is not cryptographically strong, but neither
are the functions it replaces.

Fabien Coelho, reviewed by Dean Rasheed, Aleksander Alekseev, and myself

Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2105241211230.165418@pseudo
This commit is contained in:
Tom Lane
2021-11-28 21:32:36 -05:00
parent f44ceb46ec
commit 3804539e48
50 changed files with 543 additions and 480 deletions

View File

@@ -92,6 +92,7 @@
#include "catalog/pg_tablespace.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/pg_iovec.h"
@@ -2939,7 +2940,8 @@ SetTempTablespaces(Oid *tableSpaces, int numSpaces)
* available tablespaces.
*/
if (numSpaces > 1)
nextTempTableSpace = random() % numSpaces;
nextTempTableSpace = pg_prng_uint64_range(&pg_global_prng_state,
0, numSpaces - 1);
else
nextTempTableSpace = 0;
}

View File

@@ -33,6 +33,7 @@
#endif
#include <sys/stat.h>
#include "common/pg_prng.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
@@ -180,7 +181,8 @@ dsm_postmaster_startup(PGShmemHeader *shim)
{
Assert(dsm_control_address == NULL);
Assert(dsm_control_mapped_size == 0);
dsm_control_handle = random() << 1; /* Even numbers only */
/* Use even numbers only */
dsm_control_handle = pg_prng_uint32(&pg_global_prng_state) << 1;
if (dsm_control_handle == DSM_HANDLE_INVALID)
continue;
if (dsm_impl_op(DSM_OP_CREATE, dsm_control_handle, segsize,
@@ -536,7 +538,8 @@ dsm_create(Size size, int flags)
for (;;)
{
Assert(seg->mapped_address == NULL && seg->mapped_size == 0);
seg->handle = random() << 1; /* Even numbers only */
/* Use even numbers only */
seg->handle = pg_prng_uint32(&pg_global_prng_state) << 1;
if (seg->handle == DSM_HANDLE_INVALID) /* Reserve sentinel */
continue;
if (dsm_impl_op(DSM_OP_CREATE, seg->handle, size, &seg->impl_private,
@@ -1237,7 +1240,7 @@ make_main_region_dsm_handle(int slot)
*/
handle = 1;
handle |= slot << 1;
handle |= random() << (pg_leftmost_one_pos32(dsm_control->maxitems) + 1);
handle |= pg_prng_uint32(&pg_global_prng_state) << (pg_leftmost_one_pos32(dsm_control->maxitems) + 1);
return handle;
}

View File

@@ -30,9 +30,10 @@ ifdef TAS
TASPATH = $(top_builddir)/src/backend/port/tas.o
endif
s_lock_test: s_lock.c $(top_builddir)/src/port/libpgport.a
s_lock_test: s_lock.c $(top_builddir)/src/common/libpgcommon.a $(top_builddir)/src/port/libpgport.a
$(CC) $(CPPFLAGS) $(CFLAGS) -DS_LOCK_TEST=1 $(srcdir)/s_lock.c \
$(TASPATH) -L $(top_builddir)/src/port -lpgport -o s_lock_test
$(TASPATH) -L $(top_builddir)/src/common -lpgcommon \
-L $(top_builddir)/src/port -lpgport -o s_lock_test
# see notes in src/backend/parser/Makefile
lwlocknames.c: lwlocknames.h

View File

@@ -50,6 +50,7 @@
#include <time.h>
#include <unistd.h>
#include "common/pg_prng.h"
#include "port/atomics.h"
#include "storage/s_lock.h"
@@ -144,7 +145,7 @@ perform_spin_delay(SpinDelayStatus *status)
/* increase delay by a random fraction between 1X and 2X */
status->cur_delay += (int) (status->cur_delay *
((double) random() / (double) MAX_RANDOM_VALUE) + 0.5);
pg_prng_double(&pg_global_prng_state) + 0.5);
/* wrap back to minimum delay when max is exceeded */
if (status->cur_delay > MAX_DELAY_USEC)
status->cur_delay = MIN_DELAY_USEC;
@@ -303,7 +304,7 @@ volatile struct test_lock_struct test_lock;
int
main()
{
srandom((unsigned int) time(NULL));
pg_prng_seed(&pg_global_prng_state, (uint64) time(NULL));
test_lock.pad1 = test_lock.pad2 = 0x44;