linux_stat 0.6.1 → 0.6.2

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: e6d3a089c2e214eb53f564d64014d6da9b900467e8b8285be6be6516c92fb366
4
- data.tar.gz: 6cc0a5b86b5b9163d2ae1d57b761d8cf6900ff2864987c3b2b39a5072e5d8174
3
+ metadata.gz: 98045c378f59845c66b01f0e311faed27a4def546bea99354bfd04a4883a5d38
4
+ data.tar.gz: fe4413395bca5cae357413deba7bd3f1316c845d4652289688fe8c15ee38cc13
5
5
  SHA512:
6
- metadata.gz: e9d1ce304cec1210fd3cb4f75b36a105b35bc54828106d1d880331b5d8ec6429f120be21200014599a22693950386aa838e03cc20a0fc41c0b819204c6c2da81
7
- data.tar.gz: 45ea80ecccb20956945b420bfec45ca0e3f3770233418469fd741f554977c9ab07cb14002f9a887094db0b078f5ae9cc5c1b6a25865e8024a5ade4c278dd7ed8
6
+ metadata.gz: fef16c595ecac5b2c5ad86851cd6dd8fe27211e7905f315c68ce5cb451e3ee75bf0feff1913ae9b1b66e1fdc4a935d456b4f62b758ee19f592e378c8aa2f78d1
7
+ data.tar.gz: 84cc82dbd7fe6a080f1ddd1b5c7ea4aa613ea58816893ae681187e859676c111d0cdb988fb26a8f15fd607cd37ec951f30c0492ed722e0b46bfa56449ff69eb8
data/README.md CHANGED
@@ -674,6 +674,25 @@ irb(main):005:0> LinuxStat::User.gid_by_username('InvalidUser')
674
674
  => nil
675
675
  ```
676
676
 
677
+ Or to get the current user (in docker for example):
678
+
679
+ ```
680
+ $ irb
681
+ irb(main):001:0> require 'linux_stat'
682
+ => true
683
+
684
+ irb(main):002:0> LinuxStat::User.get_current_user
685
+ => "x"
686
+
687
+ irb(main):003:0> LinuxStat::User.get_user
688
+ => "x"
689
+
690
+ irb(main):004:0> LinuxStat::User.get_login
691
+ => ""
692
+ ```
693
+
694
+ Right, the get_login() can return an empty string. But LinuxStat::User.get_user also aliased as LinuxStat::User.get_current_user shouldn't return an empty string under most circumstances.
695
+
677
696
  ## Note 5: PrettifyBytes
678
697
  Often times we need to work with KB, MB GB, TB, or KiB, MiB, GiB, TiB, etc.
679
698
  And we need some work to convert bytes to those units.
@@ -683,6 +702,7 @@ To avoid such duplication, it comes with a PrettifyBytes module.
683
702
  For example, to convert bytes to decimal suffixes:
684
703
 
685
704
  ```
705
+ $irb
686
706
  irb(main):001:0> require 'linux_stat'
687
707
  => true
688
708
 
@@ -702,52 +722,70 @@ irb(main):005:0> LinuxStat::PrettifyBytes.convert_decimal(10 ** 13)
702
722
  To convert bytes to binary suffixes:
703
723
 
704
724
  ```
705
- irb(main):007:0> LinuxStat::PrettifyBytes.convert_binary(1000)
725
+ irb(main):006:0> LinuxStat::PrettifyBytes.convert_binary(1000)
706
726
  => "1000.00 bytes"
707
727
 
708
- irb(main):008:0> LinuxStat::PrettifyBytes.convert_binary(10000)
728
+ irb(main):007:0> LinuxStat::PrettifyBytes.convert_binary(10000)
709
729
  => "9.77 kibibytes"
710
730
 
711
- irb(main):009:0> LinuxStat::PrettifyBytes.convert_binary(100000)
731
+ irb(main):008:0> LinuxStat::PrettifyBytes.convert_binary(100000)
712
732
  => "97.66 kibibytes"
713
733
 
714
- irb(main):010:0> LinuxStat::PrettifyBytes.convert_binary(10 ** 13)
734
+ irb(main):009:0> LinuxStat::PrettifyBytes.convert_binary(10 ** 13)
715
735
  => "9.09 tebibytes"
716
736
  ```
717
737
 
718
738
  To convert them to short Metric decimal suffixes:
719
739
 
720
740
  ```
721
- irb(main):017:0> LinuxStat::PrettifyBytes.convert_short_decimal(1000)
741
+ irb(main):010:0> LinuxStat::PrettifyBytes.convert_short_decimal(1000)
722
742
  => "1.00 kB"
723
743
 
724
- irb(main):018:0> LinuxStat::PrettifyBytes.convert_short_decimal(10000)
744
+ irb(main):011:0> LinuxStat::PrettifyBytes.convert_short_decimal(10000)
725
745
  => "10.00 kB"
726
746
 
727
- irb(main):019:0> LinuxStat::PrettifyBytes.convert_short_decimal(100000)
747
+ irb(main):012:0> LinuxStat::PrettifyBytes.convert_short_decimal(100000)
728
748
  => "100.00 kB"
729
749
 
730
- irb(main):020:0> LinuxStat::PrettifyBytes.convert_short_decimal(10 ** 13)
750
+ irb(main):013:0> LinuxStat::PrettifyBytes.convert_short_decimal(10 ** 13)
731
751
  => "10.00 TB"
732
752
  ```
