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 <logging.h>
11 : : #include <util/string.h>
12 : : #include <util/time.h>
13 : :
14 : : #ifdef WIN32
15 : : #include <cassert>
16 : : #include <codecvt>
17 : : #include <compat/compat.h>
18 : : #include <windows.h>
19 : : #else
20 : : #include <sys/stat.h>
21 : : #include <unistd.h>
22 : : #endif
23 : :
24 : : #ifdef HAVE_MALLOPT_ARENA_MAX
25 : : #include <malloc.h>
26 : : #endif
27 : :
28 : : #include <algorithm>
29 : : #include <cstddef>
30 : : #include <cstdint>
31 : : #include <cstdlib>
32 : : #include <locale>
33 : : #include <optional>
34 : : #include <stdexcept>
35 : : #include <string>
36 : : #include <thread>
37 : :
38 : : using util::ReplaceAll;
39 : :
40 : : // Application startup time (used for uptime calculation)
41 : : const int64_t nStartupTime = GetTime();
42 : :
43 : : #ifndef WIN32
44 : 1206 : std::string ShellEscape(const std::string& arg)
45 : : {
46 [ - + ]: 1206 : std::string escaped = arg;
47 [ + - + - : 2412 : ReplaceAll(escaped, "'", "'\"'\"'");
+ - ]
48 [ + - ]: 2412 : return "'" + escaped + "'";
49 : 1206 : }
50 : : #endif
51 : :
52 : : #if HAVE_SYSTEM
53 : 0 : void runCommand(const std::string& strCommand)
54 : : {
55 [ # # ]: 0 : if (strCommand.empty()) return;
56 : : #ifndef WIN32
57 : 0 : int nErr = ::system(strCommand.c_str());
58 : : #else
59 : : int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str());
60 : : #endif
61 [ # # ]: 0 : if (nErr) {
62 : 0 : LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr);
63 : : }
64 : : }
65 : : #endif
66 : :
67 : 1320 : void SetupEnvironment()
68 : : {
69 : : #ifdef HAVE_MALLOPT_ARENA_MAX
70 : : // glibc-specific: On 32-bit systems set the number of arenas to 1.
71 : : // By default, since glibc 2.10, the C library will create up to two heap
72 : : // arenas per core. This is known to cause excessive virtual address space
73 : : // usage in our usage. Work around it by setting the maximum number of
74 : : // arenas to 1.
75 : 1320 : if (sizeof(void*) == 4) {
76 : : mallopt(M_ARENA_MAX, 1);
77 : : }
78 : : #endif
79 : : // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
80 : : // may be invalid, in which case the "C.UTF-8" locale is used as fallback.
81 : : #if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
82 : 1320 : try {
83 [ + - ]: 1320 : std::locale(""); // Raises a runtime error if current locale is invalid
84 [ - - ]: 0 : } catch (const std::runtime_error&) {
85 : 0 : setenv("LC_ALL", "C.UTF-8", 1);
86 : 0 : }
87 : : #elif defined(WIN32)
88 : : assert(GetACP() == CP_UTF8);
89 : : // Set the default input/output charset is utf-8
90 : : SetConsoleCP(CP_UTF8);
91 : : SetConsoleOutputCP(CP_UTF8);
92 : : #endif
93 : :
94 : : #ifndef WIN32
95 : 1320 : constexpr mode_t private_umask = 0077;
96 : 1320 : umask(private_umask);
97 : : #endif
98 : 1320 : }
99 : :
100 : 228 : bool SetupNetworking()
101 : : {
102 : : #ifdef WIN32
103 : : // Initialize Windows Sockets
104 : : WSADATA wsadata;
105 : : int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
106 : : if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
107 : : return false;
108 : : #endif
109 : 228 : return true;
110 : : }
111 : :
112 : 1320 : int GetNumCores()
113 : : {
114 : 1320 : return std::thread::hardware_concurrency();
115 : : }
116 : :
117 : 0 : std::optional<size_t> GetTotalRAM()
118 : : {
119 : 0 : [[maybe_unused]] auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
120 : : #ifdef WIN32
121 : : if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys);
122 : : #elif defined(__APPLE__) || \
123 : : defined(__FreeBSD__) || \
124 : : defined(__NetBSD__) || \
125 : : defined(__OpenBSD__) || \
126 : : defined(__illumos__) || \
127 : : defined(__linux__)
128 [ # # ]: 0 : if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return clamp(1ULL * p * s);
129 : : #endif
130 : 0 : return std::nullopt;
131 : : }
132 : :
133 : : // Obtain the application startup time (used for uptime calculation)
134 : 3 : int64_t GetStartupTime()
135 : : {
136 : 3 : return nStartupTime;
137 : : }
|