Skip to content

Commit c38b917

Browse files
committed
Enhance the CPU implementation
1 parent b118a59 commit c38b917

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: hardware
2-
version: 0.2.0
2+
version: 0.2.1
33

44
authors:
55
- bararchy <[email protected]>

spec/cpu_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ describe Hardware::CPU do
66
end
77

88
it "checks the percentage used" do
9-
(0 < Hardware::CPU.used <= 100).should be_true
9+
(0 <= Hardware::CPU.used <= 100).should be_true
1010
end
1111
end

src/hardware.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "./hardware/*"
22

33
module Hardware
4-
VERSION = "0.2.0"
4+
VERSION = "0.2.1"
55
end

src/hardware/cpu.cr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Hardware::CPU
22
def self.info
33
cpu = File.read("/proc/stat").lines.first[5..-1].split(' ').map &.to_i
4+
# Array: user nice system idle iowait irq softirq steal guest guest_nice
45
{
5-
used: cpu[0] + cpu[1] + cpu[2],
6-
idle: cpu[3],
7-
total: cpu[0] + cpu[1] + cpu[2] + cpu[3],
6+
used: used = cpu[0] + cpu[1] + cpu[2] + cpu[5] + cpu[6] + cpu[7],
7+
idle: idle = cpu[3] + cpu[4],
8+
total: used + idle,
89
}
910
end
1011

@@ -14,6 +15,6 @@ module Hardware::CPU
1415
proc1 = info
1516

1617
# 100 * Usage / Total
17-
100 * ((proc1[:used] - proc0[:used]).to_f32 / (proc1[:total] - proc0[:total]).to_f32)
18+
(100 * ((proc1[:used] - proc0[:used]).to_f32 / (proc1[:total] - proc0[:total]))).round
1819
end
1920
end

0 commit comments

Comments
 (0)