Module: HeapScope::Runtime::WindowsRSS

Defined in:
lib/heapscope/runtime/windows_rss.rb

Overview

Windows working-set size via PowerShell (no native gem required). Optionally uses stdlib Fiddle when already loaded / available without warnings noise.

Class Method Summary collapse

Class Method Details

.apiObject



32
33
34
# File 'lib/heapscope/runtime/windows_rss.rb', line 32

def api
  @api ||= build_api
end

.build_apiObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/heapscope/runtime/windows_rss.rb', line 36

def build_api
  Module.new do
    extend Fiddle::Importer

    dlload "kernel32.dll", "psapi.dll"
    typealias "DWORD", "unsigned long"
    typealias "HANDLE", "void*"
    typealias "SIZE_T", "size_t"
    typealias "BOOL", "int"

    const_set(
      :Counters,
      struct([
               "DWORD cb",
               "DWORD PageFaultCount",
               "SIZE_T PeakWorkingSetSize",
               "SIZE_T WorkingSetSize",
               "SIZE_T QuotaPeakPagedPoolUsage",
               "SIZE_T QuotaPagedPoolUsage",
               "SIZE_T QuotaPeakNonPagedPoolUsage",
               "SIZE_T QuotaNonPagedPoolUsage",
               "SIZE_T PagefileUsage",
               "SIZE_T PeakPagefileUsage"
             ])
    )

    extern "HANDLE GetCurrentProcess()"
    extern "BOOL GetProcessMemoryInfo(HANDLE, void*, DWORD)"

    define_singleton_method(:working_set_bytes) do
      counters = const_get(:Counters).malloc
      counters.cb = const_get(:Counters).size
      ok = GetProcessMemoryInfo(GetCurrentProcess(), counters, counters.cb)
      ok.zero? ? nil : counters.WorkingSetSize
    end
  end
end

.fiddle_working_set_bytesObject



23
24
25
26
27
28
29
30
# File 'lib/heapscope/runtime/windows_rss.rb', line 23

def fiddle_working_set_bytes
  return nil unless Object.const_defined?(:Fiddle)

  require "fiddle/import"
  api.working_set_bytes
rescue StandardError
  nil
end

.powershell_working_set_bytesObject



14
15
16
17
18
19
20
21
# File 'lib/heapscope/runtime/windows_rss.rb', line 14

def powershell_working_set_bytes
  out = `powershell -NoProfile -Command "(Get-Process -Id #{Process.pid}).WorkingSet64"`.to_s.strip
  return nil if out.empty?

  Integer(out)
rescue StandardError
  nil
end

.working_set_bytesObject



10
11
12
# File 'lib/heapscope/runtime/windows_rss.rb', line 10

def working_set_bytes
  powershell_working_set_bytes || fiddle_working_set_bytes
end