1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Show WorkingSetSize as RSS on Windows

This commit is contained in:
Nobuyoshi Nakada 2021-08-05 14:16:09 +09:00
parent 228b3e43be
commit ae275f67ce
Notes: git 2021-08-05 17:14:59 +09:00
2 changed files with 18 additions and 6 deletions
ext/-test-/memory_status
tool/lib

View file

@ -10,11 +10,15 @@
static VALUE cMemoryStatus; static VALUE cMemoryStatus;
#undef HAVE_RSS
#undef HAVE_PEAK
static VALUE static VALUE
read_status(VALUE self) read_status(VALUE self)
{ {
VALUE size = INT2FIX(0); VALUE size = INT2FIX(0);
#if defined __APPLE__ #if defined __APPLE__
# define HAVE_RSS 1
VALUE rss; VALUE rss;
kern_return_t error; kern_return_t error;
# if defined MACH_TASK_BASIC_INFO # if defined MACH_TASK_BASIC_INFO
@ -40,14 +44,20 @@ read_status(VALUE self)
rss = ULL2NUM(taskinfo.resident_size); rss = ULL2NUM(taskinfo.resident_size);
rb_struct_aset(self, INT2FIX(1), rss); rb_struct_aset(self, INT2FIX(1), rss);
#elif defined _WIN32 #elif defined _WIN32
VALUE peak; # define HAVE_RSS 1
# define HAVE_PEAK 1
VALUE rss, peak;
PROCESS_MEMORY_COUNTERS c; PROCESS_MEMORY_COUNTERS c;
c.cb = sizeof(c); c.cb = sizeof(c);
if (!GetProcessMemoryInfo(GetCurrentProcess(), &c, c.cb)) if (!GetProcessMemoryInfo(GetCurrentProcess(), &c, c.cb))
return Qnil; return Qnil;
size = SIZET2NUM(c.PagefileUsage); size = SIZET2NUM(c.PagefileUsage);
rss = SIZET2NUM(c.WorkingSetSize);
peak = SIZET2NUM(c.PeakWorkingSetSize); peak = SIZET2NUM(c.PeakWorkingSetSize);
rb_struct_aset(self, INT2FIX(1), peak); rb_struct_aset(self, INT2FIX(2), peak);
#endif
#ifdef HAVE_RSS
rb_struct_aset(self, INT2FIX(1), rss);
#endif #endif
rb_struct_aset(self, INT2FIX(0), size); rb_struct_aset(self, INT2FIX(0), size);
return self; return self;
@ -59,9 +69,10 @@ Init_memory_status(void)
VALUE mMemory = rb_define_module("Memory"); VALUE mMemory = rb_define_module("Memory");
cMemoryStatus = cMemoryStatus =
rb_struct_define_under(mMemory, "Status", "size", rb_struct_define_under(mMemory, "Status", "size",
#if defined __APPLE__ #ifdef HAVE_RSS
"rss", "rss",
#elif defined _WIN32 #endif
#ifdef HAVE_PEAK
"peak", "peak",
#endif #endif
(char *)NULL); (char *)NULL);

View file

@ -56,11 +56,12 @@ module Memory
end end
end end
keys << :peak << :size keys.push(:size, :rss, :peak)
def self.read_status def self.read_status
if info = Win32.memory_info if info = Win32.memory_info
yield :peak, info.PeakPagefileUsage
yield :size, info.PagefileUsage yield :size, info.PagefileUsage
yield :rss, info.WorkingSetSize
yield :peak, info.PeakWorkingSetSize
end end
end end
when (require_relative 'find_executable' when (require_relative 'find_executable'