| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ut0rnd.h |
| 29 |
Random numbers and hashing |
| 30 |
|
| 31 |
Created 1/20/1994 Heikki Tuuri |
| 32 |
***********************************************************************/ |
| 33 |
|
| 34 |
#ifndef ut0rnd_h |
| 35 |
#define ut0rnd_h |
| 36 |
|
| 37 |
#include <atomic> |
| 38 |
#include <cstdint> |
| 39 |
|
| 40 |
#include "univ.i" |
| 41 |
#include "ut0byte.h" |
| 42 |
#include "ut0math.h" |
| 43 |
|
| 44 |
#include "ut0class_life_cycle.h" |
| 45 |
#include "ut0crc32.h" |
| 46 |
#include "ut0seq_lock.h" |
| 47 |
|
| 48 |
namespace ut { |
| 49 |
/** The following function generates pseudo-random 64bit integers which |
| 50 |
enumerate the value space generated by a linear congruence. This is very similar |
| 51 |
to what can be achieved with std::linear_congruential_engine<uint64_t, ...>. If |
| 52 |
you need reliably good pseudo-random numbers for some reason, please consider |
| 53 |
using std::mersenne_twister_engine<>. |
| 54 |
@return a pseudo-random number */ |
| 55 |
[[nodiscard]] static inline uint64_t random_64(); |
| 56 |
|
| 57 |
/** The following function returns fine clock count as random value. |
| 58 |
This is used for cases which need performance more than the true randomness. |
| 59 |
It is not causing any loads or stores, so it is very CPU-cache-friendly. |
| 60 |
Even for very frequent use, it doesn't cause any CPU cache pollution. |
| 61 |
@return a pseudo-random number */ |
| 62 |
[[nodiscard]] static inline uint64_t random_64_fast(); |
| 63 |
|
| 64 |
/** Generates a pseudo-random integer from a given interval. |
| 65 |
@param[in] low low limit; can generate also this value |
| 66 |
@param[in] high high limit; can generate also this value |
| 67 |
@return the pseudo-random number within the [low, high] two-side inclusive range |
| 68 |
*/ |
| 69 |
[[nodiscard]] static inline uint64_t random_from_interval(uint64_t low, |
| 70 |
uint64_t high); |
| 71 |
|
| 72 |
/** Generates a light-weight pseudo-random integer from a given interval. |
| 73 |
This is used for the cases which need performance more than the true randomness. |
| 74 |
@param[in] low low limit; can generate also this value |
| 75 |
@param[in] high high limit; can generate also this value |
| 76 |
@return the pseudo-random number within the [low, high] two-side inclusive range |
| 77 |
*/ |
| 78 |
[[nodiscard]] static inline uint64_t random_from_interval_fast(uint64_t low, |
| 79 |
uint64_t high); |
| 80 |
|
| 81 |
/** Hashes a 64-bit integer. |
| 82 |
@param[in] value 64-bit integer |
| 83 |
@return hashed value */ |
| 84 |
[[nodiscard]] static inline uint64_t hash_uint64(uint64_t value); |
| 85 |
|
| 86 |
/** Hashes a character string ending in the null character. |
| 87 |
@return hashed value */ |
| 88 |
[[nodiscard]] static inline uint64_t hash_string(const char *str); |
| 89 |
|
| 90 |
/** Hashes a pair of 64bit integers. |
| 91 |
@param[in] n1 first 64bit integer |
| 92 |
@param[in] n2 second 64bit integer |
| 93 |
@return hashed value */ |
| 94 |
[[nodiscard]] static inline uint64_t hash_uint64_pair(uint64_t n1, uint64_t n2); |
| 95 |
/** Hashes a binary buffer of given length. |
| 96 |
@param[in] buf buffer of bytes |
| 97 |
@param[in] len length |
| 98 |
@param[in] seed seed to be used in calculation. Can be previous value. A |
| 99 |
default value is just a randomly chosen number. |
| 100 |
@return hashed value */ |
| 101 |
[[nodiscard]] static inline uint64_t hash_binary( |
| 102 |
const byte *buf, size_t len, uint64_t seed = 0xacb1f3526e25dd39); |
| 103 |
|
| 104 |
/** Hashes a binary buffer of given length in the old innobase way. This is |
| 105 |
highly inefficient, don't use outside areas that require backward compatibility |
| 106 |
on data written to disk. |
| 107 |
@param[in] str buffer of bytes |
| 108 |
@param[in] len length |
| 109 |
@return hashed value */ |
| 110 |
[[nodiscard]] static inline uint32_t hash_binary_ib(const byte *str, |
| 111 |
size_t len); |
| 112 |
|
| 113 |
namespace detail { |
| 114 |
/** Seed value of ut::random_64() */ |
| 115 |
extern thread_local uint64_t random_seed; |
| 116 |
|
| 117 |
/** A helper method, it is used by hash_binary_ib for backward compatibility. |
| 118 |
NOTE: Do not use this method, it produces results that are not hashed well. |
| 119 |
Especially for sequences of pairs of <i+n, j+n> over n. */ |
| 120 |
constexpr uint32_t hash_uint32_pair_ib(uint32_t n1, uint32_t n2) { |
| 121 |
constexpr uint32_t HASH_RANDOM_MASK = 1463735687; |
| 122 |
constexpr uint32_t HASH_RANDOM_MASK2 = 1653893711; |
| 123 |
|
| 124 |
return ((((n1 ^ n2 ^ HASH_RANDOM_MASK2) << 8) + n1) ^ HASH_RANDOM_MASK) + n2; |
| 125 |
} |
| 126 |
|
| 127 |
/* Helper methods to read unaligned little-endian integers. These should be |
| 128 |
optimized to just simple, single reads on little-endian architecture like x86 |
| 129 |
and x64. It may be different for SPARC (and in particular read_from_8(&some_u64) |
| 130 |
is not equal to some_u64's value. On SPARC the unaligned reads are prohibited, |
| 131 |
so for buffer that is not known to be aligned we will read data byte by byte. |
| 132 |
Note that the following methods and usages similar to what we do in |
| 133 |
hash_binary() could be applied to crc32 algorithm calculations and an early |
| 134 |
investigation showed 10-50% improvement in CRC32 speed for buffer lengths <= |
| 135 |
500. However, there was a lot of effort put into crc32 implementation to make |
| 136 |
sure it is efficient and generates correct, simple assembly for x64, ARM (and M1 |
| 137 |
CPU) and other architectures. So in order to incorporate these ideas into CRC32 |
| 138 |
a lot of testing is required to be performed again, with care. */ |
| 139 |
|
| 140 |
static inline int64_t read_from_1(const byte *addr) { return *addr; } |
| 141 |
static inline int64_t read_from_2(const byte *addr) { |
| 142 |
return read_from_1(addr) | (read_from_1(addr + 1) << 8); |
| 143 |
} |
| 144 |
static inline int64_t read_from_4(const byte *addr) { |
| 145 |
return read_from_2(addr) | (read_from_2(addr + 2) << 16); |
| 146 |
} |
| 147 |
static inline int64_t read_from_8(const byte *addr) { |
| 148 |
return read_from_4(addr) | (read_from_4(addr + 4) << 32); |
| 149 |
} |
| 150 |
|
| 151 |
/** Table for Tabulation Hashing, precomputed hash values for all byte values. |
| 152 |
The table could be for different amount of bits per chunk than 8, but 8 seems to |
| 153 |
yield faster hash calculation than 4 or 16bits - 4bits require 16 XOR operations |
| 154 |
and sets of bit manipulations, while 16bit require bigger tables that slow down |
| 155 |
caches. The indexes are set such that hash values for a specific byte value are |
| 156 |
stored in the same cache line (8 values * 8 bytes = 64B). This way it should be |
| 157 |
much faster and less demanding on CPU caches to calculate results for small |
| 158 |
integers (where most bytes are 0). */ |
| 159 |
extern std::array<std::array<uint64_t, 8>, 256> tab_hash_lookup_table; |
| 160 |
|
| 161 |
} // namespace detail |
| 162 |
|
| 163 |
static inline uint64_t random_64() { |
| 164 |
detail::random_seed = hash_uint64(detail::random_seed); |
| 165 |
return detail::random_seed; |
| 166 |
} |
| 167 |
|
| 168 |
static inline uint64_t random_64_fast() { |
| 169 |
/* Granularity of my_timer_cycles() might be over 1, to keep constant rate for |
| 170 |
frequency changes of CPU core clocks. Drops lower 5 bits. */ |
| 171 |
const uint64_t res = my_timer_cycles(); |
| 172 |
return res != 0 ? res >> 5 : random_64(); |
| 173 |
} |
| 174 |
|
| 175 |
template <uint64_t random_64_func()> |
| 176 |
static inline uint64_t random_from_interval_gen(uint64_t low, uint64_t high) { |
| 177 |
ut_ad(high >= low); |
| 178 |
return low + (random_64_func() % (high - low + 1)); |
| 179 |
} |
| 180 |
|
| 181 |
static inline uint64_t random_from_interval(uint64_t low, uint64_t high) { |
| 182 |
return random_from_interval_gen<random_64>(low, high); |
| 183 |
} |
| 184 |
|
| 185 |
static inline uint64_t random_from_interval_fast(uint64_t low, uint64_t high) { |
| 186 |
/* FIXME: To keep backward compatibility with the previous ut_rnd_interval(), |
| 187 |
high value is not to be returned to keep same behavior for performance tuning |
| 188 |
parameter. |
| 189 |
(Bug#37212019: behavior related to innodb_spin_wait_delay changed in 8.0.30) |
| 190 |
This function is not required accurate randomness, no real problems. */ |
| 191 |
|
| 192 |
if (low == high) { |
| 193 |
return (low); |
| 194 |
} |
| 195 |
|
| 196 |
return random_from_interval_gen<random_64_fast>(low, high - 1); |
| 197 |
} |
| 198 |
|
| 199 |
static inline uint64_t hash_uint64(uint64_t value) { |
| 200 |
#ifndef CRC32_DEFAULT |
| 201 |
if (ut_crc32_cpu_enabled) { |
| 202 |
return crc32_hash_uint64(value); |
| 203 |
} |
| 204 |
#endif /* !CRC32_DEFAULT */ |
| 205 |
|
| 206 |
/* This implements Tabulation Hashing. It is a simple yet effective algorithm |
| 207 |
that easily passes the unit tests that check distributions of hash values |
| 208 |
modulo different numbers. Other techniques like one from hash_uint64_pair_fast |
| 209 |
or extended 4-universal version did have some problems for specific modulo |
| 210 |
numbers or have been providing 61bit hashes. */ |
| 211 |
uint64_t res = 0; |
| 212 |
for (size_t i = 0; i < sizeof(uint64_t); ++i) { |
| 213 |
res ^= detail::tab_hash_lookup_table[(value >> (i * 8)) & 0xFF][i]; |
| 214 |
} |
| 215 |
return res; |
| 216 |
} |
| 217 |
|
| 218 |
static inline uint64_t hash_uint64_pair(uint64_t n1, uint64_t n2) { |
| 219 |
return hash_uint64(hash_uint64(n1) ^ n2); |
| 220 |
} |
| 221 |
|
| 222 |
static inline uint64_t hash_string(const char *str) { |
| 223 |
return hash_binary(reinterpret_cast<const byte *>(str), strlen(str)); |
| 224 |
} |
| 225 |
|
| 226 |
static inline uint64_t hash_binary(const byte *str, size_t len, uint64_t seed) { |
| 227 |
/* Our implementation is slower than CRC32 while the CRC32 implementation is |
| 228 |
super fast, especially for higher lengths as it leverages 3-fold parallelism |
| 229 |
on CPU core pipeline level. */ |
| 230 |
if (len >= 15) { |
| 231 |
/* The CRC32 is returning only 32bits of hash. We take out the first 8 bytes |
| 232 |
to seed our 64bit hash just like we do when CRC32 is not used. */ |
| 233 |
const auto h = hash_uint64_pair(seed, detail::read_from_8(str)); |
| 234 |
return hash_uint64_pair(h, ut_crc32(str + 8, len - 8)); |
| 235 |
} |
| 236 |
|
| 237 |
/* The following algorithm is based on idea of applying hash_uint64_pair for |
| 238 |
64bit blocks of data. If there are less than 8 bytes remaining, we hash |
| 239 |
remaining 4, 2 and 1 bytes for all input bytes have influence on result hash. |
| 240 |
*/ |
| 241 |
|
| 242 |
uint64_t h = seed; |
| 243 |
size_t i = 0; |
| 244 |
if (len & 8) { |
| 245 |
h = hash_uint64_pair(h, detail::read_from_8(str + i)); |
| 246 |
i += 8; |
| 247 |
} |
| 248 |
uint64_t last_part = 0; |
| 249 |
if ((len & 7) == 0) { |
| 250 |
return h; |
| 251 |
} |
| 252 |
if (len & 4) { |
| 253 |
last_part |= detail::read_from_4(str + i) << (16 + 8); |
| 254 |
i += 4; |
| 255 |
} |
| 256 |
if (len & 2) { |
| 257 |
last_part |= detail::read_from_2(str + i) << 8; |
| 258 |
i += 2; |
| 259 |
} |
| 260 |
if (len & 1) { |
| 261 |
last_part |= detail::read_from_1(str + i); |
| 262 |
i += 1; |
| 263 |
} |
| 264 |
ut_ad(i == len); |
| 265 |
return hash_uint64_pair(h, last_part); |
| 266 |
} |
| 267 |
|
| 268 |
static inline uint32_t hash_binary_ib(const byte *str, size_t len) { |
| 269 |
uint32_t hash_value = 0; |
| 270 |
const byte *str_end = str + (len & 0xFFFFFFF8); |
| 271 |
|
| 272 |
ut_ad(str || !len); |
| 273 |
|
| 274 |
while (str < str_end) { |
| 275 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 276 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 277 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 278 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 279 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 280 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 281 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 282 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 283 |
} |
| 284 |
|
| 285 |
switch (len & 0x7) { |
| 286 |
case 7: |
| 287 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 288 |
[[fallthrough]]; |
| 289 |
case 6: |
| 290 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 291 |
[[fallthrough]]; |
| 292 |
case 5: |
| 293 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 294 |
[[fallthrough]]; |
| 295 |
case 4: |
| 296 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 297 |
[[fallthrough]]; |
| 298 |
case 3: |
| 299 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 300 |
[[fallthrough]]; |
| 301 |
case 2: |
| 302 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 303 |
[[fallthrough]]; |
| 304 |
case 1: |
| 305 |
hash_value = detail::hash_uint32_pair_ib(hash_value, *str++); |
| 306 |
} |
| 307 |
|
| 308 |
return (hash_value); |
| 309 |
} |
| 310 |
} // namespace ut |
| 311 |
|
| 312 |
#endif |