733
753
 
734
754
  To convert them to short IEC binary suffixes:
735
755
 
736
756
  ```
737
- irb(main):013:0> LinuxStat::PrettifyBytes.convert_short_binary(1000)
757
+ irb(main):014:0> LinuxStat::PrettifyBytes.convert_short_binary(1000)
738
758
  => "1000 B"
739
759
 
740
- irb(main):014:0> LinuxStat::PrettifyBytes.convert_short_binary(10000)
760
+ irb(main):015:0> LinuxStat::PrettifyBytes.convert_short_binary(10000)
741
761
  => "9.77 KiB"
742
762
 
743
- irb(main):015:0> LinuxStat::PrettifyBytes.convert_short_binary(100000)
763
+ irb(main):016:0> LinuxStat::PrettifyBytes.convert_short_binary(100000)
744
764
  => "97.66 KiB"
745
765
 
746
- irb(main):016:0> LinuxStat::PrettifyBytes.convert_short_binary(10 ** 13)
766
+ irb(main):017:0> LinuxStat::PrettifyBytes.convert_short_binary(10 ** 13)
747
767
  => "9.09 TiB"
748
768
  ```
749
769
 
750
- It can support values upto hundreds of yottabytes and yobibytes, or yb and yib.
770
+ It can support values upto hundreds of yottabytes and yobibytes, or yb and yib. You can also do stuff like:
771
+
772
+ ```
773
+ $ irb
774
+ irb(main):001:0> require 'linux_stat'
775
+ => true
776
+
777
+ irb(main):002:0> LinuxStat::PrettifyBytes.convert_short_decimal(LinuxStat::Mounts.device_stat('/dev/sdb1')[:total])
778
+ => "31.47 GB"
779
+
780
+ irb(main):003:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:total])
781
+ => "29.31 GiB"
782
+
783
+ irb(main):004:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:used])
784
+ => "26.80 GiB"
785
+
786
+ irb(main):005:0> LinuxStat::PrettifyBytes.convert_short_binary(LinuxStat::Mounts.device_stat('/dev/sdb1')[:available])
787
+ => "2.51 GiB"
788
+ ```
751
789
 
752
790
  Read the ri documentation for more info.
753
791
 
@@ -25,7 +25,7 @@ static VALUE getOpenMax(VALUE obj) {
25
25
  return INT2FIX(sysconf(_SC_OPEN_MAX)) ;
26
26
  }
27
27
 
