linux_stat 1.2.3 → 1.6.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.
@@ -113,10 +113,20 @@ execute.sort.each do |c|
113
113
 
114
114
  meths.each do |meth|
115
115
  arg = nil
116
- params = e.method(meth).parameters
117
116
 
118
- param = ''
117
+ arity = e.method(meth).arity
118
+ if arity > 0 || arity == -2
119
+ if c == :PrettifyBytes
120
+ arg = rand(10 ** 15)
121
+ elsif c == :FS
122
+ arg = '/'
123
+ else
124
+ next
125
+ end
126
+ end
119
127
 
128
+ params = e.method(meth).parameters
129
+ param = ''
120
130
  params.each do |p|
121
131
  case p[0]
122
132
  when :opt
@@ -124,24 +134,15 @@ execute.sort.each do |c|
124
134
  when :key
125
135
  param << "#{p[1]}:, "
126
136
  when :req
127
- param << "#{p[1] || 'arg'}, "
137
+ _arg = arg ? " = #{arg.inspect}" : ''.freeze
138
+ param << "#{p[1] || 'arg'}#{_arg}, "
128
139
  end
129
140
  end
130
141
 
131
- param.delete_suffix!(", ")
132
-
133
- if e.method(meth).arity > 0
134
- if c == :PrettifyBytes
135
- arg = rand(10 ** 15)
136
- elsif c == :FS
137
- arg = '/'
138
- else
139
- next
140
- end
141
- end
142
+ param.chomp!(", ")
142
143
 
143
144
  disp_meth = "#{meth}"
144
- disp_meth.concat(arg ? "(#{param} = #{arg.inspect})" : "(#{param})")
145
+ disp_meth.concat(arg ? "(#{param})" : "(#{param})")
145
146
 
146
147
  time = Time.now
147
148
  ret = arg ? e.send(meth, arg) : e.send(meth)
@@ -18,16 +18,16 @@ static VALUE statfs(VALUE obj, VALUE dir) {
18
18
 
19
19
  if(statvfs(d, &buf) < 0) return hash ;
20
20
 
21
- rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2FIX(buf.f_bsize)) ;
22
- rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), INT2FIX(buf.f_frsize)) ;
23
- rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), INT2FIX(buf.f_blocks)) ;
24
- rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), INT2FIX(buf.f_bfree)) ;
25
- rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), INT2FIX(buf.f_bavail)) ;
26
- rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), INT2FIX(buf.f_files)) ;
27
- rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), INT2FIX(buf.f_ffree)) ;
28
- rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), INT2FIX(buf.f_fsid)) ;
29
- rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), INT2FIX(buf.f_flag)) ;
30
- rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), INT2FIX(buf.f_namemax)) ;
21
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_size")), INT2FIX((unsigned int)buf.f_bsize)) ;
22
+ rb_hash_aset(hash, ID2SYM(rb_intern("fragment_size")), ULL2NUM((unsigned long long)buf.f_frsize)) ;
23
+ rb_hash_aset(hash, ID2SYM(rb_intern("blocks")), ULL2NUM((unsigned long long)buf.f_blocks)) ;
24
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_free")), ULL2NUM((unsigned long long)buf.f_bfree)) ;
25
+ rb_hash_aset(hash, ID2SYM(rb_intern("block_avail_unpriv")), ULL2NUM((unsigned long long)buf.f_bavail)) ;
26
+ rb_hash_aset(hash, ID2SYM(rb_intern("inodes")), ULL2NUM((unsigned long long)buf.f_files)) ;
27
+ rb_hash_aset(hash, ID2SYM(rb_intern("free_inodes")), ULL2NUM((unsigned long long)buf.f_ffree)) ;
28
+ rb_hash_aset(hash, ID2SYM(rb_intern("filesystem_id")), ULL2NUM((unsigned long long)buf.f_fsid)) ;
29
+ rb_hash_aset(hash, ID2SYM(rb_intern("mount_flags")), ULL2NUM((unsigned long long)buf.f_flag)) ;
30
+ rb_hash_aset(hash, ID2SYM(rb_intern("max_filename_length")), ULL2NUM((unsigned long long)buf.f_namemax)) ;
31
31
 
32
32
  return hash ;
33
33
  }
