sys-cpu 0.5.5-i586-linux

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,113 @@
1
+ == 0.5.5 - 17-Nov-2006
2
+ * Fixed a bug in the Linux version where there could be a key but no
3
+ associated value, causing a String#strip! call to fail. Now the value is
4
+ simply left at nil.
5
+ * Refactored the CPU.processors method on Linux, including the removal of '?'
6
+ from the CPUStruct members for the boolean methods (Ruby doesn't like them).
7
+ * Minor tweaks and updates to the documentation, including the README.
8
+ * Modified the gemspec so that it sets the platform properly for Linux
9
+ and Windows.
10
+
11
+ == 0.5.4 - 12-Jul-2006
12
+ * Added a gemspec (and a gem on RubyForge).
13
+ * The CPU.architecture method on HP-UX now returns nil if it cannot be
14
+ determined instead of "Unknown" for consistency with the other
15
+ platforms.
16
+ * Inlined the RDoc and made some minor cosmetic source code changes.
17
+
18
+ == 0.5.3 - 4-May-2006
19
+ * Fixed in a bug in the Solaris version where CPU.load_avg returned bad values
20
+ when compiled in 64 bit mode. Thanks go to James Hranicky for the spot and
21
+ both James Hranicky and Peter Tribble (via comp.unix.solaris) for patches.
22
+ * Made some modifications to the test suite. You know longer need to know
23
+ which test suite to run. All platforms now use 'ts_all.rb', which will run
24
+ the appropriate test case behind the scenes.
25
+
26
+ == 0.5.2 - 24-Jun-2005
27
+ * Bug fixed on Linux where empty lines could cause problems. This affected
28
+ both the install.rb and linux.rb files, though it only seems to have been
29
+ an issue on the 2.6+ kernel.
30
+ * Altered the behavior of the CPU.cpu_stats method on Linux. Now, only the
31
+ numbered cpu entries return associated data, unless the numberless entry
32
+ is the only entry.
33
+ * Added a sample program for Linux under the 'examples' directory.
34
+
35
+ == 0.5.1 - 5-May-2005
36
+ * Fixed a potential bug in the FreeBSD version of CPU.model.
37
+ * Eliminated some warnings in the FreeBSD version.
38
+ * Moved examples directory to the toplevel package directory.
39
+ * Renamed and updated the sample scripts.
40
+ * Added a sample script for FreeBSD.
41
+ * Removed the INSTALL file. That information is now included in the README.
42
+ * Made the CHANGES, README, and .txt files rdoc friendly.
43
+ * The dynamic documentation generation for Linux has been altered. Anything
44
+ relating to rd2 has been removed. The doc file generated is now
45
+ doc/linux.txt.
46
+ * Some $LOAD_PATH setup changes in the unit tests.
47
+
48
+ == 0.5.0 - 26-Jun-2004
49
+ * Now requires Ruby 1.8.0 or later.
50
+ * FreeBSD support added.
51
+ * Replaced 'CPUException' with 'CPUError'
52
+ * The MS Windows version has been completely revamped to use WMI instead of
53
+ the C API. Some method names have changed and some methods have been
54
+ dropped entirely. Please see the documentation for details.
55
+ * Corresponding documentation updates and test suite changes.
56
+ * Sample programs have been moved to doc/examples.
57
+ * Installation procedure somewhat revamped.
58
+ * No longer autogenerates test file.
59
+ * The .html files have been removed. You can generate the html on your own
60
+ if you like.
61
+
62
+ == 0.4.0 - 18-Sep-2003
63
+ * Added MS Windows support
64
+ * Changed some method names. The "cpu" has been dropped from most method
65
+ names. See documentation for details.
66
+ * The state() and freq() methods (where supported) now assume a default
67
+ value of zero.
68
+ * More unit tests added.
69
+ * Fixed minor issue with test.rb for those without TestUnit installed.
70
+ * Fixed issue with linux.rb file being auto-installed on non-Linux platforms.
71
+ * Minor API change for the load_avg() method on HP-UX. It now accepts a
72
+ CPU number as an argument.
73
+
74
+ == 0.3.1 - 16-Jul-2003
75
+ * Fixed a bug in the Solaris version that was using up and not
76
+ freeing file descriptors.
77
+ * Added html doc for Solaris under doc directory.
78
+ * Minor changes to test_hpux.rb and test_sunos.rb
79
+ * Minor README changes.
80
+
81
+ == 0.3.0 - 30-Jun-2003
82
+ * Added HP-UX support
83
+ * Removed the VERSION class method. Use the constant instead
84
+ * Changed license to "Artistic"
85
+ * Moved version info into its own file for convenience
86
+ * Some minor mods to the test suites
87
+ * Modified extconf.rb, moving some of the dynamic test generation
88
+ into separate files
89
+
90
+ == 0.2.2 - 25-Mar-2003
91
+ * fpu_type and cpu_type now return nil if not found (Solaris)
92
+ * CPUException is now a direct subclass of StandardError
93
+ * Modified extconf.rb script
94
+ * Minor doc updates
95
+ * Added another test to the solaris test suite
96
+ * Important note added to INSTALL file
97
+
98
+ == 0.2.1 - 12-Mar-2003
99
+ * Added the cpu_stats() class method for Linux, which contains the
100
+ data from the 'cpu' lines of /proc/stat
101
+ * Minor fix for extconf.rb (thanks Michael Granger)
102
+ * Some tests added to Linux test suite
103
+ * MANIFEST correction
104
+
105
+ == 0.2.0 - 13-Feb-2003
106
+ * Linux support added (pure Ruby only)
107
+ * Many changes to extconf.rb to support Linux version
108
+ * sys-uname prerequisite dropped
109
+ * rd2 documentation now kept separate from source
110
+
111
+ == 0.1.0 - 3-Feb-2003
112
+ * Initial release
113
+ * Currently supports Solaris (only)
data/README ADDED
@@ -0,0 +1,78 @@
1
+ = Description
2
+ A Ruby interface for getting cpu information.
3
+
4
+ = Installation
5
+ === Linux and Windows:
6
+ ruby test/ts_all.rb (optional)
7
+ ruby install.rb
8
+
9
+ === All other platforms:
10
+ ruby extconf.rb
11
+ make
12
+ ruby test/ts_all.rb (optional)
13
+ make site-install
14
+
15
+ = Notes
16
+ === All Platforms
17
+ As of version 0.5.0 Ruby 1.8.0 or later is required. It may work with
18
+ earlier versions, but I will not support it. In addition, a test file is
19
+ no longer auto-generated for you. If wish to run the test suite, cd to the
20
+ 'test' directory and run the test suite appropriate for your platform.
21
+
22
+ === Solaris
23
+ Currently there is no 'processors()' iterative method for multi-cpu systems.
24
+ I plan to add it this in a future release.
25
+
26
+ === Linux
27
+ This is pure Ruby. This version reads information out of /proc/cpuinfo and
28
+ /proc/loadavg, so if /proc isn't mounted it won't work.
29
+
30
+ The key-value information in /proc/cpuinfo is stored internally (i.e. in
31
+ memory) as an array of hashes when you first 'require' this package. This
32
+ overhead is exceptionally minimal, given that your average cpuinfo file
33
+ contains less than 1k of text (and I don't store whitespace or newlines).
34
+
35
+ The text documentation for Linux is dynamically generated during the
36
+ build process because the fields vary depending on your setup. So, don't
37
+ look at it until *after* you've installed it. You will see a doc/linux.txt
38
+ file after you run install.rb.
39
+
40
+ === HP-UX
41
+ Unlike other platforms, you can get load averages for an individual cpu (in
42
+ multi-cpu systems). See documentation for more details.
43
+
44
+ === Win32
45
+ This is a pure Ruby implementation using the win32ole package + WMI. The C
46
+ version has been scrapped.
47
+
48
+ As of version 0.5.0, the CPU.usage method has been removed in favor of the
49
+ CPU.load_avg method. This does not (currently) use a perf counter, so there
50
+ is no longer any delay. Also, the 'processors' method has been
51
+ added and the 'supported' method has been dropped. See the documentation
52
+ for other changes.
53
+
54
+ = Known Bugs
55
+ None that I'm aware of. Please report bugs on the project page at
56
+ http://www.rubyforge.org/projects/sysutils.
57
+
58
+ = Future Plans
59
+ === Solaris
60
+ Add iterative CPU.processors method.
61
+ Add more information in general, such as what 'prtdiag' shows.
62
+
63
+ = License
64
+ Ruby's.
65
+
66
+ = Copyright
67
+ (C) 2003-2006 Daniel J. Berger
68
+ All Rights Reserved
69
+
70
+ = Warranty
71
+ This package is provided "as is" and without any express or
72
+ implied warranties, including, without limitation, the implied
73
+ warranties of merchantability and fitness for a particular purpose.
74
+
75
+ = Author
76
+ Daniel J. Berger
77
+ djberg96 at gmail dot com
78
+ imperator on IRC (irc.freenode.net)
data/doc/freebsd.txt ADDED
@@ -0,0 +1,65 @@
1
+ = Description
2
+ Sys::CPU - An interface for various cpu statistics
3
+
4
+ = Synopsis
5
+ require "sys/cpu"
6
+ include Sys
7
+
8
+ # FreeBSD
9
+ puts "Architecture: " + CPU.architecture
10
+ puts "Machine: " + CPU.machine
11
+ puts "Mhz: " + CPU.cpu_freq.to_s
12
+ puts "Number of cpu's on this system: " + CPU.num_cpu.to_s
13
+ puts "CPU model: " + CPU.model
14
+ puts "Load averages: " + CPU.load_avg.join(", ")
15
+
16
+ = Constants
17
+ VERSION
18
+ Returns the current version number for this package.
19
+
20
+ = Class Methods
21
+ CPU.architecture
22
+ Returns the cpu's architecture.
23
+
24
+ CPU.freq
25
+ Returns an integer indicating the speed (i.e. frequency in Mhz) of
26
+ the cpu.
27
+
28
+ CPU.load_avg
29
+ Returns an array of three floats indicating the 1, 5 and 15 minute load
30
+ average.
31
+
32
+ CPU.machine
33
+ Returns the class of cpu (probably identical to the architecture).
34
+
35
+ CPU.model
36
+ Returns a string indicating the cpu model.
37
+
38
+ CPU.num_cpu
39
+ Returns an integer indicating the number of cpu's on the system.
40
+
41
+ = Error Classes
42
+ CPUError < StandardError
43
+ Raised is response to internal function errors, usually relating to an
44
+ invalid cpu number.
45
+
46
+ = Known Bugs
47
+ None known. Please post bug reports to the SourceForge home page at
48
+ http://www.rubyforge.org/projects/sysutils
49
+
50
+ = Copyright
51
+ (C) 2003-2006 Daniel J. Berger
52
+ All Rights Reserved
53
+
54
+ = Warranty
55
+ This package is provided "as is" and without any express or
56
+ implied warranties, including, without limitation, the implied
57
+ warranties of merchantability and fitness for a particular purpose.
58
+
59
+ = License
60
+ Ruby's
61
+
62
+ = Author
63
+ Daniel J. Berger
64
+ djberg96 at yahoo dot com
65
+ imperator/rubyhacker1 on IRC (Freenode)
data/doc/hpux.txt ADDED
@@ -0,0 +1,75 @@
1
+ = Description
2
+ Sys::CPU - An interface for various cpu statistics
3
+
4
+ = Synopsis
5
+ require "sys/cpu"
6
+ include Sys
7
+
8
+ puts "Mhz: " + CPU.cpu_freq.to_s
9
+ puts "State: " + CPU.state(0)
10
+ puts "Number of cpu's on this system: " + CPU.num_cpu.to_s
11
+ puts "Number of active cpu's: " + CPU.num_active_cpu.to_s
12
+ puts "Load averages: " + CPU.load_avg.join(", ")
13
+
14
+ = Constants
15
+ VERSION
16
+ Returns the current version number for this package.
17
+
18
+ = Class Methods
19
+ CPU.architecture
20
+ Returns the cpu architecture, e.g. PA RISC 1.2, etc.
21
+
22
+ CPU.freq(cpu_num=0)
23
+ Returns an integer indicating the speed (i.e. frequency in Mhz) of
24
+ the cpu.
25
+
26
+ CPU.load_avg
27
+ CPU.load_avg(cpu_num)
28
+ CPU.load_avg{ |cpu| ... }
29
+
30
+ In non-block form returns an array of three floats indicating the 1, 5
31
+ and 15 minute overall load average.
32
+
33
+ In block form, it returns an array of three floats indicating the 1, 5
34
+ and 15 minute load average for each cpu. Only useful on multi-cpu
35
+ systems.
36
+
37
+ If 'cpu_num' is provided, returns the load average (as a 3-element
38
+ array) for that cpu only.
39
+
40
+ You cannot provide a cpu number and use block form at the same time.
41
+
42
+ CPU.num_cpu
43
+ Returns an integer indicating the number of cpu's on the system.
44
+
45
+ CPU.num_active_cpu
46
+ Returns an integer indicating the number of active cpu's on the system.
47
+
48
+ = Error Classes
49
+ CPUError < StandardError
50
+ Raised is response to internal function errors, usually relating to an
51
+ invalid cpu number.
52
+
53
+ = Known Bugs
54
+ No bugs known. Please log any bugs at
55
+ http://www.rubyforge.org/projects/sysutils
56
+
57
+ = Future Plans
58
+ Add cpu model and type
59
+
60
+ = Copyright
61
+ (C) 2003-2006 Daniel J. Berger
62
+ All Rights Reserved
63
+
64
+ = Warranty
65
+ This package is provided "as is" and without any express or
66
+ implied warranties, including, without limitation, the implied
67
+ warranties of merchantability and fitness for a particular purpose.
68
+
69
+ = License
70
+ Ruby's
71
+
72
+ = Author
73
+ Daniel J. Berger
74
+ djberg96 at yahoo dot com
75
+ imperator/rubyhacker1 on IRC (Freenode)
data/doc/linux.txt ADDED
@@ -0,0 +1,41 @@
1
+ == Description
2
+ Sys::CPU - An interface for various cpu statistics
3
+
4
+ == Synopsis
5
+ require "sys/cpu"
6
+ include Sys
7
+
8
+ CPU.processors{ |cs|
9
+ cs.members.each{ |m|
10
+ puts "#{m}: " + cs[m].to_s
11
+ }
12
+ }
13
+
14
+ CPU.bogomips(1) # -> returns bogomips for cpu #2
15
+
16
+ == Notes
17
+ Portions of this documentation built dynamically
18
+
19
+ == Constants
20
+ VERSION
21
+ Returns the current version number for this package as a String.
22
+
23
+ == Class Methods
24
+ CPU.load_avg
25
+ Returns an array of three floats indicating the 1, 5 and 15 minute load
26
+ average.
27
+
28
+ CPU.cpu_stats
29
+ Returns a hash, with the cpu number as the key and an array as the value.
30
+ The array contains the number of seconds that the system spent in
31
+ user mode, user mode with low priority (nice), system mode, and the
32
+ idle task, respectively, for that cpu.
33
+
34
+ CPU.processors{ |cpu struct| block }
35
+ Calls the block for each processor on your system, passing a CPUStruct as the
36
+ parameter.
37
+
38
+ The exact members of the CPUStruct are the same as the class method names, except
39
+ for CPU.processors (although you may optionally omit the "?" when referring to a
40
+ struct member). These were determined when you installed this package because they
41
+ vary from one chip architecture to another.
data/doc/sunos.txt ADDED
@@ -0,0 +1,76 @@
1
+ = Description
2
+ Sys::CPU - An interface for various cpu statistics
3
+
4
+ = Synopsis
5
+ require "sys/cpu"
6
+ include Sys
7
+
8
+ # Solaris
9
+ puts "Mhz: " + CPU.cpu_freq(0).to_s
10
+ puts "State: " + CPU.state(0)
11
+ puts "Number of cpu's on this system: " + CPU.num_cpu.to_s
12
+ puts "CPU type: " + CPU.type
13
+ puts "FPU type: " + CPU.fpu_type
14
+ puts "CPU model: " + CPU.model
15
+ puts "Load averages: " + CPU.load_avg.join(", ")
16
+
17
+ = Constants
18
+ VERSION
19
+ Returns the current version number for this package.
20
+
21
+ = Class Methods
22
+ CPU.freq(cpu_num=0)
23
+ Returns an integer indicating the speed (i.e. frequency in Mhz) of
24
+ 'cpu_num', or CPU 0 if no number is provided.
25
+
26
+ CPU.type
27
+ Returns a string indicating the type of processor. This is the
28
+ architecture (e.g. sparcv9), not the exact model (e.g. Ultra-IIe).
29
+ Returns nil if not found.
30
+
31
+ CPU.fpu_type
32
+ Returns a string indicating the type of floating point unit, or nil if
33
+ not found.
34
+
35
+ CPU.load_avg
36
+ Returns an array of three floats indicating the 1, 5 and 15 minute load
37
+ average.
38
+
39
+ CPU.model
40
+ Returns a string indicating the cpu model. For now, this is the
41
+ architecture type, rather than the exact model. However, see the
42
+ 'Future Plans' section below.
43
+
44
+ CPU.num_cpu
45
+ Returns an integer indicating the number of cpu's on the system.
46
+
47
+ CPU.state(cpu_num)
48
+ Returns a string indicating the cpu state of 'cpu_num'.
49
+
50
+ = Error Classes
51
+ CPUError < StandardError
52
+ Raised is response to internal function errors, usually relating to an
53
+ invalid cpu number.
54
+
55
+ = Known Bugs
56
+ None known. Please post bug reports to the SourceForge home page at
57
+ http://www.rubyforge.org/projects/sysutils
58
+
59
+ = Future Plans
60
+ Add more information, such as what prtdiag shows.
61
+ Get the model() method to display more specific information.
62
+
63
+ = Copyright
64
+ (C) 2003-2006 Daniel J. Berger
65
+ All Rights Reserved
66
+
67
+ = Warranty
68
+ This package is provided "as is" and without any express or
69
+ implied warranties, including, without limitation, the implied
70
+ warranties of merchantability and fitness for a particular purpose.
71
+
72
+ = License
73
+ Ruby's
74
+
75
+ = Author
76
+ Daniel J. Berger
data/doc/windows.txt ADDED
@@ -0,0 +1,152 @@
1
+ = Description
2
+ Sys::CPU - An interface for various cpu statistics
3
+
4
+ = Synopsis
5
+ require "sys/cpu"
6
+ include Sys
7
+
8
+ CPU.processors{ |cs|
9
+ cs.members.each{ |m|
10
+ puts "#{m}: " + cs[m].to_s
11
+ }
12
+ }
13
+
14
+ puts "CPU Speed (Frequency): " + CPU.freq(0).to_s
15
+ puts "Load avg: " + CPU.load_avg.to_s
16
+ puts "Num CPU: " + CPU.num_cpu.to_s
17
+ puts "Type: " + CPU.type
18
+ puts "Model: " + CPU.model
19
+ puts "Architecture: " + CPU.architecture
20
+
21
+ = Constants
22
+ VERSION
23
+ Returns the current version number for this package.
24
+
25
+ = Class Methods
26
+ CPU.architecture(host=localhost)
27
+ Returns the architecture of the cpu, e.g. x86 Family 15 Model 2
28
+
29
+ CPU.freq(cpu_num=0, host=localhost)
30
+ Returns an integer indicating the speed (i.e. frequency in Mhz) of
31
+ 'cpu_num'. If cpu_num+1 is greater than the number of cpu's on
32
+ your system, or this call fails for any other reason, a CPUError
33
+ is raised.
34
+
35
+ CPU.load_avg(host=localhost)
36
+ Returns the load capacity of each processor, averaged to the last second.
37
+ Processor loading refers to the total computing burden for each
38
+ processor at one time.
39
+
40
+ Note that this attribute is actually the LoadPercentage. I may use
41
+ one of the Win32_Perf* classes in the future.
42
+
43
+ CPU.model(host=localhost)
44
+ Returns a string indicating the cpu model, e.g. Intel Pentium 4
45
+
46
+ CPU.num_cpu(host=localhost)
47
+ Returns an integer indicating the number of cpu's on the system.
48
+
49
+ CPU.processors(host=localhost){ |cpu| ... }
50
+ Returns a CPUStruct for each CPU on the system containing the following
51
+ members:
52
+
53
+ * address_width
54
+ * architecture
55
+ * availability
56
+ * caption
57
+ * config_manager_error_code
58
+ * config_manager_user_config
59
+ * cpu_status
60
+ * creation_class_name
61
+ * freq
62
+ * voltage
63
+ * data_width
64
+ * description
65
+ * device_id
66
+ * error_cleared?
67
+ * error_description
68
+ * ext_clock
69
+ * family
70
+ * install_date
71
+ * l2_cache_size
72
+ * l2_cache_speed
73
+ * last_error_code
74
+ * level
75
+ * load_avg
76
+ * manufacturer
77
+ * max_clock_speed
78
+ * name
79
+ * other_family_description
80
+ * pnp_device_id
81
+ * power_management_supported?
82
+ * power_management_capabilities
83
+ * processor_id
84
+ * processor_type
85
+ * revision
86
+ * role
87
+ * socket_designation
88
+ * status
89
+ * status_info
90
+ * stepping
91
+ * system_creation_class_name
92
+ * system_name
93
+ * unique_id
94
+ * upgrade_method
95
+ * version
96
+ * voltage_caps
97
+
98
+ Note that not all of these members will necessarily be defined. See the
99
+ NOTES section below.
100
+
101
+ CPU.type(host=localhost)
102
+ Returns a string indicating the type of processor, e.g. GenuineIntel.
103
+
104
+ = Exception Classes
105
+ CPUError < StandardError
106
+ Raised is response to internal function errors, most likely to be raised
107
+ in the event that in invalid cpu number is provided for the 'freq'
108
+ method.
109
+
110
+ = Notes
111
+ Some of the CPUStruct members may be nil. As far as I can tell, this
112
+ means that Windows is unable to determine the value for the attribute due
113
+ to your BIOS and/or BIOS settings. There is nothing clear in the
114
+ documentation I could find for this, however.
115
+
116
+ The 'family' attribute may contain the trademark symbol.
117
+
118
+ The 'status_info' attribute was not converted to a string because the
119
+ 'status' attribute already is a string. I'm not even sure why MS has both
120
+ versions.
121
+
122
+ The 'system_name' attribute appears to be the hostname. I have no idea
123
+ why this is included as part of the CPU information, but there you go.
124
+
125
+ See http://tinyurl.com/2mros (and click on 'Win32_Processor' in the left
126
+ frame) for a list of attributes and their meaning. Link courtesy of
127
+ tinyurl.com.
128
+
129
+ = Known Bugs
130
+ No bugs known. Please log any bugs at
131
+ http://www.rubyforge.org/projects/sysutils
132
+
133
+ = Future Plans
134
+ Add more detailed CPU usage information via the
135
+ Win32_PerfFormattedData_PerfOS_Processor class.
136
+
137
+ = Copyright
138
+ (C) 2003-2006 Daniel J. Berger
139
+ All Rights Reserved
140
+
141
+ = Warranty
142
+ This package is provided "as is" and without any express or
143
+ implied warranties, including, without limitation, the implied
144
+ warranties of merchantability and fitness for a particular purpose.
145
+
146
+ = License
147
+ Ruby's
148
+
149
+ = Author
150
+ Daniel J. Berger
151
+ djberg96 at yahoo dot com
152
+ imperator/rubyhacker1 on IRC (Freenode)
data/lib/sys/cpu.rb ADDED
@@ -0,0 +1,105 @@
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.5.5'
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
@@ -0,0 +1,71 @@
1
+ ###########################################################
2
+ # tc_freebsd.rb
3
+ #
4
+ # Test suite for sys-cpu on FreeBSD. This should only be
5
+ # run *after* the make process.
6
+ ###########################################################
7
+ base = File.basename(Dir.pwd)
8
+ if base == "test" || base =~ /sys-cpu/
9
+ require "ftools"
10
+ Dir.chdir ".." if base == "test"
11
+
12
+ Dir.mkdir("sys") unless File.exist?("sys")
13
+
14
+ if File.exist?("cpu.so")
15
+ File.copy("cpu.so","sys")
16
+ else
17
+ puts "No cpu.so file found. Please run extconf.rb and make first"
18
+ exit
19
+ end
20
+
21
+ $LOAD_PATH.unshift Dir.pwd
22
+ end
23
+
24
+ require "sys/cpu"
25
+ require "test/unit"
26
+ include Sys
27
+
28
+ class TC_FreeBSD < Test::Unit::TestCase
29
+ def test_architecture
30
+ assert_respond_to(CPU, :architecture)
31
+ assert_nothing_raised{ CPU.architecture }
32
+ assert_kind_of(String,CPU.architecture)
33
+ assert_raises(ArgumentError){ CPU.architecture(0) }
34
+ end
35
+
36
+ def test_cpu_freq
37
+ assert_respond_to(CPU, :freq)
38
+ assert_nothing_raised{ CPU.freq }
39
+ assert_kind_of(Integer,CPU.freq)
40
+ assert_raises(ArgumentError){ CPU.freq(0) }
41
+ end
42
+
43
+ def test_load_avg
44
+ assert_respond_to(CPU, :load_avg)
45
+ assert_nothing_raised{ CPU.load_avg }
46
+ assert_kind_of(Array,CPU.load_avg)
47
+ assert_equal(3,CPU.load_avg.length)
48
+ assert_raises(ArgumentError){ CPU.load_avg(0) }
49
+ end
50
+
51
+ def test_machine
52
+ assert_respond_to(CPU, :machine)
53
+ assert_nothing_raised{ CPU.machine }
54
+ assert_kind_of(String,CPU.machine)
55
+ assert_raises(ArgumentError){ CPU.machine(0) }
56
+ end
57
+
58
+ def test_model
59
+ assert_respond_to(CPU, :model)
60
+ assert_nothing_raised{ CPU.model }
61
+ assert_kind_of(String,CPU.model)
62
+ assert_raises(ArgumentError){ CPU.model(0) }
63
+ end
64
+
65
+ def test_num_cpu
66
+ assert_respond_to(CPU, :num_cpu)
67
+ assert_nothing_raised{ CPU.num_cpu }
68
+ assert_kind_of(Integer,CPU.num_cpu)
69
+ assert_raises(ArgumentError){ CPU.num_cpu(0) }
70
+ end
71
+ end
data/test/tc_hpux.rb ADDED
@@ -0,0 +1,63 @@
1
+ ####################################################
2
+ # tc_hpux.rb
3
+ #
4
+ # Test suite for the HP-UX platform. Note that this should only
5
+ # be run *after* the make process.
6
+ ####################################################
7
+ base = File.basename(Dir.pwd)
8
+ if base == "test" || base =~ /sys-cpu/
9
+ require "ftools"
10
+ Dir.chdir ".." if base == "test"
11
+
12
+ Dir.mkdir("sys") unless File.exists?("sys")
13
+ if File.exist?("cpu.sl")
14
+ File.copy("cpu.sl","sys")
15
+ else
16
+ puts "No cpu.sl file found. Please run extconf.rb and make first"
17
+ exit
18
+ end
19
+ $LOAD_PATH.unshift Dir.pwd
20
+ end
21
+
22
+ require "sys/cpu"
23
+ require "test/unit"
24
+ include Sys
25
+
26
+ class TC_HPUX < Test::Unit::TestCase
27
+ def test_cpu_freq
28
+ assert_respond_to(CPU, :freq)
29
+ assert_nothing_raised{ CPU.freq }
30
+ assert_nothing_raised{ CPU.freq(0) }
31
+ assert_kind_of(Integer,CPU.freq,"Invalid Type")
32
+ end
33
+
34
+ def test_num_cpu
35
+ assert_respond_to(CPU, :num_cpu)
36
+ assert_nothing_raised{ CPU.num_cpu }
37
+ assert_kind_of(Integer,CPU.num_cpu,"Invalid Type")
38
+ end
39
+
40
+ def test_num_active_cpu
41
+ assert_respond_to(CPU, :num_active_cpu)
42
+ assert_nothing_raised{ CPU.num_active_cpu }
43
+ assert_kind_of(Integer,CPU.num_active_cpu,"Invalid Type")
44
+ end
45
+
46
+ def test_cpu_architecture
47
+ assert_respond_to(CPU, :architecture)
48
+ assert_nothing_raised{ CPU.architecture }
49
+ assert_kind_of(String,CPU.architecture,"Invalid Type")
50
+ end
51
+
52
+ def test_load_avg
53
+ assert_respond_to(CPU, :load_avg)
54
+ assert_nothing_raised{ CPU.load_avg }
55
+ assert_nothing_raised{ CPU.load_avg(0) }
56
+ assert_nothing_raised{ CPU.load_avg{ |e| } }
57
+ assert_raises(ArgumentError){ CPU.load_avg(0){ } }
58
+ assert_kind_of(Array,CPU.load_avg,"Invalid Type")
59
+ assert_kind_of(Array,CPU.load_avg(0),"Invalid Type")
60
+ assert_equal(3,CPU.load_avg.length,"Bad number of elements")
61
+ assert_equal(3,CPU.load_avg(0).length,"Bad number of elements")
62
+ end
63
+ end
data/test/tc_linux.rb ADDED
@@ -0,0 +1,41 @@
1
+ ########################################
2
+ # tc_linux.rb
3
+ #
4
+ # Test Suite for sys-cpu for Linux
5
+ ########################################
6
+ base = File.basename(Dir.pwd)
7
+ if base == "test" || base =~ /sys-cpu/
8
+ require "ftools"
9
+ Dir.chdir ".." if base == "test"
10
+ Dir.mkdir("sys") unless File.exists?("sys")
11
+ File.copy("lib/sys/linux.rb","sys/cpu.rb")
12
+ $LOAD_PATH.unshift Dir.pwd
13
+ end
14
+
15
+ require "sys/cpu"
16
+ require "test/unit"
17
+ include Sys
18
+
19
+ class TC_Linux < Test::Unit::TestCase
20
+ def test_all_dynamic_methods
21
+ assert_nothing_raised{
22
+ CPU.processors{ |cs|
23
+ cs.members.each{ |m|
24
+ puts "#{m}: " + cs[m].to_s
25
+ }
26
+ }
27
+ }
28
+ end
29
+
30
+ def test_load_avg
31
+ assert_nothing_raised{ CPU.load_avg }
32
+ assert_equal(3, CPU.load_avg.length)
33
+ end
34
+
35
+ def test_cpu_stats
36
+ assert_nothing_raised{ CPU.cpu_stats }
37
+ assert_kind_of(Hash, CPU.cpu_stats)
38
+ assert_equal(true, CPU.cpu_stats["cpu0"].length >= 4)
39
+ end
40
+ end
41
+
data/test/tc_sunos.rb ADDED
@@ -0,0 +1,82 @@
1
+ ###########################################################
2
+ # tc_sunos.rb
3
+ #
4
+ # Test suite for sys-cpu on Solaris. This should only be
5
+ # run *after* the make process.
6
+ ###########################################################
7
+ base = File.basename(Dir.pwd)
8
+ if base == "test" || base =~ /sys-cpu/
9
+ require "ftools"
10
+ Dir.chdir ".." if base == "test"
11
+
12
+ Dir.mkdir("sys") unless File.exist?("sys")
13
+
14
+ if File.exist?("cpu.so")
15
+ File.copy("cpu.so","sys")
16
+ else
17
+ puts "No cpu.so file found. Please run extconf.rb and make first"
18
+ exit
19
+ end
20
+
21
+ $LOAD_PATH.unshift Dir.pwd
22
+ end
23
+
24
+ require "sys/cpu"
25
+ require "test/unit"
26
+ include Sys
27
+
28
+ # I could really use a mock object here
29
+ class TC_SunOS < Test::Unit::TestCase
30
+ def test_cpu_freq
31
+ assert_respond_to(CPU, :freq)
32
+ assert_nothing_raised{ CPU.freq }
33
+ assert_nothing_raised{ CPU.freq(0) }
34
+ assert_kind_of(Integer, CPU.freq(0))
35
+ end
36
+
37
+ def test_cpu_type
38
+ assert_respond_to(CPU, :type)
39
+ assert_nothing_raised{ CPU.type }
40
+ assert_kind_of(String, CPU.type)
41
+ end
42
+
43
+ def test_fpu_type
44
+ assert_respond_to(CPU, :fpu_type)
45
+ assert_nothing_raised{ CPU.fpu_type }
46
+ assert_kind_of(String, CPU.fpu_type)
47
+ end
48
+
49
+ def test_load_avg
50
+ assert_respond_to(CPU, :load_avg)
51
+ assert_nothing_raised{ CPU.load_avg }
52
+ assert_kind_of(Array, CPU.load_avg)
53
+ assert_equal(3, CPU.load_avg.length)
54
+ assert_kind_of(Float, CPU.load_avg.first)
55
+ end
56
+
57
+ def test_cpu_model
58
+ assert_respond_to(CPU, :model)
59
+ assert_nothing_raised{ CPU.model }
60
+ assert_kind_of(String, CPU.model)
61
+ end
62
+
63
+ def test_num_cpu
64
+ assert_respond_to(CPU, :num_cpu)
65
+ assert_nothing_raised{ CPU.num_cpu }
66
+ assert_kind_of(Integer, CPU.num_cpu)
67
+ end
68
+
69
+ def test_state
70
+ assert_respond_to(CPU, :state)
71
+ assert_nothing_raised{ CPU.state }
72
+ assert_nothing_raised{ CPU.state(0) }
73
+ assert_kind_of(String, CPU.state(0))
74
+ end
75
+
76
+ def test_exceptions
77
+ assert_raises(Sys::CPUError){ CPU.state(55) }
78
+ assert_raises(TypeError){ CPU.state("yo") }
79
+ assert_raises(Sys::CPUError){ CPU.freq(999) }
80
+ assert_raises(TypeError){ CPU.freq("yo") }
81
+ end
82
+ end
@@ -0,0 +1,44 @@
1
+ ###########################################################
2
+ # tc_version.rb
3
+ #
4
+ # The sole purpose of this test case is to verify the
5
+ # version. This reduces the pain of having separate tests
6
+ # for the VERSION constant in every single test case.
7
+ ###########################################################
8
+ base = File.basename(Dir.pwd)
9
+
10
+ if base == "test" || base =~ /sys-cpu/
11
+ require "ftools"
12
+
13
+ Dir.chdir("..") if base == "test"
14
+ Dir.mkdir("sys") unless File.exist?("sys")
15
+
16
+ case RUBY_PLATFORM
17
+ when /mswin|dos|cygwin|mingw/i
18
+ file = 'lib/sys/windows.rb'
19
+ when /linux/i
20
+ file = 'lib/sys/linux.rb'
21
+ else
22
+ require "rbconfig"
23
+ file = "cpu." << Config::CONFIG["DLEXT"]
24
+ end
25
+
26
+ if File.exist?(file)
27
+ File.copy(file, "sys/cpu.rb")
28
+ else
29
+ puts "No '#{file}' file found. Please run extconf.rb and make first"
30
+ exit
31
+ end
32
+
33
+ $LOAD_PATH.unshift Dir.pwd
34
+ end
35
+
36
+ require "sys/cpu"
37
+ require "test/unit"
38
+ include Sys
39
+
40
+ class TC_Sys_CPU_VERSION < Test::Unit::TestCase
41
+ def test_version
42
+ assert_equal('0.5.5', CPU::VERSION)
43
+ end
44
+ end
@@ -0,0 +1,76 @@
1
+ ######################################################################
2
+ # tc_windows.rb
3
+ #
4
+ # Test suite for Win32 systems.
5
+ ######################################################################
6
+ base = File.basename(Dir.pwd)
7
+ if base == "test" || base =~ /sys-cpu/
8
+ require "ftools" if base == "test"
9
+ Dir.chdir '..'
10
+ Dir.mkdir("sys") unless File.exists?("sys")
11
+ File.copy("lib/sys/windows.rb","sys/cpu.rb")
12
+ $LOAD_PATH.unshift Dir.pwd
13
+ end
14
+
15
+ require "test/unit"
16
+ require "sys/cpu"
17
+ require "socket"
18
+ include Sys
19
+
20
+ class TC_Windows_CPU < Test::Unit::TestCase
21
+ def setup
22
+ @host = Socket.gethostname
23
+ end
24
+
25
+ def test_architecture
26
+ assert_respond_to(CPU, :architecture)
27
+ assert_nothing_raised{ CPU.architecture }
28
+ assert_nothing_raised{ CPU.architecture(@host) }
29
+ assert_kind_of(String,CPU.architecture,"Invalid Type")
30
+ end
31
+
32
+ def test_freq
33
+ assert_respond_to(CPU, :freq)
34
+ assert_nothing_raised{ CPU.freq }
35
+ assert_nothing_raised{ CPU.freq(0) }
36
+ assert_nothing_raised{ CPU.freq(0,@host) }
37
+ assert_kind_of(Integer,CPU.freq,"Invalid Type")
38
+ end
39
+
40
+ def test_model
41
+ assert_respond_to(CPU, :model)
42
+ assert_nothing_raised{ CPU.model }
43
+ assert_nothing_raised{ CPU.model(@host) }
44
+ assert_kind_of(String,CPU.model,"Invalid Type")
45
+ end
46
+
47
+ def test_num_cpu
48
+ assert_respond_to(CPU, :num_cpu)
49
+ assert_nothing_raised{ CPU.num_cpu }
50
+ assert_nothing_raised{ CPU.num_cpu(@host) }
51
+ assert_kind_of(Integer,CPU.num_cpu,"Invalid Type")
52
+ end
53
+
54
+ def test_type
55
+ assert_respond_to(CPU, :type)
56
+ assert_nothing_raised{ CPU.type }
57
+ assert_nothing_raised{ CPU.type(@host) }
58
+ assert_kind_of(String,CPU.type,"Invalid Type")
59
+ end
60
+
61
+ def test_load_avg
62
+ assert_respond_to(CPU, :load_avg)
63
+ assert_nothing_raised{ CPU.load_avg }
64
+ assert_nothing_raised{ CPU.load_avg(0,@host) }
65
+ assert_kind_of(Integer,CPU.load_avg,"Invalid Type")
66
+ end
67
+
68
+ def test_processors
69
+ assert_respond_to(CPU, :processors)
70
+ assert_nothing_raised{ CPU.processors{} }
71
+ end
72
+
73
+ def teardown
74
+ @host = nil
75
+ end
76
+ end
data/test/ts_all.rb ADDED
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift Dir.pwd
2
+ $LOAD_PATH.unshift Dir.pwd + '/test'
3
+
4
+ require 'tc_version'
5
+
6
+ case RUBY_PLATFORM
7
+ when /sunos|solaris/i
8
+ require 'tc_sunos'
9
+ when /linux/i
10
+ require 'tc_linux'
11
+ when /mswin|cygwin|mingw/i
12
+ require 'tc_windows'
13
+ when /hpux/i
14
+ require 'tc_hpux'
15
+ when /bsd/i
16
+ require 'tc_freebsd'
17
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: sys-cpu
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.5
7
+ date: 2006-11-18 00:00:00 -07:00
8
+ summary: A Ruby interface for providing CPU information
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/sysutils
13
+ rubyforge_project: sysutils
14
+ description: A Ruby interface for providing CPU information
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
25
+ platform: i586-linux
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - doc/freebsd.txt
33
+ - doc/sunos.txt
34
+ - doc/windows.txt
35
+ - doc/hpux.txt
36
+ - doc/linux.txt
37
+ - test/tc_linux.rb
38
+ - test/tc_sunos.rb
39
+ - test/ts_all.rb
40
+ - test/tc_hpux.rb
41
+ - test/tc_windows.rb
42
+ - test/tc_version.rb
43
+ - test/tc_freebsd.rb
44
+ - CHANGES
45
+ - README
46
+ - lib/sys/cpu.rb
47
+ test_files:
48
+ - test/ts_all.rb
49
+ rdoc_options: []
50
+
51
+ extra_rdoc_files:
52
+ - CHANGES
53
+ - README
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ requirements: []
59
+
60
+ dependencies: []
61
+