diff options
author | Marko Mäkelä <marko.makela@mariadb.com> | 2019-12-10 11:20:26 +0200 |
---|---|---|
committer | Marko Mäkelä <marko.makela@mariadb.com> | 2019-12-10 16:59:34 +0200 |
commit | b1f2d3a8c8d9bd10e088111ca966b3f7be35df35 (patch) | |
tree | 539c2337e42ef2e051be97c2ba011ebda47b6ec4 | |
parent | d146e3dcfe3d4500eced3e097212b485659ec741 (diff) | |
download | mariadb-git-b1f2d3a8c8d9bd10e088111ca966b3f7be35df35.tar.gz |
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.
-rw-r--r-- | mysql-test/suite/innodb_zip/r/cmp_per_index.result | 31 | ||||
-rw-r--r-- | mysql-test/suite/innodb_zip/t/cmp_per_index.test | 26 | ||||
-rw-r--r-- | storage/innobase/include/ut0rnd.h | 49 | ||||
-rw-r--r-- | storage/innobase/ut/ut0rnd.cc | 2 |
4 files changed, 34 insertions, 74 deletions
diff --git a/mysql-test/suite/innodb_zip/r/cmp_per_index.result b/mysql-test/suite/innodb_zip/r/cmp_per_index.result index cdb81e7be8a..b4e34040630 100644 --- a/mysql-test/suite/innodb_zip/r/cmp_per_index.result +++ b/mysql-test/suite/innodb_zip/r/cmp_per_index.result @@ -1,3 +1,4 @@ +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; diff --git a/mysql-test/suite/innodb_zip/t/cmp_per_index.test b/mysql-test/suite/innodb_zip/t/cmp_per_index.test index 8d66277a1bd..74ed17b1213 100644 --- a/mysql-test/suite/innodb_zip/t/cmp_per_index.test +++ b/mysql-test/suite/innodb_zip/t/cmp_per_index.test @@ -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; diff --git a/storage/innobase/include/ut0rnd.h b/storage/innobase/include/ut0rnd.h index fcab293b3b0..9af8687bfd0 100644 --- a/storage/innobase/include/ut0rnd.h +++ b/storage/innobase/include/ut0rnd.h @@ -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 */ diff --git a/storage/innobase/ut/ut0rnd.cc b/storage/innobase/ut/ut0rnd.cc index 856cb82e645..8265121ef2e 100644 --- a/storage/innobase/ut/ut0rnd.cc +++ b/storage/innobase/ut/ut0rnd.cc @@ -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 */ /*@{*/ |