add resource usage stats when built in debug mode
All checks were successful
AWS-Zig Build / build-zig-0.11.0-amd64-host (push) Successful in 1m6s

This commit is contained in:
Emil Lerch 2024-02-25 17:04:08 -08:00
parent 31b363bc0d
commit 5f1b1a52be
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
2 changed files with 38 additions and 0 deletions

View File

@ -72,11 +72,30 @@ fn handleRequest(allocator: std.mem.Allocator, response: *interface.ZigResponse)
ul_response.request.target = response.request.target;
ul_response.request.headers = response.request.headers;
ul_response.request.method = std.meta.stringToEnum(std.http.Method, response.request.method) orelse std.http.Method.GET;
const builtin = @import("builtin");
const supports_getrusage = builtin.os.tag != .windows and @hasDecl(std.os.system, "rusage"); // Is Windows it?
var rss: if (supports_getrusage) std.os.rusage else void = undefined;
if (supports_getrusage and builtin.mode == .Debug)
rss = std.os.getrusage(std.os.rusage.SELF);
const response_content = try handler.?(
allocator,
response.request.content,
&ul_response,
);
if (supports_getrusage and builtin.mode == .Debug) { // and debug mode) {
const rusage = std.os.getrusage(std.os.rusage.SELF);
log.debug(
"Request complete, max RSS of process: {d}M. Incremental: {d}K, User: {d}μs, System: {d}μs",
.{
@divTrunc(rusage.maxrss, 1024),
rusage.maxrss - rss.maxrss,
(rusage.utime.tv_sec - rss.utime.tv_sec) * std.time.us_per_s +
rusage.utime.tv_usec - rss.utime.tv_usec,
(rusage.stime.tv_sec - rss.stime.tv_sec) * std.time.us_per_s +
rusage.stime.tv_usec - rss.stime.tv_usec,
},
);
}
// Copy any headers
for (ul_response.headers.list.items) |entry| {
try response.headers.append(entry.name, entry.value);

View File

@ -94,12 +94,31 @@ fn runStandaloneServer(allocator: ?std.mem.Allocator, event_handler: interface.H
aa = arena.allocator();
}
}
const builtin = @import("builtin");
const supports_getrusage = builtin.os.tag != .windows and @hasDecl(std.os.system, "rusage"); // Is Windows it?
var rss: if (supports_getrusage) std.os.rusage else void = undefined;
if (supports_getrusage and builtin.mode == .Debug)
rss = std.os.getrusage(std.os.rusage.SELF);
processRequest(aa, &server, event_handler) catch |e| {
log.err("Unexpected error processing request: {any}", .{e});
if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*);
}
};
if (supports_getrusage and builtin.mode == .Debug) { // and debug mode) {
const rusage = std.os.getrusage(std.os.rusage.SELF);
log.debug(
"Request complete, max RSS of process: {d}M. Incremental: {d}K, User: {d}μs, System: {d}μs",
.{
@divTrunc(rusage.maxrss, 1024),
rusage.maxrss - rss.maxrss,
(rusage.utime.tv_sec - rss.utime.tv_sec) * std.time.us_per_s +
rusage.utime.tv_usec - rss.utime.tv_usec,
(rusage.stime.tv_sec - rss.stime.tv_sec) * std.time.us_per_s +
rusage.stime.tv_usec - rss.stime.tv_usec,
},
);
}
}
return 0;
}