Bitcoin ABC 0.32.4
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 <common/system.h>
7
8#include <logging.h>
9#include <util/string.h>
10#include <util/time.h>
11#include <util/translation.h>
12
13#include <cstdlib>
14#include <locale>
15#include <stdexcept>
16#include <string>
17#include <thread>
18
19#ifndef WIN32
20#include <sys/stat.h>
21#else
22#include <compat.h>
23#ifdef _MSC_VER
24#pragma warning(disable : 4786)
25#pragma warning(disable : 4804)
26#pragma warning(disable : 4805)
27#pragma warning(disable : 4717)
28#endif
29
30#ifndef NOMINMAX
31#define NOMINMAX
32#endif
33#include <codecvt>
34#endif
35
36#ifdef HAVE_MALLOPT_ARENA_MAX
37#include <malloc.h>
38#endif
39
40// Application startup time (used for uptime calculation)
41const int64_t nStartupTime = GetTime();
42
43#ifndef WIN32
44std::string ShellEscape(const std::string &arg) {
45 std::string escaped = arg;
46 ReplaceAll(escaped, "'", "'\"'\"'");
47 return "'" + escaped + "'";
48}
49#endif
50
51#if defined(HAVE_SYSTEM)
52void runCommand(const std::string &strCommand) {
53 if (strCommand.empty()) {
54 return;
55 }
56#ifndef WIN32
57 int nErr = ::system(strCommand.c_str());
58#else
59 int nErr = ::_wsystem(
60 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>()
61 .from_bytes(strCommand)
62 .c_str());
63#endif
64 if (nErr) {
65 LogPrintf("runCommand error: system(%s) returned %d\n", strCommand,
66 nErr);
67 }
68}
69#endif
70
72#ifdef HAVE_MALLOPT_ARENA_MAX
73 // glibc-specific: On 32-bit systems set the number of arenas to 1. By
74 // default, since glibc 2.10, the C library will create up to two heap
75 // arenas per core. This is known to cause excessive virtual address space
76 // usage in our usage. Work around it by setting the maximum number of
77 // arenas to 1.
78 if (sizeof(void *) == 4) {
79 mallopt(M_ARENA_MAX, 1);
80 }
81#endif
82// On most POSIX systems (e.g. Linux, but not BSD) the environment's locale may
83// be invalid, in which case the "C.UTF-8" locale is used as fallback.
84#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && \
85 !defined(__OpenBSD__)
86 try {
87 // Raises a runtime error if current locale is invalid.
88 std::locale("");
89 } catch (const std::runtime_error &) {
90 setenv("LC_ALL", "C.UTF-8", 1);
91 }
92#elif defined(WIN32)
93 // Set the default input/output charset is utf-8
94 SetConsoleCP(CP_UTF8);
95 SetConsoleOutputCP(CP_UTF8);
96#endif
97}
98
100#ifdef WIN32
101 // Initialize Windows Sockets.
102 WSADATA wsadata;
103 int ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
104 if (ret != NO_ERROR || LOBYTE(wsadata.wVersion) != 2 ||
105 HIBYTE(wsadata.wVersion) != 2) {
106 return false;
107 }
108#endif
109 return true;
110}
111
113 return std::thread::hardware_concurrency();
114}
115
116// Obtain the application startup time (used for uptime calculation)
117int64_t GetStartupTime() {
118 return nStartupTime;
119}
#define LogPrintf(...)
Definition: logging.h:424
void ReplaceAll(std::string &in_out, const std::string &search, const std::string &substitute)
Definition: string.cpp:10
int64_t GetStartupTime()
Definition: system.cpp:117
bool SetupNetworking()
Definition: system.cpp:99
void SetupEnvironment()
Definition: system.cpp:71
const int64_t nStartupTime
Definition: system.cpp:41
int GetNumCores()
Return the number of cores available on the current system.
Definition: system.cpp:112
std::string ShellEscape(const std::string &arg)
Definition: system.cpp:44
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:105