linux_stat 2.5.0 → 2.5.3

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: 938eb126f7b43a731ec3d8de134f606411ff2245011eca7416782daaddd4220d
4
- data.tar.gz: 8c171887831dcbd04b24f6301ba9a8450bc9510708c3c931aac6c256444b2477
3
+ metadata.gz: 75ee9aa13037e326a0c04626cd28e0903b06372ce24344ffa38870f98dd6f0dd
4
+ data.tar.gz: d275807c8a9572fa9faaa1c562102302697f52b6a1cb36907fa71b2576c533b0
5
5
  SHA512:
6
- metadata.gz: 362dfab83cc06f4b84eee112fd0e5ca405b75ad91c94639eed76c3961514ea56d75e6f263624140a08f64298b2a080753d247f2ee3ce7d3be3a9c0023e3d4d53
7
- data.tar.gz: 14c6912aba0cf41e85fb6e79ba0c71496512b5b740db8457fe8af4f68588e06601170823a00ecab5fcef88e6c28feb8fcb98ee27c3c42bcff2ee11ac61a5194d
6
+ metadata.gz: 3bd3330e9135cffee27a82fc661462a9c94e421ee5eebe24fb322ad9241e5498bdfd84914fd58dab93481366969f414bf2aa93e9507bb967ca18a462c91fc007
7
+ data.tar.gz: 686e7f1935fea8cde6ae24624522ef642f41e21304fdae7e758f3249f3b026ea6261a77fca76bbc2f5153d1d8426f9112a1b66f124e7c3d03106c0992ca731aa
@@ -21,24 +21,53 @@ module LinuxStat
21
21
  #
22
22
  # => {0=>84.38, 1=>100.0, 2=>50.0, 3=>87.5, 4=>87.5}
23
23
  #
24
+ # It discards any offline CPU or disabled CPU.
25
+ # For example, if your system CPU has 4 cores, and you disabled core 3, the output will be:
26
+ # LinuxStat::CPU.stat
27
+ #
28
+ # => {0=>26.67, 1=>0.0, 2=>20.0, 4=>20.0}
29
+ #
24
30
  # If the information is not available, it will return an empty Hash
25
31
  def stat(sleep = ticks_to_ms_t5)
26
32
  return {} unless stat?
27
33
 
28
- data = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
34
+ data = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }
35
+ cpu_names1 = []
36
+ data.map! { |x|
37
+ splitted = x.split
38
+ name = splitted.shift[/\d*$/]
39
+ cpu_names1.push(name.empty? ? 0 : name.to_i + 1)
40
+ splitted.map!(&:to_f)
41
+ }
42
+
29
43
  sleep(sleep)
30
- data2 = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }.map! { |x| x.split.map!(&:to_f) }
44
+ data2 = IO.readlines('/proc/stat'.freeze).select { |x| x[/^cpu\d*/] }
45
+
46
+ cpu_names2 = []
47
+ data2.map! { |x|
48
+ splitted = x.split
49
+ name = splitted.shift[/\d*$/]
50
+ cpu_names2.push(name.empty? ? 0 : name.to_i + 1)
51
+ splitted.map!(&:to_f)
52
+ }
31
53
 
32
54
  # On devices like android, the core count can change anytime (hotplugging).
33
55
  # I had crashes on Termux.
34
56
  # So better just count the min number of CPU and iterate over that
35
57
  # If data.length is smaller than data2.length, we don't have enough data to compare.
36
- dl, d2l = data.length, data2.length
37
- min = dl > d2l ? d2l : dl
58
+ dl, d2l = cpu_names1.length, cpu_names2.length
59
+ if dl > d2l
60
+ min = d2l
61
+ cpu_cores = cpu_names2
62
+ else
63
+ min = dl
64
+ cpu_cores = cpu_names1
65
+ end
38
66
 
39
67
  min.times.reduce({}) do |h, x|
