cpuid 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,25 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ ext/cpuid/Makefile
23
+ ext/cpuid/*.bundle
24
+ ext/cpuid/*.so
25
+ ext/cpuid/*.o
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Edgar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = cpuid
2
+
3
+ Access x86 cpuid information from Ruby.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Michael Edgar. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cpuid"
8
+ gem.summary = "Access x86 CPUID information from Ruby"
9
+ gem.description = <<-EOF
10
+ Access x86 CPUID information from Ruby, including vendor name, brand information,
11
+ processor family, and so on.
12
+ EOF
13
+ gem.email = "michael.j.edgar@dartmouth.edu"
14
+ gem.homepage = "http://github.com/michaeledgar/cpuid"
15
+ gem.authors = ["Michael Edgar"]
16
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/test_*.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "cpuid #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,44 @@
1
+ #include <ruby.h>
2
+
3
+ static VALUE has_cpuid(VALUE self)
4
+ {
5
+ long a, c;
6
+
7
+ __asm__ __volatile__ (
8
+ "pushf\n\t"
9
+ "pop %0\n\t"
10
+ "mov %0, %1\n\t"
11
+ "xor $0x200000, %0\n\t"
12
+ "push %0\n\t"
13
+ "popf\n\t"
14
+ "pushf\n\t"
15
+ "pop %0\n\t"
16
+ : "=a" (a), "=c" (c) : : "cc"
17
+ );
18
+
19
+ return a != c ? Qtrue : Qfalse;
20
+ }
21
+
22
+ static VALUE run_cpuid(VALUE self, VALUE ax)
23
+ {
24
+ unsigned int op = NUM2UINT(ax);
25
+ unsigned int regs[4];
26
+
27
+ __asm__ __volatile__ (
28
+ "mov %%ebx, %%esi\n\t"
29
+ "cpuid\n\t"
30
+ "xchg %%ebx, %%esi"
31
+ : "=a" (regs[0]), "=S" (regs[1]),
32
+ "=c" (regs[2]), "=d" (regs[3])
33
+ : "0" (op)
34
+ );
35
+
36
+ return rb_ary_new3(4, INT2NUM(regs[0]), INT2NUM(regs[1]), INT2NUM(regs[2]), INT2NUM(regs[3]));
37
+ }
38
+
39
+ void Init_cpuid_ext()
40
+ {
41
+ VALUE m_CPUID = rb_define_module("CPUID");
42
+ rb_define_singleton_method(m_CPUID, "has_cpuid?", has_cpuid, 0);
43
+ rb_define_singleton_method(m_CPUID, "run_cpuid", run_cpuid, 1);
44
+ }
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile("cpuid_ext")
@@ -0,0 +1,2 @@
1
+ require '../ext/cpuid/cpuid_ext'
2
+ require 'cpuid/cpuid'
@@ -0,0 +1,101 @@
1
+ module CPUID
2
+ class UnsupportedFunction < StandardError; end
3
+
4
+ extend self
5
+
6
+ VENDOR_ID_FN = 0
7
+ SIGNATURE_FEATURES_FN = 1
8
+ SERIAL_NUMBER_FN = 3
9
+ MAX_EXT_FN = 0x80000000
10
+ BRAND_STR_FN_1 = 0x80000002
11
+ BRAND_STR_FN_2 = 0x80000003
12
+ BRAND_STR_FN_3 = 0x80000004
13
+
14
+ def model_information
15
+ processor_type = [
16
+ "Original OEM Processor",
17
+ "Intel OverDrive",
18
+ "Dual Processor"
19
+ ]
20
+
21
+ eax, ebx, ecx, edx = run_function(SIGNATURE_FEATURES_FN)
22
+
23
+ step = eax & 0xf
24
+ model = (eax >> 3) & 0xf
25
+ family = (eax >> 8) & 0xf
26
+ type = (eax >> 12) & 0x3
27
+ ext_model = (eax >> 16) & 0xf
28
+ ext_family = (eax >> 20) & 0xff
29
+
30
+ model = (ext_model << 4) | model
31
+ family = (ext_family << 4) | family
32
+
33
+ {:family => family, :model => model, :type => type, :step => step, :model_string => processor_type[type]}
34
+ end
35
+
36
+ def processor_serial_number
37
+ eax, ebx, ecx, edx = run_function(SERIAL_NUMBER_FN)
38
+ [signature, edx, ecx].map {|reg| register_to_hex_s(reg)}.join("-")
39
+ end
40
+
41
+ def vendor_string
42
+ eax, ebx, ecx, edx = run_function(VENDOR_ID_FN)
43
+ register_to_s(ebx) + register_to_s(edx) + register_to_s(ecx)
44
+ end
45
+
46
+ def brand_string
47
+ [BRAND_STR_FN_1, BRAND_STR_FN_2, BRAND_STR_FN_3].map do |fxn|
48
+ reg_array_to_s(run_function(fxn))
49
+ end.join
50
+ end
51
+
52
+ #private
53
+
54
+ def signature
55
+ run_function(SIGNATURE_FEATURES_FN).first
56
+ end
57
+
58
+ def run_function(fn)
59
+ if can_run(fn)
60
+ run_cpuid(fn)
61
+ else
62
+ raise UnsupportedFunction.new("The requested CPUID function 0x#{fn.to_s(16).rjust(8,"0")} is unsupported by your CPU.")
63
+ end
64
+ end
65
+
66
+ def can_run(fn)
67
+ (fn < MAX_EXT_FN && fn <= max_basic_param) || (fn >= MAX_EXT_FN && fn <= max_extended_param)
68
+ end
69
+
70
+ def max_basic_param
71
+ @max_basic_param ||= run_cpuid(VENDOR_ID_FN).first
72
+ end
73
+
74
+ def max_extended_param
75
+ @max_extended_param ||= run_cpuid(MAX_EXT_FN).first
76
+ end
77
+
78
+ def get_byte(reg, i)
79
+ (reg >> (i * 8)) & 0xFF
80
+ end
81
+
82
+ def register_to_s(reg)
83
+ str = ""
84
+ 0.upto(3) do |idx|
85
+ str << (get_byte(reg, idx)).chr
86
+ end
87
+ str
88
+ end
89
+
90
+ def register_to_hex_s(reg)
91
+ str = ""
92
+ nibs = [0,1,2,3].map {|idx| get_byte(reg, idx).to_s(16).rjust(2,"0")}
93
+ str = "#{nibs[0]}#{nibs[1]}-#{nibs[2]}#{nibs[3]}"
94
+ str
95
+ end
96
+
97
+ def reg_array_to_s(ary)
98
+ ary.map {|reg| register_to_s(reg)}.join
99
+ end
100
+
101
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'cpuid'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestCpuid < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpuid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Edgar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-19 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: " Access x86 CPUID information from Ruby, including vendor name, brand information,\n\
26
+ processor family, and so on.\n"
27
+ email: michael.j.edgar@dartmouth.edu
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.rdoc
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.rdoc
40
+ - Rakefile
41
+ - VERSION
42
+ - ext/cpuid/cpuid_ext.c
43
+ - ext/cpuid/extconf.rb
44
+ - lib/cpuid.rb
45
+ - lib/cpuid/cpuid.rb
46
+ - test/helper.rb
47
+ - test/test_cpuid.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/michaeledgar/cpuid
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --charset=UTF-8
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Access x86 CPUID information from Ruby
76
+ test_files:
77
+ - test/helper.rb
78
+ - test/test_cpuid.rb