@@ -18,9 +18,9 @@
18
18
  static VALUE count_cpu_for_pid(VALUE obj, VALUE pid) {
19
19
  cpu_set_t set ;
20
20
  CPU_ZERO(&set) ;
21
- short int stat = sched_getaffinity(FIX2INT(pid), sizeof(set), &set) ;
21
+ char status = sched_getaffinity(FIX2INT(pid), sizeof(set), &set) ;
22
22
 
23
- if (stat < 0) return Qnil ;
23
+ if (status < 0) return Qnil ;
24
24
  return INT2FIX(CPU_COUNT(&set)) ;
25
25
  }
26
26
 
@@ -4,7 +4,7 @@ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
4
  abort('Platform is not linux')
5
5
  end
6
6
 
7
- unless have_header('sys/unistd.h') && have_header('ruby.h')
7
+ unless have_header('unistd.h') && have_header('ruby.h')
8
8
  abort('Missing header')
9
9
  end
10
10
 
@@ -12,7 +12,7 @@
12
12
  #endif
13
13
 
14
14
  static VALUE getTick(VALUE obj) {
15
- short int val = sysconf(_SC_CLK_TCK) ;
15
+ int val = sysconf(_SC_CLK_TCK) ;
16
16
  if (val < 0) return Qnil ;
17
17
 
18
18
  return INT2FIX(val) ;
@@ -22,84 +22,84 @@ static VALUE getChildMax(VALUE obj) {
22
22
  long long int val = sysconf(_SC_CHILD_MAX) ;
23
23
  if (val < 0) return Qnil ;
24
24
 
25
- return INT2FIX(val) ;
25
+ return LL2NUM(val) ;
26
26
  }
27
27
 
28
28
  static VALUE getHostnameMax(VALUE obj) {
29
- int val = sysconf(_SC_HOST_NAME_MAX) ;
29
+ long long val = sysconf(_SC_HOST_NAME_MAX) ;
30
30
  if (val < 0) return Qnil ;
31
31
 
32
- return INT2FIX(val) ;
32
+ return LL2NUM(val) ;
33
33
  }
34
34
 
35
35
  static VALUE getLoginNameMax(VALUE obj) {
36
- int val = sysconf(_SC_LOGIN_NAME_MAX) ;
36
+ long long val = sysconf(_SC_LOGIN_NAME_MAX) ;
37
37
  if (val < 0) return Qnil ;
38
38
 
39
- return INT2FIX(val) ;
39
+ return LL2NUM(val) ;
40
40
  }
41
41
 
42
42
  static VALUE getOpenMax(VALUE obj) {
43
- int val = sysconf(_SC_OPEN_MAX) ;
43
+ long long val = sysconf(_SC_OPEN_MAX) ;
44
44
  if (val < 0) return Qnil ;
45
45
 
46
- return INT2FIX(val) ;
46
+ return LL2NUM(val) ;
47
47
  }
48
48
 
49
49
  static VALUE getPageSize(VALUE obj) {
50
- short int val = sysconf(_SC_PAGESIZE) ;
50
+ int val = sysconf(_SC_PAGESIZE) ;
51
51
  if (val < 0) return Qnil ;
52
52
 
53
53
  return INT2FIX(val) ;
54
54
  }
55
55
 
56
56
  static VALUE getStreamMax(VALUE obj) {
57
- int val = sysconf(_SC_STREAM_MAX) ;
57
+ long long val = sysconf(_SC_STREAM_MAX) ;
58
58
  if (val < 0) return Qnil ;
59
59
 
60
- return INT2FIX(val) ;
60
+ return LL2NUM(val) ;
61
61
  }
62
62
 
63
63
  static VALUE getTTYNameMax(VALUE obj) {
64
- int val = sysconf(_SC_TTY_NAME_MAX) ;
64
+ long long val = sysconf(_SC_TTY_NAME_MAX) ;
65
65
  if (val < 0) return Qnil ;
66
66
 
67
- return INT2FIX(val) ;
67
+ return LL2NUM(val) ;
68
68
  }
69
69
 
70
70
  static VALUE getPosixVersion(VALUE obj) {
71
- int val = sysconf(_SC_VERSION) ;
71
+ long long val = sysconf(_SC_VERSION) ;
72
72
  if (val < 0) return Qnil ;
73
73
 
74
- return INT2FIX(val) ;
74
+ return LL2NUM(val) ;
75
75
  }
76
76
 
77
77
  static VALUE getLineMax(VALUE obj) {
78
- int val = sysconf(_SC_LINE_MAX) ;
78
+ long long val = sysconf(_SC_LINE_MAX) ;
79
79
  if (val < 0) return Qnil ;
80
80
 
81
- return INT2FIX(val) ;
81
+ return LL2NUM(val) ;
82
82
  }
83
83
 
84
84
  static VALUE getExprNestMax(VALUE obj) {
85
- int val = sysconf(_SC_EXPR_NEST_MAX) ;
85
+ long long val = sysconf(_SC_EXPR_NEST_MAX) ;
86
86
  if (val < 0) return Qnil ;
87
87
 
88
- return INT2FIX(val) ;
88
+ return LL2NUM(val) ;
89
89
  }
90
90
 
91
91
  static VALUE getProcessorConfigured(VALUE obj) {
92
- short int val = sysconf(_SC_NPROCESSORS_CONF) ;
92
+ long val = sysconf(_SC_NPROCESSORS_CONF) ;
93
93
  if (val < 0) return Qnil ;
94
94
 
95
- return INT2FIX(val) ;
95
+ return LONG2NUM(val) ;
96
96
  }
97
97
 
98
98
  static VALUE getProcessorOnline(VALUE obj) {
99
- short int val = sysconf(_SC_NPROCESSORS_ONLN) ;
99
+ long val = sysconf(_SC_NPROCESSORS_ONLN) ;
100
100
  if (val < 0) return Qnil ;
101
101
 
102
- return INT2FIX(val) ;
102
+ return LONG2NUM(val) ;
103
103
  }
104
104
 
105
105
  static VALUE getUser(VALUE obj) {
@@ -108,22 +108,22 @@ static VALUE getUser(VALUE obj) {
108
108
  }
109
109
 
110
110
  static VALUE getUID(VALUE obj) {
111
- return INT2FIX(getuid()) ;
111
+ return UINT2NUM((unsigned int) getuid()) ;
112
112
  }
113
113
 
114
114
  static VALUE getGID(VALUE obj) {
115
- return INT2FIX(getgid()) ;
115
+ return UINT2NUM((unsigned int) getgid()) ;
116
116
  }
117
117
 
118
118
  static VALUE getEUID(VALUE obj) {
119
- return INT2FIX(geteuid()) ;
119
+ return UINT2NUM((unsigned int) geteuid()) ;
120
120
  }
121
121
 
122
122
  static VALUE getHostname(VALUE obj) {
123
123
  int h_max = sysconf(_SC_HOST_NAME_MAX) + 1 ;
124
124
  char hostname[h_max] ;
125
125
 
126
- short int status = gethostname(hostname, h_max) ;
126
+ char status = gethostname(hostname, h_max) ;
127
127
 
128
128
  return (status < 0) ? rb_str_new_cstr("") : rb_str_new_cstr(hostname) ;
129
129
  }
@@ -0,0 +1,11 @@
1
+ require 'mkmf'
2
+
3
+ unless have_const('linux') || RbConfig::CONFIG['arch'].to_s[/linux/]
4
+ abort('Platform is not linux')
5
+ end
6
+
7
+ unless have_header('sys/sysinfo.h') && have_header('ruby.h')
8
+ abort('Missing header')
9
+ end
10
+
11
+ create_makefile 'linux_stat/sysinfo'
@@ -0,0 +1,177 @@
1
+ #include <sys/sysinfo.h>
2
+ #include "ruby.h"
3
+
4
+ #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
5
+ #pragma GCC optimize ("O3")
6
+ #pragma GCC diagnostic warning "-Wall"
7
+ #elif defined(__clang__)
8
+ #pragma clang optimize on
9
+ #pragma clang diagnostic warning "-Wall"
10
+ #elif defined(__INTEL_COMPILER)
11
+ #pragma intel optimization_level 3
12
+ #endif
13
+
14
+ static struct sysinfo info ;
15
+
16
+ VALUE totalram(VALUE obj) {
17
+ char status = sysinfo(&info) ;
18
+ if (status < 0) return Qnil ;
19
+
20
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.totalram) ;
21
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
22
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
23
+ }
24
+
25
+ VALUE freeram(VALUE obj) {
26
+ char status = sysinfo(&info) ;
27
+ if (status < 0) return Qnil ;
28
+
29
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.freeram) ;
30
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
31
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
32
+ }
33
+
34
+ VALUE sharedram(VALUE obj) {
35
+ char status = sysinfo(&info) ;
36
+ if (status < 0) return Qnil ;
37
+
38
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.sharedram) ;
39
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
40
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
41
+ }
42
+
43
+ VALUE bufferram(VALUE obj) {
44
+ char status = sysinfo(&info) ;
45
+ if (status < 0) return Qnil ;
46
+
47
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.bufferram) ;
48
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
49
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
50
+ }
51
+
52
+ VALUE totalswap(VALUE obj) {
53
+ static struct sysinfo info ;
54
+ char status = sysinfo(&info) ;
55
+ if (status < 0) return Qnil ;
56
+
57
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.totalswap) ;
58
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
59
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
60
+ }
61
+
62
+ VALUE freeswap(VALUE obj) {
63
+ char status = sysinfo(&info) ;
64
+ if (status < 0) return Qnil ;
65
+
66
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.freeswap) ;
67
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
68
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
69
+ }
70
+
71
+ VALUE totalhigh(VALUE obj) {
72
+ char status = sysinfo(&info) ;
73
+ if (status < 0) return Qnil ;
74
+
75
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.totalhigh) ;
76
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
77
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
78
+ }
79
+
80
+ VALUE freehigh(VALUE obj) {
81
+ char status = sysinfo(&info) ;
82
+ if (status < 0) return Qnil ;
83
+
84
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.freehigh) ;
85
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
86
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
87
+ }
88
+
89
+ VALUE uptime(VALUE obj) {
90
+ char status = sysinfo(&info) ;
91
+ if (status < 0) return Qnil ;
92
+
93
+ unsigned long long v = info.uptime ;
94
+ return ULL2NUM((unsigned long long) v) ;
95
+ }
96
+
97
+ VALUE loads(VALUE obj) {
98
+ char status = sysinfo(&info) ;
99
+ if(status < 0) return rb_ary_new() ;
100
+
101
+ long double load = 1.f / (1 << SI_LOAD_SHIFT) ;
102
+
103
+ float l_1 = info.loads[0] * load ;
104
+ float l_5 = info.loads[1] * load ;
105
+ float l_15 = info.loads[2] * load ;
106
+
107
+ return rb_ary_new_from_args(3,
108
+ rb_float_new(l_1),
109
+ rb_float_new(l_5),
110
+ rb_float_new(l_15)
111
+ ) ;
112
+ }
113
+
114
+ // Some people may need this function, just keep it to not make unnecessary calls
115
+ VALUE sysinfoStat(VALUE obj) {
116
+ char status = sysinfo(&info) ;
117
+ VALUE hash = rb_hash_new() ;
118
+ if (status < 0) return hash ;
119
+
120
+ unsigned long long mem_unit = info.mem_unit ;
121
+ VALUE _rb_mem_unit = ULL2NUM(mem_unit) ;
122
+
123
+ unsigned long long _totalram = info.totalram ;
124
+ unsigned long long _freeram = info.freeram ;
125
+ unsigned long long _sharedram = info.sharedram ;
126
+ unsigned long long _bufferram = info.bufferram ;
127
+ unsigned long long _totalswap = info.totalswap ;
128
+ unsigned long long _freeswap = info.freeswap ;
129
+ unsigned long long _totalhigh = info.totalhigh ;
130
+ unsigned long long _freehigh = info.freehigh ;
131
+ unsigned long long _uptime = info.uptime ;
132
+
133
+ long double load = 1.f / (1 << SI_LOAD_SHIFT) ;
134
+
135
+ float l_1 = info.loads[0] * load ;
136
+ float l_5 = info.loads[1] * load ;
137
+ float l_15 = info.loads[2] * load ;
138
+
139
+ VALUE loads = rb_ary_new_from_args(3,
140
+ rb_float_new(l_1),
141
+ rb_float_new(l_5),
142
+ rb_float_new(l_15)
143
+ ) ;
144
+
145
+ VALUE mul = rb_intern("*") ;
146
+
147
+ rb_hash_aset(hash, ID2SYM(rb_intern("totalram")), rb_funcallv_public(ULL2NUM(_totalram), mul, 1, &_rb_mem_unit)) ;
148
+ rb_hash_aset(hash, ID2SYM(rb_intern("freeram")), rb_funcallv_public(ULL2NUM(_freeram), mul, 1, &_rb_mem_unit)) ;
149
+ rb_hash_aset(hash, ID2SYM(rb_intern("sharedram")), rb_funcallv_public(ULL2NUM(_sharedram), mul, 1, &_rb_mem_unit)) ;
150
+ rb_hash_aset(hash, ID2SYM(rb_intern("bufferram")), rb_funcallv_public(ULL2NUM(_bufferram), mul, 1, &_rb_mem_unit)) ;
151
+ rb_hash_aset(hash, ID2SYM(rb_intern("totalswap")), rb_funcallv_public(ULL2NUM(_totalswap), mul, 1, &_rb_mem_unit)) ;
152
+ rb_hash_aset(hash, ID2SYM(rb_intern("freeswap")), rb_funcallv_public(ULL2NUM(_freeswap), mul, 1, &_rb_mem_unit)) ;
153
+ rb_hash_aset(hash, ID2SYM(rb_intern("totalhigh")), rb_funcallv_public(ULL2NUM(_totalhigh), mul, 1, &_rb_mem_unit)) ;
154
+ rb_hash_aset(hash, ID2SYM(rb_intern("freehigh")), rb_funcallv_public(ULL2NUM(_freehigh), mul, 1, &_rb_mem_unit)) ;
155
+ rb_hash_aset(hash, ID2SYM(rb_intern("uptime")), rb_funcallv_public(ULL2NUM(_uptime), mul, 1, &_rb_mem_unit)) ;
156
+
157
+ rb_hash_aset(hash, ID2SYM(rb_intern("loads")), loads) ;
158
+
159
+ return hash ;
160
+ }
161
+
162
+ void Init_sysinfo() {
163
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
164
+ VALUE _sysinfo = rb_define_module_under(_linux_stat, "Sysinfo") ;
165
+
166
+ rb_define_module_function(_sysinfo, "totalram", totalram, 0) ;
167
+ rb_define_module_function(_sysinfo, "freeram", freeram, 0) ;
168
+ rb_define_module_function(_sysinfo, "sharedram", sharedram, 0) ;
169
+ rb_define_module_function(_sysinfo, "bufferram", bufferram, 0) ;
170
+ rb_define_module_function(_sysinfo, "totalswap", totalswap, 0) ;
171
+ rb_define_module_function(_sysinfo, "freeswap", freeswap, 0) ;
172
+ rb_define_module_function(_sysinfo, "totalhigh", totalhigh, 0) ;
173
+ rb_define_module_function(_sysinfo, "freehigh", freehigh, 0) ;
174
+ rb_define_module_function(_sysinfo, "uptime", uptime, 0) ;
175
+ rb_define_module_function(_sysinfo, "loads", loads, 0) ;
176
+ rb_define_module_function(_sysinfo, "stat", sysinfoStat, 0) ;
177
+ }
@@ -17,7 +17,7 @@ static char *sysname = "", *nodename = "" ;
17
17
  static char *release = "", *version = "", *machine = "" ;
