2012-02-08 08:35:27 -05:00
|
|
|
module Memory
|
|
|
|
keys = []
|
|
|
|
vals = []
|
|
|
|
|
|
|
|
case
|
2012-02-29 01:39:22 -05:00
|
|
|
when File.exist?(procfile = "/proc/self/status") && (pat = /^Vm(\w+):\s+(\d+)/) =~ File.binread(procfile)
|
2012-02-08 08:35:27 -05:00
|
|
|
PROC_FILE = procfile
|
2012-02-28 23:07:10 -05:00
|
|
|
VM_PAT = pat
|
2012-02-08 08:35:27 -05:00
|
|
|
def self.read_status
|
2012-02-14 15:13:27 -05:00
|
|
|
IO.foreach(PROC_FILE, encoding: Encoding::ASCII_8BIT) do |l|
|
2012-02-08 08:35:27 -05:00
|
|
|
yield($1.downcase.intern, $2.to_i * 1024) if VM_PAT =~ l
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
read_status {|k, v| keys << k; vals << v}
|
|
|
|
|
|
|
|
when /mswin|mingw/ =~ RUBY_PLATFORM
|
2012-11-30 06:15:36 -05:00
|
|
|
begin
|
|
|
|
require 'fiddle/import'
|
|
|
|
rescue LoadError
|
|
|
|
require 'dl/import'
|
|
|
|
end
|
2012-12-04 04:55:07 -05:00
|
|
|
begin
|
|
|
|
require 'fiddle/types'
|
|
|
|
rescue LoadError
|
|
|
|
require 'dl/types'
|
|
|
|
end
|
2012-02-08 08:35:27 -05:00
|
|
|
|
|
|
|
module Win32
|
2012-11-30 06:22:49 -05:00
|
|
|
begin
|
|
|
|
extend Fiddle::Importer
|
|
|
|
rescue NameError
|
|
|
|
extend DL::Importer
|
|
|
|
end
|
2012-02-08 08:35:27 -05:00
|
|
|
dlload "kernel32.dll", "psapi.dll"
|
2012-12-04 04:55:07 -05:00
|
|
|
begin
|
|
|
|
include Fiddle::Win32Types
|
|
|
|
rescue NameError
|
|
|
|
include DL::Win32Types
|
|
|
|
end
|
2012-02-25 00:47:16 -05:00
|
|
|
typealias "SIZE_T", "size_t"
|
2012-02-08 08:35:27 -05:00
|
|
|
|
|
|
|
PROCESS_MEMORY_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",
|
|
|
|
]
|
|
|
|
|
|
|
|
typealias "PPROCESS_MEMORY_COUNTERS", "PROCESS_MEMORY_COUNTERS*"
|
|
|
|
|
2012-12-04 11:08:05 -05:00
|
|
|
extern "HANDLE GetCurrentProcess()", :stdcall
|
|
|
|
extern "BOOL GetProcessMemoryInfo(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD)", :stdcall
|
2012-02-08 08:35:27 -05:00
|
|
|
|
|
|
|
module_function
|
|
|
|
def memory_info
|
|
|
|
size = PROCESS_MEMORY_COUNTERS.size
|
|
|
|
data = PROCESS_MEMORY_COUNTERS.malloc
|
|
|
|
data.cb = size
|
|
|
|
data if GetProcessMemoryInfo(GetCurrentProcess(), data, size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
keys << :peak << :size
|
|
|
|
def self.read_status
|
|
|
|
if info = Win32.memory_info
|
|
|
|
yield :peak, info.PeakPagefileUsage
|
|
|
|
yield :size, info.PagefileUsage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2012-02-08 13:29:52 -05:00
|
|
|
PSCMD = ["ps", "-ovsz=","-orss=", "-p"]
|
2012-02-08 08:35:27 -05:00
|
|
|
PAT = /^\s*(\d+)\s+(\d+)$/
|
|
|
|
|
|
|
|
keys << :size << :rss
|
|
|
|
def self.read_status
|
|
|
|
if PAT =~ IO.popen(PSCMD + [$$.to_s], "r", err: [:child, :out], &:read)
|
|
|
|
yield :size, $1.to_i*1024
|
|
|
|
yield :rss, $2.to_i*1024
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Status = Struct.new(*keys)
|
|
|
|
|
|
|
|
class Status
|
|
|
|
def _update
|
|
|
|
Memory.read_status do |key, val|
|
|
|
|
self[key] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Status
|
|
|
|
Header = members.map {|k| k.to_s.upcase.rjust(6)}.join('')
|
|
|
|
Format = "%6d"
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
_update
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|