linux_stat 1.5.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5da13d97b132a58afb5d56337e40f5a234cf129612a14886e931b5d01d2232d3
4
- data.tar.gz: dee75b59d2a8e2ab3f55e19c7e117869149d2c318fbc12e6e8a39c3d9433d418
3
+ metadata.gz: 2204620ae409ee97e9a3fb737bb2442ce2f60e2f8b1fe4473c31c441a3ae19ac
4
+ data.tar.gz: f04b438fc002122865f56597bb39acdfc52fd19f25bf628a4db3a3f4afb09830
5
5
  SHA512:
6
- metadata.gz: 1885696170e580ac960730d82dce2e73ca8b924211b3ee1d74c3cdde362fdaebf6fcc08a7fd49b444729186e65c68b0c15767c9baca050d0853b0460ae1ca989
7
- data.tar.gz: 8ec56355b4774c830282ca5ecb23dc3318ccc8fabd748a20af4bb0535bec6e3f0da9a3d0232ce09f58e1bf43cb9cc5917e970633dc19cc7c4b0cc9ed786df492
6
+ metadata.gz: 3fec213db7635a5763bd6082da8cbe429b0c3bb2f227e242e9cf33da59bb381dc5f008edd1bf63da576c8ad4b39fe8f56f97efb9f4ff3a4ce9f8760253afc97f
7
+ data.tar.gz: 6de48af0e03a1a91166d4cc5f438f38cc0662a9d4cbc948d663591abfcf078ddd751f5f9064362448ef76252d38eb85258a4abaa20ac88c07a6dbf28b23b762b
@@ -138,7 +138,8 @@ execute.sort.each do |c|
138
138
  param << "#{p[1] || 'arg'}#{_arg}, "
139
139
  end
140
140
  end
141
- param.delete_suffix!(", ")
141
+
142
+ param.chomp!(", ")
142
143
 
143
144
  disp_meth = "#{meth}"
144
145
  disp_meth.concat(arg ? "(#{param})" : "(#{param})")
@@ -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
 
@@ -1,5 +1,4 @@
1
1
  #include <unistd.h>
2
- #include <inttypes.h>
3
2
  #include "ruby.h"
4
3
 
5
4
  #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
@@ -13,94 +12,94 @@
13
12
  #endif
14
13
 
15
14
  static VALUE getTick(VALUE obj) {
16
- int16_t val = sysconf(_SC_CLK_TCK) ;
15
+ int val = sysconf(_SC_CLK_TCK) ;
17
16
  if (val < 0) return Qnil ;
18
17
 
19
18
  return INT2FIX(val) ;
20
19
  }
21
20
 
22
21
  static VALUE getChildMax(VALUE obj) {
23
- int64_t val = sysconf(_SC_CHILD_MAX) ;
22
+ long long int val = sysconf(_SC_CHILD_MAX) ;
24
23
  if (val < 0) return Qnil ;
25
24
 
26
- return INT2NUM(val) ;
25
+ return LL2NUM(val) ;
27
26
  }
28
27
 
29
28
  static VALUE getHostnameMax(VALUE obj) {
30
- int32_t val = sysconf(_SC_HOST_NAME_MAX) ;
29
+ long long val = sysconf(_SC_HOST_NAME_MAX) ;
31
30
  if (val < 0) return Qnil ;
32
31
 
33
- return INT2NUM(val) ;
32
+ return LL2NUM(val) ;
34
33
  }
35
34
 
36
35
  static VALUE getLoginNameMax(VALUE obj) {
37
- int32_t val = sysconf(_SC_LOGIN_NAME_MAX) ;
36
+ long long val = sysconf(_SC_LOGIN_NAME_MAX) ;
38
37
  if (val < 0) return Qnil ;
39
38
 
40
- return INT2NUM(val) ;
39
+ return LL2NUM(val) ;
41
40
  }
42
41
 
