linux_stat 2.3.0 → 2.4.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: 2caab049f35d0dcad515eff80928136273b729219cc70a7c07f6823282f0a39c
4
- data.tar.gz: eb8a5a546f795116e825bd704da6380a81b197b61ffd4125d0dc2c267e6ea808
3
+ metadata.gz: 56eb9d01b537db6e6dab3a6ee86a391a1076f63cc612ff961e33f7ab8c703129
4
+ data.tar.gz: 30a3f6df0c51dfb14c28fcbdd7d2298d4f13fa48e15ef9ef63ab13e0fd980e68
5
5
  SHA512:
6
- metadata.gz: cd77811626f97fe248885dc2686447e584801d607fb2147200852ff3debc36e0fe37fe8a139a71fe67398ac3fde074c821b68428ccbb6a9cf4126385bcfa23df
7
- data.tar.gz: 89a2646c48f68d48e02bc5593bb4f616090c5e1c30045a05eb39a3d0b1c41412853ee09c600b11d4f1e59544bf49e5409fb1d2906d666290989683a707142358
6
+ metadata.gz: 1f24a5a7fb1d8f32a2c35c7cbc5a7b9bb380efb33a2df23e269ea4ebac565f866b6a399a0361a3f4f325bad13be1b545d59726efccabb4d8d6f8045ed03c3fc6
7
+ data.tar.gz: 81c1c316b49aaab37f4e300eb597db3a3bcb538549fc6192d9661e7ca5022bd5b6942a257ced51760e2065d7aca82ae465f55e79202ba7a5f8c9e917f53f505c
data/README.md CHANGED
@@ -38,18 +38,36 @@ It only works on Linux, and detecting the OS is upto the user of this gem.
38
38
  2. Ruby: Ruby 2.3.0 and above.
39
39
 
40
40
  ## Build Dependencies:
41
- + You need to have the C compiler installed to be able to compile the C extensions.
42
- On Arch Linux:
41
+ #### You need to have the C compiler installed to be able to compile the C extensions.
42
+
43
+ + On Arch Linux / Manjaro / Archlabs / Other Arch Based Distributions
44
+ ```
45
+ # pacman -S gcc make ruby
46
+ ```
47
+
48
+ + On Debian / Ubuntu / Linux Mint / Pop!_OS / Raspberry Pi OS / Other Debian Based Distributions
49
+ ```
50
+ # apt install gcc build-essential ruby ruby-dev build-essential
51
+ ```
52
+
53
+ + Gentoo / Gentoo Based Distributions
54
+ ```
55
+ # emerge --ask dev-lang/ruby
56
+ ```
57
+
58
+ + Fedora / AmazonLinux* / CentOS* / Other RedHat Based Distributions
43
59
  ```
44
- # pacman -S gcc make
60
+ # yum install gcc ruby-devel ruby make
45
61
  ```
46
62
 
47
- On Debian based systems:
63
+ You can run linux_stat on *AmazonLinux and *CentOS if you have Ruby 2.3.0+.
64
+
65
+ + OpenSUSE
48
66
  ```
49
- # apt install gcc build-essential ruby-dev
67
+ # zypper install gcc ruby ruby-devel make
50
68
  ```
51
69
 
52
- + You can remove the packages once the program is installed.
70
+ + You can remove the above packages once the gem is installed.
53
71
 
54
72
  ---
55
73
 
@@ -62,13 +80,28 @@ You can install hwdata simply.
62
80
  + Arch:
63
81
 
64
82
  ```
65
- sudo pacman -S hwids
83
+ # pacman -S hwids
66
84
  ```
67
85
 
68
86
  + Debian based systems:
69
87
 
70
88
  ```
71
- sudo apt install hwdata
89
+ # apt install hwdata
90
+ ```
91
+
92
+ + Gentoo / Gentoo Based Distributions
93
+ ```
94
+ # emerge --ask sys-apps/hwids
95
+ ```
96
+
97
+ + Fedora / Amazon Linux / CentOS
98
+ ```
99
+ # yum install hwdata
100
+ ```
101
+
102
+ + OpenSUSE
103
+ ```
104
+ zypper install hwdata
72
105
  ```
73
106
 
74
107
  But without hwdata, it won't show such information.
data/exe/linuxstat.rb CHANGED
@@ -266,6 +266,6 @@ if PRINT_TIME
266
266
  ).*(1_000_000)
267
267
 
268
268
  puts "\e[38;2;255;255;0m:: Warning total time also depends on your terminal speed!\e[0m"
269
- puts "Total Real Time: #{T_FMT % total_real_t}ms"
270
- puts "Total CPU Time: #{T_FMT % total_cpu_t}ms"
269
+ puts "Total Real Time: #{T_FMT % total_real_t}\u03BCs"
270
+ puts "Total CPU Time: #{T_FMT % total_cpu_t}\u03BCs"
271
271
  end
data/ext/procfs/procfs.c CHANGED
@@ -13,6 +13,7 @@
13
13
  #include <stdio.h>
