linux_stat 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a392f3c04781b05bb96636a882b6e69143bef15befb094f980c7b542923e0649
4
- data.tar.gz: 4ef67fa10ab4210090790b368852cac7deee99965560b201f136f0cd093e9a87
3
+ metadata.gz: e4f9b433758109c632c7b6358b1fd6c5cda1711e1ab6b8f4a3dae0ce6cc02f6d
4
+ data.tar.gz: c0273b6cf31ae022adfeb4b9e041426de2841899d08be49f3559ab98175be54d
5
5
  SHA512:
6
- metadata.gz: 816e423f9a22797022d87a347dbcde20277d0b1d6fa8cc90001367cbdf2012e625400c2a659bbbd308fcf58363c19f2af88ec39d5bcc1fa04ccd4f61d77371b7
7
- data.tar.gz: dfae97ea5bd4ef7c0daa4c784d7d951b915121753d8b61363624aaaa12e9559968e623638004accb8438a77b820913e726856e166bd437f1073047b936a83ba1
6
+ metadata.gz: 3e7b971800222420d9156a791de5857331c923149416a7d4b5d025d57bbb7e9d3ccea8ec47d2236f308a8675bfbcf5d0a2ee0380dab92afed29dc0ace8dcb577
7
+ data.tar.gz: 7583db616fff644f2798fe779b23ed28cc762eac3d6d8aad0e4c907b21813bcaacc058570516378e7cdf495a51b4906bb5a30221ebb60536af72d91b2795a147
data/README.md CHANGED
@@ -99,7 +99,7 @@ LinuxStat::CPU.count
99
99
  => 4
100
100
 
101
101
  LinuxStat::CPU.cur_freq
102
- => [1998722, 1998401, 1974377, 1875264]
102
+ => [1999998, 2000003, 2000015, 2000009]
103
103
 
104
104
  LinuxStat::CPU.max_freq
105
105
  => [2000000, 2000000, 2000000, 2000000]
@@ -108,38 +108,38 @@ LinuxStat::CPU.model
108
108
  => "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
109
109
 
110
110
  LinuxStat::CPU.stat
111
- => {0=>6.45, 1=>0.0, 2=>0.0, 3=>12.5, 4=>0.0}
111
+ => {0=>14.29, 1=>25.0, 2=>0.0, 3=>25.0, 4=>14.29}
112
112
 
113
113
  LinuxStat::CPU.total_usage
114
- => 3.45
114
+ => 18.75
115
115
 
116
116
  LinuxStat::CPU.usage
117
- => 6.45
117
+ => 14.29
118
118
 
119
119
  LinuxStat::CPU.usages
120
- => {0=>9.68, 1=>11.11, 2=>12.5, 3=>14.29, 4=>12.5}
120
+ => {0=>16.67, 1=>14.29, 2=>28.57, 3=>0.0, 4=>25.0}
121
121
 
122
122
  ```
123
123
 
124
124
  ### LinuxStat::Filesystem
125
125
  ```
126
126
  LinuxStat::Filesystem.available
127
- => 43155402752
127
+ => 42144575488
128
128
 
129
129
  LinuxStat::Filesystem.free
130
- => 43155402752
130
+ => 42144575488
131
131
 
132
132
  LinuxStat::Filesystem.stat
133
- => {:total=>119981191168, :free=>43155402752, :used=>76825788416}
133
+ => {:total=>119981191168, :free=>42144575488, :used=>77836615680}
134
134
 
135
135
  LinuxStat::Filesystem.stat_raw
136
- => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535987, :block_avail_unpriv=>10535987, :inodes=>58612160, :free_inodes=>56718515, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
136
+ => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10289203, :block_avail_unpriv=>10289203, :inodes=>58612160, :free_inodes=>56716465, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
137
137
 
138
138
  LinuxStat::Filesystem.total
139
139
  => 119981191168
140
140
 
141
141
  LinuxStat::Filesystem.used
142
- => 76825788416
142
+ => 77836615680
143
143
 
144
144
  ```
145
145
 
@@ -154,15 +154,24 @@ LinuxStat::Kernel.build_date_string
154
154
  LinuxStat::Kernel.build_user
155
155
  => "souravgoswami@archlinux"
156
156
 
157
+ LinuxStat::Kernel.clk_tck
158
+ => 100
159
+
157
160
  LinuxStat::Kernel.compiler
158
161
  => [:gcc, "10.2.0"]
159
162
 
160
163
  LinuxStat::Kernel.compiler_version
161
164
  => "10.2.0"
162
165
 
166
+ LinuxStat::Kernel.release
167
+ => "5.9.1-xanmod1-1"
168
+
163
169
  LinuxStat::Kernel.string
164
170
  => "Linux version 5.9.1-xanmod1-1 (souravgoswami@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35.1) #1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000"
165
171
 
172
+ LinuxStat::Kernel.ticks
173
+ => 100
174
+
166
175
  LinuxStat::Kernel.version
167
176
  => "5.9.1-xanmod1-1"
168
177
 
@@ -171,29 +180,29 @@ LinuxStat::Kernel.version
171
180
  ### LinuxStat::Memory
172
181
  ```
173
182
  LinuxStat::Memory.available
174
- => 403724
183
+ => 606548
175
184
 
176
185
  LinuxStat::Memory.percent_available
177
- => 10.52
186
+ => 15.81
178
187
 
179
188
  LinuxStat::Memory.percent_used
180
- => 89.48
189
+ => 84.19
181
190
 
182
191
  LinuxStat::Memory.stat
183
- => {:total=>3836264, :used=>3432540, :available=>403724, :percent_used=>89.48, :percent_available=>10.52}
192
+ => {:total=>3836256, :used=>3229708, :available=>606548, :percent_used=>84.19, :percent_available=>15.81}
184
193
 
185
194
  LinuxStat::Memory.total
186
- => 3836264
195
+ => 3836256
187
196
 
188
197
  LinuxStat::Memory.used
189
- => 3432540
198
+ => 3229708
190
199
 
191
200
  ```
192
201
 
193
202
  ### LinuxStat::Mounts
194
203
  ```
195
204
  LinuxStat::Mounts.list
