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

memory_status.c: system call

* ext/-test-/memory_status/memory_status.c: get memory sizes by
  mach task_info system call.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-10-08 01:34:27 +00:00
parent 75838104e7
commit f0e1d72316
4 changed files with 65 additions and 2 deletions

View file

@ -1,3 +1,8 @@
Sat Oct 8 10:34:25 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/-test-/memory_status/memory_status.c: get memory sizes by
mach task_info system call.
Sat Oct 8 09:06:55 2016 Aurelien Jacobs <aurel@gnuage.org>
* lib/logger.rb (Logger::Period#next_rotate_time): fix monthly log

View file

@ -0,0 +1,8 @@
case RUBY_PLATFORM
when /darwin/
ok = true
end
if ok
create_makefile("-test-/memory_status")
end

View file

@ -0,0 +1,45 @@
#include "ruby.h"
#ifdef __APPLE__
# include <mach/mach.h>
# include <mach/message.h>
# include <mach/kern_return.h>
# include <mach/task_info.h>
#endif
static VALUE cMemoryStatus;
static VALUE
read_status(VALUE self)
{
VALUE size = INT2FIX(0);
#if defined __APPLE__
VALUE rss;
kern_return_t error;
mach_msg_type_number_t out_count;
mach_task_basic_info_data_t taskinfo;
taskinfo.virtual_size = 0;
out_count = MACH_TASK_BASIC_INFO_COUNT;
error = task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
(task_info_t)&taskinfo, &out_count);
if (error != KERN_SUCCESS) return Qnil;
size = ULL2NUM(taskinfo.virtual_size);
rss = ULL2NUM(taskinfo.resident_size);
rb_struct_aset(self, INT2FIX(1), rss);
#endif
rb_struct_aset(self, INT2FIX(0), size);
return self;
}
void
Init_memory_status(void)
{
VALUE mMemory = rb_define_module("Memory");
cMemoryStatus =
rb_struct_define_under(mMemory, "Status", "size",
#if defined __APPLE__
"rss",
#endif
(char *)NULL);
rb_define_method(cMemoryStatus, "_update", read_status, 0);
}

View file

@ -1,4 +1,9 @@
# frozen_string_literal: false
begin
require '-test-/memory_status.so'
rescue LoadError
end
module Memory
keys = []
@ -81,7 +86,7 @@ module Memory
if !keys.empty?
Status = Struct.new(*keys)
end
end
end unless defined?(Memory::Status)
if defined?(Memory::Status)
class Memory::Status
@ -89,7 +94,7 @@ if defined?(Memory::Status)
Memory.read_status do |key, val|
self[key] = val
end
end
end unless method_defined?(:_update)
Header = members.map {|k| k.to_s.upcase.rjust(6)}.join('')
Format = "%6d"