14
14
  #include <unistd.h>
15
15
  #include <string.h>
16
+ #include <glob.h>
16
17
  #include "ruby.h"
17
18
  #include "uptime.h"
18
19
  #include "statm.h"
@@ -22,6 +23,8 @@
22
23
  int Init_procfs() {
23
24
  VALUE _linux_stat = rb_define_module("LinuxStat") ;
24
25
  VALUE _procfs = rb_define_module_under(_linux_stat, "ProcFS") ;
26
+ VALUE _cpu = rb_define_module_under(_linux_stat, "CPU") ;
27
+ VALUE _process = rb_define_module_under(_linux_stat, "Process") ;
25
28
 
26
29
  // uptime
27
30
  rb_define_module_function(_procfs, "uptime_f", uptime_f, 0) ;
@@ -40,4 +43,7 @@ int Init_procfs() {
40
43
  rb_define_module_function(_procfs, "ps_state", ps_state, 1) ;
41
44
  rb_define_module_function(_procfs, "ps_times", ps_times, 1) ;
42
45
  rb_define_module_function(_procfs, "ps_stat", ps_stat, 1) ;
46
+ rb_define_module_function(_procfs, "cpu_times", cpuTimes, 0) ;
47
+ rb_define_module_function(_procfs, "list_process", listProcess, 0) ;
48
+
43
49
  }
data/ext/procfs/stat.h CHANGED
@@ -17,6 +17,29 @@ static VALUE ps_state(volatile VALUE obj, volatile VALUE pid) {
17
17
  return rb_str_new_cstr(_s) ;
18
18
  }
19
19
 
20
+ static VALUE listProcess(volatile VALUE obj) {
21
+ VALUE ary = rb_ary_new() ;
22
+
23
+ glob_t globlist ;
24
+ int status = glob("/proc/[0-9]*/", GLOB_NOSORT, NULL, &globlist) ;
25
+
26
+ if (status == GLOB_NOSPACE || status == GLOB_ABORTED || status == GLOB_NOMATCH) {
27
+ globfree(&globlist) ;
28
+ return ary ;
29
+ }
30
+
31
+ char *v, *token ;
32
+ unsigned int i = 0 ;
33
+ unsigned int num ;
34
+
35
+ while(v = globlist.gl_pathv[i++]) {
36
+ if (sscanf(v, "/proc/%u", &num) == 1) rb_ary_push(ary, UINT2NUM(num)) ;
37
+ }
38
+
39
+ globfree(&globlist) ;
40
+ return ary ;
41
+ }
42
+
20
43
  static VALUE ps_times(volatile VALUE obj, volatile VALUE pid) {
21
44
  int _pid = FIX2INT(pid) ;
22
45
  if (_pid < 0) return Qnil ;
@@ -117,3 +140,46 @@ static VALUE ps_stat(volatile VALUE obj, volatile VALUE pid) {
117
140
  INT2NUM(exit_code)
118
141
  ) ;
119
142
  }
143
+
144
+ static VALUE cpuTimes(volatile VALUE obj) {
145
+ VALUE ary = rb_ary_new() ;
146
+ FILE *f = fopen("/proc/stat", "r") ;
147
+
148
+ if (!f) return ary ;
149
+
150
+ unsigned long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice ;
151
+ char line[1024] ;
152
+ char cpuCode[7] ;
153
+ float ticks = sysconf(_SC_CLK_TCK) ;
154
+ char scanStatus ;
155
+
156
+ while(fgets(line, 1023, f)) {
157
+ if (!(line[0] == 'c' && line[1] == 'p' && line[2] == 'u')) break ;
158
+
159
+ scanStatus = sscanf(line,
160
+ "%7[cpu0-9] %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
161
+ cpuCode, &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice
162
+ ) ;
163
+
164
+ if (scanStatus != 11) break ;
165
+
166
+ VALUE innerHash = rb_hash_new() ;
167
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("cpu")), rb_str_new_cstr(cpuCode)) ;
168
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("user")), rb_float_new(user / ticks)) ;
169
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("nice")), rb_float_new(nice / ticks)) ;
170
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("system")), rb_float_new(system / ticks)) ;
171
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("idle")), rb_float_new(idle / ticks)) ;
172
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("iowait")), rb_float_new(iowait / ticks)) ;
173
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("irq")), rb_float_new(irq / ticks)) ;
174
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("softirq")), rb_float_new(softirq / ticks)) ;
175
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("steal")), rb_float_new(steal / ticks)) ;
176
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("guest")), rb_float_new(guest / ticks)) ;
177
+ rb_hash_aset(innerHash, ID2SYM(rb_intern("guest_nice")), rb_float_new(guest_nice / ticks)) ;
178
+
179
+ rb_ary_push(ary, innerHash) ;
180
+ }
181
+
182
+ fclose(f) ;
183
+
184
+ return ary ;
185
+ }
@@ -209,6 +209,10 @@ module LinuxStat
209
209
  h
210
210
  end
211
211
 
