-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathGameMemoryMonitor.cs
More file actions
435 lines (382 loc) · 15.5 KB
/
Copy pathGameMemoryMonitor.cs
File metadata and controls
435 lines (382 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* Copyright (C) 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using UnityEngine;
/// <summary>
/// A MonoBehaviour that monitors memory and process state for the application.
/// It uses a background thread for high-frequency polling of lightweight /proc stats,
/// and throttles heavier JNI calls on the main thread to avoid frame drops.
/// </summary>
public class GameMemoryMonitor : MonoBehaviour
{
public bool showDebugOverlay = true;
private string cachedOverlayText = "";
private float nextJniUpdateTime = 0f;
private float nextLogTime = 0f;
private List<AndroidProcessStats.AppProcessInfo> knownProcesses = new List<AndroidProcessStats.AppProcessInfo>();
private readonly object processLock = new object();
private Dictionary<int, PidStats> pidStatsMap = new Dictionary<int, PidStats>();
private readonly object statsLock = new object();
private Thread pollingThread;
private volatile bool isRunning = true;
private string[] pidColorHexes = new string[] {
"#FF5555", "#55FF55", "#FFFF55", "#5555FF",
"#FF55FF", "#55FFFF", "#FFFFFF", "#AAAAAA",
"#FF9999", "#9999FF"
};
/// <summary>
/// Container for tracking memory trends specific to a Process ID.
/// </summary>
public class PidStats
{
public string processName;
public string colorHex;
// Tracks the highest memory seen during each OOM adj state
public Dictionary<int, long> maxMemPerOom = new Dictionary<int, long>();
// Tracks time spent at various memory levels during each OOM adj state
public Dictionary<int, Dictionary<long, float>> memHistograms = new Dictionary<int, Dictionary<long, float>>();
// Latest snapshot data for GUI presentation
public int lastOomAdj;
public int lastImportance;
public long lastAnonRssKb;
public long lastSwapKb;
public string lastReasonComponent;
}
private void Start()
{
// PERFORMANCE: Start a background thread to read /proc files.
// File I/O (even virtual files like /proc) can occasionally block.
// Doing this off the main thread ensures we never stutter the game's framerate.
pollingThread = new Thread(BackgroundPollingLoop);
pollingThread.Start();
}
private void OnDestroy()
{
isRunning = false;
if (pollingThread != null)
{
if (!pollingThread.Join(500))
{
pollingThread.Abort();
}
}
}
private void OnApplicationPause(bool isPaused)
{
if (isPaused)
{
Debug.Log("[MemoryMonitor] Game minimized. Querying entry stats...");
LogDiagnostics("BACKGROUND_TRANSITION_START");
}
else
{
Debug.Log("[MemoryMonitor] Game resumed. Querying active stats...");
LogDiagnostics("FOREGROUND_RESUME");
}
}
/// <summary>
/// The background loop that continuously reads memory data.
/// Runs on a separate thread.
/// </summary>
private void BackgroundPollingLoop()
{
float pollInterval = 0.1f; // Poll at 10Hz to catch quick transitions
while (isRunning)
{
List<AndroidProcessStats.AppProcessInfo> currentProcesses;
// Thread Safety: Lock when reading the shared list of known processes
// which is updated by the main thread.
lock (processLock)
{
currentProcesses = new List<AndroidProcessStats.AppProcessInfo>(knownProcesses);
}
lock (statsLock)
{
foreach (var proc in currentProcesses)
{
if (!pidStatsMap.ContainsKey(proc.pid))
{
pidStatsMap[proc.pid] = new PidStats {
processName = proc.processName,
colorHex = pidColorHexes[pidStatsMap.Count % pidColorHexes.Length]
};
}
var stats = pidStatsMap[proc.pid];
UnityMemorySnapshot mem = AndroidNativeDiagnostics.CaptureMemorySnapshot(proc.pid);
if (mem.oomScoreAdj != -9999)
{
long currentTotal = mem.anonRssPlusSwap;
// Update Max
if (!stats.maxMemPerOom.ContainsKey(mem.oomScoreAdj))
{
stats.maxMemPerOom[mem.oomScoreAdj] = 0;
}
if (currentTotal > stats.maxMemPerOom[mem.oomScoreAdj])
{
stats.maxMemPerOom[mem.oomScoreAdj] = currentTotal;
}
// Update Histogram using absolute MB to prevent corruption when max changes
long bucketMb = currentTotal / 1024;
if (!stats.memHistograms.ContainsKey(mem.oomScoreAdj))
{
stats.memHistograms[mem.oomScoreAdj] = new Dictionary<long, float>();
}
if (!stats.memHistograms[mem.oomScoreAdj].ContainsKey(bucketMb))
{
stats.memHistograms[mem.oomScoreAdj][bucketMb] = 0f;
}
stats.memHistograms[mem.oomScoreAdj][bucketMb] += pollInterval;
// Update latest snapshot
stats.lastOomAdj = mem.oomScoreAdj;
stats.lastImportance = proc.importance;
stats.lastAnonRssKb = mem.anonRssKb;
stats.lastSwapKb = mem.swapKb;
stats.lastReasonComponent = proc.reasonComponent;
}
}
}
Thread.Sleep(100);
}
}
private void Update()
{
if (!showDebugOverlay)
{
return;
}
// PERFORMANCE: Throttle the heavy JNI queries to once every 2 seconds.
// ActivityManager.getRunningAppProcesses() does IPC (Inter-Process Communication)
// to the system server, which is slow and creates garbage. We do NOT want to call this every frame.
if (Time.time > nextJniUpdateTime)
{
nextJniUpdateTime = Time.time + 2.0f;
var newProcs = AndroidProcessStats.GetAllRunningAppProcesses();
// Thread Safety: Lock when writing the shared list of known processes.
lock (processLock)
{
knownProcesses = newProcs;
}
RefreshOverlayText();
}
// Print histograms every 60 seconds
if (Time.time > nextLogTime && Time.time > 10.0f)
{
nextLogTime = Time.time + 60.0f;
LogHistogramSummary();
}
}
private void LogHistogramSummary()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("[MemoryMonitor] --- 60s Periodic Histogram Summary ---");
lock (statsLock)
{
foreach (var kvp in pidStatsMap)
{
int pid = kvp.Key;
var stats = kvp.Value;
sb.AppendLine($"PID: {pid} ({stats.processName})");
foreach (var oomKvp in stats.maxMemPerOom)
{
int oom = oomKvp.Key;
long maxMem = oomKvp.Value;
float timeAbove95 = 0f;
long thresholdMb = (long)(maxMem * 0.95f) / 1024;
if (stats.memHistograms.ContainsKey(oom))
{
foreach (var bucketKvp in stats.memHistograms[oom])
{
if (bucketKvp.Key >= thresholdMb)
{
timeAbove95 += bucketKvp.Value;
}
}
}
sb.AppendLine($" OOM {oom}: Max Mem = {(maxMem / 1024f):F2} MB, Time >95% Peak = {timeAbove95:F1}s");
}
}
}
sb.AppendLine("---------------------------------------------------");
Debug.Log(sb.ToString());
}
private void RefreshOverlayText()
{
var sb = new StringBuilder();
lock (statsLock)
{
foreach (var kvp in pidStatsMap)
{
int pid = kvp.Key;
var stats = kvp.Value;
string calculatedProcGroup = MapToProcGroup(stats.lastOomAdj);
sb.AppendLine($"<color={stats.colorHex}>PID: {pid} ({stats.processName})</color>");
sb.AppendLine($"OOM Adj: {stats.lastOomAdj} | Imp: {stats.lastImportance}");
sb.AppendLine($"State: {calculatedProcGroup}");
if (Application.platform == RuntimePlatform.Android)
{
sb.AppendLine($"Current Anon+Swap: {((stats.lastAnonRssKb + stats.lastSwapKb) / 1024f):F2} MB");
if (stats.maxMemPerOom.ContainsKey(stats.lastOomAdj))
{
sb.AppendLine($"<color=#ffddaa>Max at OOM {stats.lastOomAdj}: {(stats.maxMemPerOom[stats.lastOomAdj] / 1024f):F2} MB</color>");
}
if (!string.IsNullOrEmpty(stats.lastReasonComponent))
{
sb.AppendLine($"Reason: {stats.lastReasonComponent}");
}
}
sb.AppendLine("-----------------------------");
}
}
cachedOverlayText = sb.ToString();
needsFontRecalc = true;
}
private int cachedFontSize = 14;
private bool needsFontRecalc = true;
private int lastScreenWidth = 0;
private void OnGUI()
{
if (!showDebugOverlay || string.IsNullOrEmpty(cachedOverlayText))
{
return;
}
float rectWidth = Screen.width;
float rectHeight = Screen.height * 0.5f;
Rect overlayRect = new Rect(0, 0, rectWidth, rectHeight);
// PERFORMANCE: Recalculate font size only when text changes or screen resizes.
// GUIStyle.CalcHeight is an expensive operation that computes layout.
// Doing this every frame would cause significant CPU overhead.
if (needsFontRecalc || lastScreenWidth != Screen.width)
{
lastScreenWidth = Screen.width;
needsFontRecalc = false;
// Allow Rich Text in CalcHeight
GUIStyle tempStyle = new GUIStyle(GUI.skin.label) { wordWrap = true, padding = new RectOffset(10, 10, 10, 10), richText = true };
int bestFont = 10;
for (int i = 70; i >= 10; i--)
{
tempStyle.fontSize = i;
if (tempStyle.CalcHeight(new GUIContent(cachedOverlayText), rectWidth) <= rectHeight)
{
bestFont = i;
break;
}
}
cachedFontSize = bestFont;
}
// Draw a translucent black background that doesn't block raycasts
Color oldColor = GUI.color;
GUI.color = new Color(0, 0, 0, 0.7f);
GUI.DrawTexture(overlayRect, Texture2D.whiteTexture);
GUI.color = oldColor;
// Draw the text
GUIStyle style = new GUIStyle(GUI.skin.label)
{
fontSize = cachedFontSize,
normal = { textColor = Color.green },
wordWrap = true,
padding = new RectOffset(10, 10, 10, 10),
richText = true
};
GUI.Label(overlayRect, cachedOverlayText, style);
}
private void LogDiagnostics(string contextTag)
{
var processes = AndroidProcessStats.GetAllRunningAppProcesses();
foreach (var proc in processes)
{
UnityMemorySnapshot mem = AndroidNativeDiagnostics.CaptureMemorySnapshot(proc.pid);
string calculatedProcGroup = MapToProcGroup(mem.oomScoreAdj);
string componentInfo = string.IsNullOrEmpty(proc.reasonComponent) ? "" : $" | Reason: {proc.reasonComponent}";
Debug.Log($"[{contextTag}] [PID: {proc.pid} ({proc.processName})] " +
$"OOM Score: {mem.oomScoreAdj} | " +
$"OS Importance: {proc.importance} | " +
$"Engine Proc State: {calculatedProcGroup} | " +
$"Anon RSS + Swap: {(mem.anonRssPlusSwap / 1024f):F2} MB{componentInfo}");
}
}
/// <summary>
/// Maps the raw OOM score adjustment to a human-readable Android process state.
/// </summary>
/// <param name="adj">The OOM score adjustment value.</param>
/// <returns>A string describing the state.</returns>
private string MapToProcGroup(int adj)
{
if (adj == 0)
{
return "0 (FOREGROUND_APP_ADJ): Foreground (focused/visible)";
}
if (adj == 50)
{
return "50 (RECENT_FOREGROUND): Briefly bumped to avoid disruption";
}
if (adj == 100)
{
return "100 (VISIBLE_APP_ADJ): Visible but not focused";
}
if (adj == 200)
{
return "200 (PERCEPTIBLE_APP_ADJ): Background with visible UI (e.g., PiP)";
}
if (adj >= 201 && adj <= 224)
{
return $"{adj} (PERCEPTIBLE_LESS_IMPORTANT): Less important perceptible";
}
if (adj == 225)
{
return "225 (PERCEPTIBLE_MEDIUM_APP_ADJ): Device state dependent (e.g. mic active)";
}
if (adj >= 226 && adj <= 249)
{
return $"{adj} (PERCEPTIBLE_LESS_IMPORTANT): Less important perceptible";
}
if (adj == 250)
{
return "250 (PERCEPTIBLE_LOW_APP_ADJ): Low perceptible (e.g. location)";
}
if (adj >= 300 && adj < 400)
{
return $"{adj} (BACKUP_APP_ADJ): Performing backup";
}
if (adj >= 400 && adj < 500)
{
return $"{adj} (HEAVY_WEIGHT): Heavy weight app";
}
if (adj >= 500 && adj < 600)
{
return $"{adj} (SERVICE_ADJ): Background service";
}
if (adj >= 600 && adj < 700)
{
return $"{adj} (HOME_APP_ADJ): Home/Launcher";
}
if (adj >= 700 && adj < 800)
{
return $"{adj} (PREVIOUS_APP_ADJ): Previous app (user navigated away)";
}
if (adj >= 800 && adj < 900)
{
return $"{adj} (SERVICE_B_ADJ): Less important service";
}
if (adj >= 900)
{
return $"{adj} (CACHED_APP_ADJ): Cached/Idle (expendable)";
}
return $"{adj} (UNKNOWN): Unmapped score";
}
}