40
- user, nice, sys, idle, iowait, irq, softirq, steal = *data[x].drop(1)
41
- user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2[x].drop(1)
68
+ cpu_core = cpu_cores[x]
69
+ user, nice, sys, idle, iowait, irq, softirq, steal = *data[x]
70
+ user2, nice2, sys2, idle2, iowait2, irq2, softirq2, steal2 = *data2[x]
42
71
 
43
72
  idle_then, idle_now = idle + iowait, idle2 + iowait2
44
73
  totald = idle_now.+(user2 + nice2 + sys2 + irq2 + softirq2 + steal2) - idle_then.+(user + nice + sys + irq + softirq + steal)
@@ -46,7 +75,7 @@ module LinuxStat
46
75
  res = totald.-(idle_now - idle_then).fdiv(totald).abs.*(100)
47
76
  res = res.nan? ? 0.0 : res > 100 ? 100.0 : res.round(2)
48
77
 
49
- h.store(x, res)
78
+ h.store(cpu_core, res)
50
79
  h
51
80
  end
52
81
  end
@@ -315,6 +344,7 @@ module LinuxStat
315
344
  physical_cores = []
316
345
  hyperthreaded = {}
317
346
 
347
+ return [] unless File.executable?('/sys/devices/system/cpu/'.freeze)
318
348
  entries = Dir.entries('/sys/devices/system/cpu/'.freeze)
319
349
  entries.delete(?..freeze)
320
350
  entries.delete('..'.freeze)
@@ -354,6 +384,7 @@ module LinuxStat
354
384
  def hyperthreaded_core_list
355
385
  hyperthreaded = {}
356
386
 
387
+ return [] unless File.executable?('/sys/devices/system/cpu/'.freeze)
357
388
  entries = Dir.entries('/sys/devices/system/cpu/'.freeze)
358
389
  entries.delete(?..freeze)
359
390
  entries.delete('..'.freeze)
@@ -128,7 +128,7 @@ module LinuxStat
128
128
  # Also note that if there's no info available or no PCI enabled devices, it will return an empty
129
129
  # Hash.
130
130
  def devices_stat(hwdata: true)
131
- @@sys_pci_readable ||= File.readable?('/sys/bus/pci/devices/')
131
+ @@sys_pci_readable ||= File.executable?('/sys/bus/pci/devices/')
132
132
  return devices_info(hwdata: hwdata) unless @@sys_pci_readable
133
133
 
