Coverage Report

Created: 2026-08-05 14:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/common/system.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <bitcoin-build-config.h> // IWYU pragma: keep
7
8
#include <common/system.h>
9
10
#include <util/log.h>
11
#include <util/string.h>
12
#include <util/time.h>
13
14
#ifdef WIN32
15
#include <compat/compat.h>
16
#include <util/check.h>
17
#include <codecvt>
18
#include <windows.h>
19
#else
20
#include <sys/stat.h>
21
#include <sys/types.h>
22
#include <unistd.h>
23
#endif
24
25
#ifdef HAVE_MALLOPT_ARENA_MAX
26
#include <malloc.h>
27
#endif
28
29
#include <cstdint>
30
#include <cstdlib>
31
#include <locale>
32
#include <optional>
33
#include <stdexcept>
34
#include <string>
35
#include <thread>
36
37
using util::ReplaceAll;
38
39
#ifndef WIN32
40
std::string ShellEscape(const std::string& arg)
41
26
{
42
26
    std::string escaped = arg;
43
26
    ReplaceAll(escaped, "'", "'\"'\"'");
44
26
    return "'" + escaped + "'";
45
26
}
46
#endif
47
48
#if HAVE_SYSTEM
49
void runCommand(const std::string& strCommand)
50
143
{
51
143
    if (strCommand.empty()) return;
52
143
#ifndef WIN32
53
143
    int nErr = ::system(strCommand.c_str());
54
#else
55
    int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str());
56
#endif
57
143
    if (nErr) {
58
0
        LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr);
59
0
    }
60
143
}
61
#endif
62
63
void SetupEnvironment()
64
3.19k
{
65
3.19k
#ifdef HAVE_MALLOPT_ARENA_MAX
66
    // glibc-specific: On 32-bit systems set the number of arenas to 1.
67
    // By default, since glibc 2.10, the C library will create up to two heap
68
    // arenas per core. This is known to cause excessive virtual address space
69
    // usage in our usage. Work around it by setting the maximum number of
70
    // arenas to 1.
71
3.19k
    if (sizeof(void*) == 4) {
72
0
        mallopt(M_ARENA_MAX, 1);
73
0
    }
74
3.19k
#endif
75
    // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
76
    // may be invalid, in which case the "C.UTF-8" locale is used as fallback.
77
3.19k
#if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
78
3.19k
    try {
79
3.19k
        std::locale(""); // Raises a runtime error if current locale is invalid
80
3.19k
    } catch (const std::runtime_error&) {
81
0
        setenv("LC_ALL", "C.UTF-8", 1);
82
0
    }
83
#elif defined(WIN32)
84
    assert(GetACP() == CP_UTF8);
85
    // Set the default input/output charset is utf-8
86
    SetConsoleCP(CP_UTF8);
87
    SetConsoleOutputCP(CP_UTF8);
88
#endif
89
90
3.19k
#ifndef WIN32
91
3.19k
    constexpr mode_t private_umask = 0077;
92
3.19k
    umask(private_umask);
93
3.19k
#endif
94
3.19k
}
95
96
bool SetupNetworking()
97
2.43k
{
98
#ifdef WIN32
99
    // Initialize Windows Sockets
100
    WSADATA wsadata;
101
    int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
102
    if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
103
        return false;
104
#endif
105
2.43k
    return true;
106
2.43k
}
107
108
int GetNumCores()
109
740
{
110
740
    return std::thread::hardware_concurrency();
111
740
}
112
113
std::optional<uint64_t> TryGetTotalRam()
114
5.28k
{
115
5.28k
    static const auto total_ram{[]() -> std::optional<uint64_t> {
116
#ifdef WIN32
117
        if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return m.ullTotalPhys;
118
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__illumos__) || defined(__linux__)
119
1.33k
        if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s;
120
0
#endif
121
0
        return std::nullopt;
122
1.33k
    }()};
123
5.28k
    return total_ram;
124
5.28k
}
125
126
namespace {
127
    const auto g_startup_time{SteadyClock::now()};
128
} // namespace
129
130
2
SteadyClock::duration GetUptime() { return SteadyClock::now() - g_startup_time; }