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>
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 <windows.h>
18 : : #else
19 : : #include <sys/stat.h>
20 : : #include <sys/types.h>
21 : : #include <unistd.h>
22 : : #endif
23 : :
24 : : #ifdef HAVE_MALLOPT_ARENA_MAX
25 : : #include <malloc.h>
26 : : #endif
27 : :
28 : : #include <cstdint>
29 : : #include <cstdlib>
30 : : #include <optional>
31 : : #include <string>
32 : : #include <thread>
33 : :
34 : : using util::ReplaceAll;
35 : :
36 : : #ifndef WIN32
37 : 1168 : std::string ShellEscape(const std::string& arg)
38 : : {
39 [ - + ]: 1168 : std::string escaped = arg;
40 [ + - ]: 1168 : ReplaceAll(escaped, "'", "'\"'\"'");
41 [ + - ]: 2336 : return "'" + escaped + "'";
42 : 1168 : }
43 : : #endif
44 : :
45 : : #if HAVE_SYSTEM
46 : 0 : void runCommand(const std::string& strCommand)
47 : : {
48 [ # # ]: 0 : if (strCommand.empty()) return;
49 : 0 : int nErr = ::system(strCommand.c_str());
50 [ # # ]: 0 : if (nErr) {
51 : 0 : LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr);
52 : : }
53 : : }
54 : : #endif
55 : :
56 : 1770 : void SetupEnvironment()
57 : : {
58 : : #ifdef HAVE_MALLOPT_ARENA_MAX
59 : : // glibc-specific: On 32-bit systems set the number of arenas to 1.
60 : : // By default, since glibc 2.10, the C library will create up to two heap
61 : : // arenas per core. This is known to cause excessive virtual address space
62 : : // usage in our usage. Work around it by setting the maximum number of
63 : : // arenas to 1.
64 : 1770 : if (sizeof(void*) == 4) {
65 : : mallopt(M_ARENA_MAX, 1);
66 : : }
67 : : #endif
68 : :
69 : : #if defined(WIN32)
70 : : assert(GetACP() == CP_UTF8);
71 : : // Set the default input/output charset is utf-8
72 : : SetConsoleCP(CP_UTF8);
73 : : SetConsoleOutputCP(CP_UTF8);
74 : : #endif
75 : :
76 : : #ifndef WIN32
77 : 1770 : constexpr mode_t private_umask = 0077;
78 : 1770 : umask(private_umask);
79 : : #endif
80 : 1770 : }
81 : :
82 : 238 : bool SetupNetworking()
83 : : {
84 : : #ifdef WIN32
85 : : // Initialize Windows Sockets
86 : : WSADATA wsadata;
87 : : int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
88 : : if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
89 : : return false;
90 : : #endif
91 : 238 : return true;
92 : : }
93 : :
94 : 1770 : int GetNumCores()
95 : : {
96 : 1770 : return std::thread::hardware_concurrency();
97 : : }
98 : :
99 : 3474 : std::optional<uint64_t> TryGetTotalRam()
100 : : {
101 : 3571 : static const auto total_ram{[]() -> std::optional<uint64_t> {
102 : : #ifdef WIN32
103 : : if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return m.ullTotalPhys;
104 : : #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__illumos__) || defined(__linux__)
105 [ + - ]: 97 : if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s;
106 : : #endif
107 : 0 : return std::nullopt;
108 [ + + + - ]: 3474 : }()};
109 : 3474 : return total_ram;
110 : : }
111 : :
112 : : namespace {
113 : : const auto g_startup_time{SteadyClock::now()};
114 : : } // namespace
115 : :
116 : 2 : SteadyClock::duration GetUptime() { return SteadyClock::now() - g_startup_time; }
|