Skip to content

Implement getMaxRss for Windows - #15035

Merged
kubkon merged 4 commits into
masterfrom
unknown repository
Mar 24, 2023
Merged

Implement getMaxRss for Windows#15035
kubkon merged 4 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghostghost commented Mar 21, 2023

Copy link
Copy Markdown

closes#14956

As stated in the commit message, this is done using K32GetProcessMemoryInfo to get PeakWorkingSetSize.

Since GetProcessMemoryInfo is used in Psapi.dll (Or Kernel32.dll), I will try to issue a pull request later for a ntdll refactor to this function.

Edit: Switched to kernel32 for compatibility + psapi calls kernel32 anyway.

@kubkon

Copy link
Copy Markdown
Member

Thanks for the PR! As I explained in #15039 do you think you could try implementing the kernel32 function yourself using only calls to ntdll?

@ghost

Copy link
Copy Markdown
Author

Thanks for the PR! As I explained in #15039 do you think you could try implementing the kernel32 function yourself using only calls to ntdll?

Certainly. I wasn't initially sure if I should include it in this PR, but I was going to implement GetProcessMemoryInfo in ntdll later this week. But it is reasonable to include both as there is not really much refactor to be done.

Will update this PR as soon as I implement it.

xEgoist added 2 commits March 22, 2023 06:13
In Windows, the equivalent to maxrss is PeakWorkingSetSize which is
found in PROCESS_MEMORY_COUNTERS in bytes.
Currently, this is done by calling `GetProcessMemoryInfo` in kernel32.
`GetProcessMemoryInfo` is implemented using `NtQueryInformationProcess`
with `ProcessVmCounters` to obtain `VM_COUNTERS`. The structs, enum
definitions are found in `winternl.h` or `ntddk.h` in the latest WDK.
This should give the same results as using `K32GetProcessMemoryInfo`
@ghost

Copy link
Copy Markdown
Author

I tested the GetProcessMemoryInfo implementation using this code:

conststd=@import("std");
constw=std.os.windows;
pubfnmain() !void {
conststdout_file=std.io.getStdOut().writer();
varbw=std.io.bufferedWriter(stdout_file);
conststdout=bw.writer();
constpHandle=w.kernel32.GetCurrentProcess();
varpmc: w.PROCESS_MEMORY_COUNTERS=undefined;
varpmc2: w.PROCESS_MEMORY_COUNTERS=undefined;
if (w.kernel32.K32GetProcessMemoryInfo(pHandle, &pmc, @sizeOf(w.PROCESS_MEMORY_COUNTERS)) !=0) {
trystdout.print("K32: MaxRss is {} Bytes!\n", .{pmc.PeakWorkingSetSize});
}
else {
trystdout.print("Something Went Wrong!\n", .{});
}
tryw.GetProcessMemoryInfo(pHandle, &pmc2);
trystdout.print("Nt: MaxRss is {} Bytes!\n", .{pmc2.PeakWorkingSetSize}); if (w.kernel32.K32GetProcessMemoryInfo(pHandle, &pmc, @sizeOf(w.PROCESS_MEMORY_COUNTERS)) !=0) {
trystdout.print("K32 Again: MaxRss is {} Bytes!\n", .{pmc.PeakWorkingSetSize});
} else {
trystdout.print("Something Went Wrong!\n", .{});
}
trybw.flush();
}

Both functions reported the same MaxRss.

Comment threadlib/std/os/windows.zig Outdated
Comment threadlib/std/child_process.zig Outdated
This change allows the function to return the process memory info
directly instead of copying the result of the underlying Nt function.
@kubkon

Copy link
Copy Markdown
Member

Could you take a final look at this @squeek502 and if you are happy with this I'll merge it?

Comment threadlib/std/os/windows/ntdll.zig Outdated
@squeek502

Copy link
Copy Markdown
Member

Looks good to me 👍

@kubkon
kubkon merged commit 3aa0a7e into ziglang:masterMar 24, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add Windows support for detecting maxrss of child process

2 participants

@kubkon@squeek502