Repository: bitcoin/bitcoin
File: src/randomenv.cpp (C++)

Code before:
            } else if (leaf == 7) {
                if (subleaf == 0) maxsub = ax;
                if (subleaf == maxsub) break;
            } else if (leaf == 11) {
                if ((cx & 0xff00) == 0) break;
            } else if (leaf == 13) {
                if (ax == 0 && bx == 0 && cx == 0 && dx == 0) break;
            } else {
                // For any other leaf, stop after subleaf 0.
                break;
            }
        }
    }
    // Iterate over all extended leaves
    AddCPUID(hasher, 0x80000000, 0, ax, bx, cx, dx); // Returns max extended leaf in ax
    uint32_t ext_max = ax;
    for (uint32_t leaf = 0x80000001; leaf <= ext_max && leaf <= 0x800000FF; ++leaf) {
        AddCPUID(hasher, leaf, 0, ax, bx, cx, dx);
    }
}
#endif
} // namespace

void RandAddDynamicEnv(CSHA512& hasher)
{
    RandAddSeedPerfmon(hasher);

    // Various clocks
#ifdef WIN32
    FILETIME ftime;
    GetSystemTimeAsFileTime(&ftime);
    hasher << ftime;
#else
    struct timespec ts = {};
#    ifdef CLOCK_MONOTONIC
    clock_gettime(CLOCK_MONOTONIC, &ts);
    hasher << ts;
#    endif
#    ifdef CLOCK_REALTIME
    clock_gettime(CLOCK_REALTIME, &ts);
    hasher << ts;
#    endif
#    ifdef CLOCK_BOOTTIME
    clock_gettime(CLOCK_BOOTTIME, &ts);
    hasher << ts;
#    endif
    // gettimeofday is available on all UNIX systems, but only has microsecond precision.
    struct timeval tv = {};
    gettimeofday(&tv, nullptr);
    hasher << tv;
#endif

Review comment (performance):
You may want to also modify the "// Dynamic environment data (performance monitoring, ...)" line in `SeedPeriodic()` in random.cpp.

Code after:
#    ifdef CLOCK_BOOTTIME
    clock_gettime(CLOCK_BOOTTIME, &ts);
    hasher << ts;
#    endif
    // gettimeofday is available on all UNIX systems, but only has microsecond precision.
    struct timeval tv = {};
    gettimeofday(&tv, nullptr);
    hasher << tv;
#endif
    // Probably redundant, but also use all the standard library clocks:
    hasher << std::chrono::system_clock::now().time_since_epoch().count();
    hasher << std::chrono::steady_clock::now().time_since_epoch().count();
    hasher << std::chrono::high_resolution_clock::now().time_since_epoch().count();

#ifndef WIN32
    // Current resource usage.
    struct rusage usage = {};
    if (getrusage(RUSAGE_SELF, &usage) == 0) hasher << usage;
#endif

#ifdef __linux__
    AddFile(hasher, "/proc/diskstats");
    AddFile(hasher, "/proc/vmstat");
    AddFile(hasher, "/proc/schedstat");
    AddFile(hasher, "/proc/zoneinfo");
    AddFile(hasher, "/proc/meminfo");
    AddFile(hasher, "/proc/softirqs");
    AddFile(hasher, "/proc/stat");
    AddFile(hasher, "/proc/self/schedstat");
    AddFile(hasher, "/proc/self/status");
#endif

#ifdef HAVE_SYSCTL
#  ifdef CTL_KERN
#    if defined(KERN_PROC) && defined(KERN_PROC_ALL)
    AddSysctl<CTL_KERN, KERN_PROC, KERN_PROC_ALL>(hasher);
#    endif
#  endif
#  ifdef CTL_HW
#    ifdef HW_DISKSTATS
    AddSysctl<CTL_HW, HW_DISKSTATS>(hasher);
#    endif
#  endif
#  ifdef CTL_VM
#    ifdef VM_LOADAVG
    AddSysctl<CTL_VM, VM_LOADAVG>(hasher);
#    endif
#    ifdef VM_TOTAL
    AddSysctl<CTL_VM, VM_TOTAL>(hasher);
#    endif
#    ifdef VM_METER