linux_stat 0.5.2 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1c68e104d55d755cd1cef1e8901a7dd67b1a901b132d9a7786fabfd75b41cf5a
4
- data.tar.gz: 94d102c6244b8da52b5f7aa7b1ea903546b5ef6e615c6317233b510005b6ded7
3
+ metadata.gz: e6d3a089c2e214eb53f564d64014d6da9b900467e8b8285be6be6516c92fb366
4
+ data.tar.gz: 6cc0a5b86b5b9163d2ae1d57b761d8cf6900ff2864987c3b2b39a5072e5d8174
5
5
  SHA512:
6
- metadata.gz: b94d6afea3d307c845f316ce21f66e93944e1b33ad9576ecd8d55e299d6d520732677f99ea8fba3dcb3f6f5c6c8aae98526cc21b2c0218d7a8d49bdd7896cdec
7
- data.tar.gz: cecbaee79f9844c5a6ad83720071a7977697edeb958d2336a68fa9f3b19ccb28074641c9620c88574dd0b5074ba6069a3457543a2cad86c11d9c5562d6e83da6
6
+ metadata.gz: e9d1ce304cec1210fd3cb4f75b36a105b35bc54828106d1d880331b5d8ec6429f120be21200014599a22693950386aa838e03cc20a0fc41c0b819204c6c2da81
7
+ data.tar.gz: 45ea80ecccb20956945b420bfec45ca0e3f3770233418469fd741f554977c9ab07cb14002f9a887094db0b078f5ae9cc5c1b6a25865e8024a5ade4c278dd7ed8
data/README.md CHANGED
@@ -103,7 +103,7 @@ LinuxStat::CPU.count
103
103
  => 4
104
104
 
105
105
  LinuxStat::CPU.cur_freq
106
- => [2000050, 2000024, 1998134, 1975870]
106
+ => [1863999, 1934584, 1996927, 1978102]
107
107
 
108
108
  LinuxStat::CPU.max_freq
109
109
  => [2000000, 2000000, 2000000, 2000000]
@@ -112,48 +112,48 @@ LinuxStat::CPU.model
112
112
  => "Intel(R) Core(TM) i3-6006U CPU @ 2.00GHz"
113
113
 
114
114
  LinuxStat::CPU.stat
115
- => {0=>3.57, 1=>0.0, 2=>0.0, 3=>0.0, 4=>0.0}
115
+ => {0=>20.0, 1=>50.0, 2=>0.0, 3=>0.0, 4=>0.0}
116
116
 
117
117
  LinuxStat::CPU.total_usage
118
- => 3.45
118
+ => 0.0
119
119
 
120
120
  LinuxStat::CPU.usage
121
- => 9.68
121
+ => 25.0
122
122
 
123
123
  LinuxStat::CPU.usages
124
- => {0=>6.67, 1=>0.0, 2=>25.0, 3=>12.5, 4=>14.29}
124
+ => {0=>0.0, 1=>0.0, 2=>0.0, 3=>0.0, 4=>0.0}
125
125
 
126
126
  ```
127
127
 
128
128
  ### LinuxStat::Filesystem
129
129
  ```
130
130
  LinuxStat::Filesystem.available
131
- => 36234563584
131
+ => 34612666368
132
132
 
133
133
  LinuxStat::Filesystem.free
134
- => 36234563584
134
+ => 34612666368
135
135
 
136
136
  LinuxStat::Filesystem.stat
137
- => {:total=>119981191168, :free=>36234563584, :used=>83746627584}
137
+ => {:total=>119981191168, :free=>34612666368, :used=>85368524800}
138
138
 
139
139
  LinuxStat::Filesystem.stat_raw
140
- => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>8846329, :block_avail_unpriv=>8846329, :inodes=>58612160, :free_inodes=>56635312, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
140
+ => {:block_size=>4096, :fragment_size=>4096, :blocks=>29292283, :block_free=>8450358, :block_avail_unpriv=>8450358, :inodes=>58612160, :free_inodes=>56582049, :filesystem_id=>2050, :mount_flags=>1024, :max_filename_length=>255}
141
141
 
142
142
  LinuxStat::Filesystem.total
143
143
  => 119981191168
144
144
 
145
145
  LinuxStat::Filesystem.used
146
- => 83746627584
146
+ => 85368524800
147
147
 
148
148
  ```
149
149
 
150
150
  ### LinuxStat::Kernel
151
151
  ```
152
152
  LinuxStat::Kernel.build_date
153
- => 2020-10-21 01:11:20 +0000
153
+ => 2020-11-20 07:44:55 +0000
154
154
 
155
155
  LinuxStat::Kernel.build_date_string
156
- => "21 Oct 2020 01:11:20 +0000"
156
+ => "20 Nov 2020 07:44:55 +0000"
157
157
 
158
158
  LinuxStat::Kernel.build_user
159
159
  => "souravgoswami@archlinux"
@@ -168,45 +168,60 @@ LinuxStat::Kernel.compiler_version
168
168
  => "10.2.0"
169
169
 
170
170
  LinuxStat::Kernel.release
171
- => "5.9.1-xanmod1-1"
171
+ => "5.9.9-xanmod1-1"
172
172
 
173
173
  LinuxStat::Kernel.string