196
- => ["proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0", "sys /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0", "dev /dev devtmpfs rw,nosuid,relatime,size=1891796k,nr_inodes=472949,mode=755 0 0", "run /run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0", "...
205
+ => ["proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0", "sys /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0", "dev /dev devtmpfs rw,nosuid,relatime,size=1891804k,nr_inodes=472951,mode=755 0 0", "run /run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0", "...
197
206
 
198
207
  LinuxStat::Mounts.root
199
208
  => "/dev/sda2"
@@ -205,14 +214,14 @@ LinuxStat::Mounts.root_mount_options
205
214
  => "rw,noatime,attr2,inode64,logbufs=8,logbsize=32k,noquota"
206
215
 
207
216
  LinuxStat::Mounts.tmpfs
208
- => {"/dev/shm"=>"tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0", "/sys/fs/cgroup"=>"tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,size=4096k,nr_inodes=1024,mode=755 0 0", "/ramdisk"=>"tmpfs /ramdisk tmpfs rw,nosuid,nodev,relatime,size=6291456k 0 0", "/tmp...
217
+ => {"/dev/shm"=>"tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0", "/sys/fs/cgroup"=>"tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,size=4096k,nr_inodes=1024,mode=755 0 0", "/cache"=>"tmpfs /cache tmpfs rw,nosuid,nodev,relatime,size=2097152k 0 0", "/ramdisk...
209
218
 
210
219
  ```
211
220
 
212
221
  ### LinuxStat::Net
213
222
  ```
214
223
  LinuxStat::Net.ipv4_private
215
- => "192.168.0.106"
224
+ => "192.168.0.101"
216
225
 
217
226
  ```
218
227
 
@@ -230,33 +239,39 @@ LinuxStat::OS.hostname
230
239
  LinuxStat::OS.lsb_release
231
240
  => {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
232
241
 
242
+ LinuxStat::OS.machine
243
+ => "x86_64"
244
+
245
+ LinuxStat::OS.nodename
246
+ => "archlinux"
247
+
233
248
  LinuxStat::OS.os_release
234
249
  => {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/"...
235
250
 
236
251
  LinuxStat::OS.uptime
237
- => {:hour=>10, :minute=>54, :second=>22.45}
252
+ => {:hour=>9, :minute=>10, :second=>19.41}
238
253
 
239
254
  ```
240
255
 
241
256
  ### LinuxStat::Process
242
257
  ```
243
258
  LinuxStat::Process.count
244
- => 210
259
+ => 221
245
260
 
246
261
  LinuxStat::Process.idle
247
- => [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 119, 122, 131, 134, 140, 152, 153, 179, 181, 183, 184, 191, 192, 193, 194, 195, 196, 198, 236, 286, 314, 324, 346, 385, 3540, 3541, 3542, 3543, 3544, 3545, 30463, 3...
262
+ => [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 120, 122, 131, 134, 140, 152, 153, 168, 170, 173, 174, 182, 183, 184, 185, 186, 187, 214, 224, 268, 308, 313, 314, 330, 2357, 2358, 2359, 2360, 2361, 2362, 25094, 2...
248
263
 
249
264
  LinuxStat::Process.list
250
- => [1, 2, 3, 4, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 46, 47, 48, 49, 50, 51, 52, 102, 103, 104, 106, 107, 108, 109, 110, 112, 114, 115, 117, 118, 119, 122, 131, 134,...
265
+ => [1, 2, 3, 4, 6, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 39, 40, 41, 42, 46, 47, 48, 49, 50, 51, 52, 102, 103, 104, 106, 107, 108, 109, 110, 112, 114, 115, 117, 118, 120, 122, 131, 134,...
251
266
 
252
267
  LinuxStat::Process.names
253
- => {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H-events_highpri", 9=>"mm_percpu_wq", 10=>"ksoftirqd/0", 11=>"rcuc/0", 12=>"rcu_preempt", 13=>"rcub/0", 14=>"migration/0", 15=>"idle_inject/0", 16=>"cpuhp/0", 17=>"cpuhp/1", 18...
268
+ => {1=>"systemd", 2=>"kthreadd", 3=>"rcu_gp", 4=>"rcu_par_gp", 6=>"kworker/0:0H-kblockd", 9=>"mm_percpu_wq", 10=>"ksoftirqd/0", 11=>"rcuc/0", 12=>"rcu_preempt", 13=>"rcub/0", 14=>"migration/0", 15=>"idle_inject/0", 16=>"cpuhp/0", 17=>"cpuhp/1", 18=>"idle...
254
269
 
255
270
  LinuxStat::Process.running
256
- => [33736]
271
+ => [30804]
257
272
 
258
273
  LinuxStat::Process.sleeping
259
- => [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115, 178, 180, 197, 225, 239, 309, 317, 329, 332, 333, 334, 338, 367, 368, 369, 370, 378, 381, 383, 384, 402,...
274
+ => [1, 2, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 38, 40, 41, 42, 46, 47, 48, 50, 51, 52, 112, 114, 115, 167, 169, 188, 215, 228, 307, 309, 311, 316, 317, 319, 320, 354, 355, 356, 357, 364, 368, 370, 384, 397,...
260
275
 
261
276
  LinuxStat::Process.types
262
277
  => {1=>:sleeping, 2=>:sleeping, 3=>:idle, 4=>:idle, 6=>:idle, 9=>:idle, 10=>:sleeping, 11=>:sleeping, 12=>:idle, 13=>:sleeping, 14=>:sleeping, 15=>:sleeping, 16=>:sleeping, 17=>:sleeping, 18=>:sleeping, 19=>:sleeping, 20=>:sleeping, 21=>:sleeping, 23=>:i...
@@ -266,31 +281,118 @@ LinuxStat::Process.zombie
266
281
 
267
282
  ```
268
283
 
284
+ ### LinuxStat::ProcessInfo
285
+ ```
286
+ LinuxStat::ProcessInfo.cmdline
287
+ => "ruby bin/linuxstat.rb -md"
288
+
289
+ LinuxStat::ProcessInfo.command_name
290
+ => "ruby"
291
+
292
+ LinuxStat::ProcessInfo.cpu_stat
293
+ => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>2}
294
+
295
+ LinuxStat::ProcessInfo.cpu_usage
296
+ => 0.0
297
+
298
+ LinuxStat::ProcessInfo.last_executed_cpu
299
+ => 2
300
+
301
+ LinuxStat::ProcessInfo.mem_stat
302
+ => {:memory=>9644, :virtual_memory=>79568, :resident_memory=>15764}
303
+
304
+ LinuxStat::ProcessInfo.memory
305
+ => 9644
306
+
307
+ LinuxStat::ProcessInfo.resident_memory
308
+ => 15764
309
+
310
+ LinuxStat::ProcessInfo.threads
311
+ => 1
312
+
313
+ LinuxStat::ProcessInfo.total_io
314
+ => {:read_bytes=>0, :write_bytes=>0}
315
+
316
+ LinuxStat::ProcessInfo.virtual_memory
317
+ => 79568
318
+
319
+ ```
320
+
269
321
  ### LinuxStat::Swap
270
322
  ```
271
323
  LinuxStat::Swap.any?
272
324
  => true
273
325
 
274
326
  LinuxStat::Swap.available
275
- => 1913936
327
+ => 2256212
276
328
 
277
329
  LinuxStat::Swap.list
278
- => {"/dev/zram0"=>[:partition, 4194300, 2280364, -2]}
330
+ => {"/dev/zram0"=>[:partition, 4194300, 1938088, -2]}
279
331
 
280
332
  LinuxStat::Swap.percent_available
281
- => 45.63
333
+ => 53.79
282
334
 
283
335
  LinuxStat::Swap.percent_used
284
- => 54.37
336
+ => 46.21
285
337
 
286
338
  LinuxStat::Swap.stat
287
- => {:total=>4194300, :used=>2280364, :available=>1913936, :percent_used=>54.37, :percent_available=>45.63}
339
+ => {:total=>4194300, :used=>1938088, :available=>2256212, :percent_used=>46.21, :percent_available=>53.79}
288
340
 
289
341
  LinuxStat::Swap.total
290
342
  => 4194300
291
343
 
292
344
  LinuxStat::Swap.used
293
- => 2280364
345
+ => 1938088
346
+
347
+ ```
348
+
349
+ ### LinuxStat::Sysconf
350
+ ```
351
+ LinuxStat::Sysconf.child_max
352
+ => 2000000
353
+
354
+ LinuxStat::Sysconf.hostname_max
355
+ => 64
356
+
357
+ LinuxStat::Sysconf.login_name_max
358
+ => 256
359
+
360
+ LinuxStat::Sysconf.open_max
361
+ => 8192
362
+
363
+ LinuxStat::Sysconf.page_size_max
364
+ => 4096
365
+
366
+ LinuxStat::Sysconf.posix_version
367
+ => 200809
368
+
369
+ LinuxStat::Sysconf.sc_clk_tck
370
+ => 100
371
+
372
+ LinuxStat::Sysconf.stream_max
373
+ => 16
374
+
375
+ LinuxStat::Sysconf.tty_name_max
376
+ => 32
377
+
378
+ ```
379
+
380
+ ### LinuxStat::Uname
381
+ ```
382
+ LinuxStat::Uname.machine
383
+ => "x86_64"
384
+
385
+ LinuxStat::Uname.nodename
386
+ => "archlinux"
387
+
388
+ LinuxStat::Uname.release
389
+ => "5.9.1-xanmod1-1"
390
+
391
+ LinuxStat::Uname.sysname
392
+ => "Linux"
393
+
394
+ LinuxStat::Uname.version
395
+ => "#1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000"
294
396
 
295
397
  ```
296
398
 
@@ -29,6 +29,7 @@ execute.sort.each do |c|
29
29
  next if e.class != Module && e.class != Class
30
30
 
31
31
  meths = e.methods(false).sort
32
+ next if meths.any? { |a| e.method(a).arity > 0 }
32
33
 
33
34
  if meths.length > 0
34
35
  if MARKDOWN
@@ -25,6 +25,7 @@ static VALUE statfs(VALUE obj, VALUE dir) {
25
25
  }
26
26
 
27
27
  void Init_fs_stat() {
28
- VALUE fs = rb_define_module("FS") ;
28
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
29
+ VALUE fs = rb_define_module_under(_linux_stat, "FS") ;
29
30
  rb_define_module_function(fs, "stat", statfs, 1) ;
30
31
  }
@@ -0,0 +1,7 @@
1
+ require 'mkmf'
2
+
3
+ unless (have_header('sys/unistd.h') && have_header('ruby.h'))
4
+ abort('Missing header')
5
+ end
6
+
7
+ create_makefile 'linux_stat/sysconf'
@@ -0,0 +1,53 @@
1
+ #include <unistd.h>
2
+ #include "ruby.h"
3
+
4
+ static VALUE getTick(VALUE obj) {
5
+ return INT2FIX(sysconf(_SC_CLK_TCK)) ;
6
+ }
7
+
8
+ static VALUE getChildMax(VALUE obj) {
9
+ return INT2FIX(sysconf(_SC_CHILD_MAX)) ;
10
+ }
11
+
12
+ static VALUE getHostnameMax(VALUE obj) {
13
+ return INT2FIX(sysconf(_SC_HOST_NAME_MAX)) ;
14
+ }
15
+
16
+ static VALUE getLoginNameMax(VALUE obj) {
17
+ return INT2FIX(sysconf(_SC_LOGIN_NAME_MAX)) ;
18
+ }
19
+
20
+ static VALUE getOpenMax(VALUE obj) {
21
+ return INT2FIX(sysconf(_SC_OPEN_MAX)) ;
22
+ }
23
+
24
+ static VALUE getPageSizeMax(VALUE obj) {
25
+ return INT2FIX(sysconf(_SC_PAGESIZE)) ;
26
+ }
27
+
28
+ static VALUE getStreamMax(VALUE obj) {
29
+ return INT2FIX(sysconf(_SC_STREAM_MAX)) ;
30
+ }
31
+
32
+ static VALUE getTTYNameMax(VALUE obj) {
33
+ return INT2FIX(sysconf(_SC_TTY_NAME_MAX)) ;
34
+ }
35
+
36
+ static VALUE getPosixVersion(VALUE obj) {
37
+ return INT2FIX(sysconf(_SC_VERSION)) ;
38
+ }
39
+
40
+ void Init_sysconf() {
41
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
42
+ VALUE _sysconf = rb_define_module_under(_linux_stat, "Sysconf") ;
43
+
44
+ rb_define_module_function(_sysconf, "sc_clk_tck", getTick, 0) ;
45
+ rb_define_module_function(_sysconf, "child_max", getChildMax, 0) ;
46
+ rb_define_module_function(_sysconf, "hostname_max", getHostnameMax, 0) ;
47
+ rb_define_module_function(_sysconf, "login_name_max", getLoginNameMax, 0) ;
48
+ rb_define_module_function(_sysconf, "open_max", getOpenMax, 0) ;
49
+ rb_define_module_function(_sysconf, "page_size_max", getPageSizeMax, 0) ;
50
+ rb_define_module_function(_sysconf, "stream_max", getStreamMax, 0) ;
51
+ rb_define_module_function(_sysconf, "tty_name_max", getTTYNameMax, 0) ;
52
+ rb_define_module_function(_sysconf, "posix_version", getPosixVersion, 0) ;
53
+ }
@@ -10,9 +10,9 @@ void init_buf() {
10
10
  status = uname(&buf) ;
11
11
  }
12
12
 
13
- static VALUE getMachine(VALUE obj) {
14
- VALUE machine = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.machine) ;
15
- return machine ;
13
+ static VALUE getSysname(VALUE obj) {
14
+ VALUE sysname = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.sysname) ;
15
+ return sysname ;
16
16
  }
17
17
 
18
18
  static VALUE getNodename(VALUE obj) {
@@ -20,10 +20,30 @@ static VALUE getNodename(VALUE obj) {
20
20
  return nodename ;
21
21
  }
22
22
 
23
+ static VALUE getRelease(VALUE obj) {
24
+ VALUE release = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.release) ;
25
+ return release ;
26
+ }
27
+
28
+ static VALUE getVersion(VALUE obj) {
29
+ VALUE version = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.version) ;
30
+ return version ;
31
+ }
32
+
33
+ static VALUE getMachine(VALUE obj) {
34
+ VALUE machine = status < 0 ? rb_str_new_cstr("") : rb_str_new_cstr(buf.machine) ;
35
+ return machine ;
36
+ }
37
+
23
38
  void Init_utsname() {
24
39
  init_buf() ;
25
40
 
26
- VALUE _uname = rb_define_module("Uname") ;
27
- rb_define_module_function(_uname, "machine", getMachine, 0) ;
41
+ VALUE _linux_stat = rb_define_module("LinuxStat") ;
42
+ VALUE _uname = rb_define_module_under(_linux_stat, "Uname") ;
43
+
44
+ rb_define_module_function(_uname, "sysname", getSysname, 0) ;
28
45
  rb_define_module_function(_uname, "nodename", getNodename, 0) ;
46
+ rb_define_module_function(_uname, "release", getRelease, 0) ;
47
+ rb_define_module_function(_uname, "version", getVersion, 0) ;
48
+ rb_define_module_function(_uname, "machine", getMachine, 0) ;
29
49
  }
@@ -2,7 +2,6 @@ require "linux_stat/version"
2
2
  require "linux_stat/battery"
3
3
  require "linux_stat/bios"
4
4
  require "linux_stat/cpu"
5
- require "linux_stat/kernel"
6
5
  require "linux_stat/memory"
7
6
  require "linux_stat/net"
8
7
 
@@ -15,3 +14,7 @@ require "linux_stat/mounts"
15
14
 
16
15
  require "linux_stat/fs_stat"
17
16
  require "linux_stat/filesystem"
17
+
18
+ require "linux_stat/sysconf"
19
+ require "linux_stat/kernel"
20
+ require "linux_stat/process_info"
@@ -9,7 +9,7 @@ module LinuxStat
9
9
  end
10
10
 
11
11
  # Returns the details of the battery.
12
- #If the battery is not present it will return an empty Hash.
12
+ # If the battery is not present it will return an empty Hash.
13
13
  def stat
14
14
  st = status.downcase
15
15
  return {} unless present?
@@ -27,7 +27,7 @@ module LinuxStat
27
27
  end
28
28
 
29
29
  # Returns the model of the battery.
30
- #If the battery is not present or the information isn't available it will return an empty String.
30
+ # If the battery is not present or the information isn't available it will return an empty String.
31
31
  def model
32
32
  return ''.freeze unless model_readable?
33
33
  IO.read(File.join(PATH, 'model_name')).tap(&:strip!)
@@ -3,9 +3,10 @@ module LinuxStat
3
3
  class << self
4
4
  # Returns the model of the BIOS.
5
5
  # If the information is not available it will return a frozen empty string.
6
- # The output is also cached ; as changing the value in runtime is unexpected.
6
+ #
7
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
7
8
  def model
8
- # Cached ; as changing the value in runtime is unexpected
9
+ # cached (memoized) ; as changing the value in runtime is unexpected
9
10
  @@model ||= if File.readable?('/sys/devices/virtual/dmi/id/product_name')
10
11
  IO.read('/sys/devices/virtual/dmi/id/product_name').tap(&:strip!)
11
12
  elsif File.readable?('/sys/firmware/devicetree/base/model')
@@ -17,9 +18,10 @@ module LinuxStat
17
18
 
18
19
  # Returns the vendor of the BIOS.
19
20
  # If the information is not available it will return a frozen empty string.
20
- # The output is also cached ; as changing the value in runtime is unexpected.
21
+ #
22
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
21
23
  def vendor
22
- # Cached ; as changing the value in runtime is unexpected
24
+ # cached (memoized) ; as changing the value in runtime is unexpected
23
25
  @@vendor ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_vendor')
24
26
  IO.read('/sys/devices/virtual/dmi/id/bios_vendor').tap(&:strip!)
25
27
  else
@@ -29,7 +31,8 @@ module LinuxStat
29
31
 
30
32
  # Returns the version of the BIOS.
31
33
  # If the information is not available it will return a frozen empty string.
32
- # The output is also cached ; as changing the value in runtime is unexpected.
34
+ #
35
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
33
36
  def version
34
37
  @@version ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_version')
35
38
  IO.read('/sys/devices/virtual/dmi/id/bios_version').tap(&:strip!)
@@ -40,7 +43,8 @@ module LinuxStat
40
43
 
41
44
  # Returns the date of the BIOS.
42
45
  # If the information is not available it will return a frozen empty string.
43
- # The output is also cached ; as changing the value in runtime is unexpected.
46
+ #
47
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
44
48
  def date
45
49
  @@date ||= if File.readable?('/sys/devices/virtual/dmi/id/bios_date')
46
50
  IO.read('/sys/devices/virtual/dmi/id/bios_date').tap(&:strip!)
@@ -19,7 +19,14 @@ module LinuxStat
19
19
  sleep(sleep)
20
20
  data2 = IO.readlines('/proc/stat').select! { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
21
21
 
22
- data.size.times.reduce({}) do |h, x|
22
+ # On devices like android, the core count can change anytime.
23
+ # I had crashes on Termux.
24
+ # So better just count the min number of CPU and iterate over that
25
+ # If data.length is smaller than data2.length, we don't have enough data to compare.
26
+ dl, d2l = data.length, data2.length
27
+ min = dl > d2l ? d2l : dl
28
+
29
+ min.times.reduce({}) do |h, x|
23
30
  user, nice, sys, idle, iowait, irq, softirq, steal = *data[x].drop(1)
24
31
  user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2[x].drop(1)
25
32
 
@@ -63,7 +70,8 @@ module LinuxStat
63
70
 
64
71
  # Returns the model of processor.
65
72
  # If the information isn't available, it will return en empty string.
66
- # The output is also cached ; as changing the value in runtime is unexpected.
73
+ #
74
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
67
75
  def model
68
76
  @@name ||= cpuinfo.find { |x| x.start_with?('model name') }.to_s.split(?:)[-1].to_s.strip
69
77
  end
@@ -1,7 +1,5 @@
1
1
  module LinuxStat
2
2
  module Filesystem
3
- prepend FS
4
-
5
3
  class << self
6
4
  # stat(fs = '/')
7
5
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
@@ -12,7 +10,7 @@ module LinuxStat
12
10
  # 3. used space (in kilobytes)
13
11
  #
14
12
  # In a hash format:
15
- # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
13
+ # {:total=>119981191168, :free=>43155574784, :used=>76825616384, :available=>43155574784}
16
14
  #
17
15
  # If the stat can't be acquired, this method will return an empty Hash.
18
16
  def stat(fs = ?/.freeze)
@@ -23,7 +21,7 @@ module LinuxStat
23
21
  {
24
22
  total: s[:block_size] * s[:blocks],
25
23
  free: s[:block_size] * s[:block_free],
26
- used: s[:blocks].-(s[:block_free]) * s[:block_size],
24
+ used: s[:blocks].-(s[:block_free]) * s[:block_size]
27
25
  }
28
26
  end
29
27
 
@@ -83,11 +81,11 @@ module LinuxStat
83
81
  # Where fs is the directory of the file system (like / or /tmp/ or /run/media/thumbdrive).
84
82
  #
85
83
  # It returns a Hash with the following data (for example):
86
- # {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
84
+ # {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>10535967, :block_avail_unpriv=>10535967, :inodes=>58612160, :free_inodes=>56718550, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
87
85
  #
88
86
  # If the stat can't be acquired, this method will return an empty Hash.
89
- def stat_raw(fs = '/'.freeze)
90
- FS.stat(fs)
87
+ def stat_raw(fs = ?/.freeze)
88
+ LinuxStat::FS.stat(fs)
91
89
  end
92
90
  end
93
91
  end
@@ -3,7 +3,7 @@ module LinuxStat
3
3
  class << self
4
4
  # Returns the Linux Kernel version.
5
5
  # If the information isn't available, it will return a frozen empty string.
6
- # The output is also cached ; as changing the value in runtime is unexpected.
6
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
7
7
  def version
8
8
  return ''.freeze if string.empty?
9
9
  @@version ||= splitted[2]
@@ -11,7 +11,7 @@ module LinuxStat
11
11
 
12
12
  # Returns the name of the user who built the kernel using KBUILD_FLAGS.
13
13
  # If the information isn't available, it will return a frozen empty string.
14
- # The output is also cached ; as changing the value in runtime is unexpected.
14
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
15
15
  def build_user
16
16
  @@build_user ||= string.split(/(\(.+\))/).each(&:strip!)
17
17
  .reject(&:empty?).find { |x| x[/^\(.+\)$/] }.to_s
@@ -20,7 +20,7 @@ module LinuxStat
20
20
 
21
21
  # Returns the compiler used to compile the Linux Kernel.
22
22
  # If the information isn't available, it will return a frozen empty string.
23
- # The output is also cached ; as changing the value in runtime is unexpected.
23
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
24
24
  def compiler
25
25
  return ''.freeze if string.empty?
26
26
 
@@ -39,7 +39,7 @@ module LinuxStat
39
39
 
40
40
  # Returns the compiler version used to compile the Linux Kernel.
41
41
  # If the information isn't available, it will return a frozen empty string.
42
- # The output is also cached ; as changing the value in runtime is unexpected.
42
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
43
43
  def compiler_version
44
44
  @@compiler_version ||= string.split(/(\(.+?\))/).each(&:strip!)
45
45
  .reject(&:empty?)[2..4].to_a
@@ -62,7 +62,7 @@ module LinuxStat
62
62
  # You have to use regexp yourself to get the proper zone.
63
63
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
64
64
  #
65
- # The output is also cached ; as changing the value in runtime is unexpected.
65
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
66
66
  def build_date
67
67
  return nil if splitted.empty?
68
68
 
@@ -107,7 +107,7 @@ module LinuxStat
107
107
  # You have to use regexp yourself to get the proper zone.
108
108
  # Use LinuxStat::Kernel.build_date_string to get the original string if you need that.
109
109
  #
110
- # The output is also cached ; as changing the value in runtime is unexpected.
110
+ # The output is also cached (memoized) ; as changing the value in runtime is unexpected.
111
111
  def build_date_string
112
112
  return nil if splitted.empty?
113
113
 
@@ -141,6 +141,15 @@ module LinuxStat
141
141
  @@string ||= File.readable?('/proc/version') ? IO.read('/proc/version', 1024).tap(&:strip!) : ''
142
142
  end
143
143
 
144
+ # Returns the sc_clk_tck or the same output from command `getconf CLK_TCK`.
145
+ # Also, clk_tck is an alias of this method.
146
+ # The output is also cached ; as changing the value in runtime is unexpected.
147
+ def ticks
148
+ @@tick ||= LinuxStat::Sysconf.sc_clk_tck
149
+ end
150
+
151
+ alias clk_tck ticks
152
+
144
153
  private
145
154
  def splitted
146
155
  @@string_splitted ||= string.split
@@ -2,9 +2,10 @@ module LinuxStat
2
2
  module Memory
3
3
  class << self
4
4
  # Returns the memory details reported by /proc/meminfo. In this format:
5
- # {:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}
5
+ # {:total=>3836264, :used=>3097952, :available=>738312, :percent_used=>80.75, :percent_available=>19.25}
6
+ #
7
+ # The values are in Kilobyte.
6
8
  #
7
- # The value is in Kilobyte.
8
9
  # If the statistics is not available, it will return an empty Hash.
9
10
  def stat
10
11
  return {} unless meminfo?
@@ -31,6 +32,7 @@ module LinuxStat
31
32
 
32
33
  # Returns the total memory details reported by /proc/meminfo.
33
34
  # The value is in Kilobyte.
35
+ #
34
36
  # It retuns an Integer but if the info is not available, it will return nil.
35
37
  def total
36
38
  return nil unless meminfo?
@@ -39,6 +41,7 @@ module LinuxStat
39
41
 
40
42
  # Returns the total memory details reported by /proc/meminfo.
41
43
  # The value is in Kilobyte.
44
+ #
42
45
  # It retuns an Integer but if the info is not available, it will return nil
43
46
  def available
44
47
  return nil unless meminfo?
@@ -47,6 +50,7 @@ module LinuxStat
47
50
 
48
51
  # Returns the amount of memory used reported by /proc/meminfo.
49
52
  # The value is in Kilobyte.
53
+ #
50
54
  # It retuns an Integer but if the info is not available, it will return nil.
51
55
  def used
52
56
  return nil unless meminfo?
@@ -55,6 +59,7 @@ module LinuxStat
55
59
  end
56
60
 
57
61
  # Returns the percentage of memory used reported by /proc/meminfo.
62
+ #
58
63
  # It retuns an Integer but if the info is not available, it will return nil
59
64
  def percent_used
60
65
  return nil unless meminfo?
@@ -64,6 +69,7 @@ module LinuxStat
64
69
  end
65
70
 
66
71
  # Returns the percentage of memory used reported by /proc/meminfo.
72
+ #
67
73
  # It retuns an Integer but if the info is not available, it will return nil
68
74
  def percent_available
69
75
  return nil unless meminfo?
@@ -1,10 +1,8 @@
1
1
  module LinuxStat
2
2
  module OS
3
- prepend Uname
4
-
5
3
  class << self
6
4
  # Reads /etc/os-release and returns a Hash. For example:
7
- # {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/", :LOGO=>"archlinux"}
5
+ # {:NAME=>"Arch Linux", :PRETTY_NAME=>"Arch Linux", :ID=>"arch", :BUILD_ID=>"rolling", :ANSI_COLOR=>"38;2;23;147;209", :HOME_URL=>"https://www.archlinux.org/", :DOCUMENTATION_URL=>"https://wiki.archlinux.org/", :SUPPORT_URL=>"https://bbs.archlinux.org/", :BUG_REPORT_URL=>"https://bugs.archlinux.org/", :LOGO=>"archlinux"}
8
6
  #
9
7
  # If the info isn't available, it will return an empty Hash.
10
8
  #
@@ -13,12 +11,12 @@ module LinuxStat
13
11
  # The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
14
12
  # isn't expected in runtime.
15
13
  def os_release
16
- # Cached ; as changing the value in runtime is unexpected
14
+ # cached (memoized) ; as changing the value in runtime is unexpected
17
15
  @@os_release ||= File.readable?('/etc/os-release') ? release('/etc/os-release') : {}
18
16
  end
19
17
 
20
18
  # Reads /etc/lsb-release and returns a Hash. For example:
21
- # {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
19
+ # {:LSB_VERSION=>"1.4", :DISTRIB_ID=>"Arch", :DISTRIB_RELEASE=>"rolling", :DISTRIB_DESCRIPTION=>"Arch Linux"}
22
20
  #
23
21
  # If the info isn't available, it will return an empty Hash.
24
22
  #
@@ -27,12 +25,13 @@ module LinuxStat
27
25
  # The information is also cached, and once loaded, won't change in runtime. Because changing the /etc/lsb-release
28
26
  # isn't expected in runtime.
29
27
  def lsb_release
30
- # Cached ; as changing the value in runtime is unexpected
28
+ # cached (memoized) ; as changing the value in runtime is unexpected
31
29
  @@lsb_release ||= File.readable?('/etc/lsb-release') ? release('/etc/lsb-release') : {}
32
30
  end
33
31
 
34
32
  # Reads /etc/lsb-release or /etc/os-release tries to get information about the distribution.
35
33
  # If the information isn't available, it will read and return /etc/issue.
34
+ #
36
35
  # The return type is String.
37
36
  # If none of the info is available, it will return an empty frozen String.
38
37
  def distribution
@@ -54,18 +53,21 @@ module LinuxStat
54
53
  end
55
54
 
56
55
  # Uses utsname.h to determine the machine
56
+ #
57
57
  # It returns a String but if the info isn't available, it will return an empty String
58
58
  def machine
59
- @@machine ||= Uname.machine
59
+ @@machine ||= LinuxStat::Uname.machine
60
60
  end
61
61
 
62
62
  # Uses utsname.h to determine the system nodename
63
+ #
63
64
  # It returns String but if the info isn't available, it will return an empty String
64
65
  def nodename
65
- @@nodename ||= Uname.nodename
66
+ @@nodename ||= LinuxStat::Uname.nodename
66
67
  end
67
68
 
68
69
  # Reads /etc/hostname and returns the hostname.
70
+ #
69
71
  # The return type is String.
70
72
  # If the info info isn't available, it will return 'localhost'.
71
73
  def hostname
@@ -90,7 +92,7 @@ module LinuxStat
90
92
  end
91
93
 
92
94
  # Reads /proc/uptime and returns the system uptime:
93
- # {:hour=>10, :minute=>34, :second=>12.59}
95
+ # {:hour=>10, :minute=>34, :second=>12.59}
94
96
  #
95
97
  # If the stat isn't available, an empty hash is returned.
96
98
  def uptime
@@ -0,0 +1,310 @@
1
+ module LinuxStat
2
+ module ProcessInfo
3
+ class << self
4
+ # total_io(pid = $$)
5
+ # Where pid is the process ID.
6
+ # By default it is the id of the current process ($$)
7
+ #
8
+ # It retuns the total read/write caused by a process.
9
+ # The output is Hash. For example, a sample output:
10
+ # {:read_bytes=>0, :write_bytes=>0}
11
+ #
12
+ # The output is only based on the total disk IO the process has done.
13
+ #
14
+ # If the info isn't available it will return an empty Hash.
15
+ def total_io(pid = $$)
16
+ return {} unless File.readable?("/proc/#{pid}/io".freeze)
17
+ out = {}
18
+
19
+ IO.readlines("/proc/#{pid}/io".freeze).each { |x|
20
+ x.strip!
21
+
22
+ if x[/^(read|write)_bytes:\s*\d*$/]
23
+ splitted = x.split
24
+ out.merge!(splitted[0].split(?:)[0].to_sym => splitted[-1].to_i)
25
+ end
26
+ }
27
+
28
+ out
29
+ end
30
+
31
+ # cmdline(pid = $$)
32
+ # Where pid is the process ID.
33
+ # By default it is the id of the current process ($$)
34
+ #
35
+ # It retuns the total command of the process.
36
+ # The output is String. For example, a sample output:
37
+ # "ruby -r linux_stat -e p LinuxStat::ProcessInfo.cmdline"
38
+ #
39
+ # If the info isn't available it will return an empty frozen String.
40
+ def cmdline(pid = $$)
41
+ file = "/proc/#{pid}/cmdline".freeze
42
+ return ''.freeze unless File.readable?(file)
43
+
44
+ _cmdline = IO.read(file)
45
+ _cmdline.gsub!(?\u0000, ?\s)
46
+ _cmdline.tap(&:strip!)
47
+ end
48
+
49
+ # command_name(pid = $$)
50
+ # Where pid is the process ID.
51
+ # By default it is the id of the current process ($$)
52
+ #
53
+ # It retuns the total command name of the process.
54
+ # The output is String. For example, a sample output:
55
+ # "ruby"
56
+ #
57
+ # If the info isn't available it will return an empty frozen String.
58
+ def command_name(pid = $$)
59
+ # Do note that the /proc/ppid/comm may not contain the full name
60
+ file = "/proc/#{pid}/cmdline".freeze
61
+ return ''.freeze unless File.readable?(file)
62
+
63
+ _cmdline = IO.read(file)
64
+ _cmdline.gsub!(?\u0000, ?\s)
65
+ File.split(_cmdline.tap(&:strip!).split[0])[-1]
66
+ end
67
+
68
+ # mem_stat(pid = $$)
69
+ # Where pid is the process ID.
70
+ # By default it is the id of the current process ($$)
71
+ #
72
+ # It retuns the memory, virtual memory, and resident memory of the process.
73
+ # All values are in Kilobytes.
74
+ #
75
+ # The output is a Hash. For example, a sample output:
76
+ # {:memory=>8656, :virtual_memory=>78272, :resident_memory=>14072}
77
+ #
78
+ # Note:
79
+ # If you need only memory usage of a process, run LinuxStat::ProcessInfo.memory(pid)
80
+ # If you need only virtual memory for a process, run LinuxStat::ProcessInfo.virtual_memory(pid)
81
+ # If you need only resident memory of a process, run LinuxStat::ProcessInfo.resident_memory(pid)
82
+ #
83
+ # This method opens opens multiple files.
84
+ # But if you need all of the info, then running this method once is efficient.
85
+ #
86
+ # If the info isn't available it will return an empty Hash.
87
+ def mem_stat(pid = $$)
88
+ stat_file = "/proc/#{pid}/stat".freeze
89
+ status_file = "/proc/#{pid}/status".freeze
90
+
91
+ stat = if File.readable?(stat_file)
92
+ IO.read(stat_file).split
93
+ else
94
+ []
95
+ end
96
+
97
+ status = if File.readable?(status_file)
98
+ IO.readlines(status_file)
99
+ else
100
+ []
101
+ end
102
+
103
+ _rss_anon = status.find { |x| x.start_with?('RssAnon') }
104
+ rss_anon = _rss_anon ? _rss_anon.split[1].to_i : nil
105
+
106
+ _virtual_memory = stat[22]
107
+ vm = _virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
108
+
109
+ _vm_rss = status.find { |x| x.start_with?('VmRSS') }
110
+ vm_rss = _vm_rss ? _vm_rss.split[1].to_i : nil
111
+
112
+ {
113
+ memory: rss_anon,
114
+ virtual_memory: vm,
115
+ resident_memory: vm_rss
116
+ }
117
+ end
118
+
119
+ # memory(pid = $$)
120
+ # Where pid is the process ID.
121
+ # By default it is the id of the current process ($$)
122
+ #
123
+ # It retuns the memory of the process.
124
+ # The value is in Kilobytes.
125
+ # The output is an Integer. For example, a sample output:
126
+ # 8664
127
+ #
128
+ # If the info isn't available it will return nil.
129
+ def memory(pid = $$)
130
+ _rss_anon = IO.readlines("/proc/#{pid}/status")
131
+ .find { |x| x.start_with?('RssAnon') }
132
+
133
+ _rss_anon ? _rss_anon.split[1].to_i : nil
134
+ end
135
+
136
+ # virtual_memory(pid = $$)
137
+ # Where pid is the process ID.
138
+ # By default it is the id of the current process ($$)
139
+ #
140
+ # It retuns the virtual memory for the process.
141
+ # The value is in Kilobytes.
142
+ # The output is an Integer. For example, a sample output:
143
+ # 78376
144
+ #
145
+ # If the info isn't available it will return nil.
146
+ def virtual_memory(pid = $$)
147
+ _virtual_memory = IO.read("/proc/#{pid}/stat".freeze).split[22]
148
+ _virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
149
+ end
150
+
151
+ # resident_memory(pid = $$)
152
+ # Where pid is the process ID.
153
+ # By default it is the id of the current process ($$)
154
+ #
155
+ # It retuns the resident memory for the process.
156
+ # The value is in Kilobytes.
157
+ # The output is an Integer. For example, a sample output:
158
+ # 14012
159
+ #
160
+ # If the info isn't available it will return nil.
161
+ def resident_memory(pid = $$)
162
+ _vm_rss = IO.readlines("/proc/#{pid}/status")
163
+ .find { |x| x.start_with?('VmRSS') }
164
+
165
+ _vm_rss ? _vm_rss.split[1].to_i : nil
166
+ end
167
+
168
+ # cpu_stat(pid: $$, sleep: 0.05)
169
+ # Where pid is the process ID and sleep time is the interval between measurements.
170
+ # By default it is the id of the current process ($$), and sleep is 0.05
171
+ #
172
+ # Note 1:
173
+ # Do note that the sleep time can slow down your application.
174
+ # And it's only needed for the cpu usage calculation.
175
+ #
176
+ # It retuns the CPU usage, threads, and the last executed CPU in Hash.
177
+ # For example:
178
+ # {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>1}
179
+ #
180
+ # But if the info isn't available, it will return an empty Hash.
181
+ #
182
+ # The :cpu_usage is in percentage. It's also divided with the number
183
+ # of CPU. :cpu_usage for example, will return 25.0 if the CPU count
184
+ # is 4, and the process is using 100% of a thread / core.
185
+ # A value of 100.0 indicates it is using 100% processing power.
186
+ #
187
+ # The :threads returns the number of threads for the process.
188
+ # The value is a Integer.
189
+ #
190
+ # Note 2:
191
+ # If you just need the CPU usage run LinuxStat::ProcessInfo.cpu_usage(pid = $$)
192
+ # If you just need the threads run LinuxStat::ProcessInfo.threads(pid = $$)
193
+ # If you just need the last executed CPU run LinuxStat::ProcessInfo.last_executed_cpu(pid = $$)
194
+ # Running this method is slower and it opens multiple files at once
195
+ #
196
+ # Only use this method if you need all of the data at once, in such case, it's more efficient to use this method.
197
+ #
198
+ # The :last_executed_cpu also returns an Integer indicating
199
+ # the last executed cpu of the process.
200
+ def cpu_stat(pid: $$, sleep: 0.05)
201
+ file = "/proc/#{pid}/stat"
202
+ return {} unless File.readable?(file)
203
+
204
+ ticks = get_ticks
205
+
206
+ utime, stime, starttime = IO.read(file)
207
+ .split.values_at(13, 14, 21).map(&:to_f)
208
+ uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
209
+
210
+ total_time = utime + stime
211
+ idle1 = uptime - starttime - total_time
212
+
213
+ sleep(sleep)
214
+ stat = IO.read(file).split
215
+
216
+ utime2, stime2, starttime2 = stat.values_at(13, 14, 21).map(&:to_f)
217
+ uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
218
+
219
+ total_time2 = utime2 + stime2
220
+ idle2 = uptime - starttime2 - total_time2
221
+
222
+ totald = idle2.+(total_time2).-(idle1 + total_time)
223
+ cpu = totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
224
+
225
+ {
226
+ cpu_usage: cpu,
227
+ threads: stat[19].to_i,
228
+ last_executed_cpu: stat[38].to_i
229
+ }
230
+ end
231
+
232
+ # cpu_usage(pid: $$, sleep: 0.05)
233
+ # Where pid is the process ID and sleep time is the interval between measurements.
234
+ # By default it is the id of the current process ($$), and sleep is 0.05
235
+ #
236
+ # It retuns the CPU usage in Float.
237
+ # For example:
238
+ # 10.0
239
+ # A value of 100.0 indicates it is using 100% processing power.
240
+ #
241
+ # But if the info isn't available, it will return nil.
242
+ #
243
+ # This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
244
+ def cpu_usage(pid: $$, sleep: 0.05)
245
+ file = "/proc/#{pid}/stat"
246
+ return nil unless File.readable?(file)
247
+
248
+ ticks = get_ticks
249
+
250
+ utime, stime, starttime = IO.read(file)
251
+ .split.values_at(13, 14, 21).map(&:to_f)
252
+ uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
253
+
254
+ total_time = utime + stime
255
+ idle1 = uptime - starttime - total_time
256
+
257
+ sleep(sleep)
258
+
259
+ utime2, stime2, starttime2 = IO.read(file)
260
+ .split.values_at(13, 14, 21).map(&:to_f)
261
+ uptime = IO.read('/proc/uptime'.freeze).to_f * ticks
262
+
263
+ total_time2 = utime2 + stime2
264
+ idle2 = uptime - starttime2 - total_time2
265
+
266
+ totald = idle2.+(total_time2).-(idle1 + total_time)
267
+ totald.-(idle2 - idle1).fdiv(totald).*(100).round(2).abs./(LinuxStat::CPU.count)
268
+ end
269
+
270
+ # threads(pid = $$)
271
+ # Where pid is the process ID.
272
+ # By default it is the id of the current process ($$)
273
+ #
274
+ # It retuns the threads for the current process in Integer.
275
+ # For example:
276
+ # 1
277
+ # But if the info isn't available, it will return nil.
278
+ #
279
+ # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
280
+ def threads(pid = $$)
281
+ file = "/proc/#{pid}/stat".freeze
282
+ return nil unless File.readable?(file)
283
+
284
+ IO.read(file).split[19].to_i
285
+ end
286
+
287
+ # last_executed_cpu(pid = $$)
288
+ # Where pid is the process ID.
289
+ # By default it is the id of the current process ($$)
290
+ #
291
+ # It retuns the last executed CPU in Integer.
292
+ # For example:
293
+ # 2
294
+ # But if the info isn't available, it will return nil.
295
+ #
296
+ # This method is way more efficient than running LinuxStat::ProcessInfo.cpu_stat()
297
+ def last_executed_cpu(pid = $$)
298
+ file = "/proc/#{pid}/stat".freeze
299
+ return nil unless File.readable?(file)
300
+
301
+ IO.read("/proc/#{pid}/stat".freeze).split[38].to_i
302
+ end
303
+
304
+ private
305
+ def get_ticks
306
+ @@ticks ||= Sysconf.sc_clk_tck
307
+ end
308
+ end
309
+ end
310
+ end
@@ -21,6 +21,7 @@ module LinuxStat
21
21
 
22
22
  # Show aggregated used and available swap.
23
23
  # The values are in kilobytes.
24
+ #
24
25
  # The return type is Hash.
25
26
  # If the info isn't available, the return type is an empty Hash.
26
27
  def stat
@@ -44,6 +45,7 @@ module LinuxStat
44
45
 
45
46
  # Show total amount of swap.
46
47
  # The value is in kilobytes.
48
+ #
47
49
  # The return type is a Integer but if the info isn't available, it will return nil.
48
50
  def total
49
51
  return nil unless swaps_readable?
@@ -52,6 +54,7 @@ module LinuxStat
52
54
 
53
55
  # Show total amount of available swap.
54
56
  # The value is in kilobytes.
57
+ #
55
58
  # The return type is a Integer but if the info isn't available, it will return nil.
56
59
  def available
57
60
  return nil unless swaps_readable?
@@ -61,6 +64,7 @@ module LinuxStat
61
64
 
62
65
  # Show total amount of used swap.
63
66
  # The value is in kilobytes.
67
+ #
64
68
  # The return type is a Integer but if the info isn't available, it will return nil.
65
69
  def used
66
70
  return nil unless swaps_readable?
@@ -79,7 +83,8 @@ module LinuxStat
79
83
  values_t[-1].sum.*(100).fdiv(total).round(2)
80
84
  end
81
85
 
82
- # Show percentage of swap available.
86
+ # Shows the percentage of swap available.
87
+ #
83
88
  # The return type is a Float but if the info isn't available, it will return nil.
84
89
  def percent_available
85
90
  return nil unless swaps_readable?
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
@@ -15,8 +15,9 @@ email:
15
15
  - souravgoswami@protonmail.com
16
16
  executables: []
17
17
  extensions:
18
- - ext/utsname/extconf.rb
19
18
  - ext/fs_stat/extconf.rb
19
+ - ext/sysconf/extconf.rb
20
+ - ext/utsname/extconf.rb
20
21
  extra_rdoc_files:
21
22
  - README.md
22
23
  files:
@@ -26,6 +27,8 @@ files:
26
27
  - bin/setup
27
28
  - ext/fs_stat/extconf.rb
28
29
  - ext/fs_stat/fs_stat.c
30
+ - ext/sysconf/extconf.rb
31
+ - ext/sysconf/sysconf.c
29
32
  - ext/utsname/extconf.rb
30
33
  - ext/utsname/utsname.c
31
34
  - lib/linux_stat.rb
@@ -39,6 +42,7 @@ files:
39
42
  - lib/linux_stat/net.rb
40
43
  - lib/linux_stat/os.rb
41
44
  - lib/linux_stat/process.rb
45
+ - lib/linux_stat/process_info.rb
42
46
  - lib/linux_stat/swap.rb
43
47
  - lib/linux_stat/version.rb
44
48
  homepage: https://github.com/Souravgoswami/linux_stat/