ReviewOS

also looking at this

craft-native/craft

feat(android): getMemoryUsage and log — ratchet 102 -> 100

#143
Merged chris wants to merge feat/android-tier0-memory-log into main
7 files +377 -3
packages/zig/src/android_dispatch.zigmodified+59-1
Changes to packages/zig/src/android_dispatch.zig
@@ -37,6 +37,7 @@ const std = @import("std");
3737const builtin = @import("builtin");
3838const jni = @import("jni_runtime.zig");
3939const device = @import("bridge_android_device.zig");
40const system = @import("bridge_android_system.zig");
4041
4142const Jni = jni.Jni;
4243
@@ -122,6 +123,20 @@ const natives = [_]jni.JNINativeMethod{
122123 .signature = "(Landroid/app/Activity;)Ljava/lang/String;",
123124 .fnPtr = @ptrCast(&nativeGetDeviceInfo),
124125 },
126 // No Activity parameter: `Runtime` and `Log` are static, so neither of
127 // these needs anything the app hands over. The signature says so, and a
128 // signature that asked for one would fail registration rather than be
129 // quietly ignored.
130 .{
131 .name = "nativeGetMemoryUsage",
132 .signature = "()Ljava/lang/String;",
133 .fnPtr = @ptrCast(&nativeGetMemoryUsage),
134 },
135 .{
136 .name = "nativeLog",
137 .signature = "(Ljava/lang/String;)Z",
138 .fnPtr = @ptrCast(&nativeLog),
139 },
125140};
126141
127142/// How binding went. A value rather than a log line, so every path is
@@ -184,6 +199,49 @@ fn nativeGetDeviceInfo(env: jni.JNIEnv, _: jni.jobject, activity: jni.jobject) c
184199 return j.newStringUtf(owned.ptr) catch null;
185200}
186201
202/// `nativeGetMemoryUsage()` — the JVM heap, as JSON.
203fn nativeGetMemoryUsage(env: jni.JNIEnv, _: jni.jobject) callconv(.c) jni.jstring {
204 const j = Jni.init(env);
205
206 var arena = std.heap.ArenaAllocator.init(backing);
207 defer arena.deinit();
208 const allocator = arena.allocator();
209
210 const usage = system.readMemory(j) catch |err| {
211 std.log.warn("craft: getMemoryUsage fell through to the shim ({s})", .{@errorName(err)});
212 return null;
213 };
214
215 const json = system.renderMemory(allocator, usage) catch return null;
216 const owned = allocator.allocSentinel(u8, json.len, 0) catch return null;
217 @memcpy(owned, json);
218
219 return j.newStringUtf(owned.ptr) catch null;
220}
221
222/// `nativeLog(message)` — returns whether Zig wrote the line.
223///
224/// A boolean rather than void, because "not mine" has to be expressible.
225/// `void` would leave the Kotlin unable to tell a successful native log from a
226/// runtime that declined, and it would log the message twice or not at all.
227fn nativeLog(env: jni.JNIEnv, _: jni.jobject, message: jni.jstring) callconv(.c) jni.jboolean {
228 const j = Jni.init(env);
229
230 var arena = std.heap.ArenaAllocator.init(backing);
231 defer arena.deinit();
232 const allocator = arena.allocator();
233
234 const text = j.stringToUtf8(allocator, message) catch return jni.JNI_FALSE;
235 const owned = allocator.allocSentinel(u8, text.len, 0) catch return jni.JNI_FALSE;
236 @memcpy(owned, text);
237
238 system.writeLog(j, owned.ptr) catch |err| {
239 std.log.warn("craft: log fell through to the shim ({s})", .{@errorName(err)});
240 return jni.JNI_FALSE;
241 };
242 return jni.JNI_TRUE;
243}
244
187245// =============================================================================
188246// Tests
189247// =============================================================================
@@ -295,7 +353,7 @@ test "the registered natives name methods the Kotlin actually declares" {
295353 // A descriptor is checked by the JVM at registration, so a wrong one fails
296354 // at load rather than at call — but only if the *name* matches something.
297355 // These two strings are the contract with CraftBridge.kt.
298 try testing.expectEqual(@as(usize, 1), natives.len);
356 try testing.expectEqual(@as(usize, 3), natives.len);
299357 try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name));
300358
301359 // And the class they bind to is the fixed one, not the templated bridge.