@@ -40,6 +40,45 @@ pub fn destroy() void {
4040 heap = null ;
4141}
4242
43+ pub fn getRSS () i64 {
44+ if (@import ("builtin" ).mode != .Debug ) {
45+ // just don't trust my implementation, plus a caller might not know
46+ // that this requires parsing some unstructured data
47+ @compileError ("Only available in debug builds" );
48+ }
49+ var buf : [4096 ]u8 = undefined ;
50+ var fba = std .heap .FixedBufferAllocator .init (& buf );
51+ var writer = std .Io .Writer .Allocating .init (fba .allocator ());
52+
53+ c .mi_stats_print_out (struct {
54+ fn print (msg : [* c ]const u8 , data : ? * anyopaque ) callconv (.c ) void {
55+ const w : * std.Io.Writer = @ptrCast (@alignCast (data .? ));
56+ w .writeAll (std .mem .span (msg )) catch | err | {
57+ std .debug .print ("Failed to write mimalloc data: {}" , .{err });
58+ };
59+ }
60+ }.print , & writer .writer );
61+
62+ const data = writer .written ();
63+ const index = std .mem .indexOf (u8 , data , "rss: " ) orelse return -1 ;
64+ const sep = std .mem .indexOfScalarPos (u8 , data , index + 5 , ' ' ) orelse return -2 ;
65+ const value = std .fmt .parseFloat (f64 , data [index + 5.. sep ]) catch return -3 ;
66+ const unit = data [sep + 1.. ];
67+ if (std .mem .startsWith (u8 , unit , "KiB," )) {
68+ return @as (i64 , @intFromFloat (value )) * 1024 ;
69+ }
70+
71+ if (std .mem .startsWith (u8 , unit , "MiB," )) {
72+ return @as (i64 , @intFromFloat (value )) * 1024 * 1024 ;
73+ }
74+
75+ if (std .mem .startsWith (u8 , unit , "GiB," )) {
76+ return @as (i64 , @intFromFloat (value )) * 1024 * 1024 * 1024 ;
77+ }
78+
79+ return -4 ;
80+ }
81+
4382pub export fn m_alloc (size : usize ) callconv (.c ) ? * anyopaque {
4483 std .debug .assert (heap != null );
4584 return c .mi_heap_malloc (heap .? , size );
0 commit comments