linux_stat 1.2.2 → 1.5.1

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,14 @@ 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
-
131
141
  param.delete_suffix!(", ")
132
142
 
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
-
143
143
  disp_meth = "#{meth}"
144
- disp_meth.concat(arg ? "(#{param} = #{arg.inspect})" : "(#{param})")
144
+ disp_meth.concat(arg ? "(#{param})" : "(#{param})")
145
145
 
146
146
  time = Time.now
147
147
  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
  }
@@ -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
 
@@ -1,4 +1,5 @@
1
1
  #include <unistd.h>
2
+ #include <inttypes.h>
2
3
  #include "ruby.h"
3
4
 
4
5
  #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
@@ -12,94 +13,94 @@
12
13
  #endif
13
14
 
14
15
  static VALUE getTick(VALUE obj) {
15
- short int val = sysconf(_SC_CLK_TCK) ;
16
+ int16_t val = sysconf(_SC_CLK_TCK) ;
16
17
  if (val < 0) return Qnil ;
17
18
 
18
19
  return INT2FIX(val) ;
19
20
  }
20
21
 
21
22
  static VALUE getChildMax(VALUE obj) {
22
- long long int val = sysconf(_SC_CHILD_MAX) ;
23
+ int64_t val = sysconf(_SC_CHILD_MAX) ;
23
24
  if (val < 0) return Qnil ;
24
25
 
25
- return INT2FIX(val) ;
26
+ return INT2NUM(val) ;
26
27
  }
27
28
 
28
29
  static VALUE getHostnameMax(VALUE obj) {
29
- int val = sysconf(_SC_HOST_NAME_MAX) ;
30
+ int32_t val = sysconf(_SC_HOST_NAME_MAX) ;
30
31
  if (val < 0) return Qnil ;
31
32
 
32
- return INT2FIX(val) ;
33
+ return INT2NUM(val) ;
33
34
  }
34
35
 
35
36
  static VALUE getLoginNameMax(VALUE obj) {
36
- int val = sysconf(_SC_LOGIN_NAME_MAX) ;
37
+ int32_t val = sysconf(_SC_LOGIN_NAME_MAX) ;
37
38
  if (val < 0) return Qnil ;
38
39
 
39
- return INT2FIX(val) ;
40
+ return INT2NUM(val) ;
40
41
  }
41
42
 
42
43
  static VALUE getOpenMax(VALUE obj) {
43
- int val = sysconf(_SC_OPEN_MAX) ;
44
+ int64_t val = sysconf(_SC_OPEN_MAX) ;
44
45
  if (val < 0) return Qnil ;
45
46
 
46
- return INT2FIX(val) ;
47
+ return INT2NUM(val) ;
47
48
  }
48
49
 
49
50
  static VALUE getPageSize(VALUE obj) {
50
- short int val = sysconf(_SC_PAGESIZE) ;
51
+ int32_t val = sysconf(_SC_PAGESIZE) ;
51
52
  if (val < 0) return Qnil ;
52
53
 
53
- return INT2FIX(val) ;
54
+ return INT2NUM(val) ;
54
55
  }
55
56
 
56
57
  static VALUE getStreamMax(VALUE obj) {
57
- int val = sysconf(_SC_STREAM_MAX) ;
58
+ int64_t val = sysconf(_SC_STREAM_MAX) ;
58
59
  if (val < 0) return Qnil ;
59
60
 
60
- return INT2FIX(val) ;
61
+ return INT2NUM(val) ;
61
62
  }
62
63
 
63
64
  static VALUE getTTYNameMax(VALUE obj) {
64
- int val = sysconf(_SC_TTY_NAME_MAX) ;
65
+ int32_t val = sysconf(_SC_TTY_NAME_MAX) ;
65
66
  if (val < 0) return Qnil ;
66
67
 
67
- return INT2FIX(val) ;
68
+ return INT2NUM(val) ;
68
69
  }
69
70
 
