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