vmstat 1.0.0 → 1.1.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/ext/vmstat/vmstat.c CHANGED
@@ -1,276 +1,56 @@
1
- #include <sys/types.h>
2
- #include <sys/socket.h>
3
- #include <sys/sysctl.h>
4
- #include <net/if.h>
5
- #include <net/if_mib.h>
6
- #include <net/if_types.h>
7
- #include <sys/param.h>
8
- #include <sys/mount.h>
9
- #if defined(__APPLE__)
10
- #include <mach/mach.h>
11
- #include <mach/mach_host.h>
12
- #endif
13
1
  #include <vmstat.h>
14
-
15
- // helper methodsd
16
- int system_int(const char *);
17
- unsigned long long system_ull(const char *);
2
+ #include <hw/posix.h>
3
+ #include <hw/statfs.h>
4
+ #include <hw/sysctl.h>
5
+ #include <hw/mach.h>
6
+ #include <hw/bsd.h>
18
7
 
19
8
  void Init_vmstat() {
20
- vmstat = rb_define_module("Vmstat");
9
+ VALUE vmstat = rb_define_module("Vmstat");
21
10
 
11
+ /*
12
+ * Below the list of platform implementations. The Platforms in square
13
+ * brackets are implemented in ruby not in c.
14
+ */
15
+
16
+ // MAC, FreeBSD, [LINUX]
17
+ #if defined(VMSTAT_NETWORK_INTERFACES)
22
18
  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
19
  #endif
31
- }
32
-
33
- VALUE vmstat_network_interfaces(VALUE self) {
34
- VALUE devices = rb_ary_new();
35
- int i, err;
36
- struct ifmibdata mibdata;
37
- size_t len = sizeof(mibdata);
38
- int ifmib_path[] = {
39
- CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, -1, IFDATA_GENERAL
40
- };
41
20
 
42
- for (i = 1, err = 0; err == 0; i++) {
43
- ifmib_path[4] = i; // set the current row
44
- err = sysctl(ifmib_path, 6, &mibdata, &len, NULL, 0);
45
- if (err == 0) {
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);
56
- }
57
- }
58
-
59
- return devices;
60
- }
61
-
62
- #if defined(__APPLE__)
63
- VALUE vmstat_cpu(VALUE self) {
64
- VALUE cpus = rb_ary_new();
65
- processor_info_array_t cpuInfo;
66
- mach_msg_type_number_t numCpuInfo;
67
- natural_t numCPUsU = 0U;
68
- kern_return_t err = host_processor_info(mach_host_self(),
69
- PROCESSOR_CPU_LOAD_INFO, &numCPUsU, &cpuInfo, &numCpuInfo);
70
-
71
- if(err == KERN_SUCCESS) {
72
- unsigned i;
73
-
74
- for(i = 0U; i < numCPUsU; ++i) {
75
- int pos = CPU_STATE_MAX * i;
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]));
82
- rb_ary_push(cpus, cpu);
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
- }
90
- }
91
-
92
- return cpus;
93
- }
94
-
95
- VALUE vmstat_memory(VALUE self) {
96
- VALUE memory = Qnil;
97
- vm_size_t pagesize;
98
- uint host_count = HOST_VM_INFO_COUNT;
99
- kern_return_t err;
100
- vm_statistics_data_t vm_stat;
101
-
102
- err = host_page_size(mach_host_self(), &pagesize);
103
- if (err == KERN_SUCCESS) {
104
- err = host_statistics(mach_host_self(), HOST_VM_INFO,
105
- (host_info_t)&vm_stat, &host_count);
106
- if (err == KERN_SUCCESS) {
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));
129
- }
130
- }
131
-
132
- return memory;
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
- }
155
- #elif __linux
156
- // linux
157
- #elif __unix // all unices not caught above
158
- typedef struct {
159
- long user;
160
- long nice;
161
- long system;
162
- long intr;
163
- long idle;
164
- } cpu_time_t;
165
-
166
- VALUE vmstat_cpu(VALUE self) {
167
- VALUE cpus = rb_ary_new();
168
- int cpu_count = system_int("hw.ncpu");
169
- size_t len = sizeof(cpu_time_t) * cpu_count;
170
- cpu_time_t * cp_times = ALLOC_N(cpu_time_t, cpu_count);
171
- cpu_time_t * cp_time;
172
- int i;
173
-
174
- if (sysctlbyname("kern.cp_times", cp_times, &len, NULL, 0) == 0) {
175
- for (i = 0; i < cpu_count; i++) {
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));
183
- rb_ary_push(cpus, cpu);
184
- }
185
- }
186
-
187
- free(cp_times);
188
-
189
- return cpus;
190
- }
191
-
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));
209
- return memory;
210
- }
211
- #elif __posix
212
- // POSIX
21
+ // MAC, FreeBSD, [LINUX]
22
+ #if defined(VMSTAT_CPU)
23
+ rb_define_singleton_method(vmstat, "cpu", vmstat_cpu, 0);
213
24
  #endif
