-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
612 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
name: hardware | ||
version: 0.4.0 | ||
version: 0.5.0 | ||
|
||
authors: | ||
- bararchy <bar.hofesh@safe-t.com> | ||
- Julien Reichardt <mi@jrei.ch> | ||
|
||
crystal: 0.25.1 | ||
crystal: 0.31.1 | ||
|
||
license: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
require "./spec_helper" | ||
require "spec" | ||
require "../src/cpu" | ||
|
||
describe Hardware::CPU do | ||
cpu = Hardware::CPU.new | ||
it "parses '/proc/stat'" do | ||
cpu.stat.should be_a Array(Int32) | ||
it "returns the usage compared to a previous CPU'" do | ||
cpu = Hardware::CPU.new | ||
sleep 6 | ||
cpu_usage = cpu.usage Hardware::CPU.new | ||
cpu_usage.should be > 0 | ||
cpu_usage.should be <= 100 | ||
end | ||
|
||
it "checks the percentage used" do | ||
cpu.previous_used.should be_a Int32 | ||
cpu.previous_idle_wait.should be_a Int32 | ||
it "parses the last field (guest_nice)" do | ||
Hardware::CPU.new.guest_nice.should be_a Int32 | ||
end | ||
|
||
it "checks the percentage used" do | ||
sleep 0.1 | ||
usage = cpu.usage | ||
usage.should be >= 0 | ||
usage.should be <= 100 | ||
it "returns the usage by mutating self" do | ||
cpu = Hardware::CPU.new | ||
sleep 6 | ||
cpu_usage = cpu.usage! | ||
cpu_usage.should be > 0 | ||
cpu_usage.should be <= 100 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require "./spec_helper" | ||
require "spec" | ||
|
||
describe Hardware do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require "./spec_helper" | ||
require "spec" | ||
require "../src/net" | ||
|
||
describe Hardware::Net do | ||
net = Hardware::Net.new | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require "spec" | ||
require "../../src/pid" | ||
|
||
describe Hardware::PID::Stat do | ||
describe "cpu_time" do | ||
it "returns without children" do | ||
stat = Hardware::PID::Stat.new pid: 1 | ||
stat.cpu_time.should be > 0 | ||
end | ||
|
||
it "returns with children" do | ||
stat = Hardware::PID::Stat.new pid: 1 | ||
stat.cpu_time(children: true).should be > 0 | ||
end | ||
end | ||
|
||
it "checks cpu_usage percentage of all processes" do | ||
channel = Channel(Float64).new | ||
pids_count = 0 | ||
Hardware::PID.each do |pid| | ||
spawn do | ||
stat = pid.stat | ||
sleep 4 | ||
channel.send stat.cpu_usage! | ||
end | ||
pids_count += 1 | ||
end | ||
max_cpu_usage = 0 | ||
pids_count.times do | ||
cpu_usage = channel.receive | ||
cpu_usage.should be >= 0 | ||
cpu_usage.should be <= 100 | ||
max_cpu_usage = cpu_usage if max_cpu_usage < cpu_usage | ||
end | ||
# At least one process in the system should have a cpu_usage superior to 0 | ||
max_cpu_usage.should be > 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "spec" | ||
require "../../src/pid" | ||
|
||
describe Hardware::PID::Status do | ||
status = Hardware::PID.new | ||
|
||
it "parses status name" do | ||
status.name.should eq "crystal-run-spec.tmp" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# CPU related informations of your system. | ||
# | ||
# ``` | ||
# cpu = Hardware::CPU.new | ||
# loop do | ||
# sleep 1 | ||
# p cpu.usage!.to_i # => 17 | ||
# end | ||
# ``` | ||
struct Hardware::CPU | ||
# CPU number. `nil` means the whole cores in total. | ||
getter number : Int32? | ||
|
||
# Creates a new CPU stat to monitor the given core. | ||
# | ||
# Must be lower than `System.cpu_count`, or `nil` for the whole cores in total. | ||
def initialize(number : Int32? = nil, parse_stats : Bool = true) | ||
if @number = number | ||
raise "CPU number must be superior or equal to 0, and inferior to #{System.cpu_count}" unless 0 <= number < System.cpu_count | ||
end | ||
parse_stat_file if parse_stats | ||
end | ||
|
||
{% begin %} | ||
{% stats = %w(user nice system idle iowait irq softirq steal guest guest_nice) %} | ||
{% for stat in stats %} | ||
# Returns the {{stat}} stat field. | ||
getter {{stat.id}} : Int32 { parse_stat_file; @{{stat.id}} || raise "Field not parsed: '{{stat.id}}'" } | ||
{% end %} | ||
|
||
private def parse_stat_line(column_num : Int32, buffer : IO) | ||
case column_num | ||
{% i = 1 %} | ||
{% for stat in stats %} | ||
when {{i}} then @{{stat.id}} = buffer.to_s.to_i | ||
{% i = i + 1 %} | ||
{% end %} | ||
end | ||
buffer.clear | ||
end | ||
{% end %} | ||
|
||
private def parse_stat_file | ||
# /proc/stat content: | ||
# cpu | ||
# cpu0 | ||
# cpu1 | ||
# ... | ||
line_num = -1 | ||
column_num = 0 | ||
buffer = IO::Memory.new | ||
File.open "/proc/stat", &.each_char do |char| | ||
if !@number && line_num == -1 || line_num == @number | ||
if (char == ' ' || char == '\n') && !buffer.empty? | ||
parse_stat_line column_num, buffer | ||
|
||
buffer.clear | ||
column_num += 1 | ||
else | ||
buffer << char | ||
end | ||
end | ||
if char == '\n' | ||
line_num += 1 | ||
break if line_num < System.cpu_count | ||
column_num = 0 | ||
end | ||
end | ||
end | ||
|
||
# Sum of `user`, `nice`, `system`, `irq`, `softirq` and `steal`. | ||
getter used : Int32 { user + nice + system + irq + softirq + steal } | ||
|
||
# Sum of `idle` and `iowait`. | ||
getter idle_total : Int32 { idle + iowait } | ||
|
||
# Sum of `used` and `idle_total` | ||
getter total : Int32 { used + idle_total } | ||
|
||
# Returns each CPU usage in percentage based on the previous `CPU`. | ||
def usage(previous_cpu : CPU = self) : Float64 | ||
# Usage Time / Total Time * 100 | ||
(used - previous_cpu.used) / (total - previous_cpu.total) * 100 | ||
end | ||
|
||
# Like `#usage`, but mutates the instance. | ||
# | ||
# ``` | ||
# cpu = Hardware::CPU.new | ||
# loop do | ||
# sleep 1 | ||
# p cpu.usage!.to_i # => 17 | ||
# end | ||
# ``` | ||
def usage! : Float64 | ||
# 100 * Usage Time / Total Time | ||
@user || raise "Stat file not previously parsed" | ||
previous_cpu = self | ||
@used = @idle_total = @total = nil | ||
parse_stat_file | ||
usage previous_cpu | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require "./hardware/**" | ||
require "./*" | ||
|
||
module Hardware | ||
VERSION = "0.4.0" | ||
end |
Oops, something went wrong.