diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-10-08 01:34:27 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-10-08 01:34:27 +0000 |
commit | f0e1d723164d99bf733fbb607ef78a440bd40c9f (patch) | |
tree | d31a5a15cb0566a7c66bd67cf03d67ce87bb0f14 /ext/-test- | |
parent | 75838104e7b5c66dbac07ef4881e29197ced5404 (diff) | |
download | ruby-f0e1d723164d99bf733fbb607ef78a440bd40c9f.tar.gz |
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
Diffstat (limited to 'ext/-test-')
-rw-r--r-- | ext/-test-/memory_status/extconf.rb | 8 | ||||
-rw-r--r-- | ext/-test-/memory_status/memory_status.c | 45 |
2 files changed, 53 insertions, 0 deletions
diff --git a/ext/-test-/memory_status/extconf.rb b/ext/-test-/memory_status/extconf.rb new file mode 100644 index 0000000000..8bfbfe6321 --- /dev/null +++ b/ext/-test-/memory_status/extconf.rb @@ -0,0 +1,8 @@ +case RUBY_PLATFORM +when /darwin/ + ok = true +end + +if ok + create_makefile("-test-/memory_status") +end diff --git a/ext/-test-/memory_status/memory_status.c b/ext/-test-/memory_status/memory_status.c new file mode 100644 index 0000000000..813a8349c4 --- /dev/null +++ b/ext/-test-/memory_status/memory_status.c @@ -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); +} |