134
134
  Dir['/sys/bus/pci/devices/*/'.freeze].sort!.map! { |x|
@@ -215,7 +215,7 @@ module LinuxStat
215
215
  # But if the information isn't available, it will return nil.
216
216
  def count
217
217
  @@proc_pci_readable ||= File.readable?('/proc/bus/pci/devices')
218
- @@sys_pci_readable ||= File.readable?('/sys/bus/pci/devices/')
218
+ @@sys_pci_readable ||= File.executable?('/sys/bus/pci/devices/')
219
219
 
220
220
  if @@proc_pci_readable
221
221
  IO.readlines('/proc/bus/pci/devices'.freeze).length
@@ -64,7 +64,7 @@ module LinuxStat
64
64
 
65
65
  private
66
66
  def hwmon_readable?
67
- @@hwmon_readable ||= File.readable?("/sys/class/hwmon/")
67
+ @@hwmon_readable ||= File.executable?("/sys/class/hwmon/")
68
68
  end
69
69
 
70
70
  def query_hwmon(mon, key, div = false)
@@ -78,20 +78,46 @@ module LinuxStat
78
78
 
79
79
  while x = files[i += 1]
80
80
  splitted = File.split(x)
81
- path = File.join(splitted[0..-2])
82
-
83
- n = splitted[-1][/.*_/]
81
+ n = splitted.pop[/.*_/]
82
+ path = File.join(splitted)
84
83
 
85
84
  label_f = "#{path}/#{n}label"
86
- label = File.readable?(label_f) ? IO.read(label_f, encoding: 'ASCII-8BIT'.freeze).strip : nil
85
+
86
+ # Some of the files may have no content
87
+ # They can also raise Errno::EIO, Errno::ENODATA as well.
88
+ label = if File.readable?(label_f)
89
+ begin
90
+ IO.read(label_f, encoding: 'ASCII-8BIT'.freeze).strip
91
+ rescue Errno::EIO, Errno::ENODEV, Errno::ENODATA
92
+ nil
93
+ end
94
+ end
87
95
 
88
96
  temp_crit_f = "#{path}/#{n}crit"
89
- temp_crit = File.readable?(temp_crit_f) ? IO.read(temp_crit_f, encoding: 'ASCII-8BIT'.freeze).to_i : nil
97
+ temp_crit = if File.readable?(temp_crit_f)
98
+ begin
99
+ IO.read(temp_crit_f, encoding: 'ASCII-8BIT'.freeze).to_i
100
+ rescue Errno::EIO, Errno::ENODEV, Errno::ENODATA
101
+ nil
102
+ end
103
+ end
90
104
 
91
105
  temp_max_f = "#{path}/#{n}max"
92
- temp_max = File.readable?(temp_max_f) ? IO.read(temp_max_f, encoding: 'ASCII-8BIT'.freeze).to_i : nil
106
+ temp_max = if File.readable?(temp_max_f)
107
+ begin
108
+ IO.read(temp_max_f, encoding: 'ASCII-8BIT'.freeze).to_i
109
+ rescue Errno::EIO, Errno::ENODEV, Errno::ENODATA
110
+ nil
111
+ end
112
+ end
93
113
 
94
- value = File.readable?(x) ? IO.read(x).to_i : nil
114
+ value = if File.readable?(x)
115
+ begin
116
+ IO.read(x).to_i
117
+ rescue Errno::EIO, Errno::ENODEV, Errno::ENODATA
118
+ nil
119
+ end
120
+ end
95
121
 
96
122
  if dir != path
97
123
  dir = path
@@ -100,17 +126,17 @@ module LinuxStat
100
126
  end
101
127
 
102
128
  if div
103
- value /= 1000.0
129
+ value /= 1000.0 if value
104
130
  temp_max /= 1000.0 if temp_max
105
131
  temp_crit /= 1000.0 if temp_crit
106
132
  end
107
133
 
108
134
  h = {path: path, name: name}
109
135
 
110
- h.store(:label, label) if label
136
+ h.store(:label, label)
111
137
  h.store(key, value)
112
- h.store(:temp_crit, temp_crit) if temp_crit
113
- h.store(:temp_crit, temp_max) if temp_max
138
+ h.store(:temp_crit, temp_crit)
139
+ h.store(:temp_crit, temp_max)
114
140
 
115
141
  ret.push(h)
116
142
  end
@@ -47,7 +47,7 @@ module LinuxStat
47
47
  # Also note that if there's no info available or no USB devices, it will return an empty
48
48
  # Hash.
49
49
  def devices_stat(hwdata: true)
50
- @@sys_usb_readable ||= File.readable?('/sys/bus/usb/devices/')
50
+ @@sys_usb_readable ||= File.executable?('/sys/bus/usb/devices/')
51
51
  return [] unless @@sys_usb_readable
52
52
 
53
53
  Dir['/sys/bus/usb/devices/*/'.freeze].sort!.map! { |x|
@@ -137,7 +137,7 @@ module LinuxStat
137
137
  #
138
138
  # But if the information isn't available, it will return nil.
139
139
  def count
140
- @@sys_usb_readable ||= File.readable?('/sys/bus/usb/devices/')
140
+ @@sys_usb_readable ||= File.executable?('/sys/bus/usb/devices/')
141
141
  return nil unless @@sys_usb_readable
142
142
 
143
143
  Dir['/sys/bus/usb/devices/*/'.freeze].count { |x|
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "2.5.0"
2
+ VERSION = "2.5.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linux_stat
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-23 00:00:00.000000000 Z
11
+ date: 2022-05-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
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  requirements: []
93
- rubygems_version: 3.2.29
93
+ rubygems_version: 3.3.8
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Efficient linux system reporting gem