70
71
  static VALUE getPosixVersion(VALUE obj) {
71
- int val = sysconf(_SC_VERSION) ;
72
+ int32_t val = sysconf(_SC_VERSION) ;
72
73
  if (val < 0) return Qnil ;
73
74
 
74
- return INT2FIX(val) ;
75
+ return INT2NUM(val) ;
75
76
  }
76
77
 
77
78
  static VALUE getLineMax(VALUE obj) {
78
- int val = sysconf(_SC_LINE_MAX) ;
79
+ int32_t val = sysconf(_SC_LINE_MAX) ;
79
80
  if (val < 0) return Qnil ;
80
81
 
81
- return INT2FIX(val) ;
82
+ return INT2NUM(val) ;
82
83
  }
83
84
 
84
85
  static VALUE getExprNestMax(VALUE obj) {
85
- int val = sysconf(_SC_EXPR_NEST_MAX) ;
86
+ int32_t val = sysconf(_SC_EXPR_NEST_MAX) ;
86
87
  if (val < 0) return Qnil ;
87
88
 
88
- return INT2FIX(val) ;
89
+ return INT2NUM(val) ;
89
90
  }
90
91
 
91
92
  static VALUE getProcessorConfigured(VALUE obj) {
92
- short int val = sysconf(_SC_NPROCESSORS_CONF) ;
93
+ int32_t val = sysconf(_SC_NPROCESSORS_CONF) ;
93
94
  if (val < 0) return Qnil ;
94
95
 
95
- return INT2FIX(val) ;
96
+ return INT2NUM(val) ;
96
97
  }
97
98
 
98
99
  static VALUE getProcessorOnline(VALUE obj) {
99
- short int val = sysconf(_SC_NPROCESSORS_ONLN) ;
100
+ int32_t val = sysconf(_SC_NPROCESSORS_ONLN) ;
100
101
  if (val < 0) return Qnil ;
101
102
 
102
- return INT2FIX(val) ;
103
+ return INT2NUM(val) ;
103
104
  }
104
105
 
