Port the Developer app's resource monitor to iOS

This commit is contained in:
James Babcock committed 2026-10-31 14:45:43 -07:00
1 parent c5f80a0b00
commit f47d82b871
13 files changed
+344 -74

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
// Exercise the iOS resource sampler's mach/malloc/rusage queries natively (on macOS).
const { execFileSync } = require('node:child_process');
const { mkdtempSync, rmSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { join, resolve } = require('node:path');
const root = resolve(__dirname, '..'), dir = mkdtempSync(join(tmpdir(), 'faceclaw-resource-usage-'));
try {
execFileSync('xcrun', ['clang', '-fobjc-arc', '-framework', 'Foundation', '-I', 'App_Resources/iOS/src',
'tests/native/ios-resource-usage-test.m', 'App_Resources/iOS/src/FaceclawResourceUsage.m',
'-o', join(dir, 'resource-usage-test')], { cwd: root, stdio: 'inherit' });
execFileSync(join(dir, 'resource-usage-test'), [], { stdio: 'inherit' });
} finally { rmSync(dir, { recursive: true, force: true }); }
+59
View File
@@ -0,0 +1,59 @@
#import <Foundation/Foundation.h>
#import <stdlib.h>
#import <string.h>
#import "FaceclawResourceUsage.h"
static NSDictionary *sample(void) {
NSData *json = [[FaceclawResourceUsage sample] dataUsingEncoding:NSUTF8StringEncoding];
return [NSJSONSerialization JSONObjectWithData:json options:0 error:nil];
}
#define CHECK(cond, ...) do { if (!(cond)) { NSLog(@"FAIL " __VA_ARGS__); return 1; } } while (0)
int main(void) {
@autoreleasepool {
NSDictionary *before = sample();
CHECK([before isKindOfClass:NSDictionary.class], @"sample is not a JSON object");
for (NSString *key in @[@"atMs", @"cpuMs", @"footprintKb", @"footprintPeakKb", @"residentKb", @"mallocKb",
@"threads", @"thermalState", @"lowPower"]) {
CHECK([before[key] isKindOfClass:NSNumber.class], @"missing %@ in %@", key, before);
}
CHECK(before[@"availableKb"] == nil, @"os_proc_available_memory is iOS-only: %@", before);
CHECK([before[@"footprintKb"] longLongValue] > 0 && [before[@"residentKb"] longLongValue] > 0, @"memory %@", before);
CHECK([before[@"footprintPeakKb"] longLongValue] >= [before[@"footprintKb"] longLongValue], @"peak %@", before);
CHECK([before[@"threads"] intValue] >= 1, @"threads %@", before);
// Two busy threads alive at sampling time, ~150 ms of work between them;
// 64 MiB of touched malloc memory.
NSCondition *gate = [NSCondition new];
__block BOOL release = NO;
__block volatile double sink = 0;
for (int t = 0; t < 2; t++) {
[NSThread detachNewThreadWithBlock:^{
NSDate *until = [NSDate dateWithTimeIntervalSinceNow:0.075];
while (until.timeIntervalSinceNow > 0) for (int i = 0; i < 10000; i++) sink += i * 0.5;
[gate lock]; while (!release) [gate wait]; [gate unlock];
}];
}
size_t bytes = 64u << 20;
char *block = malloc(bytes);
memset(block, 1, bytes);
[NSThread sleepForTimeInterval:0.2];
NSDictionary *after = sample();
long long elapsed = [after[@"atMs"] longLongValue] - [before[@"atMs"] longLongValue];
double cpu = [after[@"cpuMs"] doubleValue] - [before[@"cpuMs"] doubleValue];
CHECK(elapsed >= 190 && elapsed < 5000, @"monotonic clock advanced %lld ms over a 200 ms sleep", elapsed);
CHECK(cpu >= 100, @"CPU advanced only %.1f ms over ~150 ms of busy work", cpu);
CHECK([after[@"threads"] intValue] >= [before[@"threads"] intValue] + 2, @"threads %@ -> %@", before[@"threads"], after[@"threads"]);
CHECK([after[@"mallocKb"] longLongValue] - [before[@"mallocKb"] longLongValue] >= 60 * 1024,
@"malloc %@ -> %@ after 64 MiB", before[@"mallocKb"], after[@"mallocKb"]);
CHECK([after[@"footprintKb"] longLongValue] - [before[@"footprintKb"] longLongValue] >= 60 * 1024,
@"footprint %@ -> %@ after touching 64 MiB", before[@"footprintKb"], after[@"footprintKb"]);
[gate lock]; release = YES; [gate broadcast]; [gate unlock];
free(block);
printf("PASS: resource sample reports footprint %lld KiB, malloc %lld KiB, %d threads, %.0f ms CPU over %lld ms\n",
[after[@"footprintKb"] longLongValue], [after[@"mallocKb"] longLongValue], [after[@"threads"] intValue], cpu, elapsed);
}
return 0;
}
+48
View File
@@ -0,0 +1,48 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
// The resource monitor's platform samplers, over stand-ins for the iOS
// FaceclawResourceUsage JSON and the Android FaceclawResourceUsage LongArray.
function ios(raw) {
const load = loader({ FaceclawResourceUsage: { sample: () => JSON.stringify(raw) } });
return load('app/native/resource-usage.ios.ts');
}
function android(raw) {
const load = loader({ com: { faceclaw: { app: { FaceclawResourceUsage: { sample: () => raw } } } } });
return load('app/native/resource-usage.ts');
}
test('iOS charts footprint, resident and malloc, and reports the jetsam limit, peak and thermal state', () => {
const usage = ios({
atMs: 5000, cpuMs: 1234.5, footprintKb: 200 * 1024, footprintPeakKb: 300 * 1024, residentKb: 150 * 1024,
mallocKb: 64 * 1024, availableKb: 2848 * 1024, threads: 23, thermalState: 2, lowPower: true,
});
assert.deepEqual(Array.from(usage.MEMORY_SERIES), ['footprint', 'resident', 'malloc']);
const sample = usage.sampleResourceUsage();
assert.equal(sample.atMs, 5000);
assert.equal(sample.cpuMs, 1234.5);
assert.equal(sample.threads, 23);
assert.deepEqual(Array.from(sample.memoryMb), [200, 150, 64]);
assert.equal(sample.status, 'limit 3.0G · peak 300M · thermal serious · Low Power Mode');
});
test('iOS omits the limit without jetsam headroom, and fields the native query could not fill', () => {
const sample = ios({ atMs: 1, footprintKb: 1024, thermalState: 0, lowPower: false }).sampleResourceUsage();
assert.equal(sample.cpuMs, 0);
assert.equal(sample.threads, 0);
assert.deepEqual(Array.from(sample.memoryMb), [1, 0, 0]);
assert.equal(sample.status, 'thermal nominal');
});
test('Android charts RSS, native and Java heap, and reports peak RSS and committed Java heap', () => {
const usage = android([9000, 4321, 400 * 1024, 48 * 1024, 12 * 1024, 24 * 1024, 87, 512 * 1024]);
assert.deepEqual(Array.from(usage.MEMORY_SERIES), ['RSS', 'native', 'Java']);
const sample = usage.sampleResourceUsage();
assert.equal(sample.atMs, 9000);
assert.equal(sample.cpuMs, 4321);
assert.equal(sample.threads, 87);
assert.deepEqual(Array.from(sample.memoryMb), [400, 48, 12]);
assert.equal(sample.status, 'peak RSS 512M · Java heap 24M committed');
});