linux_stat 2.5.0 → 2.5.1

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: 2fd7fd4cae80d13f9e3004928809ada275053cffc32630d05f7ab3f1fb9c2514
4
+ data.tar.gz: 36f07d5f2f149979466a30d63d6d8cc31e77a028454be0bbe25b2af0513e1bb4
5
5
  SHA512:
6
- metadata.gz: 362dfab83cc06f4b84eee112fd0e5ca405b75ad91c94639eed76c3961514ea56d75e6f263624140a08f64298b2a080753d247f2ee3ce7d3be3a9c0023e3d4d53
7
- data.tar.gz: 14c6912aba0cf41e85fb6e79ba0c71496512b5b740db8457fe8af4f68588e06601170823a00ecab5fcef88e6c28feb8fcb98ee27c3c42bcff2ee11ac61a5194d
6
+ metadata.gz: 2ef9b96aacee268b4c0d74a8093d4161a0d21fea3165533c0d0f993c6cda302f1e96f51a1292192b18ffed35b2a0f6e83fdb2ac03583da8e088e97ec2c230f19
7
+ data.tar.gz: 82c397f568ebfd7f578b401ec245b10971412931b06df1433e6637848f506a642eb68046025ad3c3a66bafe678aea1b4845507cbc9a2a13d80f107ec93d667c4
@@ -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
@@ -1,3 +1,3 @@
1
1
  module LinuxStat
2
- VERSION = "2.5.0"
2
+ VERSION = "2.5.1"
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: 2.5.0
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami