vmstat 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -3,10 +3,11 @@
3
3
  This is a focused and fast library to get system information like:
4
4
 
5
5
  * _Memory_ (free, active, ...)
6
- * _Network Devices_ (name, in bytes, out bytes, ...)
6
+ * _Network Interfaces_ (name, in bytes, out bytes, ...)
7
7
  * _CPU_ (user, system, nice, idle)
8
8
  * _Load_ Average
9
9
  * _Disk_ (type, disk path, free bytes, total bytes, ...)
10
+ * _Current Task_ (used bytes and usage time *MAC OS X ONLY*)
10
11
 
11
12
  *It currently supports:*
12
13
 
@@ -34,117 +35,106 @@ Or install it yourself as:
34
35
 
35
36
  ## Usage
36
37
 
37
- Just require the library.
38
+ Just require the library and make a snapshot or use the distinct methods to just capture parts of the statistics.
38
39
 
39
40
  require "vmstat"
40
-
41
- ### Memory
42
-
43
- Vmstat.memory # => {...}
44
-
45
- The result might look like this:
46
-
47
- {:pagesize=>4096,
48
- :wired=>668069,
49
- :active=>185474,
50
- :inactive=>546881,
51
- :free=>694851,
52
- :wired_bytes=>2240024576,
53
- :active_bytes=>2736410624,
54
- :inactive_bytes=>759701504,
55
- :free_bytes=>2846109696,
56
- :zero_filled=>176898850,
57
- :reactivated=>41013,
58
- :purgeable=>344211,
59
- :purged=>11580,
60
- :pageins=>2254222,
61
- :pageouts=>0,
62
- :faults=>204524839,
63
- :copy_on_write_faults=>14379007,
64
- :lookups=>2255061,
65
- :hits=>95}
66
-
67
-
68
- ### Network Devices
69
-
70
- Vmstat.network # => {...}
71
-
72
- The result might look like this:
73
-
74
- {:lo0=>
75
- {:in_bytes=>9082607,
76
- :in_errors=>0,
77
- :in_drops=>0,
78
- :out_bytes=>9082607,
79
- :out_errors=>0,
80
- :type=>24},
81
- :gif0=>
82
- {:in_bytes=>0,
83
- :in_errors=>0,
84
- :in_drops=>0,
85
- :out_bytes=>0,
86
- :out_errors=>0,
87
- :type=>55},
88
- :stf0=>
89
- {:in_bytes=>0,
90
- :in_errors=>0,
91
- :in_drops=>0,
92
- :out_bytes=>0,
93
- :out_errors=>0,
94
- :type=>57},
95
- :en0=>
96
- {:in_bytes=>7108891975,
97
- :in_errors=>0,
98
- :in_drops=>0,
99
- :out_bytes=>479503412,
100
- :out_errors=>0,
101
- :type=>6},
102
- :p2p0=>
103
- {:in_bytes=>0,
104
- :in_errors=>0,
105
- :in_drops=>0,
106
- :out_bytes=>0,
107
- :out_errors=>0,
108
- :type=>6}}
109
-
110
- ### CPU
111
-
112
- Vmstat.cpu # => [...]
113
-
114
- The result might look like this, depending on the number of cpus:
115
-
116
- [{:user=>297003, :system=>475804, :nice=>0, :idle=>11059640},
117
- {:user=>23349, :system=>14186, :nice=>0, :idle=>11792093},
118
- {:user=>247171, :system=>195309, :nice=>0, :idle=>11387262},
119
- {:user=>21783, :system=>12823, :nice=>0, :idle=>11794993},
120
- {:user=>221323, :system=>163723, :nice=>0, :idle=>11444653},
121
- {:user=>20608, :system=>11808, :nice=>0, :idle=>11797154},
122
- {:user=>195015, :system=>145772, :nice=>0, :idle=>11488869},
123
- {:user=>19793, :system=>11077, :nice=>0, :idle=>11798671}]
124
-
125
- ### Load
126
-
127
- Vmstat.load_avg #=> [0.35791015625, 0.4296875, 0.4609375]
128
-
129
- ### Disk
130
-
131
- To get the disk data one can pass the mount point or the device name.
132
-
133
- # like so
134
- Vmstat.disk("/") # => {...}
135
-
136
- # or so
137
- Vmstat.disk("/dev/disk0s2") # => {...}
138
-
139
- The result looks like this:
140
-
141
- {:type => :devfs,
142
- :origin => "devfs",
143
- :mount => "/dev",
144
- :free_bytes => 0,
145
- :available_bytes => 0,
146
- :used_bytes => 192000,
147
- :total_bytes => 192000}
41
+ require "pp"
42
+ Vmstat.snapshot # => #<Vmstat::Snapshot:0x007fe5f22df660
43
+ # @at=2012-10-09 21:48:57 +0200,
44
+ # @boot_time=2012-10-09 18:42:37 +0200,
45
+ # @cpu=
46
+ # [#<struct Vmstat::Cpu
47
+ # num=0,
48
+ # user=187167,
49
+ # system=144466,
50
+ # nice=0,
51
+ # idle=786622>,
52
+ # #<struct Vmstat::Cpu num=1, user=2819, system=1641, nice=0, idle=1113782>,
53
+ # #<struct Vmstat::Cpu num=2, user=158698, system=95186, nice=0, idle=864359>,
54
+ # #<struct Vmstat::Cpu num=3, user=2702, system=1505, nice=0, idle=1114035>,
55
+ # #<struct Vmstat::Cpu num=4, user=140231, system=78248, nice=0, idle=899764>,
56
+ # #<struct Vmstat::Cpu num=5, user=2468, system=1314, nice=0, idle=1114460>,
57
+ # #<struct Vmstat::Cpu num=6, user=120764, system=66284, nice=0, idle=931195>,
58
+ # #<struct Vmstat::Cpu num=7, user=2298, system=1207, nice=0, idle=1114737>],
59
+ # @disks=
60
+ # [#<struct Vmstat::Disk
61
+ # type=:hfs,
62
+ # origin="/dev/disk0s2",
63
+ # mount="/",
64
+ # block_size=4096,
65
+ # free_blocks=51470668,
66
+ # available_blocks=51406668,
67
+ # total_blocks=61069442>],
68
+ # @load_average=
69
+ # #<struct Vmstat::LoadAverage
70
+ # one_minute=1.74072265625,
71
+ # five_minutes=1.34326171875,
72
+ # fifteen_minutes=1.1845703125>,
73
+ # @memory=
74
+ # #<struct Vmstat::Memory
75
+ # pagesize=4096,
76
+ # wired=1037969,
77
+ # active=101977,
78
+ # inactive=484694,
79
+ # free=470582,
80
+ # pageins=102438,
81
+ # pageouts=0,
82
+ # zero_filled=930821373,
83
+ # reactivated=33,
84
+ # purgeable=88184,
85
+ # purged=0,
86
+ # faults=90277275,
87
+ # copy_on_write_faults=668693,
88
+ # lookups=516021,
89
+ # hits=17>,
90
+ # @network_interfaces=
91
+ # [#<struct Vmstat::NetworkInterface
92
+ # name=:lo0,
93
+ # in_bytes=6209398,
94
+ # in_errors=0,
95
+ # in_drops=0,
96
+ # out_bytes=6209398,
97
+ # out_errors=0,
98
+ # type=24>,
99
+ # #<struct Vmstat::NetworkInterface
100
+ # name=:gif0,
101
+ # in_bytes=0,
102
+ # in_errors=0,
103
+ # in_drops=0,
104
+ # out_bytes=0,
105
+ # out_errors=0,
106
+ # type=55>,
107
+ # #<struct Vmstat::NetworkInterface
108
+ # name=:stf0,
109
+ # in_bytes=0,
110
+ # in_errors=0,
111
+ # in_drops=0,
112
+ # out_bytes=0,
113
+ # out_errors=0,
114
+ # type=57>,
115
+ # #<struct Vmstat::NetworkInterface
116
+ # name=:en0,
117
+ # in_bytes=1321276010,
118
+ # in_errors=0,
119
+ # in_drops=0,
120
+ # out_bytes=410426678,
121
+ # out_errors=0,
122
+ # type=6>,
123
+ # #<struct Vmstat::NetworkInterface
124
+ # name=:p2p0,
125
+ # in_bytes=0,
126
+ # in_errors=0,
127
+ # in_drops=0,
128
+ # out_bytes=0,
129
+ # out_errors=0,
130
+ # type=6>],
131
+ # @task=
132
+ # #<struct Vmstat::Task
133
+ # suspend_count=0,
134
+ # virtual_size=2551554048,
135
+ # resident_size=19628032,
136
+ # user_time_ms=28,
137
+ # system_time_ms=83>>
148
138
 
