/Users/eugenesiegel/btc/bitcoin/src/common/system.cpp
| Line | Count | Source (jump to first uncovered line) | 
| 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 <logging.h> | 
| 11 |  | #include <util/string.h> | 
| 12 |  | #include <util/time.h> | 
| 13 |  |  | 
| 14 |  | #ifdef WIN32 | 
| 15 |  | #include <codecvt> | 
| 16 |  | #include <compat/compat.h> | 
| 17 |  | #include <windows.h> | 
| 18 |  | #else | 
| 19 |  | #include <sys/stat.h> | 
| 20 |  | #include <unistd.h> | 
| 21 |  | #endif | 
| 22 |  |  | 
| 23 |  | #ifdef HAVE_MALLOPT_ARENA_MAX | 
| 24 |  | #include <malloc.h> | 
| 25 |  | #endif | 
| 26 |  |  | 
| 27 |  | #include <algorithm> | 
| 28 |  | #include <cstddef> | 
| 29 |  | #include <cstdint> | 
| 30 |  | #include <cstdlib> | 
| 31 |  | #include <locale> | 
| 32 |  | #include <optional> | 
| 33 |  | #include <stdexcept> | 
| 34 |  | #include <string> | 
| 35 |  | #include <thread> | 
| 36 |  |  | 
| 37 |  | using util::ReplaceAll; | 
| 38 |  |  | 
| 39 |  | // Application startup time (used for uptime calculation) | 
| 40 |  | const int64_t nStartupTime = GetTime(); | 
| 41 |  |  | 
| 42 |  | #ifndef WIN32 | 
| 43 |  | std::string ShellEscape(const std::string& arg) | 
| 44 | 0 | { | 
| 45 | 0 |     std::string escaped = arg; | 
| 46 | 0 |     ReplaceAll(escaped, "'", "'\"'\"'"); | 
| 47 | 0 |     return "'" + escaped + "'"; | 
| 48 | 0 | } | 
| 49 |  | #endif | 
| 50 |  |  | 
| 51 |  | #if HAVE_SYSTEM | 
| 52 |  | void runCommand(const std::string& strCommand) | 
| 53 | 0 | { | 
| 54 | 0 |     if (strCommand.empty()) return; | 
| 55 | 0 | #ifndef WIN32 | 
| 56 | 0 |     int nErr = ::system(strCommand.c_str()); | 
| 57 |  | #else | 
| 58 |  |     int nErr = ::_wsystem(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().from_bytes(strCommand).c_str()); | 
| 59 |  | #endif | 
| 60 | 0 |     if (nErr) | 
| 61 | 0 |         LogPrintf("runCommand error: system(%s) returned %d\n", strCommand, nErr);| Line | Count | Source |  | 361 | 0 | #define LogPrintf(...) LogInfo(__VA_ARGS__) | Line | Count | Source |  | 356 | 0 | #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__) | Line | Count | Source |  | 350 | 0 | #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(std::source_location::current(), category, level, should_ratelimit, __VA_ARGS__) | 
 | 
 | 
 | 
| 62 | 0 | } | 
| 63 |  | #endif | 
| 64 |  |  | 
| 65 |  | void SetupEnvironment() | 
| 66 | 51.2k | { | 
| 67 |  | #ifdef HAVE_MALLOPT_ARENA_MAX | 
| 68 |  |     // glibc-specific: On 32-bit systems set the number of arenas to 1. | 
| 69 |  |     // By default, since glibc 2.10, the C library will create up to two heap | 
| 70 |  |     // arenas per core. This is known to cause excessive virtual address space | 
| 71 |  |     // usage in our usage. Work around it by setting the maximum number of | 
| 72 |  |     // arenas to 1. | 
| 73 |  |     if (sizeof(void*) == 4) { | 
| 74 |  |         mallopt(M_ARENA_MAX, 1); | 
| 75 |  |     } | 
| 76 |  | #endif | 
| 77 |  |     // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale | 
| 78 |  |     // may be invalid, in which case the "C.UTF-8" locale is used as fallback. | 
| 79 |  | #if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) | 
| 80 |  |     try { | 
| 81 |  |         std::locale(""); // Raises a runtime error if current locale is invalid | 
| 82 |  |     } catch (const std::runtime_error&) { | 
| 83 |  |         setenv("LC_ALL", "C.UTF-8", 1); | 
| 84 |  |     } | 
| 85 |  | #elif defined(WIN32) | 
| 86 |  |     // Set the default input/output charset is utf-8 | 
| 87 |  |     SetConsoleCP(CP_UTF8); | 
| 88 |  |     SetConsoleOutputCP(CP_UTF8); | 
| 89 |  | #endif | 
| 90 |  |  | 
| 91 | 51.2k | #ifndef WIN32 | 
| 92 | 51.2k |     constexpr mode_t private_umask = 0077; | 
| 93 | 51.2k |     umask(private_umask); | 
| 94 | 51.2k | #endif | 
| 95 | 51.2k | } | 
| 96 |  |  | 
| 97 |  | bool SetupNetworking() | 
| 98 | 0 | { | 
| 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 || HIBYTE(wsadata.wVersion) != 2) | 
| 104 |  |         return false; | 
| 105 |  | #endif | 
| 106 | 0 |     return true; | 
| 107 | 0 | } | 
| 108 |  |  | 
| 109 |  | int GetNumCores() | 
| 110 | 51.2k | { | 
| 111 | 51.2k |     return std::thread::hardware_concurrency(); | 
| 112 | 51.2k | } | 
| 113 |  |  | 
| 114 |  | std::optional<size_t> GetTotalRAM() | 
| 115 | 0 | { | 
| 116 | 0 |     [[maybe_unused]] auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }}; | 
| 117 |  | #ifdef WIN32 | 
| 118 |  |     if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys); | 
| 119 |  | #elif defined(__APPLE__) || \ | 
| 120 |  |       defined(__FreeBSD__) || \ | 
| 121 |  |       defined(__NetBSD__) || \ | 
| 122 |  |       defined(__OpenBSD__) || \ | 
| 123 |  |       defined(__illumos__) || \ | 
| 124 |  |       defined(__linux__) | 
| 125 | 0 |     if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return clamp(1ULL * p * s); | 
| 126 | 0 | #endif | 
| 127 | 0 |     return std::nullopt; | 
| 128 | 0 | } | 
| 129 |  |  | 
| 130 |  | // Obtain the application startup time (used for uptime calculation) | 
| 131 |  | int64_t GetStartupTime() | 
| 132 | 0 | { | 
| 133 | 0 |     return nStartupTime; | 
| 134 | 0 | } |