Bitcoin Core 31.99.0
P2P Digital Currency
system.cpp
Go to the documentation of this file.
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 <locale>
31#include <optional>
32#include <stdexcept>
33#include <string>
34#include <thread>
35
37
38#ifndef WIN32
39std::string ShellEscape(const std::string& arg)
40{
41 std::string escaped = arg;
42 ReplaceAll(escaped, "'", "'\"'\"'");
43 return "'" + escaped + "'";
44}
45#endif
46
47#if HAVE_SYSTEM
48void runCommand(const std::string& strCommand)
49{
50 if (strCommand.empty()) return;
51 int nErr = ::system(strCommand.c_str());
52 if (nErr) {
53 LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr);
54 }
55}
56#endif
57
59{
60#ifdef HAVE_MALLOPT_ARENA_MAX
61 // glibc-specific: On 32-bit systems set the number of arenas to 1.
62 // By default, since glibc 2.10, the C library will create up to two heap
63 // arenas per core. This is known to cause excessive virtual address space
64 // usage in our usage. Work around it by setting the maximum number of
65 // arenas to 1.
66 if (sizeof(void*) == 4) {
67 mallopt(M_ARENA_MAX, 1);
68 }
69#endif
70 // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
71 // may be invalid, in which case the "C.UTF-8" locale is used as fallback.
72#if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
73 try {
74 std::locale(""); // Raises a runtime error if current locale is invalid
75 } catch (const std::runtime_error&) {
76 setenv("LC_ALL", "C.UTF-8", 1);
77 }
78#elif defined(WIN32)
79 assert(GetACP() == CP_UTF8);
80 // Set the default input/output charset is utf-8
81 SetConsoleCP(CP_UTF8);
82 SetConsoleOutputCP(CP_UTF8);
83#endif
84
85#ifndef WIN32
86 constexpr mode_t private_umask = 0077;
87 umask(private_umask);
88#endif
89}
90
92{
93#ifdef WIN32
94 // Initialize Windows Sockets
95 WSADATA wsadata;
96 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
97 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
98 return false;
99#endif
100 return true;
101}
102
104{
105 return std::thread::hardware_concurrency();
106}
107
108std::optional<uint64_t> TryGetTotalRam()
109{
110 static const auto total_ram{[]() -> std::optional<uint64_t> {
111#ifdef WIN32
112 if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return m.ullTotalPhys;
113#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__illumos__) || defined(__linux__)
114 if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s;
115#endif
116 return std::nullopt;
117 }()};
118 return total_ram;
119}
120
121namespace {
122 const auto g_startup_time{SteadyClock::now()};
123} // namespace
124
125SteadyClock::duration GetUptime() { return SteadyClock::now() - g_startup_time; }
int ret
bool SetupNetworking()
Definition: system.cpp:91
SteadyClock::duration GetUptime()
Monotonic uptime (not affected by system time changes).
Definition: system.cpp:125
std::optional< uint64_t > TryGetTotalRam()
Return the total RAM available on the current system, if detectable.
Definition: system.cpp:108
void SetupEnvironment()
Definition: system.cpp:58
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:103
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:39
#define LogWarning(...)
Definition: log.h:126
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:14
assert(!tx.IsCoinBase())