sys-cpu 0.6.1-x86-linux → 0.6.2-x86-linux

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.
data/lib/sys/cpu.rb DELETED
@@ -1,105 +0,0 @@
1
- ##########################################################
2
- # linux.rb (sys-cpu) - pure Ruby version for Linux
3
- ##########################################################
4
- module Sys
5
- cpu_file = "/proc/cpuinfo"
6
- cpu_hash = {}
7
- $cpu_array = []
8
-
9
- # Parse the info out of the /proc/cpuinfo file
10
- IO.foreach(cpu_file){ |line|
11
- line.strip!
12
- next if line.empty?
13
-
14
- key, val = line.split(":")
15
- key.strip!
16
- key.gsub!(/\s+/,"_")
17
- key.downcase!
18
- val.strip! if val
19
-
20
- if cpu_hash.has_key?(key)
21
- $cpu_array.push(cpu_hash.dup)
22
- cpu_hash.clear
23
- end
24
-
25
- # Turn yes/no attributes into booleans
26
- if val == 'yes'
27
- val = true
28
- elsif val == 'no'
29
- val = false
30
- end
31
- cpu_hash[key] = val
32
- }
33
-
34
- $cpu_array.push(cpu_hash)
35
-
36
- class CPU
37
-
38
- VERSION = '0.6.1'
39
- CPUStruct = Struct.new("CPUStruct", *$cpu_array.first.keys)
40
-
41
- # In block form, yields a CPUStruct for each CPU on the system. In
42
- # non-block form, returns an Array of CPUStruct's.
43
- #
44
- # The exact members of the struct vary on Linux systems.
45
- #
46
- def self.processors
47
- array = []
48
- $cpu_array.each{ |hash|
49
- struct = CPUStruct.new
50
- struct.members.each{ |m| struct.send("#{m}=", hash[m]) }
51
- if block_given?
52
- yield struct
53
- else
54
- array << struct
55
- end
56
- }
57
- array unless block_given?
58
- end
59
-
60
- #--
61
- # Create singleton methods for each of the attributes
62
- #
63
- def self.method_missing(id,arg=0)
64
- rv = $cpu_array[arg][id.to_s]
65
- if rv.nil?
66
- id = id.to_s + "?"
67
- rv = $cpu_array[arg][id]
68
- end
69
- rv
70
- end
71
-
72
- # Returns a 3 element Array corresponding to the 1, 5 and 15 minute
73
- # load average for the system.
74
- #
75
- def self.load_avg
76
- load_avg_file = "/proc/loadavg"
77
- IO.readlines(load_avg_file).first.split[0..2].map{|e| e.to_f}
78
- end
79
-
80
- # Returns a hash of arrays that contain the number of seconds that the
81
- # system spent in user mode, user mode with low priority (nice), system
82
- # mode, and the idle task, respectively.
83
- #
84
- def self.cpu_stats
85
- cpu_stat_file = "/proc/stat"
86
- hash = {} # Hash needed for multi-cpu systems
87
-
88
- lines = IO.readlines(cpu_stat_file)
89
- lines.each_with_index{ |line, i|
90
- array = line.split
91
- break unless array[0] =~ /cpu/ # 'cpu' entries always on top
92
-
93
- # Some machines list a 'cpu' and a 'cpu0'. In this case, only
94
- # return values for the numbered cpu entry.
95
- if lines[i].split[0] == "cpu" && lines[i+1].split[0] =~ /cpu\d/
96
- next
97
- end
98
-
99
- vals = array[1..-1].map{ |e| e = e.to_i / 100 } # 100 jiffies/sec.
100
- hash[array[0]] = vals
101
- }
102
- hash
103
- end
104
- end
105
- end
data/test/tc_cpu.rb DELETED
@@ -1,17 +0,0 @@
1
- $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
2
- require 'tc_version'
3
-
4
- case RUBY_PLATFORM
5
- when /bsd|darwin|mach|osx/i
6
- require 'tc_bsd'
7
- when /hpux/i
8
- require 'tc_hpux'
9
- when /linux/i
10
- require 'tc_linux'
11
- when /sunos|solaris/i
12
- require 'tc_sunos'
13
- when /mswin|win32|dos|mingw|cygwin/i
14
- require 'tc_windows'
15
- else
16
- raise "Platform not supported"
17
- end
data/test/tc_linux.rb DELETED
@@ -1,34 +0,0 @@
1
- ###########################################################
2
- # tc_linux.rb
3
- #
4
- # Test Suite for sys-cpu for Linux. This should be run via
5
- # the 'rake test' task.
6
- ###########################################################
7
- require "sys/cpu"
8
- require "test/unit"
9
- require "tc_version"
10
- include Sys
11
-
12
- class TC_Linux < Test::Unit::TestCase
13
- def test_all_dynamic_methods
14
- assert_nothing_raised{
15
- CPU.processors{ |cs|
16
- cs.members.each{ |m|
17
- puts "#{m}: " + cs[m].to_s
18
- }
19
- }
20
- }
21
- end
22
-
23
- def test_load_avg
24
- assert_nothing_raised{ CPU.load_avg }
25
- assert_equal(3, CPU.load_avg.length)
26
- end
27
-
28
- def test_cpu_stats
29
- assert_nothing_raised{ CPU.cpu_stats }
30
- assert_kind_of(Hash, CPU.cpu_stats)
31
- assert_equal(true, CPU.cpu_stats["cpu0"].length >= 4)
32
- end
33
- end
34
-