105
106
  static VALUE getUser(VALUE obj) {
@@ -108,15 +109,15 @@ static VALUE getUser(VALUE obj) {
108
109
  }
109
110
 
110
111
  static VALUE getUID(VALUE obj) {
111
- return INT2FIX(getuid()) ;
112
+ return INT2NUM(getuid()) ;
112
113
  }
113
114
 
114
115
  static VALUE getGID(VALUE obj) {
115
- return INT2FIX(getgid()) ;
116
+ return INT2NUM(getgid()) ;
116
117
  }
117
118
 
118
119
  static VALUE getEUID(VALUE obj) {
119
- return INT2FIX(geteuid()) ;
120
+ return INT2NUM(geteuid()) ;
120
121
  }
121
122
 
122
123
  static VALUE getHostname(VALUE obj) {
@@ -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,130 @@
1
+ #include <sys/sysinfo.h>
2
+ #include <inttypes.h>
3
+ #include "ruby.h"
4
+
5
+ #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
6
+ #pragma GCC optimize ("O3")
7
+ #pragma GCC diagnostic warning "-Wall"
8
+ #elif defined(__clang__)
9
+ #pragma clang optimize on
10
+ #pragma clang diagnostic warning "-Wall"
11
+ #elif defined(__INTEL_COMPILER)
12
+ #pragma intel optimization_level 3
13
+ #endif
14
+
15
+ static struct sysinfo info ;
16
+
17
+ VALUE totalram(VALUE obj) {
18
+ static struct sysinfo info ;
19
+ short status = sysinfo(&info) ;
20
+ if (status < 0) return Qnil ;
21
+
22
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.totalram) ;
23
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
24
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
25
+ }
26
+
27
+ VALUE freeram(VALUE obj) {
28
+ short status = sysinfo(&info) ;
29
+ if (status < 0) return Qnil ;
30
+
31
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.freeram) ;
32
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
33
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
34
+ }
35
+
36
+ VALUE sharedram(VALUE obj) {
37
+ short status = sysinfo(&info) ;
38
+ if (status < 0) return Qnil ;
39
+
40
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.sharedram) ;
41
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
42
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
43
+ }
44
+
45
+ VALUE bufferram(VALUE obj) {
46
+ short status = sysinfo(&info) ;
47
+ if (status < 0) return Qnil ;
48
+
49
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.bufferram) ;
50
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
51
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
52
+ }
53
+
54
+ VALUE totalswap(VALUE obj) {
55
+ static struct sysinfo info ;
56
+ short status = sysinfo(&info) ;
57
+ if (status < 0) return Qnil ;
58
+
59
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.totalswap) ;
60
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
61
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
62
+ }
63
+
64
+ VALUE freeswap(VALUE obj) {
65
+ short status = sysinfo(&info) ;
66
+ if (status < 0) return Qnil ;
67
+
68
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.freeswap) ;
69
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
70
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
71
+ }
72
+
73
+ VALUE totalhigh(VALUE obj) {
74
+ short status = sysinfo(&info) ;
75
+ if (status < 0) return Qnil ;
76
+
77
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.totalhigh) ;
78
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
79
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
80
+ }
81
+
82
+ VALUE freehigh(VALUE obj) {
83
+ short status = sysinfo(&info) ;
84
+ if (status < 0) return Qnil ;
85
+
86
+ VALUE _rb_v = ULL2NUM((unsigned long long) info.freehigh) ;
87
+ VALUE _rb_mem_unit = ULL2NUM((unsigned long long) info.mem_unit) ;
88
+ return rb_funcallv_public(_rb_v, rb_intern("*"), 1, &_rb_mem_unit) ;
89
+ }
90
+
91
+ VALUE uptime(VALUE obj) {
92
+ short status = sysinfo(&info) ;
93
+ if (status < 0) return Qnil ;
94
+
95
+ uint64_t v = info.uptime ;
96
+ return ULL2NUM((unsigned long long) v) ;
97
+ }
98
+
99
+ VALUE loads(VALUE obj) {
100
+ short status = sysinfo(&info) ;
101
+ if(status < 0) return rb_ary_new() ;
102
+
103
+ long double load = 1.f / (1 << SI_LOAD_SHIFT) ;
104
+
105
+ float l_1 = info.loads[0] * load ;
106
+ float l_5 = info.loads[1] * load ;
107
+ float l_15 = info.loads[2] * load ;
108
+
109
+ return rb_ary_new_from_args(3,
110
+ rb_float_new(l_1),
111
+ rb_float_new(l_5),
112
+ rb_float_new(l_15)
113
+ ) ;
114
+ }
115
+
116
+ void Init_sysinfo() {
117
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
118
+ VALUE _sysinfo = rb_define_module_under(_linux_stat, "Sysinfo") ;
119
+
120
+ rb_define_module_function(_sysinfo, "totalram", totalram, 0) ;
121
+ rb_define_module_function(_sysinfo, "freeram", freeram, 0) ;
122
+ rb_define_module_function(_sysinfo, "sharedram", sharedram, 0) ;
123
+ rb_define_module_function(_sysinfo, "bufferram", bufferram, 0) ;
124
+ rb_define_module_function(_sysinfo, "totalswap", totalswap, 0) ;
125
+ rb_define_module_function(_sysinfo, "freeswap", freeswap, 0) ;
126
+ rb_define_module_function(_sysinfo, "totalhigh", totalhigh, 0) ;
127
+ rb_define_module_function(_sysinfo, "freehigh", freehigh, 0) ;
128
+ rb_define_module_function(_sysinfo, "uptime", uptime, 0) ;
129
+ rb_define_module_function(_sysinfo, "loads", loads, 0) ;
130
+ }
@@ -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'
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various battery related information of the current system.
3
+
2
4
  module Battery
3
5
  PATH = "/sys/class/power_supply/BAT0".freeze
