Commit b1f2d3a8 authored by Marko Mäkelä's avatar Marko Mäkelä

MDEV-21256: Replace the 64-bit LCG with a 32-bit Galois LFSR

We should not need anywhere near 32 bits of entropy, so we might
just limit ourselves to a 32-bit random number generator.

Also, it might be cheaper to use exclusive-or, bit shifting and
conditional jumps, instead of multiplication and addition.

We use relaxed atomic operations on the global random number generator
state in order in an attempt to silence any warnings about race conditions.
There is an obvious race condition between the load and store in
ut_rnd_gen(), but we do not think that it matters much that the
state of the random number generator could 'stutter'.

This change seems makes the 'uncompress_ops' nondeterministic
in innodb_zip.cmp_per_index after the restart. It looks like
there is an inherent race condition in the test, because the
table could be opened for InnoDB statistics recalculation
already before innodb_cmp_per_index_enabled was set. We might
end up having uncompress_ops anywhere between 0 and 9, or perhaps
even more. Let us remove that part of the test.
parent d146e3dc
SET @save_enabled= @@GLOBAL.innodb_cmp_per_index_enabled;
SET GLOBAL innodb_cmp_per_index_enabled=ON;
SELECT * FROM information_schema.innodb_cmp_per_index;
CREATE TABLE t (
......@@ -70,33 +71,5 @@ index_name PRIMARY
compress_ops 65
compress_ops_ok 65
uncompress_ops 0
SHOW CREATE TABLE t;
Table t
Create Table CREATE TABLE `t` (
`a` int(11) NOT NULL,
`b` varchar(512) DEFAULT NULL,
`c` varchar(16) DEFAULT NULL,
PRIMARY KEY (`a`),
KEY `b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 KEY_BLOCK_SIZE=2
SET GLOBAL innodb_cmp_per_index_enabled=ON;
SELECT COUNT(*) FROM t IGNORE INDEX(b);
COUNT(*) 128
SELECT
database_name,
table_name,
index_name,
compress_ops,
compress_ops_ok,
CASE WHEN uncompress_ops=6 and @@innodb_compression_level IN (4,8,9) THEN 9
ELSE uncompress_ops END as uncompress_ops
FROM information_schema.innodb_cmp_per_index
ORDER BY 1, 2, 3;
database_name test
table_name t
index_name PRIMARY
compress_ops 0
compress_ops_ok 0
uncompress_ops 4
DROP TABLE t;
SET GLOBAL innodb_cmp_per_index_enabled=default;
SET GLOBAL innodb_cmp_per_index_enabled=@save_enabled;
......@@ -21,6 +21,7 @@ if (`SELECT @@innodb_log_compressed_pages = 0`)
-- vertical_results
SET @save_enabled= @@GLOBAL.innodb_cmp_per_index_enabled;
SET GLOBAL innodb_cmp_per_index_enabled=ON;
# reset any leftover stats from previous tests
......@@ -92,29 +93,6 @@ ELSE compress_ops_ok END as compress_ops_ok,
uncompress_ops
FROM information_schema.innodb_cmp_per_index
ORDER BY 1, 2, 3;
# restart mysqld and see that uncompress ops also gets increased when
# selecting from the table again
-- source include/restart_mysqld.inc
SHOW CREATE TABLE t;
SET GLOBAL innodb_cmp_per_index_enabled=ON;
SELECT COUNT(*) FROM t IGNORE INDEX(b);
SELECT
database_name,
table_name,
index_name,
compress_ops,
compress_ops_ok,
CASE WHEN uncompress_ops=6 and @@innodb_compression_level IN (4,8,9) THEN 9
ELSE uncompress_ops END as uncompress_ops
FROM information_schema.innodb_cmp_per_index
ORDER BY 1, 2, 3;
DROP TABLE t;
SET GLOBAL innodb_cmp_per_index_enabled=default;
SET GLOBAL innodb_cmp_per_index_enabled=@save_enabled;
......@@ -32,28 +32,37 @@ Created 1/20/1994 Heikki Tuuri
#ifndef UNIV_INNOCHECKSUM
/** Seed value of ut_rnd_gen() */
extern uint64_t ut_rnd_current;
extern int32 ut_rnd_current;
/** @return a pseudo-random 64-bit number */
inline uint64_t ut_rnd_gen()
/** @return a pseudo-random 32-bit number */
inline uint32_t ut_rnd_gen()
{
/*
This is a linear congruential pseudo random number generator.
The formula and the constants
being used are:
X[n+1] = (a * X[n] + c) mod m
where:
X[0] = my_interval_timer()
a = 1103515245 (3^5 * 5 * 7 * 129749)
c = 12345 (3 * 5 * 823)
m = 18446744073709551616 (1<<64), implicit */
if (UNIV_UNLIKELY(!ut_rnd_current)) {
ut_rnd_current = my_interval_timer();
}
ut_rnd_current = 1103515245 * ut_rnd_current + 12345;
return ut_rnd_current;
/* This is a Galois linear-feedback shift register.
https://en.wikipedia.org/wiki/Linear-feedback_shift_register#Galois_LFSRs
The generating primitive Galois Field polynomial is the Castagnoli
polynomial that was made popular by CRC-32C:
x^32+x^28+x^27+x^26+x^25+x^23+x^22+x^20+
x^19+x^18+x^14+x^13+x^11+x^10+x^9+x^8+x^6+1 */
const uint32_t crc32c= 0x1edc6f41;
uint32_t rnd= my_atomic_load32_explicit(&ut_rnd_current,
MY_MEMORY_ORDER_RELAXED);
if (UNIV_UNLIKELY(rnd == 0))
{
rnd= static_cast<uint32_t>(my_interval_timer());
if (!rnd) rnd= 1;
}
else
{
bool lsb= rnd & 1;
rnd>>= 1;
if (lsb)
rnd^= crc32c;
}
my_atomic_store32_explicit(&ut_rnd_current, rnd, MY_MEMORY_ORDER_RELAXED);
return rnd;
}
/** @return a random number between 0 and n-1, inclusive */
......
......@@ -27,7 +27,7 @@ Created 5/11/1994 Heikki Tuuri
#include "ut0rnd.h"
/** Seed value of ut_rnd_gen() */
uint64_t ut_rnd_current;
int32 ut_rnd_current;
/** These random numbers are used in ut_find_prime */
/*@{*/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment