20 #ifdef __GNUC__ // for get_pretty_type_name()
41 std::ldiv_t result = std::div(dividee, dividor);
42 return result.rem != 0 ? (result.quot + 1) : result.quot;
45 std::string
replace_all(
const std::string& target,
const std::string& search,
46 const std::string& replacement) {
47 std::string subject = target;
49 std::size_t pos = subject.find(search);
50 if (pos != std::string::npos) {
51 subject.replace(pos, search.size(), replacement);
59 std::string
replace_all(
const std::string& target,
const std::string& search,
61 std::stringstream str;
63 std::string rep = str.str();
72 if (error_number == 0) {
75 std::stringstream str;
77 str <<
"[Errno " << error_number <<
"] " << std::strerror(error_number);
83 ssize_t len = ::readlink(
"/proc/self/exe", buf,
sizeof(buf));
85 std::cerr <<
"Failed to get the path of current executable. error=" <<
os_error() << std::endl;
88 return std::string(buf, len);
106 for (uint16_t i = 0; i < 16U; ++i) {
107 buffer[i] = kUpperHexChars[(v.
val_ >> ((15 - i) * 4)) & 0xFU];
110 for (start_pos = 0; start_pos < 15U; ++start_pos) {
111 if (buffer[start_pos] !=
'0') {
119 o <<
"0x" << (buffer + start_pos);
126 for (uint32_t i = 0; i < v.
str_.size() && i < v.
max_bytes_; ++i) {
127 if (i > 0 && i % 8U == 0) {
130 o << kUpperHexChars[(v.
str_[i] >> 4) & 0xFU] << kUpperHexChars[v.
str_[i] & 0xFU];
140 o << i <<
":" <<
static_cast<int>(v.
data_[i]);
154 char* demangled = abi::__cxa_demangle(mangled_name,
nullptr,
nullptr, &status);
156 std::string ret(demangled);
165 if (threshold <= 2) {
167 }
else if (threshold < 5000) {
169 uint16_t small_primes[] = {3677, 2347, 1361, 773, 449, 263, 151, 89, 41, 17, 2};
170 for (
int i = 0;; ++i) {
171 if (threshold > small_primes[i]) {
172 return small_primes[i];
177 uint64_t prev = 3677;
178 for (uint64_t i = 22;; ++i) {
179 uint64_t cur = (i * i * i * i * i - 133 * i * i * i * i + 6729 * i * i * i
180 - 158379 * i * i + 1720294 * i - 6823316) >> 2;
181 if (cur >= threshold) {
183 }
else if (cur <= prev) {
197 std::this_thread::yield();
210 std::hash<std::thread::id> h;
217 if (RUNNING_ON_VALGRIND) {
223 const uint64_t kMinSleepCycles = 1ULL << 7;
224 const uint64_t kMaxSleepCycles = 1ULL << 12;
225 uint64_t wait_cycles_max =
spins_ < 5U ? kMinSleepCycles <<
spins_ : kMaxSleepCycles;
226 uint32_t wait_cycles = kMinSleepCycles +
rnd_.
next_uint32() % (wait_cycles_max - kMinSleepCycles);
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
FileStatus status(const Path &p)
Returns the status of the file.
void wait_rdtsc_cycles(uint64_t cycles)
Wait until the given CPU cycles elapse.
std::string get_current_executable_path()
Returns the full path of current executable.
std::string replace_all(const std::string &target, const std::string &search, const std::string &replacement)
target.replaceAll(search, replacement).
const char * kUpperHexChars
Equivalent to std::hex in case the stream doesn't support it.
void spinlock_yield()
Invoke _mm_pause(), x86 PAUSE instruction, or something equivalent in the env.
Write only first few bytes to stream.
std::string os_error()
Thread-safe strerror(errno).
uint64_t generate_almost_prime_below(uint64_t threshold)
Generate a prime or some number that is almost prime less than the given number.
std::string demangle_type_name(const char *mangled_name)
Demangle the given C++ type name if possible (otherwise the original string).
Convenient way of writing hex integers to stream.
Implements an RDTSC (Real-time time stamp counter) wait to emulate latency on slower devices...
int64_t int_div_ceil(int64_t dividee, int64_t dividor)
Efficient ceil(dividee/dividor) for integer.
std::ostream & operator<<(std::ostream &o, const Hex &v)