Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 <optional>
31#include <string>
32#include <thread>
33
35
36#ifndef WIN32
37std::string ShellEscape(const std::string& arg)
38{
39 std::string escaped = arg;
40 ReplaceAll(escaped, "'", "'\"'\"'");
41 return "'" + escaped + "'";
42}
43#endif
44
45#if HAVE_SYSTEM
46void runCommand(const std::string& strCommand)
47{
48 if (strCommand.empty()) return;
49 int nErr = ::system(strCommand.c_str());
50 if (nErr) {
51 LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr);
52 }
53}
54#endif
55
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 if (sizeof(void*) == 4) {
66 }
67#endif
68
69#if defined(WIN32)
70 assert(GetACP() == CP_UTF8);
71 // Set the default input/output charset is utf-8
74#endif
75
76#ifndef WIN32
77 constexpr mode_t private_umask = 0077;
79#endif
80}
81
83{
84#ifdef WIN32
85 // Initialize Windows Sockets
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 return true;
92}
93
95{
96 return std::thread::hardware_concurrency();
97}
98
99std::optional<uint64_t> TryGetTotalRam()
100{
101 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 if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s;
106#endif
107 return std::nullopt;
108 }()};
109 return total_ram;
110}
111
112namespace {
113 const auto g_startup_time{SteadyClock::now()};
114} // namespace
115
116SteadyClock::duration GetUptime() { return SteadyClock::now() - g_startup_time; }
int ret
bool SetupNetworking()
Definition system.cpp:82
SteadyClock::duration GetUptime()
Monotonic uptime (not affected by system time changes).
Definition system.cpp:116
std::optional< uint64_t > TryGetTotalRam()
Return the total RAM available on the current system, if detectable.
Definition system.cpp:99
void SetupEnvironment()
Definition system.cpp:56
int GetNumCores()
Return the number of cores available on the current system.
Definition system.cpp:94
std::string ShellEscape(const std::string &arg)
Definition system.cpp:37
#define LogWarning(...)
Definition log.h:137
void ReplaceAll(std::string &in_out, std::string_view search, std::string_view substitute)
Replace every non-overlapping occurrence of search with substitute, treating both literally; the repl...
Definition string.cpp:14
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())