4
6
 
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various BIOS related information of the current system.
3
+
2
4
  module BIOS
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various CPU related information of the current system.
3
+
2
4
  module CPU
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Filesystem related information of the current system.
3
+
2
4
  module Filesystem
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Kernel related information of the current system.
3
+
2
4
  module Kernel
3
5
  class << self
4
6
  ##
@@ -1,4 +1,6 @@
1
1
  module LinuxStat
2
+ # Shows various Memory related information of the current system.
3
+
2
4
  module Memory
3
5
  class << self
4
6
  ##
@@ -11,7 +13,7 @@ module LinuxStat
11
13
  def stat
12
14
  return {} unless meminfo?
13
15
 
14
- memory = IO.foreach('/proc/meminfo').first(3)
16
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
15
17
 
16
18
  total = memory[0].split[1].to_i
17
19
  available = memory[2].split[1].to_i
@@ -33,13 +35,23 @@ module LinuxStat
33
35
  end
34
36
 
35
37
  ##
36
- # Returns the total memory details reported by /proc/meminfo.
38
+ # Returns the total memory reported by LS::Sysinfo.totalram()
37
39
  # The value is in Kilobyte.
38
40
  #
39
41
  # It retuns an Integer but if the info is not available, it will return nil.
40
42
  def total
41
- return nil unless meminfo?
42
- IO.foreach('/proc/meminfo').first.split[1].to_i
43
+ v = LinuxStat::Sysinfo.totalram
44
+ v ? v.fdiv(1024).to_i : nil
45
+ end
46
+
47
+ ##
48
+ # Returns the free memory reported by LS::Sysinfo.freeram()
49
+ # The value is in Kilobyte.
50
+ #
51
+ # It retuns an Integer but if the info is not available, it will return nil.
52
+ def free
53
+ v = LinuxStat::Sysinfo.freeram
54
+ v ? v.fdiv(1024).to_i : nil
43
55
  end
44
56
 
45
57
  ##
@@ -49,7 +61,7 @@ module LinuxStat
49
61
  # It retuns an Integer but if the info is not available, it will return nil
50
62
  def available
51
63
  return nil unless meminfo?
52
- IO.foreach('/proc/meminfo').first(3)[-1].split[1].to_i
64
+ IO.foreach('/proc/meminfo'.freeze).first(3)[-1].split[1].to_i
53
65
  end
54
66
 
55
67
  ##
@@ -59,7 +71,7 @@ module LinuxStat
59
71
  # It retuns an Integer but if the info is not available, it will return nil.
60
72
  def used
61
73
  return nil unless meminfo?
62
- memory = IO.foreach('/proc/meminfo').first(3)
74
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
63
75
  memory[0].split[1].to_i - memory[2].split[1].to_i
64
76
  end
65
77
 
@@ -69,7 +81,7 @@ module LinuxStat
69
81
  # It retuns an Integer but if the info is not available, it will return nil
70
82
  def percent_used
71
83
  return nil unless meminfo?
72
- memory = IO.foreach('/proc/meminfo').first(3)
84
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
73
85
  total = memory[0].split[1].to_i
74
86
  total.-(memory[2].split[1].to_i).*(100).fdiv(total).round(2)
75
87
  end
@@ -80,13 +92,13 @@ module LinuxStat
80
92
  # It retuns an Integer but if the info is not available, it will return nil
81
93
  def percent_available
82
94
  return nil unless meminfo?
83
- memory = IO.foreach('/proc/meminfo').first(3)
95
+ memory = IO.foreach('/proc/meminfo'.freeze).first(3)
84
96
  memory[2].split[1].to_i.*(100).fdiv(memory[0].split[1].to_i).round(2)
85
97
  end
86
98
 
87
99
  private
88
100
  def meminfo?
89
- @@readable ||= File.readable?('/proc/meminfo')
101
+ @@readable ||= File.readable?('/proc/meminfo'.freeze)
90
102
  end
91
103
  end
92
104
  end