174
- => "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"
174
+ => "Linux version 5.9.9-xanmod1-1 (souravgoswami@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35.1) #1 SMP PREEMPT Fri, 20 Nov 2020 07:44:55 +0000"
175
175
 
176
176
  LinuxStat::Kernel.ticks
177
177
  => 100
178
178
 
179
179
  LinuxStat::Kernel.version
180
- => "5.9.1-xanmod1-1"
180
+ => "5.9.9-xanmod1-1"
181
181
 
182
182
  ```
183
183
 
184
184
  ### LinuxStat::Memory
185
185
  ```
186
186
  LinuxStat::Memory.available
187
- => 602148
187
+ => 185060
188
188
 
189
189
  LinuxStat::Memory.percent_available
190
- => 15.7
190
+ => 4.82
191
191
 
192
192
  LinuxStat::Memory.percent_used
193
- => 84.3
193
+ => 95.18
194
194
 
195
195
  LinuxStat::Memory.stat
196
- => {:total=>3836256, :used=>3234108, :available=>602148, :percent_used=>84.3, :percent_available=>15.7}
196
+ => {:total=>3836228, :used=>3651168, :available=>185060, :percent_used=>95.18, :percent_available=>4.82}
197
197
 
198
198
  LinuxStat::Memory.total
199
- => 3836256
199
+ => 3836228
200
200
 
201
201
  LinuxStat::Memory.used
202
- => 3234108
202
+ => 3651168
203
203
 
204
204
  ```
205
205
 
206
206
  ### LinuxStat::Mounts
207
207
  ```
