Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <bitcoin-build-config.h> // IWYU pragma: keep8#include <common/system.h>10#include <util/log.h>11#include <util/string.h>12#include <util/time.h>14#ifdef WIN3215#include <compat/compat.h>16#include <util/check.h>17#include <windows.h>18#else19#include <sys/stat.h>20#include <sys/types.h>21#include <unistd.h>22#endif24#ifdef HAVE_MALLOPT_ARENA_MAX25#include <malloc.h>26#endif28#include <cstdint>29#include <cstdlib>30#include <locale>31#include <optional>32#include <stdexcept>33#include <string>34#include <thread>36using util::ReplaceAll;38#ifndef WIN3239std::string ShellEscape(const std::string& arg)40{41std::string escaped = arg;42ReplaceAll(escaped, "'", "'\"'\"'");43return "'" + escaped + "'";44}45#endif47#if HAVE_SYSTEM48void runCommand(const std::string& strCommand)49{50if (strCommand.empty()) return;51int nErr = ::system(strCommand.c_str());52if (nErr) {53LogWarning("runCommand error: system(%s) returned %d", strCommand, nErr);54}55}56#endif58void SetupEnvironment()59{60#ifdef HAVE_MALLOPT_ARENA_MAX61// glibc-specific: On 32-bit systems set the number of arenas to 1.62// By default, since glibc 2.10, the C library will create up to two heap63// arenas per core. This is known to cause excessive virtual address space64// usage in our usage. Work around it by setting the maximum number of65// arenas to 1.66if (sizeof(void*) == 4) {67mallopt(M_ARENA_MAX, 1);68}69#endif70// On most POSIX systems (e.g. Linux, but not BSD) the environment's locale71// may be invalid, in which case the "C.UTF-8" locale is used as fallback.72#if !defined(WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__)73try {74std::locale(""); // Raises a runtime error if current locale is invalid75} catch (const std::runtime_error&) {76setenv("LC_ALL", "C.UTF-8", 1);77}78#elif defined(WIN32)79assert(GetACP() == CP_UTF8);80// Set the default input/output charset is utf-881SetConsoleCP(CP_UTF8);82SetConsoleOutputCP(CP_UTF8);83#endif85#ifndef WIN3286constexpr mode_t private_umask = 0077;87umask(private_umask);88#endif89}91bool SetupNetworking()92{93#ifdef WIN3294// Initialize Windows Sockets95WSADATA wsadata;96int ret = WSAStartup(MAKEWORD(2,2), &wsadata);97if (ret != NO_ERROR || LOBYTE(wsadata.wVersion ) != 2 || HIBYTE(wsadata.wVersion) != 2)98return false;99#endif100return true;101}103int GetNumCores()104{105return std::thread::hardware_concurrency();106}108std::optional<uint64_t> TryGetTotalRam()109{110static const auto total_ram{[]() -> std::optional<uint64_t> {111#ifdef WIN32112if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return m.ullTotalPhys;113#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__illumos__) || defined(__linux__)114if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return 1ULL * p * s;115#endif116return std::nullopt;117}()};118return total_ram;119}121namespace {122const auto g_startup_time{SteadyClock::now()};123} // namespace125SteadyClock::duration GetUptime() { return SteadyClock::now() - g_startup_time; }
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.