|
| 1 | +// Copyright (c) 2025 The Bitcoin Knots developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <bitcoin-build-config.h> // IWYU pragma: keep |
| 6 | + |
| 7 | +#include <util/memory_profiler.h> |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | + |
| 11 | +#ifdef WIN32 |
| 12 | +#include <windows.h> |
| 13 | +#include <psapi.h> |
| 14 | +#elif defined(__linux__) |
| 15 | +#include <fstream> |
| 16 | +#include <sstream> |
| 17 | +#include <string> |
| 18 | +#elif defined(__APPLE__) |
| 19 | +#include <sys/resource.h> |
| 20 | +#include <mach/mach.h> |
| 21 | +#endif |
| 22 | + |
| 23 | +std::unique_ptr<MemoryProfiler> g_memory_profiler; |
| 24 | + |
| 25 | +MemoryProfiler::MemoryProfiler() |
| 26 | +{ |
| 27 | + m_profiler_thread = std::make_unique<std::thread>(&MemoryProfiler::ProfilerThread, this); |
| 28 | +} |
| 29 | + |
| 30 | +MemoryProfiler::~MemoryProfiler() |
| 31 | +{ |
| 32 | + m_shutdown = true; |
| 33 | + { |
| 34 | + std::lock_guard<std::mutex> cv_lock(m_cv_mutex); |
| 35 | + m_cv.notify_all(); |
| 36 | + } |
| 37 | + if (m_profiler_thread && m_profiler_thread->joinable()) { |
| 38 | + m_profiler_thread->join(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +size_t MemoryProfiler::GetProcessRSS() |
| 43 | +{ |
| 44 | +#ifdef WIN32 |
| 45 | + PROCESS_MEMORY_COUNTERS_EX pmc; |
| 46 | + if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc))) { |
| 47 | + return static_cast<size_t>(pmc.PrivateUsage); // Current commit charge |
| 48 | + } |
| 49 | +#elif defined(__linux__) |
| 50 | + std::ifstream status("/proc/self/status"); |
| 51 | + std::string line; |
| 52 | + while (std::getline(status, line)) { |
| 53 | + if (line.compare(0, 6, "VmRSS:") == 0) { |
| 54 | + size_t pos = line.find_first_of("0123456789"); |
| 55 | + if (pos != std::string::npos) { |
| 56 | + size_t kb = std::stoull(line.substr(pos)); |
| 57 | + return kb * 1024; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +#elif defined(__APPLE__) |
| 62 | + struct mach_task_basic_info info; |
| 63 | + mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; |
| 64 | + if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count) == KERN_SUCCESS) { |
| 65 | + return static_cast<size_t>(info.resident_size); |
| 66 | + } |
| 67 | +#endif |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +void MemoryProfiler::StartFlushProfiling() |
| 72 | +{ |
| 73 | + // Check if already profiling and stop previous profile if needed |
| 74 | + if (m_profiling.load()) { |
| 75 | + StopFlushProfiling(); |
| 76 | + } |
| 77 | + |
| 78 | + LOCK(m_mutex); |
| 79 | + |
| 80 | + m_current_profile = std::make_unique<FlushProfile>(); |
| 81 | + m_current_profile->start_time = std::chrono::steady_clock::now(); |
| 82 | + |
| 83 | + // Capture initial memory state (lock already held) |
| 84 | + auto initial_sample = CaptureMemorySampleLocked("pre_flush", true); |
| 85 | + m_current_profile->pre_flush_memory_mb = initial_sample.process_rss_mb; |
| 86 | + m_current_profile->peak_memory_mb = initial_sample.process_rss_mb; |
| 87 | + m_current_profile->samples.push_back(initial_sample); |
| 88 | + |
| 89 | + m_profiling = true; |
| 90 | + |
| 91 | + // Notify profiler thread |
| 92 | + { |
| 93 | + std::lock_guard<std::mutex> cv_lock(m_cv_mutex); |
| 94 | + m_cv.notify_one(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +std::unique_ptr<MemoryProfiler::FlushProfile> MemoryProfiler::StopFlushProfiling() |
| 99 | +{ |
| 100 | + LOCK(m_mutex); |
| 101 | + |
| 102 | + if (!m_current_profile) { |
| 103 | + return nullptr; |
| 104 | + } |
| 105 | + |
| 106 | + m_profiling = false; |
| 107 | + |
| 108 | + // Capture final memory state (lock already held) |
| 109 | + auto final_sample = CaptureMemorySampleLocked("post_flush", true); |
| 110 | + m_current_profile->samples.push_back(final_sample); |
| 111 | + m_current_profile->post_flush_memory_mb = final_sample.process_rss_mb; |
| 112 | + m_current_profile->end_time = std::chrono::steady_clock::now(); |
| 113 | + |
| 114 | + // Calculate peak from all samples |
| 115 | + for (const auto& sample : m_current_profile->samples) { |
| 116 | + m_current_profile->peak_memory_mb = std::max(m_current_profile->peak_memory_mb, sample.process_rss_mb); |
| 117 | + } |
| 118 | + |
| 119 | + // Store in history |
| 120 | + m_flush_history.push_back(*m_current_profile); |
| 121 | + if (m_flush_history.size() > MAX_HISTORY_SIZE) { |
| 122 | + m_flush_history.pop_front(); |
| 123 | + } |
| 124 | + |
| 125 | + return std::move(m_current_profile); |
| 126 | +} |
| 127 | + |
| 128 | +std::vector<MemoryProfiler::FlushProfile> MemoryProfiler::GetRecentProfiles(size_t count) const |
| 129 | +{ |
| 130 | + LOCK(m_mutex); |
| 131 | + |
| 132 | + std::vector<FlushProfile> result; |
| 133 | + size_t start_idx = m_flush_history.size() > count ? m_flush_history.size() - count : 0; |
| 134 | + |
| 135 | + for (size_t i = start_idx; i < m_flush_history.size(); ++i) { |
| 136 | + result.push_back(m_flush_history[i]); |
| 137 | + } |
| 138 | + |
| 139 | + return result; |
| 140 | +} |
| 141 | + |
| 142 | +void MemoryProfiler::ProfilerThread() |
| 143 | +{ |
| 144 | + while (!m_shutdown) { |
| 145 | + // Use cv_mutex for condition variable |
| 146 | + std::unique_lock<std::mutex> cv_lock(m_cv_mutex); |
| 147 | + |
| 148 | + // Wait for either profiling to start or shutdown |
| 149 | + m_cv.wait_for(cv_lock, std::chrono::milliseconds(100), [this] { |
| 150 | + return m_profiling.load() || m_shutdown.load(); |
| 151 | + }); |
| 152 | + |
| 153 | + cv_lock.unlock(); |
| 154 | + |
| 155 | + if (m_shutdown) break; |
| 156 | + |
| 157 | + if (m_profiling) { |
| 158 | + LOCK(m_mutex); |
| 159 | + if (m_current_profile) { |
| 160 | + // Need to capture sample without holding m_mutex to avoid deadlock |
| 161 | + auto profile_copy = *m_current_profile; // Copy current profile state |
| 162 | + LEAVE_CRITICAL_SECTION(m_mutex); |
| 163 | + |
| 164 | + auto sample = CaptureMemorySample("profiling"); |
| 165 | + |
| 166 | + ENTER_CRITICAL_SECTION(m_mutex); |
| 167 | + // Double-check we still have a profile after reacquiring lock |
| 168 | + if (m_current_profile && m_profiling) { |
| 169 | + m_current_profile->samples.push_back(sample); |
| 170 | + m_current_profile->peak_memory_mb = std::max(m_current_profile->peak_memory_mb, sample.process_rss_mb); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +MemoryProfiler::MemorySample MemoryProfiler::CaptureMemorySample(const std::string& phase) const |
| 178 | +{ |
| 179 | + return CaptureMemorySampleLocked(phase, false); |
| 180 | +} |
| 181 | + |
| 182 | +MemoryProfiler::MemorySample MemoryProfiler::CaptureMemorySampleLocked(const std::string& phase, bool lock_already_held) const |
| 183 | +{ |
| 184 | + MemorySample sample; |
| 185 | + sample.timestamp = std::chrono::steady_clock::now(); |
| 186 | + sample.phase = phase; |
| 187 | + |
| 188 | + // Just get RSS - nothing else |
| 189 | + size_t rss_bytes = GetProcessRSS(); |
| 190 | + sample.process_rss_mb = rss_bytes > 0 ? rss_bytes / (1 << 20) : 0; |
| 191 | + |
| 192 | + return sample; |
| 193 | +} |
0 commit comments