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