Bitcoin Core  25.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-2022 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 <util/system.h>
7 
8 #include <logging.h>
9 #include <util/string.h>
10 #include <util/syserror.h>
11 #include <util/time.h>
12 
13 #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
14 #include <pthread.h>
15 #include <pthread_np.h>
16 #endif
17 
18 #ifndef WIN32
19 #include <sched.h>
20 #include <sys/stat.h>
21 #else
22 #include <codecvt>
23 #endif
24 
25 #ifdef HAVE_MALLOPT_ARENA_MAX
26 #include <malloc.h>
27 #endif
28 
29 #include <cstdlib>
30 #include <locale>
31 #include <stdexcept>
32 #include <string>
33 #include <thread>
34 
35 // Application startup time (used for uptime calculation)
36 const int64_t nStartupTime = GetTime();
37 
38 #ifndef WIN32
39 std::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
48 void runCommand(const std::string& strCommand)
49 {
50  if (strCommand.empty()) return;
51 #ifndef WIN32
52  int nErr = ::system(strCommand.c_str());
53 #else
54  int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str());
55 #endif
56  if (nErr)
57  LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);
58 }
59 #endif
60 
62 {
63 #ifdef HAVE_MALLOPT_ARENA_MAX
64  // glibc-specific: On 32-bit systems set the number of arenas to 1.
65  // By default, since glibc 2.10, the C library will create up to two heap
66  // arenas per core. This is known to cause excessive virtual address space
67  // usage in our usage. Work around it by setting the maximum number of
68  // arenas to 1.
69  if (sizeof(void*) == 4) {
70  mallopt(M_ARENA_MAX, 1);
71  }
72 #endif
73  // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
74  // may be invalid, in which case the "C.UTF-8" locale is used as fallback.
75 #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)
76  try {
77  std::locale(""); // Raises a runtime error if current locale is invalid
78  } catch (const std::runtime_error&) {
79  setenv("LC_ALL", "C.UTF-8", 1);
80  }
81 #elif defined(WIN32)
82  // Set the default input/output charset is utf-8
83  SetConsoleCP(CP_UTF8);
84  SetConsoleOutputCP(CP_UTF8);
85 #endif
86 
87 #ifndef WIN32
88  constexpr mode_t private_umask = 0077;
89  umask(private_umask);
90 #endif
91 }
92 
94 {
95 #ifdef WIN32
96  // Initialize Windows Sockets
97  WSADATA wsadata;
98  int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
99  if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)
100  return false;
101 #endif
102  return true;
103 }
104 
106 {
107  return std::thread::hardware_concurrency();
108 }
109 
110 // Obtain the application startup time (used for uptime calculation)
111 int64_t GetStartupTime()
112 {
113  return nStartupTime;
114 }
115 
117 {
118 #ifdef SCHED_BATCH
119  const static sched_param param{};
120  const int rc = pthread_setschedparam(pthread_self(), SCHED_BATCH, &param);
121  if (rc != 0) {
122  LogPrintf("Failed to pthread_setschedparam: %s\n", SysErrorString(rc));
123  }
124 #endif
125 }
int ret
#define LogPrintf(...)
Definition: logging.h:236
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:15
int64_t GetTime()
Definition: time.cpp:97
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:10
int64_t GetStartupTime()
Definition: system.cpp:111
bool SetupNetworking()
Definition: system.cpp:93
void ScheduleBatchPriority()
On platforms that support it, tell the kernel the calling thread is CPU-intensive and non-interactive...
Definition: system.cpp:116
void SetupEnvironment()
Definition: system.cpp:61
const int64_t nStartupTime
Definition: system.cpp:36
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:105
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:39