Branch data Line data Source code
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 : 0 : std::string ShellEscape(const std::string& arg)
41 : : {
42 [ # # ]: 0 : std::string escaped = arg;
43 [ # # # # : 0 : ReplaceAll(escaped, "'", "'\"'\"'");
# # ]
44 [ # # ]: 0 : return "'" + escaped + "'";
45 : 0 : }
46 : : #endif
47 : :
48 : : #if HAVE_SYSTEM
49 : 0 : void runCommand(const std::string& strCommand)
50 : : {
51 [ # # ]: 0 : if (strCommand.empty()) return;
52 : : #ifndef WIN32
53 : 0 : 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 [ # # ]: 0 : if (nErr) {
58 : 0 : LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr);
59 : : }
60 : : }
61 : : #endif
62 : :
63 : 707 : void SetupEnvironment()
64 : : {
65 : : #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 : 707 : if (sizeof(void*) == 4) {
72 : : mallopt(M_ARENA_MAX, 1);
73 : : }
74 : : #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 : : #if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
78 : 707 : try {
79 [ + - ]: 707 : std::locale(""); // Raises a runtime error if current locale is invalid
80 [ - - ]: 0 : } 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 : : #ifndef WIN32
91 : 707 : constexpr mode_t private_umask = 0077;
92 : 707 : umask(private_umask);
93 : : #endif
94 : 707 : }
95 : :
96 : 153 : bool SetupNetworking()
97 : : {
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 : 153 : return true;
106 : : }
107 : :
108 : 740 : int GetNumCores()
109 : : {
110 : 740 : return std::thread::hardware_concurrency();
111 : : }
112 : :
113 : 877 : std::optional<uint64_t> TryGetTotalRam()
114 : : {
115 : 998 : 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 [ + - ]: 121 : if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s;
120 : : #endif
121 : 0 : return std::nullopt;
122 [ + + + - ]: 877 : }()};
123 : 877 : return total_ram;
124 : : }
125 : :
126 : : namespace {
127 : : const auto g_startup_time{SteadyClock::now()};
128 : : } // namespace
129 : :
130 : 0 : SteadyClock::duration GetUptime() { return SteadyClock::now() - g_startup_time; }
|