149
139
  ## Contributing
150
140
 
@@ -8,66 +8,30 @@
8
8
  #include <sys/mount.h>
9
9
  #if defined(__APPLE__)
10
10
  #include <mach/mach.h>
11
+ #include <mach/mach_host.h>
11
12
  #endif
12
13
  #include <vmstat.h>
13
14
 
15
+ // helper methodsd
16
+ int system_int(const char *);
17
+ unsigned long long system_ull(const char *);
18
+
14
19
  void Init_vmstat() {
15
20
  vmstat = rb_define_module("Vmstat");
16
21
 
17
- rb_define_singleton_method(vmstat, "network", method_network, 0);
18
- rb_define_singleton_method(vmstat, "cpu", method_cpu, 0);
19
- rb_define_singleton_method(vmstat, "memory", method_memory, 0);
20
- rb_define_singleton_method(vmstat, "disk", method_disk, 1);
21
- rb_define_singleton_method(vmstat, "load_avg", method_load_avg, 0);
22
- rb_define_singleton_method(vmstat, "boot_time", method_boot_time, 0);
23
-
24
- // widely used symbols
25
- SYM_TYPE = ID2SYM(rb_intern("type"));
26
- SYM_FREE = ID2SYM(rb_intern("free"));
27
-
28
- // network symbols
29
- SYM_IN_BYTES = ID2SYM(rb_intern("in_bytes"));
30
- SYM_IN_ERRORS = ID2SYM(rb_intern("in_errors"));
31
- SYM_IN_DROPS = ID2SYM(rb_intern("in_drops"));
32
- SYM_OUT_BYTES = ID2SYM(rb_intern("out_bytes"));
33
- SYM_OUT_ERRORS = ID2SYM(rb_intern("out_errors"));
34
-
35
- // cpu symbols
36
- SYM_USER = ID2SYM(rb_intern("user"));
37
- SYM_SYSTEM = ID2SYM(rb_intern("system"));
38
- SYM_NICE = ID2SYM(rb_intern("nice"));
39
- SYM_IDLE = ID2SYM(rb_intern("idle"));
40
-
41
- // memory symbols
42
- SYM_PAGESIZE = ID2SYM(rb_intern("pagesize"));
43
- SYM_WIRED = ID2SYM(rb_intern("wired"));
44
- SYM_ACTIVE = ID2SYM(rb_intern("active"));
45
- SYM_INACTIVE = ID2SYM(rb_intern("inactive"));
46
- SYM_WIRED_BYTES = ID2SYM(rb_intern("wired_bytes"));
47
- SYM_ACTIVE_BYTES = ID2SYM(rb_intern("active_bytes"));
48
- SYM_INACTIVE_BYTES = ID2SYM(rb_intern("inactive_bytes"));
49
- SYM_FREE_BYTES = ID2SYM(rb_intern("free_bytes"));
50
- SYM_ZERO_FILLED = ID2SYM(rb_intern("zero_filled"));
51
- SYM_REACTIVATED = ID2SYM(rb_intern("reactivated"));
52
- SYM_PURGEABLE = ID2SYM(rb_intern("purgeable"));
53
- SYM_PURGED = ID2SYM(rb_intern("purged"));
54
- SYM_PAGEINS = ID2SYM(rb_intern("pageins"));
55
- SYM_PAGEOUTS = ID2SYM(rb_intern("pageouts"));
56
- SYM_FAULTS = ID2SYM(rb_intern("faults"));
57
- SYM_COW_FAULTS = ID2SYM(rb_intern("copy_on_write_faults"));
58
- SYM_LOOKUPS = ID2SYM(rb_intern("lookups"));
59
- SYM_HITS = ID2SYM(rb_intern("hits"));
60
-
61
- // disk symbols
62
- SYM_ORIGIN = ID2SYM(rb_intern("origin"));
63
- SYM_MOUNT = ID2SYM(rb_intern("mount"));
64
- SYM_AVAILABLE_BYTES = ID2SYM(rb_intern("available_bytes"));
65
- SYM_USED_BYTES = ID2SYM(rb_intern("used_bytes"));
66
- SYM_TOTAL_BYTES = ID2SYM(rb_intern("total_bytes"));
22
+ rb_define_singleton_method(vmstat, "network_interfaces", vmstat_network_interfaces, 0);
23
+ rb_define_singleton_method(vmstat, "cpu", vmstat_cpu, 0);
24
+ rb_define_singleton_method(vmstat, "memory", vmstat_memory, 0);
25
+ rb_define_singleton_method(vmstat, "disk", vmstat_disk, 1);
26
+ rb_define_singleton_method(vmstat, "load_average", vmstat_load_average, 0);
27
+ rb_define_singleton_method(vmstat, "boot_time", vmstat_boot_time, 0);
28
+ #if defined(__APPLE__)
29
+ rb_define_singleton_method(vmstat, "task", vmstat_task, 0);
30
+ #endif
67
31
  }
