/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 <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 | | std::string ShellEscape(const std::string& arg) |
38 | 27 | { |
39 | 27 | std::string escaped = arg; |
40 | 27 | ReplaceAll(escaped, "'", "'\"'\"'"); |
41 | 27 | return "'" + escaped + "'"; |
42 | 27 | } |
43 | | #endif |
44 | | |
45 | | #if HAVE_SYSTEM |
46 | | void runCommand(const std::string& strCommand) |
47 | 144 | { |
48 | 144 | if (strCommand.empty()) return; |
49 | 144 | int nErr = ::system(strCommand.c_str()); |
50 | 144 | if (nErr) { |
51 | 0 | LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr); |
52 | 0 | } |
53 | 144 | } |
54 | | #endif |
55 | | |
56 | | void SetupEnvironment() |
57 | 3.34k | { |
58 | 3.34k | #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 | 3.34k | if (sizeof(void*) == 4) { |
65 | 0 | mallopt(M_ARENA_MAX, 1); |
66 | 0 | } |
67 | 3.34k | #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 | 3.34k | #ifndef WIN32 |
77 | 3.34k | constexpr mode_t private_umask = 0077; |
78 | 3.34k | umask(private_umask); |
79 | 3.34k | #endif |
80 | 3.34k | } |
81 | | |
82 | | bool SetupNetworking() |
83 | 2.52k | { |
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 | 2.52k | return true; |
92 | 2.52k | } |
93 | | |
94 | | int GetNumCores() |
95 | 809 | { |
96 | 809 | return std::thread::hardware_concurrency(); |
97 | 809 | } |
98 | | |
99 | | std::optional<uint64_t> TryGetTotalRam() |
100 | 5.70k | { |
101 | 5.70k | 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 | 1.42k | if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s; |
106 | 0 | #endif |
107 | 0 | return std::nullopt; |
108 | 1.42k | }()}; |
109 | 5.70k | return total_ram; |
110 | 5.70k | } |
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; } |