212
+ def times
213
+ LinuxStat::ProcFS.cpu_times
214
+ end
215
+
212
216
  ##
213
217
  # Returns a Hash with max core frequencies corresponding to the CPUs.
214
218
  #
@@ -1,25 +1,15 @@
1
1
  module LinuxStat
2
2
  # Shows various information about a process that is either
3
3
  # running, sleeping, idle or a zombie.
4
-
5
4
  module Process
6
5
  class << self
7
6
  ##
8
7
  # Returns the list of processes from /proc/.
8
+ # The output doesn't guarantee a sorted list, in fact it's shuffled most of the time.
9
9
  #
10
10
  # The return type is an Array of Integers.
11
11
  def list
12
- d = Dir['/proc/*'.freeze]
13
- ret, i = [], -1
14
- count = d.length
15
-
16
- while(i += 1) < count
17
- pid = File.split(d[i])[1]
18
- pid_i = pid.to_i
19
- ret << pid_i if pid_i.to_s == pid
20
- end
21
-
22
- ret
12
+ LinuxStat::ProcFS.list_process
23
13
  end
24
14
 
25
15
  ##
@@ -27,7 +17,7 @@ module LinuxStat
27
17
  #
28
18
  # The return type is Integer.
29
19
  def count
30
- list.length
20
+ LinuxStat::ProcFS.list_process.length
31
21
  end
32
22
 
33
23
  ##
@@ -36,7 +26,7 @@ module LinuxStat
36
26
  def names
37
27
  h, i = {}, -1
38
28
 
39
- l = list
29
+ l = LinuxStat::ProcFS.list_process
40
30
  count = l.length
41
31
 
42
32
  while(i += 1) < count
@@ -57,7 +47,7 @@ module LinuxStat
57
47
  def cmdlines
58
48
  h, i = {}, -1
59
49
 
60
- l = list
50
+ l = LinuxStat::ProcFS.list_process
61
51
  count = l.length
62
52
 
63
53
  while(i += 1) < count
@@ -66,7 +56,7 @@ module LinuxStat
66
56
  begin
67
57
  cmdlines = IO.read("/proc/#{x}/cmdline").strip
68
58
  cmdlines.gsub!(?\u0000.freeze, ?\s.freeze)
69
- h.merge!( x => cmdlines)
59
+ h.merge!(x => cmdlines)
70
60
  rescue StandardError
71
61
  end
72
62
  end
@@ -78,7 +68,7 @@ module LinuxStat
78
68
  def types
79
69
  h, i = {}, -1
80
70
 
81
- l = list
71
+ l = LinuxStat::ProcFS.list_process
82
72
  count = l.length
83
73
 
84
74
  while(i += 1) < count
@@ -109,7 +99,7 @@ module LinuxStat
109
99
  # Returns all the id of processes that are sleeping.
110
100
  # The return type is an Array of Integers.
111
101
  def sleeping
112
- list.select { |x|
102
+ LinuxStat::ProcFS.list_process.select { |x|
113
103
  LinuxStat::ProcFS.ps_state(x) == ?S.freeze
114
104
  }
115
105
  end
@@ -118,7 +108,7 @@ module LinuxStat
118
108
  # Returns all the id of processes that are idle.
119
109
  # The return type is an Array of Integers.
120
110
  def idle
121
- list.select { |x|
111
+ LinuxStat::ProcFS.list_process.select { |x|
122
112
  LinuxStat::ProcFS.ps_state(x) == ?I.freeze
123
113
  }
124
114
  end
@@ -127,7 +117,7 @@ module LinuxStat
127
117
  # Returns all the id of processes that are zombies.
128
118
  # The return type is an Array of Integers.
129
119
  def zombie
130
- list.select { |x|
120
+ LinuxStat::ProcFS.list_process.select { |x|
131
121
  LinuxStat::ProcFS.ps_state(x) == ?Z.freeze
132
122
  }
133
123
  end
@@ -136,7 +126,7 @@ module LinuxStat
136
126
  # Returns all the id of processes that are running.
137
127
  # The return type is an Array of Integers.
138
128
  def running
139
- list.select { |x|
129
+ LinuxStat::ProcFS.list_process.select { |x|
140
130
  LinuxStat::ProcFS.ps_state(x) == ?R.freeze
141
131
  }
142
132
  end
@@ -145,7 +135,7 @@ module LinuxStat
145
135
  # Returns all the id of processes that are stopped.
146
136
  # The return type is an Array of Integers.
147
137
  def stopped
148
- list.select { |x|
138
+ LinuxStat::ProcFS.list_process.select { |x|
149
139
  v = LinuxStat::ProcFS.ps_state(x)
150
140
  v == ?T.freeze || v == ?t.freeze
151
141
  }
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "2.3.0"
2
+ VERSION = "2.4.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: 2.3.0
4
+ version: 2.4.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-06-07 00:00:00.000000000 Z
11
+ date: 2021-08-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
- rubygems_version: 3.2.18
90
+ rubygems_version: 3.2.21
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Efficient linux system reporting gem