28
- static VALUE getPageSizeMax(VALUE obj) {
28
+ static VALUE getPageSize(VALUE obj) {
29
29
  return INT2FIX(sysconf(_SC_PAGESIZE)) ;
30
30
  }
31
31
 
@@ -67,7 +67,7 @@ void Init_sysconf() {
67
67
  rb_define_module_function(_sysconf, "hostname_max", getHostnameMax, 0) ;
68
68
  rb_define_module_function(_sysconf, "login_name_max", getLoginNameMax, 0) ;
69
69
  rb_define_module_function(_sysconf, "open_max", getOpenMax, 0) ;
70
- rb_define_module_function(_sysconf, "page_size_max", getPageSizeMax, 0) ;
70
+ rb_define_module_function(_sysconf, "pagesize", getPageSize, 0) ;
71
71
  rb_define_module_function(_sysconf, "stream_max", getStreamMax, 0) ;
72
72
  rb_define_module_function(_sysconf, "tty_name_max", getTTYNameMax, 0) ;
73
73
  rb_define_module_function(_sysconf, "posix_version", getPosixVersion, 0) ;
@@ -70,10 +70,10 @@ module LinuxStat
70
70
  # By default it is the id of the current process ($$)
71
71
  #
72
72
  # It retuns the memory, virtual memory, and resident memory of the process.
73
- # All values are in Kilobytes.
73
+ # All values are in kilobytes.
74
74
  #
75
75
  # The output is a Hash. For example, a sample output:
76
- # {:memory=>8656, :virtual_memory=>78272, :resident_memory=>14072}
76
+ # {:memory=>8515.584, :virtual_memory=>79781.888, :resident_memory=>13955.072}
77
77
  #
78
78
  # Note:
79
79
  # If you need only memory usage of a process, run LinuxStat::ProcessInfo.memory(pid)
@@ -85,34 +85,22 @@ module LinuxStat
85
85
  #
86
86
  # If the info isn't available it will return an empty Hash.
87
87
  def mem_stat(pid = $$)
88
- stat_file = "/proc/#{pid}/stat".freeze
89
- status_file = "/proc/#{pid}/status".freeze
88
+ statm = "/proc/#{pid}/statm".freeze
90
89
 
91
- stat = if File.readable?(stat_file)
92
- IO.read(stat_file).split
90
+ data = if File.readable?(statm)
91
+ IO.read(statm).split
93
92
  else
94
- []
93
+ return {}
95
94
  end
96
95
 
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
96
+ _rss_anon = (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
97
+ _virtual_memory = data[0] ? data[0].to_i*(pagesize).fdiv(1000) : nil
98
+ _resident_memory = data[1] ? data[1].to_i.*(pagesize).fdiv(1000) : nil
111
99
 
112
100
  {
113
- memory: rss_anon,
114
- virtual_memory: vm,
115
- resident_memory: vm_rss
101
+ memory: _rss_anon,
102
+ virtual_memory: _virtual_memory,
103
+ resident_memory: _resident_memory
116
104
  }
117
105
  end
118
106
 
@@ -121,17 +109,17 @@ module LinuxStat
121
109
  # By default it is the id of the current process ($$)
122
110
  #
123
111
  # It retuns the memory of the process.
124
- # The value is in Kilobytes.
112
+ # The value is in kilobytes.
125
113
  # The output is an Integer. For example, a sample output:
126
- # 8664
114
+ # 8523.776
127
115
  #
128
116
  # If the info isn't available it will return nil.
129
117
  def memory(pid = $$)
130
- file = "/proc/#{pid}/status".freeze
118
+ file = "/proc/#{pid}/statm".freeze
131
119
  return nil unless File.readable?(file)
132
120
 
133
- _rss_anon = IO.readlines(file).find { |x| x.start_with?('RssAnon') }
134
- _rss_anon ? _rss_anon.split[1].to_i : nil
121
+ data = IO.read(file).split
122
+ (data[1] && data[2]) ? data[1].to_i.-(data[2].to_i).*(pagesize).fdiv(1000) : nil
135
123
  end
136
124
 
137
125
  # virtual_memory(pid = $$)
@@ -139,17 +127,17 @@ module LinuxStat
139
127
  # By default it is the id of the current process ($$)
140
128
  #
141
129
  # It retuns the virtual memory for the process.
142
- # The value is in Kilobytes.
130
+ # The value is in kilobytes.
143
131
  # The output is an Integer. For example, a sample output:
144
- # 78376
132
+ # 79781.888
145
133
  #
146
134
  # If the info isn't available it will return nil.
147
135
  def virtual_memory(pid = $$)
148
- file = "/proc/#{pid}/stat".freeze
136
+ file = "/proc/#{pid}/statm".freeze
149
137
  return nil unless File.readable?(file)
150
138
 
151
- _virtual_memory = IO.read(file).split[22]
152
- _virtual_memory ? _virtual_memory.to_i.fdiv(1024).to_i : nil
139
+ _virtual_memory = IO.read(file).split[0]
140
+ _virtual_memory ? _virtual_memory.to_i.*(pagesize).fdiv(1000) : nil
153
141
  end
154
142
 
155
143
  # resident_memory(pid = $$)
@@ -157,19 +145,17 @@ module LinuxStat
157
145
  # By default it is the id of the current process ($$)
158
146
  #
159
147
  # It retuns the resident memory for the process.
160
- # The value is in Kilobytes.
148
+ # The value is in kilobytes.
161
149
  # The output is an Integer. For example, a sample output:
162
- # 14012
150
+ # 13996.032
163
151
  #
164
152
  # If the info isn't available it will return nil.
165
153
  def resident_memory(pid = $$)
166
- file = "/proc/#{pid}/status".freeze
154
+ file = "/proc/#{pid}/statm".freeze
167
155
  return nil unless File.readable?(file)
168
156
 
169
- _vm_rss = IO.readlines(file)
170
- .find { |x| x.start_with?('VmRSS') }
171
-
172
- _vm_rss ? _vm_rss.split[1].to_i : nil
157
+ _vm_rss = IO.read(file).split[1]
158
+ _vm_rss ? _vm_rss.to_i.*(pagesize).fdiv(1000) : nil
173
159
  end
174
160
 
175
161
  # cpu_stat(pid: $$, sleep: 1.0 / LinuxStat::Sysconf.sc_clk_tck)
@@ -292,7 +278,8 @@ module LinuxStat
292
278
  file = "/proc/#{pid}/stat".freeze
293
279
  return nil unless File.readable?(file)
294
280
 
295
- IO.read(file).split[19].to_i
281
+ data = IO.read(file).split[19]
282
+ data ? data.to_i : nil
296
283
  end
297
284
 
298
285
  # last_executed_cpu(pid = $$)
@@ -376,6 +363,10 @@ module LinuxStat
376
363
  def ticks_to_ms
377
364
  @@ms ||= 1.0 / get_ticks
378
365
  end
366
+
367
+ def pagesize
368
+ @@pagesize ||= LinuxStat::Sysconf.pagesize
369
+ end
379
370
  end
380
371
  end
381
372
  end
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
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.6.1
4
+ version: 0.6.2
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-08 00:00:00.000000000 Z
11
+ date: 2020-12-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Linux only, efficient linux system utilization reporting and system monitoring
14
14
  gem