214
25
 
215
- VALUE vmstat_disk(VALUE self, VALUE path) {
216
- VALUE disk = Qnil;
217
- struct statfs stat;
218
-
219
- if (statfs(StringValueCStr(path), &stat) != -1) {
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));
228
- }
229
-
230
- return disk;
231
- }
232
-
233
- VALUE vmstat_load_average(VALUE self) {
234
- VALUE load = Qnil;
235
- double loadavg[AVGCOUNT];
236
-
237
- getloadavg(&loadavg[0], AVGCOUNT);
238
-
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]));
26
+ // MAC, FreeBSD, [LINUX]
27
+ #if defined(VMSTAT_MEMORY)
28
+ rb_define_singleton_method(vmstat, "memory", vmstat_memory, 0);
29
+ #endif
243
30
 
244
- return load;
245
- }
31
+ // MAC, FreeBSD, LINUX
32
+ #if defined(VMSTAT_DISK)
33
+ rb_define_singleton_method(vmstat, "disk", vmstat_disk, 1);
34
+ #endif
246
35
 
247
- static int BOOT_TIME_MIB[] = { CTL_KERN, KERN_BOOTTIME };
36
+ // MAC, FreeBSD, LINUX
37
+ #if defined(VMSTAT_LOAD_AVERAGE)
38
+ rb_define_singleton_method(vmstat, "load_average", vmstat_load_average, 0);
39
+ #endif
248
40
 
249
- VALUE vmstat_boot_time(VALUE self) {
250
- struct timeval tv;
251
- size_t size = sizeof(tv);
41
+ // MAC, FreeBSD, [LINUX]
42
+ #if defined(VMSTAT_BOOT_TIME)
43
+ rb_define_singleton_method(vmstat, "boot_time", vmstat_boot_time, 0);
44
+ #endif
252
45
 
253
- if (sysctl(BOOT_TIME_MIB, 2, &tv, &size, NULL, 0) == 0) {
254
- return rb_time_new(tv.tv_sec, tv.tv_usec);
255
- } else {
256
- return Qnil;
257
- }
258
- }
46
+ // MAC
47
+ #if defined(VMSTAT_TASK)
48
+ rb_define_singleton_method(vmstat, "task", vmstat_task, 0);
49
+ #endif
259
50
 
260
- int system_int(const char * name) {
261
- int number;
262
- size_t number_size = sizeof(number);
263
- sysctlbyname(name, &number, &number_size, NULL, 0);
264
- return number;
51
+ // MAC, FreeBSD, LINUX
52
+ #if defined(VMSTAT_PAGESIZE)
53
+ rb_define_singleton_method(vmstat, "pagesize", vmstat_pagesize, 0);
54
+ #endif
265
55
  }
266
56
 
267
- unsigned long long system_ull(const char * name) {
268
- long number;
269
- size_t number_size = sizeof(number);
270
- if (sysctlbyname(name, &number, &number_size, NULL, 0) == -1) {
271
- perror("sysctlbyname");
272
- return -1;
273
- } else {
274
- return number;
275
- }
276
- }
data/ext/vmstat/vmstat.h CHANGED
@@ -1,15 +1,17 @@
1
1
  #include <ruby.h>
2
2
 
3
+ #ifndef _VMSTAT_H_
4
+ #define _VMSTAT_H_
5
+
3
6
  #define AVGCOUNT 3
4
7
 
5
- VALUE vmstat = Qnil;
6
8
  VALUE vmstat_network_interfaces(VALUE self);
7
9
  VALUE vmstat_cpu(VALUE self);
8
10
  VALUE vmstat_memory(VALUE self);
9
11
  VALUE vmstat_disk(VALUE self, VALUE path);
10
12
  VALUE vmstat_load_average(VALUE self);
11
13
  VALUE vmstat_boot_time(VALUE self);
12
-
13
- #if defined(__APPLE__)
14
14
  VALUE vmstat_task(VALUE self);
15
- #endif
15
+ VALUE vmstat_pagesize(VALUE self);
16
+
17
+ #endif /* _VMSTAT_H_ */
data/lib/vmstat.rb CHANGED
@@ -4,76 +4,72 @@ module Vmstat
4
4
  autoload :Cpu, "vmstat/cpu"
5
5
  autoload :NetworkInterface, "vmstat/network_interface"
6
6
  autoload :Disk, "vmstat/disk"
