2017-06-20 07:10:37 -04:00
|
|
|
# frozen_string_literal: true
|
2016-10-07 21:34:27 -04:00
|
|
|
begin
|
|
|
|
require '-test-/memory_status.so'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2012-02-08 08:35:27 -05:00
|
|
|
module Memory
|
|
|
|
keys = []
|
|
|
|
|
|
|
|
case
|
2016-03-08 22:48:33 -05:00
|
|
|
when File.exist?(procfile = "/proc/self/status") && (pat = /^Vm(\w+):\s+(\d+)/) =~ (data = 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
|
|
|
|
|
2016-03-08 22:48:33 -05:00
|
|
|
data.scan(pat) {|k, v| keys << k.downcase.intern}
|
2012-02-08 08:35:27 -05:00
|
|
|
|
|
|
|
when /mswin|mingw/ =~ RUBY_PLATFORM
|
2014-10-31 17:13:09 -04:00
|
|
|
require 'fiddle/import'
|
|
|
|
require 'fiddle/types'
|
2012-02-08 08:35:27 -05:00
|
|
|
|
|
|
|
module Win32
|
2014-10-31 17:13:09 -04:00
|
|
|
extend Fiddle::Importer
|
2012-02-08 08:35:27 -05:00
|
|
|
dlload "kernel32.dll", "psapi.dll"
|
2014-10-31 17:13:09 -04:00
|
|
|
include Fiddle::Win32Types
|
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
|
|
|
|
|
2021-08-05 01:16:09 -04:00
|
|
|
keys.push(:size, :rss, :peak)
|
2012-02-08 08:35:27 -05:00
|
|
|
def self.read_status
|
|
|
|
if info = Win32.memory_info
|
|
|
|
yield :size, info.PagefileUsage
|
2021-08-05 01:16:09 -04:00
|
|
|
yield :rss, info.WorkingSetSize
|
|
|
|
yield :peak, info.PeakWorkingSetSize
|
2012-02-08 08:35:27 -05:00
|
|
|
end
|
|
|
|
end
|
2016-03-08 23:22:50 -05:00
|
|
|
when (require_relative 'find_executable'
|
|
|
|
pat = /^\s*(\d+)\s+(\d+)$/
|
|
|
|
pscmd = EnvUtil.find_executable("ps", "-ovsz=", "-orss=", "-p", $$.to_s) {|out| pat =~ out})
|
|
|
|
pscmd.pop
|
|
|
|
PAT = pat
|
|
|
|
PSCMD = pscmd
|
2012-02-08 08:35:27 -05:00
|
|
|
|
|
|
|
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
|
2016-03-08 23:22:50 -05:00
|
|
|
else
|
|
|
|
def self.read_status
|
|
|
|
raise NotImplementedError, "unsupported platform"
|
|
|
|
end
|
2012-02-08 08:35:27 -05:00
|
|
|
end
|
|
|
|
|
2016-03-08 23:22:50 -05:00
|
|
|
if !keys.empty?
|
|
|
|
Status = Struct.new(*keys)
|
|
|
|
end
|
2016-10-07 21:34:27 -04:00
|
|
|
end unless defined?(Memory::Status)
|
2012-02-08 08:35:27 -05:00
|
|
|
|
2016-03-08 23:22:50 -05:00
|
|
|
if defined?(Memory::Status)
|
|
|
|
class Memory::Status
|
2012-02-08 08:35:27 -05:00
|
|
|
def _update
|
|
|
|
Memory.read_status do |key, val|
|
|
|
|
self[key] = val
|
|
|
|
end
|
2021-08-29 03:32:12 -04:00
|
|
|
self
|
2016-10-07 21:34:27 -04:00
|
|
|
end unless method_defined?(:_update)
|
2012-02-08 08:35:27 -05:00
|
|
|
|
|
|
|
Header = members.map {|k| k.to_s.upcase.rjust(6)}.join('')
|
|
|
|
Format = "%6d"
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
_update
|
|
|
|
end
|
2014-03-08 23:17:38 -05:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
status = each_pair.map {|n,v|
|
|
|
|
"#{n}:#{v}"
|
|
|
|
}
|
|
|
|
"{#{status.join(",")}}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.parse(str)
|
|
|
|
status = allocate
|
|
|
|
str.scan(/(?:\A\{|\G,)(#{members.join('|')}):(\d+)(?=,|\}\z)/) do
|
|
|
|
status[$1] = $2.to_i
|
|
|
|
end
|
|
|
|
status
|
|
|
|
end
|
2012-02-08 08:35:27 -05:00
|
|
|
end
|
2015-06-25 05:35:46 -04:00
|
|
|
|
|
|
|
# On some platforms (e.g. Solaris), libc malloc does not return
|
|
|
|
# freed memory to OS because of efficiency, and linking with extra
|
|
|
|
# malloc library is needed to detect memory leaks.
|
|
|
|
#
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /solaris2\.(?:9|[1-9][0-9])/i # Solaris 9, 10, 11,...
|
|
|
|
bits = [nil].pack('p').size == 8 ? 64 : 32
|
|
|
|
if ENV['LD_PRELOAD'].to_s.empty? &&
|
|
|
|
ENV["LD_PRELOAD_#{bits}"].to_s.empty? &&
|
|
|
|
(ENV['UMEM_OPTIONS'].to_s.empty? ||
|
|
|
|
ENV['UMEM_OPTIONS'] == 'backend=mmap') then
|
|
|
|
envs = {
|
|
|
|
'LD_PRELOAD' => 'libumem.so',
|
|
|
|
'UMEM_OPTIONS' => 'backend=mmap'
|
|
|
|
}
|
|
|
|
args = [
|
|
|
|
envs,
|
|
|
|
"--disable=gems",
|
|
|
|
"-v", "-",
|
|
|
|
]
|
|
|
|
_, err, status = EnvUtil.invoke_ruby(args, "exit(0)", true, true)
|
|
|
|
if status.exitstatus == 0 && err.to_s.empty? then
|
2016-03-08 23:22:50 -05:00
|
|
|
Memory::NO_MEMORY_LEAK_ENVS = envs
|
2015-06-25 05:35:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end #case RUBY_PLATFORM
|
|
|
|
|
2012-02-08 08:35:27 -05:00
|
|
|
end
|