Skip to content

Commit de6c0ae

Browse files
committed
merge bitcoin#27189: Use steady clock in SeedStrengthen, FindBestImplementation, FlushStateToDisk
1 parent 5bed6ea commit de6c0ae

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

ci/dash/lint-tidy.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ iwyu_tool.py \
2424
"src/dbwrapper.cpp" \
2525
"src/init" \
2626
"src/node/chainstate.cpp" \
27+
"src/node/minisketchwrapper.cpp" \
2728
"src/policy/feerate.cpp" \
2829
"src/policy/packages.cpp" \
2930
"src/policy/settings.cpp" \
3031
"src/primitives/transaction.cpp" \
32+
"src/random.cpp" \
3133
"src/rpc/fees.cpp" \
3234
"src/rpc/signmessage.cpp" \
3335
"src/test/fuzz/txorphan.cpp" \

src/node/minisketchwrapper.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ static constexpr uint32_t BITS = 32;
2323

2424
uint32_t FindBestImplementation()
2525
{
26-
std::optional<std::pair<int64_t, uint32_t>> best;
26+
std::optional<std::pair<SteadyClock::duration, uint32_t>> best;
2727

2828
uint32_t max_impl = Minisketch::MaxImplementation();
2929
for (uint32_t impl = 0; impl <= max_impl; ++impl) {
30-
std::vector<int64_t> benches;
30+
std::vector<SteadyClock::duration> benches;
3131
uint64_t offset = 0;
3232
/* Run a little benchmark with capacity 32, adding 184 entries, and decoding 11 of them once. */
3333
for (int b = 0; b < 11; ++b) {
3434
if (!Minisketch::ImplementationSupported(BITS, impl)) break;
3535
Minisketch sketch(BITS, impl, 32);
36-
auto start = GetTimeMicros();
36+
auto start = SteadyClock::now();
3737
for (uint64_t e = 0; e < 100; ++e) {
3838
sketch.Add(e*1337 + b*13337 + offset);
3939
}
4040
for (uint64_t e = 0; e < 84; ++e) {
4141
sketch.Add(e*1337 + b*13337 + offset);
4242
}
4343
offset += (*sketch.Decode(32))[0];
44-
auto stop = GetTimeMicros();
44+
auto stop = SteadyClock::now();
4545
benches.push_back(stop - start);
4646
}
4747
/* Remember which implementation has the best median benchmark time. */

src/random.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,14 +295,14 @@ static void SeedHardwareSlow(CSHA512& hasher) noexcept {
295295
}
296296

297297
/** Use repeated SHA512 to strengthen the randomness in seed32, and feed into hasher. */
298-
static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA512& hasher) noexcept
298+
static void Strengthen(const unsigned char (&seed)[32], SteadyClock::duration dur, CSHA512& hasher) noexcept
299299
{
300300
CSHA512 inner_hasher;
301301
inner_hasher.Write(seed, sizeof(seed));
302302

303303
// Hash loop
304304
unsigned char buffer[64];
305-
int64_t stop = GetTimeMicros() + microseconds;
305+
const auto stop{SteadyClock::now() + dur};
306306
do {
307307
for (int i = 0; i < 1000; ++i) {
308308
inner_hasher.Finalize(buffer);
@@ -312,7 +312,7 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51
312312
// Benchmark operation and feed it into outer hasher.
313313
int64_t perf = GetPerformanceCounter();
314314
hasher.Write((const unsigned char*)&perf, sizeof(perf));
315-
} while (GetTimeMicros() < stop);
315+
} while (SteadyClock::now() < stop);
316316

317317
// Produce output from inner state and feed it to outer hasher.
318318
inner_hasher.Finalize(buffer);
@@ -566,13 +566,13 @@ static void SeedSlow(CSHA512& hasher, RNGState& rng) noexcept
566566
}
567567

568568
/** Extract entropy from rng, strengthen it, and feed it into hasher. */
569-
static void SeedStrengthen(CSHA512& hasher, RNGState& rng, int microseconds) noexcept
569+
static void SeedStrengthen(CSHA512& hasher, RNGState& rng, SteadyClock::duration dur) noexcept
570570
{
571571
// Generate 32 bytes of entropy from the RNG, and a copy of the entropy already in hasher.
572572
unsigned char strengthen_seed[32];
573573
rng.MixExtract(strengthen_seed, sizeof(strengthen_seed), CSHA512(hasher), false);
574574
// Strengthen the seed, and feed it into hasher.
575-
Strengthen(strengthen_seed, microseconds, hasher);
575+
Strengthen(strengthen_seed, dur, hasher);
576576
}
577577

578578
static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
@@ -592,7 +592,7 @@ static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
592592
LogPrint(BCLog::RANDOM, "Feeding %i bytes of dynamic environment data into RNG\n", hasher.Size() - old_size);
593593

594594
// Strengthen for 10 ms
595-
SeedStrengthen(hasher, rng, 10000);
595+
SeedStrengthen(hasher, rng, 10ms);
596596
}
597597

598598
static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
@@ -612,7 +612,7 @@ static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
612612
LogPrint(BCLog::RANDOM, "Feeding %i bytes of environment data into RNG\n", hasher.Size() - old_size);
613613

614614
// Strengthen for 100 ms
615-
SeedStrengthen(hasher, rng, 100000);
615+
SeedStrengthen(hasher, rng, 100ms);
616616
}
617617

618618
enum class RNGLevel {

src/util/time.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <compat/compat.h>
1010

11-
#include <chrono>
11+
#include <chrono> // IWYU pragma: export
1212
#include <cstdint>
1313
#include <string>
1414

src/validation.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,12 +2815,12 @@ bool CChainState::FlushStateToDisk(
28152815
}
28162816
}
28172817
}
2818-
const auto nNow = GetTime<std::chrono::microseconds>();
2818+
const auto nNow{SteadyClock::now()};
28192819
// Avoid writing/flushing immediately after startup.
2820-
if (m_last_write.count() == 0) {
2820+
if (m_last_write == decltype(m_last_write){}) {
28212821
m_last_write = nNow;
28222822
}
2823-
if (m_last_flush.count() == 0) {
2823+
if (m_last_flush == decltype(m_last_flush){}) {
28242824
m_last_flush = nNow;
28252825
}
28262826
// The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
@@ -2892,7 +2892,7 @@ bool CChainState::FlushStateToDisk(
28922892
m_last_flush = nNow;
28932893
full_flush_completed = true;
28942894
TRACE5(utxocache, flush,
2895-
(int64_t)(GetTimeMicros() - nNow.count()), // in microseconds (µs)
2895+
int64_t{Ticks<std::chrono::microseconds>(SteadyClock::now() - nNow)},
28962896
(uint32_t)mode,
28972897
(uint64_t)coins_count,
28982898
(uint64_t)coins_mem_usage,

src/validation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,8 @@ class CChainState
818818
void UpdateTip(const CBlockIndex* pindexNew)
819819
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
820820

821-
std::chrono::microseconds m_last_write{0};
822-
std::chrono::microseconds m_last_flush{0};
821+
SteadyClock::time_point m_last_write{};
822+
SteadyClock::time_point m_last_flush{};
823823

824824
friend ChainstateManager;
825825
};

0 commit comments

Comments
 (0)