linux_stat 2.5.0 → 2.5.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 +4 -4
- data/lib/linux_stat/cpu.rb +36 -7
- data/lib/linux_stat/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fd7fd4cae80d13f9e3004928809ada275053cffc32630d05f7ab3f1fb9c2514
|
4
|
+
data.tar.gz: 36f07d5f2f149979466a30d63d6d8cc31e77a028454be0bbe25b2af0513e1bb4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ef9b96aacee268b4c0d74a8093d4161a0d21fea3165533c0d0f993c6cda302f1e96f51a1292192b18ffed35b2a0f6e83fdb2ac03583da8e088e97ec2c230f19
|
7
|
+
data.tar.gz: 82c397f568ebfd7f578b401ec245b10971412931b06df1433e6637848f506a642eb68046025ad3c3a66bafe678aea1b4845507cbc9a2a13d80f107ec93d667c4
|
data/lib/linux_stat/cpu.rb
CHANGED
@@ -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*/] }
|
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*/] }
|
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 =
|
37
|
-
|
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
|
-
|
41
|
-
|
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(
|
78
|
+
h.store(cpu_core, res)
|
50
79
|
h
|
51
80
|
end
|
52
81
|
end
|
data/lib/linux_stat/version.rb
CHANGED