7
+ autoload :LinuxDisk, "vmstat/linux_disk"
7
8
  autoload :Memory, "vmstat/memory"
8
9
  autoload :Task, "vmstat/task"
9
10
  autoload :LoadAverage, "vmstat/load_average"
11
+ autoload :ProcFS, "vmstat/procfs"
12
+ autoload :Stub, "vmstat/stub"
10
13
  autoload :Snapshot, "vmstat/snapshot"
14
+ extend Stub # the default null implementation
11
15
 
12
- # Creates a full snapshot of the systems hardware statistics.
13
- # @param [Array<String>] the paths to the disks to snapshot.
14
- # @return [Vmstat::Snapshot] a snapshot of all statistics.
15
- # @example
16
- # Vmstat.snapshot # => #<struct Vmstat::Snapshot ...>
17
- def self.snapshot(paths = ["/"])
18
- Snapshot.new(paths)
19
- end
20
-
16
+ # @!method self.boot_time
21
17
  # Fetches the boot time of the system.
22
18
  # @return [Time] the boot time as regular time object.
23
19
  # @example
24
20
  # Vmstat.boot_time # => 2012-10-09 18:42:37 +0200
25
- def self.boot_time
26
- # implemented in native extension ...
27
- end
28
-
21
+
22
+ # @!method self.cpu
29
23
  # Fetches the cpu statistics (usage counter for user, nice, system and idle)
30
24
  # @return [Array<Vmstat::Cpu>] the array of cpu counter
31
25
  # @example
32
26
  # Vmstat.cpu # => [#<struct Vmstat::Cpu ...>, #<struct Vmstat::Cpu ...>]
33
- def self.cpu
34
- # implemented in native extension ...
35
- end
36
-
27
+
28
+ # @!method self.disk(path)
37
29
  # Fetches the usage data and other useful disk information for the given path.
38
30
  # @param [String] path the path (mount point or device path) to the disk
39
31
  # @return [Vmstat::Disk] the disk information
40
32
  # @example
41
33
  # Vmstat.disk("/") # => #<struct Vmstat::Disk type=:hfs, ...>
42
- def self.disk(path)
43
- # implemented in native extension ...
44
- end
45
-
34
+
35
+ # @!method self.load_average
46
36
  # Fetches the load average for the current system.
47
37
  # @return [Vmstat::LoadAverage] the load average data
48
38
  # @example
49
39
  # Vmstat.load_average # => #<struct Vmstat::LoadAverage one_minute=...>
50
- def self.load_average
51
- # implemented in native extension ...
52
- end
53
-
40
+
41
+ # @!method self.memory
54
42
  # Fetches the memory usage information.
55
43
  # @return [Vmstat::Memory] the memory data like free, used und total.
56
44
  # @example
57
45
  # Vmstat.memory # => #<struct Vmstat::Memory ...>
58
- def self.memory
59
- # implemented in native extension ...
60
- end
61
-
46
+
47
+ # @!method self.network_interfaces
62
48
  # Fetches the information for all available network devices.
63
49
  # @return [Array<Vmstat::NetworkInterface>] the network device information
64
50
  # @example
65
51
  # Vmstat.network_interfaces # => [#<struct Vmstat::NetworkInterface ...>, ...]
66
- def self.network_interfaces
67
- # implemented in native extension ...
68
- end
69
-
52
+
53
+ # @!method self.pagesize
54
+ # Fetches pagesize of the current system.
55
+ # @return [Fixnum] the pagesize of the current system in bytes.
56
+ # @example
57
+ # Vmstat.pagesize # => 4096
58
+
59
+ # @!method self.task
70
60
  # Fetches time and memory usage for the current process.
71
61
  # @note Currently only on Mac OS X
72
62
  # @return [Array<Vmstat::Task>] the network device information
73
63
  # @example
74
64
  # Vmstat.task # => #<struct Vmstat::Task ...>
75
- def self.task
76
- # implemented in native extension ...
65
+
66
+ # Creates a full snapshot of the systems hardware statistics.
67
+ # @param [Array<String>] the paths to the disks to snapshot.
68
+ # @return [Vmstat::Snapshot] a snapshot of all statistics.
69
+ # @example
70
+ # Vmstat.snapshot # => #<struct Vmstat::Snapshot ...>
71
+ def self.snapshot(paths = ["/"])
72
+ Snapshot.new(paths)
77
73
  end
78
74
 
79
75
  # Filters all available ethernet devices.
@@ -90,3 +86,7 @@ module Vmstat
90
86
  end
91
87
 
92
88
  require "vmstat/vmstat" # native lib
89
+
90
+ if RUBY_PLATFORM =~ /linux/
91
+ Vmstat.send(:extend, Vmstat::ProcFS)
92
+ end