68
32
 
69
- VALUE method_network(VALUE self) {
70
- VALUE devices = rb_hash_new();
33
+ VALUE vmstat_network_interfaces(VALUE self) {
34
+ VALUE devices = rb_ary_new();
71
35
  int i, err;
72
36
  struct ifmibdata mibdata;
73
37
  size_t len = sizeof(mibdata);
@@ -79,14 +43,16 @@ VALUE method_network(VALUE self) {
79
43
  ifmib_path[4] = i; // set the current row
80
44
  err = sysctl(ifmib_path, 6, &mibdata, &len, NULL, 0);
81
45
  if (err == 0) {
82
- VALUE device = rb_hash_new();
83
- rb_hash_aset(device, SYM_IN_BYTES, ULL2NUM(mibdata.ifmd_data.ifi_ibytes));
84
- rb_hash_aset(device, SYM_IN_ERRORS, ULL2NUM(mibdata.ifmd_data.ifi_ierrors));
85
- rb_hash_aset(device, SYM_IN_DROPS, ULL2NUM(mibdata.ifmd_data.ifi_iqdrops));
86
- rb_hash_aset(device, SYM_OUT_BYTES, ULL2NUM(mibdata.ifmd_data.ifi_obytes));
87
- rb_hash_aset(device, SYM_OUT_ERRORS, ULL2NUM(mibdata.ifmd_data.ifi_oerrors));
88
- rb_hash_aset(device, SYM_TYPE, ULL2NUM(mibdata.ifmd_data.ifi_type));
89
- rb_hash_aset(devices, ID2SYM(rb_intern(mibdata.ifmd_name)), device);
46
+ VALUE device = rb_funcall(rb_path2class("Vmstat::NetworkInterface"),
47
+ rb_intern("new"), 7, ID2SYM(rb_intern(mibdata.ifmd_name)),
48
+ ULL2NUM(mibdata.ifmd_data.ifi_ibytes),
49
+ ULL2NUM(mibdata.ifmd_data.ifi_ierrors),
50
+ ULL2NUM(mibdata.ifmd_data.ifi_iqdrops),
51
+ ULL2NUM(mibdata.ifmd_data.ifi_obytes),
52
+ ULL2NUM(mibdata.ifmd_data.ifi_oerrors),
53
+ ULL2NUM(mibdata.ifmd_data.ifi_type));
54
+
55
+ rb_ary_push(devices, device);
90
56
  }
91
57
  }
92
58
 
@@ -94,7 +60,7 @@ VALUE method_network(VALUE self) {
94
60
  }
95
61
 
96
62
  #if defined(__APPLE__)
97
- VALUE method_cpu(VALUE self) {
63
+ VALUE vmstat_cpu(VALUE self) {
98
64
  VALUE cpus = rb_ary_new();
99
65
  processor_info_array_t cpuInfo;
100
66
  mach_msg_type_number_t numCpuInfo;
@@ -104,21 +70,29 @@ VALUE method_cpu(VALUE self) {
104
70
 
105
71
  if(err == KERN_SUCCESS) {
106
72
  unsigned i;
73
+
107
74
  for(i = 0U; i < numCPUsU; ++i) {
108
- VALUE cpu = rb_hash_new();
109
75
  int pos = CPU_STATE_MAX * i;
110
- rb_hash_aset(cpu, SYM_USER, ULL2NUM(cpuInfo[pos + CPU_STATE_USER]));
111
- rb_hash_aset(cpu, SYM_SYSTEM, ULL2NUM(cpuInfo[pos + CPU_STATE_SYSTEM]));
112
- rb_hash_aset(cpu, SYM_NICE, ULL2NUM(cpuInfo[pos + CPU_STATE_NICE]));
113
- rb_hash_aset(cpu, SYM_IDLE, ULL2NUM(cpuInfo[pos + CPU_STATE_IDLE]));
76
+ VALUE cpu = rb_funcall(rb_path2class("Vmstat::Cpu"),
77
+ rb_intern("new"), 5, ULL2NUM(i),
78
+ ULL2NUM(cpuInfo[pos + CPU_STATE_USER]),
79
+ ULL2NUM(cpuInfo[pos + CPU_STATE_SYSTEM]),
80
+ ULL2NUM(cpuInfo[pos + CPU_STATE_NICE]),
81
+ ULL2NUM(cpuInfo[pos + CPU_STATE_IDLE]));
114
82
  rb_ary_push(cpus, cpu);
115
83
  }
84
+
85
+ err = vm_deallocate(mach_task_self(), (vm_address_t)cpuInfo,
86
+ (vm_size_t)sizeof(*cpuInfo) * numCpuInfo);
87
+ if (err != KERN_SUCCESS) {
88
+ rb_bug("vm_deallocate: %s\n", mach_error_string(err));
89
+ }
116
90
  }
117
91
 
118
92
  return cpus;
119
93
  }
120
94
 
121
- VALUE method_memory(VALUE self) {
95
+ VALUE vmstat_memory(VALUE self) {
122
96
  VALUE memory = Qnil;
123
97
  vm_size_t pagesize;
124
98
  uint host_count = HOST_VM_INFO_COUNT;
@@ -130,31 +104,54 @@ VALUE method_memory(VALUE self) {
130
104
  err = host_statistics(mach_host_self(), HOST_VM_INFO,
131
105
  (host_info_t)&vm_stat, &host_count);
132
106
  if (err == KERN_SUCCESS) {
133
- memory = rb_hash_new();
134
- rb_hash_aset(memory, SYM_PAGESIZE, ULL2NUM(pagesize));
135
- rb_hash_aset(memory, SYM_WIRED, ULL2NUM(vm_stat.active_count));
136
- rb_hash_aset(memory, SYM_ACTIVE, ULL2NUM(vm_stat.inactive_count));
137
- rb_hash_aset(memory, SYM_INACTIVE, ULL2NUM(vm_stat.wire_count));
138
- rb_hash_aset(memory, SYM_FREE, ULL2NUM(vm_stat.free_count));
139
- rb_hash_aset(memory, SYM_WIRED_BYTES, ULL2NUM(vm_stat.wire_count * pagesize));
140
- rb_hash_aset(memory, SYM_ACTIVE_BYTES, ULL2NUM(vm_stat.active_count * pagesize));
141
- rb_hash_aset(memory, SYM_INACTIVE_BYTES, ULL2NUM(vm_stat.inactive_count * pagesize));
142
- rb_hash_aset(memory, SYM_FREE_BYTES, ULL2NUM(vm_stat.free_count * pagesize));
143
- rb_hash_aset(memory, SYM_ZERO_FILLED, ULL2NUM(vm_stat.zero_fill_count));
144
- rb_hash_aset(memory, SYM_REACTIVATED, ULL2NUM(vm_stat.reactivations));
145
- rb_hash_aset(memory, SYM_PURGEABLE, ULL2NUM(vm_stat.purgeable_count));
146
- rb_hash_aset(memory, SYM_PURGED, ULL2NUM(vm_stat.purges));
147
- rb_hash_aset(memory, SYM_PAGEINS, ULL2NUM(vm_stat.pageins));
148
- rb_hash_aset(memory, SYM_PAGEOUTS, ULL2NUM(vm_stat.pageouts));
149
- rb_hash_aset(memory, SYM_FAULTS, ULL2NUM(vm_stat.faults));
150
- rb_hash_aset(memory, SYM_COW_FAULTS, ULL2NUM(vm_stat.cow_faults));
151
- rb_hash_aset(memory, SYM_LOOKUPS, ULL2NUM(vm_stat.lookups));
152
- rb_hash_aset(memory, SYM_HITS, ULL2NUM(vm_stat.hits));
107
+ memory = rb_funcall(rb_path2class("Vmstat::Memory"),
108
+ rb_intern("new"), 15, ULL2NUM(pagesize),
109
+ ULL2NUM(vm_stat.active_count),
110
+ ULL2NUM(vm_stat.inactive_count),
111
+ ULL2NUM(vm_stat.wire_count),
112
+ ULL2NUM(vm_stat.free_count),
113
+ ULL2NUM(vm_stat.pageins),
114
+ ULL2NUM(vm_stat.pageouts),
115
+ ULL2NUM(vm_stat.zero_fill_count),
116
+ ULL2NUM(vm_stat.reactivations),
117
+ ULL2NUM(vm_stat.purgeable_count),
118
+ ULL2NUM(vm_stat.purges),
119
+ ULL2NUM(vm_stat.faults),
120
+ ULL2NUM(vm_stat.cow_faults),
121
+ ULL2NUM(vm_stat.lookups),
122
+ ULL2NUM(vm_stat.hits));
123
+ }
124
+
125
+ err = vm_deallocate(mach_task_self(), (vm_address_t)pagesize,
126
+ (vm_size_t)host_count);
127
+ if (err != KERN_SUCCESS) {
128
+ rb_bug("vm_deallocate: %s\n", mach_error_string(err));
153
129
  }
154
130
  }
155
-
131
+
156
132
  return memory;
157
133
  }
134
+
135
+ VALUE vmstat_task(VALUE self) {
136
+ VALUE task = Qnil;
137
+ struct task_basic_info info;
138
+ kern_return_t err;
139
+ mach_msg_type_number_t size = sizeof(info);
140
+
141
+ err = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
142
+ if (err == KERN_SUCCESS) {
143
+ task = rb_funcall(rb_path2class("Vmstat::Task"),
144
+ rb_intern("new"), 5, LONG2NUM(info.suspend_count),
145
+ LONG2NUM(info.virtual_size),
146
+ LONG2NUM(info.resident_size),
147
+ LONG2NUM(info.user_time.seconds * 1000 + info.user_time.microseconds),
148
+ LONG2NUM(info.system_time.seconds * 1000 + info.system_time.microseconds));
149
+ } else {
150
+ rb_bug("task_info: %s\n", mach_error_string(err));
151
+ }
152
+
153
+ return task;
154
+ }
158
155
  #elif __linux
159
156
  // linux
160
157
  #elif __unix // all unices not caught above
@@ -166,98 +163,94 @@ typedef struct {
166
163
  long idle;
167
164
  } cpu_time_t;
168
165
 
169
- VALUE method_cpu(VALUE self) {
166
+ VALUE vmstat_cpu(VALUE self) {
170
167
  VALUE cpus = rb_ary_new();
171
168
  int cpu_count = system_int("hw.ncpu");
172
169
  size_t len = sizeof(cpu_time_t) * cpu_count;
173
170
  cpu_time_t * cp_times = ALLOC_N(cpu_time_t, cpu_count);
171
+ cpu_time_t * cp_time;
174
172
  int i;
175
173
 
176
174
  if (sysctlbyname("kern.cp_times", cp_times, &len, NULL, 0) == 0) {
177
175
  for (i = 0; i < cpu_count; i++) {
178
- VALUE cpu = rb_hash_new();
179
- cpu_time_t cp_time = cp_times[i];
180
- rb_hash_aset(cpu, SYM_USER, LONG2NUM(cp_time.user));
181
- rb_hash_aset(cpu, SYM_SYSTEM, LONG2NUM(cp_time.system + cp_time.intr));
182
- rb_hash_aset(cpu, SYM_NICE, LONG2NUM(cp_time.nice));
183
- rb_hash_aset(cpu, SYM_IDLE, LONG2NUM(cp_time.idle));
176
+ cp_time = &cp_times[i];
177
+ VALUE cpu = rb_funcall(rb_path2class("Vmstat::Cpu"),
178
+ rb_intern("new"), 5, ULL2NUM(i),
179
+ ULL2NUM(cp_time->user),
180
+ ULL2NUM(cp_time->system + cp_time->intr),
181
+ ULL2NUM(cp_time->nice),
182
+ ULL2NUM(cp_time->idle));
184
183
  rb_ary_push(cpus, cpu);
185
184
  }
186
185
  }
186
+
187
187
  free(cp_times);
188
188
 
189
189
  return cpus;
190
190
  }
191
191
 
192
- VALUE method_memory(VALUE self) {
193
- VALUE memory = rb_hash_new();
194
- unsigned long long pagesize = system_ull("vm.stats.vm.v_page_size");
195
-
196
- rb_hash_aset(memory, SYM_PAGESIZE, ULL2NUM(pagesize));
197
- rb_hash_aset(memory, SYM_WIRED, ULL2NUM(system_ull("vm.stats.vm.v_inactive_count")));
198
- rb_hash_aset(memory, SYM_ACTIVE, ULL2NUM(system_ull("vm.stats.vm.v_active_count")));
199
- rb_hash_aset(memory, SYM_INACTIVE, ULL2NUM(system_ull("vm.stats.vm.v_wire_count")));
200
- rb_hash_aset(memory, SYM_FREE, ULL2NUM(system_ull("vm.stats.vm.v_free_count")));
201
- rb_hash_aset(memory, SYM_WIRED_BYTES, ULL2NUM((system_ull("vm.stats.vm.v_cache_count") +
202
- system_ull("vm.stats.vm.v_wire_count")) * pagesize));
203
- rb_hash_aset(memory, SYM_ACTIVE_BYTES, ULL2NUM(system_ull("vm.stats.vm.v_active_count") * pagesize));
204
- rb_hash_aset(memory, SYM_INACTIVE_BYTES, ULL2NUM(system_ull("vm.stats.vm.v_inactive_count") * pagesize));
205
- rb_hash_aset(memory, SYM_FREE_BYTES, ULL2NUM(system_ull("vm.stats.vm.v_free_count") * pagesize));
206
- rb_hash_aset(memory, SYM_ZERO_FILLED, ULL2NUM(system_ull("vm.stats.misc.zero_page_count")));
207
- rb_hash_aset(memory, SYM_REACTIVATED, ULL2NUM(system_ull("vm.stats.vm.v_reactivated")));
208
- rb_hash_aset(memory, SYM_PURGEABLE, Qnil);
209
- rb_hash_aset(memory, SYM_PURGED, Qnil);
210
- rb_hash_aset(memory, SYM_PAGEINS, Qnil);
211
- rb_hash_aset(memory, SYM_PAGEOUTS, Qnil);
212
- rb_hash_aset(memory, SYM_FAULTS, ULL2NUM(system_ull("vm.stats.vm.v_vm_faults")));
213
- rb_hash_aset(memory, SYM_COW_FAULTS, ULL2NUM(system_ull("vm.stats.vm.v_cow_faults")));
214
- rb_hash_aset(memory, SYM_LOOKUPS, Qnil);
215
- rb_hash_aset(memory, SYM_HITS, Qnil);
216
-
192
+ VALUE vmstat_memory(VALUE self) {
193
+ VALUE memory = rb_funcall(rb_path2class("Vmstat::Memory"),
194
+ rb_intern("new"), 15, ULL2NUM(system_ull("vm.stats.vm.v_page_size")),
195
+ ULL2NUM(system_ull("vm.stats.vm.v_active_count")),
196
+ ULL2NUM(system_ull("vm.stats.vm.v_wire_count")),
197
+ ULL2NUM(system_ull("vm.stats.vm.v_inactive_count")),
198
+ ULL2NUM(system_ull("vm.stats.vm.v_free_count")),
199
+ ULL2NUM(Qnil),
200
+ ULL2NUM(Qnil),
201
+ ULL2NUM(system_ull("vm.stats.misc.zero_page_count")),
202
+ ULL2NUM(system_ull("vm.stats.vm.v_reactivated")),
203
+ ULL2NUM(Qnil),
204
+ ULL2NUM(Qnil),
205
+ ULL2NUM(system_ull("vm.stats.vm.v_vm_faults")),
206
+ ULL2NUM(system_ull("vm.stats.vm.v_cow_faults")),
207
+ ULL2NUM(Qnil),
208
+ ULL2NUM(Qnil));
217
209
  return memory;
218
210
  }
219
211
  #elif __posix
220
212
  // POSIX
221
213
  #endif
222
214
 
223
- VALUE method_disk(VALUE self, VALUE path) {
215
+ VALUE vmstat_disk(VALUE self, VALUE path) {
224
216
  VALUE disk = Qnil;
225
217
  struct statfs stat;
226
218
 
227
219
  if (statfs(StringValueCStr(path), &stat) != -1) {
228
- disk = rb_hash_new();
229
- rb_hash_aset(disk, SYM_TYPE, ID2SYM(rb_intern(stat.f_fstypename)));
230
- rb_hash_aset(disk, SYM_ORIGIN, rb_str_new(stat.f_mntfromname, strlen(stat.f_mntfromname)));
231
- rb_hash_aset(disk, SYM_MOUNT, rb_str_new(stat.f_mntonname, strlen(stat.f_mntonname)));
232
- rb_hash_aset(disk, SYM_FREE_BYTES, ULL2NUM(stat.f_bfree * stat.f_bsize));
233
- rb_hash_aset(disk, SYM_AVAILABLE_BYTES, ULL2NUM(stat.f_bavail * stat.f_bsize));
234
- rb_hash_aset(disk, SYM_USED_BYTES, ULL2NUM((stat.f_blocks - stat.f_bfree) * stat.f_bsize));
235
- rb_hash_aset(disk, SYM_TOTAL_BYTES, ULL2NUM(stat.f_blocks * stat.f_bsize));
220
+ disk = rb_funcall(rb_path2class("Vmstat::Disk"),
221
+ rb_intern("new"), 7, ID2SYM(rb_intern(stat.f_fstypename)),
222
+ rb_str_new(stat.f_mntfromname, strlen(stat.f_mntfromname)),
223
+ rb_str_new(stat.f_mntonname, strlen(stat.f_mntonname)),
224
+ ULL2NUM(stat.f_bsize),
225
+ ULL2NUM(stat.f_bfree),
226
+ ULL2NUM(stat.f_bavail),
227
+ ULL2NUM(stat.f_blocks));
236
228
  }
237
229
 
238
230
  return disk;
239
231
  }
240
232
 
241
- #define AVGCOUNT 3
242
- VALUE method_load_avg(VALUE self) {
243
- VALUE loads = rb_ary_new();
233
+ VALUE vmstat_load_average(VALUE self) {
234
+ VALUE load = Qnil;
244
235
  double loadavg[AVGCOUNT];
245
- int i;
246
236
 
247
237
  getloadavg(&loadavg[0], AVGCOUNT);
248
- for(i = 0; i < AVGCOUNT; i++) {
249
- rb_ary_push(loads, rb_float_new(loadavg[i]));
250
- }
251
238
 
252
- return loads;
239
+ load = rb_funcall(rb_path2class("Vmstat::LoadAverage"),
240
+ rb_intern("new"), 3, rb_float_new(loadavg[0]),
241
+ rb_float_new(loadavg[1]),
242
+ rb_float_new(loadavg[2]));
243
+
244
+ return load;
253
245
  }
254
246
 
255
- VALUE method_boot_time(VALUE self) {
247
+ static int BOOT_TIME_MIB[] = { CTL_KERN, KERN_BOOTTIME };
248
+
249
+ VALUE vmstat_boot_time(VALUE self) {
256
250
  struct timeval tv;
257
251
  size_t size = sizeof(tv);
258
- static int which[] = { CTL_KERN, KERN_BOOTTIME };
259
252
 
260
- if (sysctl(which, 2, &tv, &size, NULL, 0) == 0) {
253
+ if (sysctl(BOOT_TIME_MIB, 2, &tv, &size, NULL, 0) == 0) {
261
254
  return rb_time_new(tv.tv_sec, tv.tv_usec);
262
255
  } else {
263
256
  return Qnil;