43
42
  static VALUE getOpenMax(VALUE obj) {
44
- int64_t val = sysconf(_SC_OPEN_MAX) ;
43
+ long long val = sysconf(_SC_OPEN_MAX) ;
45
44
  if (val < 0) return Qnil ;
46
45
 
47
- return INT2NUM(val) ;
46
+ return LL2NUM(val) ;
48
47
  }
49
48
 
50
49
  static VALUE getPageSize(VALUE obj) {
51
- int32_t val = sysconf(_SC_PAGESIZE) ;
50
+ int val = sysconf(_SC_PAGESIZE) ;
52
51
  if (val < 0) return Qnil ;
53
52
 
54
- return INT2NUM(val) ;
53
+ return INT2FIX(val) ;
55
54
  }
56
55
 
57
56
  static VALUE getStreamMax(VALUE obj) {
58
- int64_t val = sysconf(_SC_STREAM_MAX) ;
57
+ long long val = sysconf(_SC_STREAM_MAX) ;
59
58
  if (val < 0) return Qnil ;
60
59
 
61
- return INT2NUM(val) ;
60
+ return LL2NUM(val) ;
62
61
  }
63
62
 
64
63
  static VALUE getTTYNameMax(VALUE obj) {
65
- int32_t val = sysconf(_SC_TTY_NAME_MAX) ;
64
+ long long val = sysconf(_SC_TTY_NAME_MAX) ;
66
65
  if (val < 0) return Qnil ;
67
66
 
68
- return INT2NUM(val) ;
67
+ return LL2NUM(val) ;
69
68
  }
70
69
 
71
70
  static VALUE getPosixVersion(VALUE obj) {
72
- int32_t val = sysconf(_SC_VERSION) ;
71
+ long long val = sysconf(_SC_VERSION) ;
73
72
  if (val < 0) return Qnil ;
74
73
 
75
- return INT2NUM(val) ;
74
+ return LL2NUM(val) ;
76
75
  }
77
76
 
78
77
  static VALUE getLineMax(VALUE obj) {
79
- int32_t val = sysconf(_SC_LINE_MAX) ;
78
+ long long val = sysconf(_SC_LINE_MAX) ;
80
79
  if (val < 0) return Qnil ;
81
80
 
82
- return INT2NUM(val) ;
81
+ return LL2NUM(val) ;
83
82
  }
84
83
 
85
84
  static VALUE getExprNestMax(VALUE obj) {
86
- int32_t val = sysconf(_SC_EXPR_NEST_MAX) ;
85
+ long long val = sysconf(_SC_EXPR_NEST_MAX) ;
87
86
  if (val < 0) return Qnil ;
88
87
 
89
- return INT2NUM(val) ;
88
+ return LL2NUM(val) ;
90
89
  }
91
90
 
92
91
  static VALUE getProcessorConfigured(VALUE obj) {
93
- int32_t val = sysconf(_SC_NPROCESSORS_CONF) ;
92
+ long val = sysconf(_SC_NPROCESSORS_CONF) ;
94
93
  if (val < 0) return Qnil ;
95
94
 
96
- return INT2NUM(val) ;
95
+ return LONG2NUM(val) ;
97
96
  }
98
97
 
99
98
  static VALUE getProcessorOnline(VALUE obj) {
100
- int32_t val = sysconf(_SC_NPROCESSORS_ONLN) ;
99
+ long val = sysconf(_SC_NPROCESSORS_ONLN) ;
101
100
  if (val < 0) return Qnil ;
102
101
 
103
- return INT2NUM(val) ;
102
+ return LONG2NUM(val) ;
104
103
  }
105
104
 
