ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
MemoryProfiler.cpp
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
23#include "core/MemoryProfiler.h"
24#include "core/AppConfig.h"
25#include <QDebug>
26#include <QMutex>
27
28// Cross-platform memory profiling
29#ifdef _WIN32
30 #include <windows.h>
31 #include <psapi.h>
32#else
33 #include <sys/resource.h>
34 #ifdef __APPLE__
35 #include <mach/mach.h>
36 #else
37 #include <unistd.h>
38 #include <fstream>
39 #endif
40#endif
41
42namespace acav {
43
44namespace {
45
46QMutex gLogMutex;
47MemoryProfiler::LogCallback gLogCallback;
48
49MemoryProfiler::LogCallback getLogCallback() {
50 QMutexLocker locker(&gLogMutex);
51 return gLogCallback;
52}
53
54} // namespace
55
56void MemoryProfiler::setLogCallback(LogCallback callback) {
57 QMutexLocker locker(&gLogMutex);
58 gLogCallback = std::move(callback);
59}
60
61bool MemoryProfiler::isEnabled() {
63}
64
66#ifdef _WIN32
67 // Windows: Use GetProcessMemoryInfo
68 PROCESS_MEMORY_COUNTERS_EX pmc;
69 if (GetProcessMemoryInfo(GetCurrentProcess(),
70 (PROCESS_MEMORY_COUNTERS*)&pmc,
71 sizeof(pmc))) {
72 return static_cast<long>(pmc.PeakWorkingSetSize / (1024 * 1024));
73 }
74 return -1;
75#else
76 // Unix/macOS: Use getrusage
77 struct rusage usage;
78 if (getrusage(RUSAGE_SELF, &usage) == 0) {
79#ifdef __APPLE__
80 // macOS: ru_maxrss is in bytes
81 return usage.ru_maxrss / (1024 * 1024);
82#else
83 // Linux: ru_maxrss is in kilobytes
84 return usage.ru_maxrss / 1024;
85#endif
86 }
87 return -1;
88#endif
89}
90
92#ifdef _WIN32
93 // Windows: Use GetProcessMemoryInfo for current working set
94 PROCESS_MEMORY_COUNTERS_EX pmc;
95 if (GetProcessMemoryInfo(GetCurrentProcess(),
96 (PROCESS_MEMORY_COUNTERS*)&pmc,
97 sizeof(pmc))) {
98 return static_cast<long>(pmc.WorkingSetSize / (1024 * 1024));
99 }
100 return -1;
101#elif defined(__APPLE__)
102 // macOS: Use mach task info for current memory
103 struct mach_task_basic_info info;
104 mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;
105 kern_return_t kerr = task_info(mach_task_self(),
106 MACH_TASK_BASIC_INFO,
107 (task_info_t)&info,
108 &size);
109 if (kerr == KERN_SUCCESS) {
110 return static_cast<long>(info.resident_size / (1024 * 1024));
111 }
112 return -1;
113#else
114 // Linux: Read from /proc/self/status
115 std::ifstream status("/proc/self/status");
116 std::string line;
117 while (std::getline(status, line)) {
118 if (line.substr(0, 6) == "VmRSS:") {
119 long rss_kb = 0;
120 sscanf(line.c_str(), "VmRSS: %ld kB", &rss_kb);
121 return rss_kb / 1024;
122 }
123 }
124 return -1;
125#endif
126}
127
128void MemoryProfiler::checkpoint(const QString &label) {
129 if (!isEnabled()) {
130 return;
131 }
132
133 long currentMB = getCurrentMemoryMB();
134 long peakMB = getPeakMemoryMB();
135
136 const LogCallback callback = getLogCallback();
137 if (currentMB >= 0 && peakMB >= 0) {
138 const QString message = QString("[MEM] %1: %2 MB (peak: %3 MB)")
139 .arg(label, -40)
140 .arg(currentMB)
141 .arg(peakMB);
142 if (callback) {
143 callback(message);
144 } else {
145 qDebug().noquote() << message;
146 }
147 } else {
148 const QString message =
149 QString("[MEM] Failed to retrieve memory usage for: %1").arg(label);
150 if (callback) {
151 callback(message);
152 } else {
153 qWarning().noquote() << message;
154 }
155 }
156}
157
158} // namespace acav
Application configuration and settings.
Cross-platform memory profiling utility.
static AppConfig & instance()
Get the singleton instance.
bool getMemoryProfilingEnabled() const
Whether memory profiling checkpoints are enabled (default: false).
static long getCurrentMemoryMB()
Get current actual memory usage in MB (current RSS).
static void checkpoint(const QString &label)
Log current memory usage with a label.
static long getPeakMemoryMB()
Get current peak memory usage in MB (maximum RSS since process start).
static void setLogCallback(LogCallback callback)
Set a callback for memory log messages (GUI hook).