18
18
 
19
19
  void init_buf() {
20
- short status = uname(&buf) ;
20
+ char status = uname(&buf) ;
21
21
 
22
22
  if (status > -1) {
23
23
  sysname = buf.sysname ;
@@ -23,19 +23,23 @@ require "linux_stat/version"
23
23
  # But might be required by other module functions in "Dependent Modules" section
24
24
  require "linux_stat/battery"
25
25
  require "linux_stat/bios"
26
- require "linux_stat/memory"
27
26
  require "linux_stat/net"
28
27
  require "linux_stat/pci"
29
28
  require "linux_stat/process"
30
- require "linux_stat/swap"
29
+ require "linux_stat/thermal"
31
30
  require "linux_stat/usb"
32
31
 
33
32
  # Dependent Modules
34
33
  # Modules that can have reverse dependency
35
34
 
35
+ # LinuxStat::CPU.sysinfo dependent modules
36
+ require "linux_stat/sysinfo"
37
+ require "linux_stat/swap"
38
+ require "linux_stat/memory"
39
+
36
40
  # LinuxStat::CPU.nproc dependent modules
37
- require "linux_stat/cpu"
38
41
  require "linux_stat/nproc"
42
+ require "linux_stat/cpu"
39
43
 
40
44
  # LinuxStat::Uname dependent modules
41
45
  require 'linux_stat/utsname'