106
105
  static VALUE getUser(VALUE obj) {
@@ -109,22 +108,22 @@ static VALUE getUser(VALUE obj) {
109
108
  }
110
109
 
111
110
  static VALUE getUID(VALUE obj) {
112
- return INT2NUM(getuid()) ;
111
+ return UINT2NUM((unsigned int) getuid()) ;
113
112
  }
114
113
 
115
114
  static VALUE getGID(VALUE obj) {
116
- return INT2NUM(getgid()) ;
115
+ return UINT2NUM((unsigned int) getgid()) ;
117
116
  }
118
117
 
119
118
  static VALUE getEUID(VALUE obj) {
120
- return INT2NUM(geteuid()) ;
119
+ return UINT2NUM((unsigned int) geteuid()) ;
121
120
  }
122
121
 
123
122
  static VALUE getHostname(VALUE obj) {
124
123
  int h_max = sysconf(_SC_HOST_NAME_MAX) + 1 ;
125
124
  char hostname[h_max] ;
126
125
 
127
- short int status = gethostname(hostname, h_max) ;
126
+ char status = gethostname(hostname, h_max) ;
128
127
 
129
128
  return (status < 0) ? rb_str_new_cstr("") : rb_str_new_cstr(hostname) ;
130
129
  }
@@ -1,5 +1,4 @@
1
1
  #include <sys/sysinfo.h>
2
- #include <inttypes.h>
3
2
  #include "ruby.h"
4
3
 
5
4
  #if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
@@ -15,8 +14,7 @@
15
14
  static struct sysinfo info ;
16
15
 
17
16
  VALUE totalram(VALUE obj) {
18
- static struct sysinfo info ;
19
- short status = sysinfo(&info) ;
17
+ char status = sysinfo(&info) ;
20
18
  if (status < 0) return Qnil ;
21
19
 
22
20
  VALUE _rb_v = ULL2NUM((unsigned long long) info.totalram) ;
@@ -25,7 +23,7 @@ VALUE totalram(VALUE obj) {
25
23
  }
26
24
 
27
25
  VALUE freeram(VALUE obj) {
28
- short status = sysinfo(&info) ;
26
+ char status = sysinfo(&info) ;
29
27
  if (status < 0) return Qnil ;
30
28
 
31
29
  VALUE _rb_v = ULL2NUM((unsigned long long) info.freeram) ;
@@ -34,7 +32,7 @@ VALUE freeram(VALUE obj) {
34
32
  }
35
33
 
36
34
  VALUE sharedram(VALUE obj) {
37
- short status = sysinfo(&info) ;
35
+ char status = sysinfo(&info) ;
38
36
  if (status < 0) return Qnil ;
39
37
 
40
38
  VALUE _rb_v = ULL2NUM((unsigned long long) info.sharedram) ;
@@ -43,7 +41,7 @@ VALUE sharedram(VALUE obj) {
43
41
  }
44
42
 
45
43
  VALUE bufferram(VALUE obj) {
46
- short status = sysinfo(&info) ;
44
+ char status = sysinfo(&info) ;
47
45
  if (status < 0) return Qnil ;
48
46
 
49
47
  VALUE _rb_v = ULL2NUM((unsigned long long) info.bufferram) ;
@@ -53,7 +51,7 @@ VALUE bufferram(VALUE obj) {
53
51
 
54
52
  VALUE totalswap(VALUE obj) {
55
53
  static struct sysinfo info ;
56
- short status = sysinfo(&info) ;
54
+ char status = sysinfo(&info) ;
57
55
  if (status < 0) return Qnil ;
58
56
 
59
57
  VALUE _rb_v = ULL2NUM((unsigned long long) info.totalswap) ;
@@ -62,7 +60,7 @@ VALUE totalswap(VALUE obj) {
62
60
  }
63
61
 
64
62
  VALUE freeswap(VALUE obj) {
65
- short status = sysinfo(&info) ;
63
+ char status = sysinfo(&info) ;
66
64
  if (status < 0) return Qnil ;
67
65
 
68
66
  VALUE _rb_v = ULL2NUM((unsigned long long) info.freeswap) ;
@@ -71,7 +69,7 @@ VALUE freeswap(VALUE obj) {
71
69
  }
72
70
 
73
71
  VALUE totalhigh(VALUE obj) {
74
- short status = sysinfo(&info) ;
72
+ char status = sysinfo(&info) ;
75
73
  if (status < 0) return Qnil ;
76
74
 
77
75
  VALUE _rb_v = ULL2NUM((unsigned long long) info.totalhigh) ;
@@ -80,7 +78,7 @@ VALUE totalhigh(VALUE obj) {
80
78
  }
81
79
 
82
80
  VALUE freehigh(VALUE obj) {
83
- short status = sysinfo(&info) ;
81
+ char status = sysinfo(&info) ;
84
82
  if (status < 0) return Qnil ;
85
83
 
86
84
  VALUE _rb_v = ULL2NUM((unsigned long long) info.freehigh) ;
@@ -89,15 +87,15 @@ VALUE freehigh(VALUE obj) {
89
87
  }
90
88
 
91
89
  VALUE uptime(VALUE obj) {
92
- short status = sysinfo(&info) ;
90
+ char status = sysinfo(&info) ;
93
91
  if (status < 0) return Qnil ;
94
92
 
95
- uint64_t v = info.uptime ;
93
+ unsigned long long v = info.uptime ;
96
94
  return ULL2NUM((unsigned long long) v) ;
97
95
  }
98
96
 
99
97
  VALUE loads(VALUE obj) {
100
- short status = sysinfo(&info) ;
98
+ char status = sysinfo(&info) ;
101
99
  if(status < 0) return rb_ary_new() ;
102
100
 
103
101
  long double load = 1.f / (1 << SI_LOAD_SHIFT) ;
@@ -113,6 +111,54 @@ VALUE loads(VALUE obj) {
113
111
  ) ;
114
112
  }
115
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
+
116
162
  void Init_sysinfo() {
117
163
  VALUE _linux_stat = rb_define_module("LinuxStat") ;
118
164
  VALUE _sysinfo = rb_define_module_under(_linux_stat, "Sysinfo") ;
@@ -127,4 +173,5 @@ void Init_sysinfo() {
127
173
  rb_define_module_function(_sysinfo, "freehigh", freehigh, 0) ;
128
174
  rb_define_module_function(_sysinfo, "uptime", uptime, 0) ;
129
175
  rb_define_module_function(_sysinfo, "loads", loads, 0) ;
176
+ rb_define_module_function(_sysinfo, "stat", sysinfoStat, 0) ;
130
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 ;
@@ -140,6 +140,8 @@ module LinuxStat
140
140
  #
141
141
  # => {:hour=>10, :minute=>34, :second=>12.59}
142
142
  #
143
+ # Using uptime is 10x slower than using uptime_i
144
+ #
143
145
  # If the stat isn't available, an empty hash is returned.
144
146
  def uptime
145
147
  return {} unless uptime_readable?
@@ -158,11 +160,27 @@ module LinuxStat
158
160
  }.freeze
159
161
  end
160
162
 
163
+ ##
164
+ # Returns Float uptime of the system reported by /proc/uptime
165
+ # LinuxStat::OS.uptime_f
166
+ #
167
+ # => 28956.34
168
+ #
169
+ # The value is generally rounded to 2 decimal places.
170
+ #
171
+ # Using uptime_f is 10x slower than using uptime_i
172
+ #
173
+ # If the stat isn't available, nil is returned.
174
+ def uptime_f
175
+ return nil unless uptime_readable?
176
+ IO.foreach('/proc/uptime'.freeze, ' ').next.to_f
177
+ end
178
+
161
179
  ##
162
180
  # Returns uptime of the system reported by LinuxStat::Sysinfo.uptime()
163
181
  # LinuxStat::OS.uptime_i
164
182
  #
165
- # 28956
183
+ # => 28956
166
184
  #
167
185
  # If the stat isn't available, nil is returned.
168
186
  def uptime_i
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION ||= "1.5.1"
2
+ VERSION ||= "1.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-22 00:00:00.000000000 Z
11
+ date: 2021-01-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -71,7 +71,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - ">="
73
73
  - !ruby/object:Gem::Version
74
- version: 2.5.0
74
+ version: 2.4.0
75
75
  required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - ">="