208
+ LinuxStat::Mounts.device_stat
209
+ => {:mountpoint=>"/", :total=>119981191168, :free=>34098106368, :available=>34098106368, :used=>85883084800, :percent_used=>71.58, :percent_free=>28.42, :percent_available=>28.42}
210
+
211
+ LinuxStat::Mounts.devices_stat
212
+ => {"proc"=>{:mountpoint=>"/proc", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_free=>NaN, :percent_available=>NaN}, "sys"=>{:mountpoint=>"/sys", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_fre...
213
+
208
214
  LinuxStat::Mounts.list
209
- => ["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", "...
215
+ => ["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=1892904k,nr_inodes=473226,mode=755 0 0", "run /run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0", "...
216
+
217
+ LinuxStat::Mounts.list_devices
218
+ => ["proc", "sys", "dev", "run", "/dev/sda2", "securityfs", "tmpfs", "devpts", "tmpfs", "cgroup2", "cgroup", "pstore", "none", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "cgroup", "systemd-1", "deb...
219
+
220
+ LinuxStat::Mounts.list_devices_mount_point
221
+ => {"proc"=>"/proc", "sys"=>"/sys", "dev"=>"/dev", "run"=>"/run", "/dev/sda2"=>"/", "securityfs"=>"/sys/kernel/security", "tmpfs"=>"/run/user/1000", "devpts"=>"/dev/pts", "cgroup2"=>"/sys/fs/cgroup/unified", "cgroup"=>"/sys/fs/cgroup/perf_event", "pstore...
222
+
223
+ LinuxStat::Mounts.mount_point
224
+ => "/"
210
225
 
211
226
  LinuxStat::Mounts.root
212
227
  => "/dev/sda2"
@@ -225,7 +240,7 @@ LinuxStat::Mounts.tmpfs
225
240
  ### LinuxStat::Net
226
241
  ```
227
242
  LinuxStat::Net.ipv4_private
228
- => "192.168.43.107"
243
+ => "192.168.0.102"
229
244
 
230
245
  ```
231
246
 
@@ -253,17 +268,17 @@ LinuxStat::OS.os_release
253
268
  => {: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/"...
254
269
 
255
270
  LinuxStat::OS.uptime
256
- => {:hour=>15, :minute=>19, :second=>34.72}
271
+ => {:hour=>9, :minute=>21, :second=>17.73}
257
272
 
258
273
  ```
259
274
 
260
275
  ### LinuxStat::Process
261
276
  ```
262
277
  LinuxStat::Process.count
263
- => 228
278
+ => 214
264
279
 
265
280
  LinuxStat::Process.idle
266
- => [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 119, 121, 131, 134, 140, 152, 153, 174, 176, 178, 182, 183, 187, 188, 189, 190, 191, 192, 225, 231, 280, 327, 329, 335, 5823, 5824, 5825, 5826, 5827, 5828, 43047, 4...
281
+ => [3, 4, 6, 9, 12, 23, 30, 37, 39, 49, 102, 103, 104, 106, 107, 108, 109, 110, 117, 118, 119, 121, 131, 134, 140, 152, 153, 178, 180, 183, 184, 188, 192, 193, 194, 195, 196, 197, 199, 241, 287, 337, 341, 349, 3799, 3800, 3801, 3802, 3803, 3804, 17865, 2...
267
282
 
268
283
  LinuxStat::Process.list
269
284
  => [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, 121, 131, 134,...
@@ -272,10 +287,10 @@ LinuxStat::Process.names
272
287
  => {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...
273
288
 
274
289
  LinuxStat::Process.running
275
- => [49287]
290
+ => [24893]
276
291
 
277
292
  LinuxStat::Process.sleeping
278
- => [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, 173, 175, 193, 219, 235, 301, 312, 313, 315, 326, 328, 340, 363, 364, 365, 366, 373, 376, 378, 381, 395,...
293
+ => [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, 177, 179, 198, 228, 246, 310, 316, 323, 324, 325, 336, 338, 370, 371, 372, 373, 380, 384, 388, 399, 410,...
279
294
 
280
295
  LinuxStat::Process.types
281
296
  => {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...
@@ -288,37 +303,46 @@ LinuxStat::Process.zombie
288
303
  ### LinuxStat::ProcessInfo
289
304
  ```
290
305
  LinuxStat::ProcessInfo.cmdline
291
- => "/usr/bin/ruby /home/sourav/.gem/ruby/2.7.0/bin/linuxstat.rb --html -md"
306
+ => "ruby exe/linuxstat.rb -md"
292
307
 
293
308
  LinuxStat::ProcessInfo.command_name
294
309
  => "ruby"
295
310
 
296
311
  LinuxStat::ProcessInfo.cpu_stat
297
- => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>3}
312
+ => {:cpu_usage=>0.0, :threads=>1, :last_executed_cpu=>2}
298
313
 
299
314
  LinuxStat::ProcessInfo.cpu_usage
300
315
  => 0.0
301
316
 
317
+ LinuxStat::ProcessInfo.gid
318
+ => {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
319
+
302
320
  LinuxStat::ProcessInfo.last_executed_cpu
303
- => 3
321
+ => 2
304
322
 
305
323
  LinuxStat::ProcessInfo.mem_stat
306
- => {:memory=>11748, :virtual_memory=>81680, :resident_memory=>17928}
324
+ => {:memory=>9752, :virtual_memory=>79824, :resident_memory=>15564}
307
325
 
308
326
  LinuxStat::ProcessInfo.memory
309
- => 11748
327
+ => 9752
328
+
329
+ LinuxStat::ProcessInfo.owner
330
+ => "sourav"
310
331
 
311
332
  LinuxStat::ProcessInfo.resident_memory
312
- => 17928
333
+ => 15564
313
334
 
314
335
  LinuxStat::ProcessInfo.threads
315
336
  => 1
316
337
 
317
338
  LinuxStat::ProcessInfo.total_io
318
- => {:read_bytes=>3932160, :write_bytes=>0}
339
+ => {:read_bytes=>0, :write_bytes=>0}
340
+
341
+ LinuxStat::ProcessInfo.uid
342
+ => {:real=>1000, :effective=>1000, :saved_set=>1000, :filesystem_uid=>1000}
319
343
 
320
344
  LinuxStat::ProcessInfo.virtual_memory
321
- => 81680
345
+ => 79824
322
346
 
323
347
  ```
324
348
 
@@ -328,25 +352,25 @@ LinuxStat::Swap.any?
328
352
  => true
329
353
 
330
354
  LinuxStat::Swap.available
331
- => 3358184
355
+ => 1929788
332
356
 
333
357
  LinuxStat::Swap.list
334
- => {"/dev/zram0"=>[:partition, 4194300, 836116, -2]}
358
+ => {"/dev/zram0"=>[:partition, 4194300, 2264512, -2]}
335
359
 
336
360
  LinuxStat::Swap.percent_available
337
- => 80.07
361
+ => 46.01
338
362
 
339
363
  LinuxStat::Swap.percent_used
340
- => 19.93
364
+ => 53.99
341
365
 
342
366
  LinuxStat::Swap.stat
343
- => {:total=>4194300, :used=>836116, :available=>3358184, :percent_used=>19.93, :percent_available=>80.07}
367
+ => {:total=>4194300, :used=>2264512, :available=>1929788, :percent_used=>53.99, :percent_available=>46.01}
344
368
 
345
369
  LinuxStat::Swap.total
346
370
  => 4194300
347
371
 
348
372
  LinuxStat::Swap.used
349
- => 836116
373
+ => 2264512
350
374
 
351
375
  ```
352
376
 
@@ -405,24 +429,30 @@ LinuxStat::Uname.nodename
405
429
  => "archlinux"
406
430
 
407
431
  LinuxStat::Uname.release
408
- => "5.9.1-xanmod1-1"
432
+ => "5.9.9-xanmod1-1"
409
433
 
410
434
  LinuxStat::Uname.sysname
411
435
  => "Linux"
412
436
 
413
437
  LinuxStat::Uname.version
414
- => "#1 SMP PREEMPT Wed, 21 Oct 2020 01:11:20 +0000"
438
+ => "#1 SMP PREEMPT Fri, 20 Nov 2020 07:44:55 +0000"
415
439
 
416
440
  ```
417
441
 
418
442
  ### LinuxStat::User
419
443
  ```
444
+ LinuxStat::User.get_current_user
445
+ => "sourav"
446
+
420
447
  LinuxStat::User.get_euid
421
448
  => 1000
422
449
 
423
450
  LinuxStat::User.get_gid
424
451
  => 1000
425
452
 
453
+ LinuxStat::User.get_login
454
+ => "sourav"
455
+
426
456
  LinuxStat::User.get_uid
427
457
  => 1000
428
458
 
@@ -627,7 +657,7 @@ irb(main):006:0> LinuxStat::User.home_by_gid(0)
627
657
  Or to get the UID/GID by username:
628
658
 
629
659
  ```
630
- $ irb
660
+ $ irb
631
661
  irb(main):001:0> require 'linux_stat'
632
662
  => true
633
663
 
@@ -644,6 +674,81 @@ irb(main):005:0> LinuxStat::User.gid_by_username('InvalidUser')
644
674
  => nil
645
675
  ```
646
676
 
677
+ ## Note 5: PrettifyBytes
678
+ Often times we need to work with KB, MB GB, TB, or KiB, MiB, GiB, TiB, etc.
679
+ And we need some work to convert bytes to those units.
680
+ Because LinuxStat provides a lot of data in bytes, and kilobytes, it's quite tedious to convert them all the time.
681
+ To avoid such duplication, it comes with a PrettifyBytes module.
682
+
683
+ For example, to convert bytes to decimal suffixes:
684
+
685
+ ```
686
+ irb(main):001:0> require 'linux_stat'
687
+ => true
688
+
689
+ irb(main):002:0> LinuxStat::PrettifyBytes.convert_decimal(1000)
690
+ => "1.00 kilobyte"
691
+
692
+ irb(main):003:0> LinuxStat::PrettifyBytes.convert_decimal(10000)
693
+ => "10.00 kilobytes"
694
+
695
+ irb(main):004:0> LinuxStat::PrettifyBytes.convert_decimal(100000)
696
+ => "100.00 kilobytes"
697
+
698
+ irb(main):005:0> LinuxStat::PrettifyBytes.convert_decimal(10 ** 13)
699
+ => "10.00 terabytes"
700
+ ```
701
+
702
+ To convert bytes to binary suffixes:
703
+
704
+ ```
705
+ irb(main):007:0> LinuxStat::PrettifyBytes.convert_binary(1000)
706
+ => "1000.00 bytes"
707
+
708
+ irb(main):008:0> LinuxStat::PrettifyBytes.convert_binary(10000)
709
+ => "9.77 kibibytes"
710
+
711
+ irb(main):009:0> LinuxStat::PrettifyBytes.convert_binary(100000)
712
+ => "97.66 kibibytes"
713
+
714
+ irb(main):010:0> LinuxStat::PrettifyBytes.convert_binary(10 ** 13)
715
+ => "9.09 tebibytes"
716
+ ```
717
+
718
+ To convert them to short Metric decimal suffixes:
719
+
720
+ ```
721
+ irb(main):017:0> LinuxStat::PrettifyBytes.convert_short_decimal(1000)
722
+ => "1.00 kB"
723
+
724
+ irb(main):018:0> LinuxStat::PrettifyBytes.convert_short_decimal(10000)
725
+ => "10.00 kB"
726
+
727
+ irb(main):019:0> LinuxStat::PrettifyBytes.convert_short_decimal(100000)
728
+ => "100.00 kB"
729
+
730
+ irb(main):020:0> LinuxStat::PrettifyBytes.convert_short_decimal(10 ** 13)
731
+ => "10.00 TB"
732
+ ```
733
+
734
+ To convert them to short IEC binary suffixes:
735
+
736
+ ```
737
+ irb(main):013:0> LinuxStat::PrettifyBytes.convert_short_binary(1000)
738
+ => "1000 B"
739
+
740
+ irb(main):014:0> LinuxStat::PrettifyBytes.convert_short_binary(10000)
741
+ => "9.77 KiB"
742
+
743
+ irb(main):015:0> LinuxStat::PrettifyBytes.convert_short_binary(100000)
744
+ => "97.66 KiB"
745
+
746
+ irb(main):016:0> LinuxStat::PrettifyBytes.convert_short_binary(10 ** 13)
747
+ => "9.09 TiB"
748
+ ```
749
+
750
+ It can support values upto hundreds of yottabytes and yobibytes, or yb and yib.
751
+
647
752
  Read the ri documentation for more info.
648
753
 
649
754
  ---
@@ -1,7 +1,9 @@
1
1
  #include <sys/statvfs.h>
2
2
  #include "ruby.h"
3
+
3
4
  #pragma GCC optimize ("O3")
4
5
  #pragma clang optimize on
6
+ #pragma once
5
7
 
6
8
  static VALUE statfs(VALUE obj, VALUE dir) {
7
9
  struct statvfs buf ;
@@ -1,6 +1,10 @@
1
1
  #include <unistd.h>
2
2
  #include "ruby.h"
3
3
 
4
+ #pragma GCC optimize ("O3")
5
+ #pragma clang optimize on
6
+ #pragma once
7
+
4
8
  static VALUE getTick(VALUE obj) {
5
9
  return INT2FIX(sysconf(_SC_CLK_TCK)) ;
6
10
  }
@@ -1,7 +1,9 @@
1
1
  #include <sys/utsname.h>
2
2
  #include "ruby.h"
3
+
3
4
  #pragma GCC optimize ("O3")
4
5
  #pragma clang optimize on
6
+ #pragma once
5
7
 
6
8
  static struct utsname buf ;
7
9
  static short status ;
@@ -1,21 +1,27 @@
1
+ # Independed and LinuxStat specific unrelated modules
1
2
  require "linux_stat/version"
3
+ require 'linux_stat/prettify_bytes'
4
+
5
+ # Independed and LinuxStat related modules
2
6
  require "linux_stat/battery"
3
7
  require "linux_stat/bios"
4
8
  require "linux_stat/cpu"
5
9
  require "linux_stat/memory"
6
10
  require "linux_stat/net"
11
+ require "linux_stat/process"
12
+ require "linux_stat/swap"
7
13
 
14
+ # LinuxStat::Uname dependent modules
8
15
  require 'linux_stat/utsname'
9
16
  require "linux_stat/os"
10
17
 
11
- require "linux_stat/process"
12
- require "linux_stat/swap"
13
- require "linux_stat/mounts"
14
-
18
+ # LinuxStat::FS dependent modules
15
19
  require "linux_stat/fs_stat"
16
20
  require "linux_stat/filesystem"
21
+ require "linux_stat/mounts"
17
22
 
23
+ # LinuxStat::Sysconf dependent modules
18
24
  require "linux_stat/sysconf"
19
25
  require "linux_stat/kernel"
20
- require "linux_stat/process_info"
21
26
  require 'linux_stat/user'
27
+ require "linux_stat/process_info"
@@ -1,8 +1,9 @@
1
1
  module LinuxStat
2
2
  module CPU
3
3
  class << self
4
- # stat(sleep = 0.075)
4
+ # stat(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
5
5
  # Where sleep is the delay to gather the data.
6
+ # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
6
7
  # This method returns the cpu usage of all threads.
7
8
  #
8
9
  # The first one is aggregated CPU usage reported by the Linux kernel.
@@ -12,7 +13,7 @@ module LinuxStat
12
13
  # {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
13
14
  #
14
15
  # If the information is not available, it will return an empty Hash
15
- def stat(sleep = 0.075)
16
+ def stat(sleep = ticks_to_ms)
16
17
  return {} unless stat?
17
18
 
18
19
  data = IO.readlines('/proc/stat').select! { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
@@ -33,20 +34,24 @@ module LinuxStat
33
34
  idle_then, idle_now = idle + iowait, idle2 + iowait2
34
35
  totald = idle_now.+(user2 + nice2 + sys2 + irq2 + softirq2 + steal2) - idle_then.+(user + nice + sys + irq + softirq + steal)
35
36
 
37
+ res = totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
38
+ res = 0.0 if res.nan?
39
+
36
40
  h.merge!(
37
- x => totald.-(idle_now - idle_then).fdiv(totald).*(100).round(2).abs
41
+ x => res
38
42
  )
39
43
  end
40
44
  end
41
45
 
42
- # total_usage(sleep = 0.075)
46
+ # total_usage(sleep = 1.0 / LinuxStat::Sysconf.sc_clk_tck)
43
47
  # Where sleep is the delay to gather the data.
48
+ # The minimum possible value at anytime is 1.0 / LinuxStat::Sysconf.sc_clk_tck
44
49
  # This method returns the cpu usage of all threads.
45
50
  #
46
51
  # It's like running LinuxStat::CPU.stat[0] but it's much more efficient and calculates just the aggregated usage which is available at the top of the /proc/stat file.
47
52
  #
48
53
  # If the information is not available, it will return nil.
49
- def total_usage(sleep = 0.075)
54
+ def total_usage(sleep = ticks_to_ms)
50
55
  return nil unless stat?
51
56
 
52
57
  data = IO.foreach('/proc/stat').first.split.tap(&:shift).map!(&:to_f)
@@ -113,6 +118,10 @@ module LinuxStat
113
118
  def stat?
114
119
  @@stat_readable ||= File.readable?('/proc/stat')
115
120
  end
121
+
122
+ def ticks_to_ms
123
+ @@ms ||= 1.0 / LinuxStat::Sysconf.sc_clk_tck
124
+ end
116
125
  end
117
126
  end
118
127
  end
@@ -1,7 +1,8 @@
1
1
  module LinuxStat
2
2
  module Mounts
3
3
  class << self
4
- # Reads /proc/mounts and returns list of devices.
4
+ # Reads /proc/mounts and returns the output splitted with \n.
5
+ # In other words, it's same as running IO.readlines('/proc/mounts').each(&:strip!)
5
6
  #
6
7
  # It returns an Array.
7
8
  # If the info isn't available or /proc/mounts is not readable, it will return an empty Array.
@@ -9,6 +10,14 @@ module LinuxStat
9
10
  mounts
10
11
  end
11
12
 
13
+ # Reads /proc/mounts and returns list of devices.
14
+ #
15
+ # It returns an Array.
16
+ # If the info isn't available or /proc/mounts is not readable, it will return an empty Array.
17
+ def list_devices
18
+ mounts.map { |x| x.split(?\s.freeze).first }
19
+ end
20
+
12
21
  # Reads /proc/mounts and returns partition name of the device mounted at /.
13
22
  #
14
23
  # It returns a String.
@@ -45,6 +54,135 @@ module LinuxStat
45
54
  ret
46
55
  end
47
56
 
57
+ # mount_point(dev = root)
58
+ # Where device = block device.
59
+ # The default argument is the root block device.
60
+ #
61
+ # It helps you find the mountpoint of a block device.
62
+ # For example:
63
+ # LinuxStat::Mounts.mount_point('/dev/sdb1')
64
+ # => "/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f"
65
+ #
66
+ # The return type is String.
67
+ # But if the status isn't available or the device isn't mounted, it will return an empty String.
68
+ def mount_point(dev = root)
69
+ m = ''
70
+ mounts.each do |x|
71
+ x.strip!
72
+
73
+ unless x.empty?
74
+ _x = x.split
75
+ if _x[0] == dev
76
+ m.replace(_x[1])
77
+ break
78
+ end
79
+ end
80
+ end
81
+ m
82
+ end
83
+
84
+ # list_devices_mount_point()
85
+ #
86
+ # It shows all the block devices corresponding to mount points.
87
+ #
88
+ # For example:
89
+ # LinuxStat::Mounts.list_devices_mount_point
90
+ # => {"proc"=>"/proc", "sys"=>"/sys", "dev"=>"/dev", "run"=>"/run", "/dev/sda2"=>"/", "securityfs"=>"/sys/kernel/security", "tmpfs"=>"/run/user/1000", "devpts"=>"/dev/pts", "cgroup2"=>"/sys/fs/cgroup/unified", "cgroup"=>"/sys/fs/cgroup/perf_event", "pstore"=>"/sys/fs/pstore", "none"=>"/sys/fs/bpf", "systemd-1"=>"/proc/sys/fs/binfmt_misc", "debugfs"=>"/sys/kernel/debug", "mqueue"=>"/dev/mqueue", "hugetlbfs"=>"/dev/hugepages", "tracefs"=>"/sys/kernel/tracing", "configfs"=>"/sys/kernel/config", "fusectl"=>"/sys/fs/fuse/connections", "gvfsd-fuse"=>"/run/user/1000/gvfs", "/dev/sdb1"=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", "binfmt_misc"=>"/proc/sys/fs/binfmt_misc"}
91
+ #
92
+ # The return type is Hash.
93
+ # But if the status isn't available or the device isn't mounted, it will return an empty String.
94
+ def list_devices_mount_point
95
+ m = {}
96
+ mounts.each do |x|
97
+ x.strip!
98
+
99
+ unless x.empty?
100
+ _x = x.split
101
+ m.merge!(_x[0] => _x[1])
102
+ end
103
+ end
104
+ m
105
+ end
106
+
107
+ # devices_stat
108
+ # [ Not to confuse this method with device_stat(dev) which shows only one device's info ]
109
+ #
110
+ # It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)
111
+ #
112
+ # For example:
113
+ # LinuxStat::Mounts.devices_stat
114
+ # => {"proc"=>{:mountpoint=>"/proc", :total=>0, :free=>0, :available=>0, :used=>0, :percent_used=>NaN, :percent_free=>NaN, :percent_available=>NaN}, "/dev/sdb1"=>{:mountpoint=>"/run/media/sourav/5c2b7af7-d4c3-4ab4-a035-06d18ffc8e6f", :total=>31466008576, :free=>2693931008, :available=>2693931008, :used=>28772077568, :percent_used=>91.44, :percent_free=>8.56, :percent_available=>8.56}}
115
+ #
116
+ # The return type is Hash.
117
+ # But if the status isn't available, it will return an empty Hash.
118
+ def devices_stat
119
+ # Code duplication is fine if it gives maximum performance
120
+ m = {}
121
+ mounts.each do |x|
122
+ x.strip!
123
+
124
+ unless x.empty?
125
+ _x = x.split
126
+ total, free, available, used = fs_info(_x[1])
127
+
128
+ m.merge!(_x[0] => {
129
+ mountpoint: _x[1],
130
+
131
+ total: total,
132
+ free: free,
133
+ available: available,
134
+ used: used,
135
+
136
+ percent_used: used.*(100).fdiv(total).round(2),
137
+ percent_free: free.*(100).fdiv(total).round(2),
138
+ percent_available: available.*(100).fdiv(total).round(2),
139
+ })
140
+ end
141
+ end
142
+ m
143
+ end
144
+
145
+ # device_stat(dev = root)
146
+ # [ Not to confuse this method with devices_stat() which shows all devices ]
147
+ # It shows all the block devices corresponding to mount points and data from LinuxStat::FS.stat(arg)
148
+ #
149
+ # For example:
150
+ # LinuxStat::Mounts.device_stat('/dev/sda2')
151
+ # => {"/dev/sda2"=>{:mountpoint=>"/", :total=>119981191168, :free=>35298562048, :available=>35298562048, :used=>84682629120, :percent_used=>70.58, :percent_free=>29.42, :percent_available=>29.42}}
152
+ #
153
+ # The return type is Hash.
154
+ # But if the status isn't available, it will return an empty Hash.
155
+ def device_stat(dev = root)
156
+ # Code duplication is fine if it gives maximum performance
157
+ m = {}
158
+ mounts.each do |x|
159
+ x.strip!
160
+
161
+ unless x.empty?
162
+ _x = x.split
163
+ next if _x[0] != dev
164
+
165
+ total, free, available, used = fs_info(_x[1])
166
+
167
+ m.merge!({
168
+ mountpoint: _x[1],
169
+
170
+ total: total,
171
+ free: free,
172
+ available: available,
173
+ used: used,
174
+
175
+ percent_used: used.*(100).fdiv(total).round(2),
176
+ percent_free: free.*(100).fdiv(total).round(2),
177
+ percent_available: available.*(100).fdiv(total).round(2),
178
+ })
179
+
180
+ break
181
+ end
182
+ end
183
+ m
184
+ end
185
+
48
186
  private
49
187
  def mount_readable?
50
188
  @@mount_readable ||= File.readable?('/proc/mounts')
@@ -59,6 +197,19 @@ module LinuxStat
59
197
  return [] unless mount_readable?
60
198
  @@root ||= IO.foreach('/proc/mounts').find { |x| x.split[1] == '/'.freeze }.split
61
199
  end
200
+
201
+ def fs_info(dev)
202
+ # => [total, free, available, used]
203
+ s = LinuxStat::FS.stat(dev)
204
+ s.default = 0
205
+
206
+ [
207
+ s[:block_size] * s[:blocks],
208
+ s[:block_size] * s[:block_free],
209
+ s[:block_size] * s[:block_avail_unpriv],
210
+ s[:blocks].-(s[:block_free]) * s[:block_size]
211
+ ]
212
+ end
62
213
  end
63
214
  end
64
215
  end
@@ -0,0 +1,75 @@
1
+ module LinuxStat
2
+ module PrettifyBytes
3
+ class << self
4
+ # Converts a number to decimal byte units and outputs with the metric prefix
5
+ # For example,
6
+ #
7
+ # LinuxStat::PrettifyBytes.convert_decimal(1000) # => "1.0 kilobyte"
8
+ # LinuxStat::PrettifyBytes.convert_decimal(1000 ** 3) # => "1.0 gigabyte"
9
+ # LinuxStat::PrettifyBytes.convert_decimal(1024 ** 3) # => "1.07 gigabytes"
10
+ def convert_decimal(n)
11
+ @@d_units ||= %W(#{''} kilo mega giga tera peta exa zetta)
12
+ .map.with_index { |x, i| [x, 1000.**(i + 1)] }
13
+ unit = @@d_units.find { |x| n < x[1] } || ['yotta'.freeze, 10 ** 27]
14
+
15
+ converted = n.fdiv(unit[1] / 1000).round(2)
16
+ "#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
17
+ end
18
+
19
+ # Converts a number to binary byte units and outputs with the IEC prefix
20
+ # For example,
21
+ #
22
+ # LinuxStat::PrettifyBytes.convert_binary(1000) # => "1000.0 bytes"
23
+ # LinuxStat::PrettifyBytes.convert_binary(1000 ** 3) # => "953.67 mebibytes"
24
+ # LinuxStat::PrettifyBytes.convert_binary(1024 ** 3) # => "1.0 gibibyte"
25
+ def convert_binary(n)
26
+ @@b_units ||= %W(#{''} kibi mebi gibi tebi pebi exbi zebi)
27
+ .map.with_index { |x, i| [x, 1024.**(i + 1)] }
28
+ unit = @@b_units.find { |x| n < x[1] } || ['yobi'.freeze, 10 ** 27]
29
+
30
+ converted = n.fdiv(unit[1] / 1024).round(2)
31
+ "#{pad_left(converted)} #{unit[0]}byte#{?s.freeze if converted != 1}"
32
+ end
33
+
34
+ # Converts a number to decimal byte units
35
+ # For example,
36
+ #
37
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000) # => "1.0 kB"
38
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1000 ** 3) # => "1.0 GB"
39
+ # LinuxStat::PrettifyBytes.convert_short_decimal(1024 ** 3) # => "1.07 GB"
40
+ def convert_short_decimal(n)
41
+ @@sd_units ||= %W(#{''} k M G T P E Z)
42
+ .map.with_index { |x, i| [x, 1000.**(i + 1)] }
43
+ unit = @@sd_units.find { |x| n < x[1] } || [?Y.freeze, 10 ** 27]
44
+
45
+ converted = n.fdiv(unit[1] / 1000).round(2)
46
+ "#{pad_left(converted)} #{unit[0]}B"
47
+ end
48
+
49
+ # Converts a number to binary byte units
50
+ # For example,
51
+ #
52
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000) # => "1000 B"
53
+ # LinuxStat::PrettifyBytes.convert_short_binary(1000 ** 3) # => "953.67 MiB"
54
+ # LinuxStat::PrettifyBytes.convert_short_binary(1024 ** 3) # => "1.0 GiB"
55
+ def convert_short_binary(n)
56
+ return "#{n} B" if n < 1024
57
+
58
+ @@sb_units ||= %W(#{''} K M G T P E Z)
59
+ .map.with_index { |x, i| [x, 1024.**(i + 1)] }
60
+ unit = @@sb_units.find { |x| n < x[1] } || [?Y.freeze, 1024 ** 9]
61
+
62
+ converted = n.fdiv(unit[1] / 1024).round(2)
63
+ "#{pad_left(converted)} #{unit[0]}iB"
64
+ end
65
+
66
+ private
67
+ def pad_left(n, mantissa_length = 2)
68
+ n = n.round(mantissa_length)
69
+ exp, mant = n.to_s.split(?..freeze)
70
+ m = mant.length < mantissa_length ? mant + ?0.freeze * (mantissa_length - mant.length) : mant
71
+ exp + ?..freeze + m
72
+ end
73
+ end
74
+ end
75
+ end
@@ -172,9 +172,11 @@ module LinuxStat
172
172
  _vm_rss ? _vm_rss.split[1].to_i : nil
173
173
  end
174
174
 
175
- # cpu_stat(pid: $$, sleep: 0.05)
175
+ # cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
176
176
  # Where pid is the process ID and sleep time is the interval between measurements.
177
- # By default it is the id of the current process ($$), and sleep is 0.05
177
+ #
178
+ # By default it is the id of the current process ($$), and sleep is LinuxStat::Sysconf.sc_clk_tck
179
+ # The smallest amount of available sleep time is 1.0 / LinuxStat::Sysconf.sc_clk_tck.
178
180
  #
179
181
  # Note 1:
180
182
  # Do note that the sleep time can slow down your application.
@@ -204,7 +206,7 @@ module LinuxStat
204
206
  #
205
207
  # The :last_executed_cpu also returns an Integer indicating
206
208
  # the last executed cpu of the process.
207
- def cpu_stat(pid: $$, sleep: 0.05)
209
+ def cpu_stat(pid: $$, sleep: ticks_to_ms)
208
210
  file = "/proc/#{pid}/stat"
209
211
  return {} unless File.readable?(file)
210
212
 
@@ -236,9 +238,11 @@ module LinuxStat
236
238
  }
237
239
  end
238
240
 
239
- # cpu_usage(pid: $$, sleep: 0.05)
241
+ # cpu_usage(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
240
242
  # Where pid is the process ID and sleep time is the interval between measurements.
241
- # By default it is the id of the current process ($$), and sleep is 0.05
243
+ #
244
+ # By default it is the id of the current process ($$), and sleep is 1.0 / LinuxStat::Sysconf.sc_clk_tck
245
+ # The smallest amount of available sleep time is LinuxStat::Sysconf.sc_clk_tck.
242
246
  #
243
247
  # It retuns the CPU usage in Float.
244
248
  # For example:
@@ -248,7 +252,7 @@ module LinuxStat
248
252
  # But if the info isn't available, it will return nil.
249
253
  #
250
254
  # This method is more efficient than running LinuxStat::ProcessInfo.cpu_stat()
251
- def cpu_usage(pid: $$, sleep: 0.05)
255
+ def cpu_usage(pid: $$, sleep: ticks_to_ms)
252
256
  file = "/proc/#{pid}/stat"
253
257
  return nil unless File.readable?(file)
254
258
 
@@ -305,17 +309,73 @@ module LinuxStat
305
309
  file = "/proc/#{pid}/stat".freeze
306
310
  return nil unless File.readable?(file)
307
311
 
308
- IO.read("/proc/#{pid}/stat".freeze).split[38].to_i
312
+ IO.read(file).split[38].to_i
313
+ end
314
+
315
+ # uid(pid = $$)
316
+ # returns the UIDs of the process as an Array of Integers.
317
+ #
318
+ # If the info isn't available it returns an empty Array.
319
+ def uid(pid = $$)
320
+ file = "/proc/#{pid}/status".freeze
321
+ return nil unless File.readable?(file)
322
+
323
+ data = IO.readlines(file.freeze).find { |x|
324
+ x[/Uid.*\d*/]
325
+ }.to_s.split.drop(1)
326
+
327
+ {
328
+ real: data[0].to_i,
329
+ effective: data[1].to_i,
330
+ saved_set: data[2].to_i,
331
+ filesystem_uid: data[3].to_i
332
+ }
333
+ end
334
+
335
+ # gid(pid = $$)
336
+ # returns the GIDs of the process as an Hash containing the following data:
337
+ # :real, :effective, :saved_set, :filesystem_uid
338
+ #
339
+ # If the info isn't available it returns an empty Hash.
340
+ def gid(pid = $$)
341
+ file = "/proc/#{pid}/status".freeze
342
+ return nil unless File.readable?(file)
343
+
344
+ data = IO.readlines(file.freeze).find { |x|
345
+ x[/Gid.*\d*/]
346
+ }.split.drop(1)
347
+
348
+ {
349
+ real: data[0].to_i,
350
+ effective: data[1].to_i,
351
+ saved_set: data[2].to_i,
352
+ filesystem_uid: data[3].to_i
353
+ }
309
354
  end
310
355
 
311
- # def owned_by
356
+ # owner(pid = $$)
357
+ # Returns the owner of the process
358
+ # But if the status is not available, it will return an empty frozen String.
359
+ def owner(pid = $$)
360
+ file = "/proc/#{pid}/status".freeze
361
+ return ''.freeze unless File.readable?(file)
362
+
363
+ gid = IO.readlines(file.freeze).find { |x|
364
+ x[/Gid.*\d*/]
365
+ }.split.drop(1)[2].to_i
312
366
 
313
- # end
367
+ LinuxStat::User.username_by_gid(gid)
368
+ end
314
369
 
315
370
  private
316
371
  def get_ticks
317
372
  @@ticks ||= Sysconf.sc_clk_tck
318
373
  end
374
+
375
+ # Just to avoid multiple calculations!...
376
+ def ticks_to_ms
377
+ @@ms ||= 1.0 / get_ticks
378
+ end
319
379
  end
320
380
  end
321
381
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.5.2"
2
+ VERSION = "0.6.1"
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: 0.5.2
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-06 00:00:00.000000000 Z
11
+ date: 2020-12-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem
@@ -44,6 +44,7 @@ files:
44
44
  - lib/linux_stat/mounts.rb
45
45
  - lib/linux_stat/net.rb
46
46
  - lib/linux_stat/os.rb
47
+ - lib/linux_stat/prettify_bytes.rb
47
48
  - lib/linux_stat/process.rb
48
49
  - lib/linux_stat/process_info.rb
49
50
  - lib/linux_stat/swap.rb