Skip to content

Commit 448b3aa

Browse files
hsbtclaude
andcommitted
Report RSS on Windows
Windows has neither /proc nor ps nor getrusage, so RSS and MAXRSS were always 0. The working set and its peak from GetProcessMemoryInfo are the closest equivalents, read in a file that only Windows requires. Fiddle is loaded with the harness, because loading it after Bundler.setup resets Bundler between iterations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3cb699d commit 448b3aa

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎lib/harness.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ def get_rss
7373
# First, grab a line like "62796 kB". Checking the Linux kernel source, Rss will always be in kB.
7474
rss_desc = File.read(mem_rollup_file).lines.detect { |line| line.start_with?("Rss") }.split(":", 2)[1][/(\d+)/, 1]
7575
1024 * Integer(rss_desc)
76+
elsif defined?(WindowsMemoryCounters)
77+
WindowsMemoryCounters.read&.fetch(:working_set) || 0
7678
else
7779
# Collect our own peak mem usage as soon as reasonable after finishing the last iteration.
7880
# This method is only accurate to kilobytes, but is nicely portable and doesn't require
@@ -163,7 +165,12 @@ def macos_resident_size
163165
buffer[RI_RESIDENT_SIZE_OFFSET, 8].unpack1('Q')
164166
end
165167

168+
# Loaded after load_fiddle is defined, since it calls it right away.
169+
require_relative "harness/windows_memory_counters" if RUBY_PLATFORM.match?(/mswin|mingw/)
170+
166171
def get_maxrss
172+
return WindowsMemoryCounters.read&.fetch(:peak_working_set) if defined?(WindowsMemoryCounters)
173+
167174
load_fiddle
168175

169176
require 'rbconfig/sizeof'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# Resolved when the harness loads, before use_gemfile. Loading fiddle after Bundler.setup goes through Bundler.reset!,
4+
# which redefines Kernel#gem and invalidates YJIT code if it happens between iterations. A dropped Fiddle::Handle is
5+
# freed by a deferred finalizer, whose interrupt also sends YJIT code back to the interpreter, so kernel32 stays open.
6+
module WindowsMemoryCounters
7+
begin
8+
load_fiddle
9+
@kernel32 = Fiddle.dlopen("kernel32")
10+
@process = Fiddle::Function.new(@kernel32['GetCurrentProcess'], [], Fiddle::TYPE_VOIDP).call
11+
@get_info = Fiddle::Function.new(@kernel32['K32GetProcessMemoryInfo'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT)
12+
rescue LoadError, Fiddle::DLError, NameError
13+
warn "Failed to get memory counters: #{$!.message}"
14+
end
15+
16+
# The working set of this process and its peak in bytes, the Windows counterparts of RSS and max RSS.
17+
def self.read
18+
return nil unless @get_info
19+
20+
# PROCESS_MEMORY_COUNTERS: two DWORDs followed by eight SIZE_Ts, the first two being PeakWorkingSetSize and WorkingSetSize.
21+
buffer = "\0".b * (8 + 8 * Fiddle::SIZEOF_SIZE_T)
22+
buffer[0, 4] = [buffer.bytesize].pack('L')
23+
return nil if @get_info.call(@process, buffer, buffer.bytesize).zero?
24+
25+
peak_working_set, working_set = buffer.unpack('x8J2')
26+
{ working_set: working_set, peak_working_set: peak_working_set }
27+
end
28+
end

0 commit comments

Comments
 (0)