Module: CPUID

Extended by:
CPUID
Included in:
CPUID
Defined in:
lib/cpuid/cpuid.rb

Defined Under Namespace

Classes: UnsupportedFunction

Constant Summary collapse

VENDOR_ID_FN =
0
SIGNATURE_FEATURES_FN =
1
SERIAL_NUMBER_FN =
3
MAX_EXT_FN =
0x80000000
BRAND_STR_FN_1 =
0x80000002
BRAND_STR_FN_2 =
0x80000003
BRAND_STR_FN_3 =
0x80000004
ADDRESS_SIZE_FN =
0x80000008

Instance Method Summary collapse

Instance Method Details

#brand_stringObject



47
48
49
50
51
# File 'lib/cpuid/cpuid.rb', line 47

def brand_string
  [BRAND_STR_FN_1, BRAND_STR_FN_2, BRAND_STR_FN_3].map do |fxn|
    reg_array_to_s(run_function(fxn))
  end.join
end

#can_run(fn) ⇒ Object



75
76
77
# File 'lib/cpuid/cpuid.rb', line 75

def can_run(fn)
  (fn < MAX_EXT_FN && fn <= max_basic_param) || (fn >= MAX_EXT_FN && fn <= max_extended_param)
end

#get_byte(reg, i) ⇒ Object



87
88
89
# File 'lib/cpuid/cpuid.rb', line 87

def get_byte(reg, i)
  (reg >> (i * 8)) & 0xFF
end

#max_basic_paramObject



79
80
81
# File 'lib/cpuid/cpuid.rb', line 79

def max_basic_param
  @max_basic_param ||= run_cpuid(VENDOR_ID_FN).first
end

#max_extended_paramObject



83
84
85
# File 'lib/cpuid/cpuid.rb', line 83

def max_extended_param
  @max_extended_param ||= run_cpuid(MAX_EXT_FN).first
end

#model_informationObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cpuid/cpuid.rb', line 15

def model_information
   processor_type = [
    "Original OEM Processor",
    "Intel OverDrive",
    "Dual Processor"
  ]

  eax, ebx, ecx, edx = run_function(SIGNATURE_FEATURES_FN)

  step = eax & 0xf
  model = (eax >> 3) & 0xf
  family = (eax >> 8) & 0xf
  type = (eax >> 12) & 0x3
  ext_model = (eax >> 16) & 0xf
  ext_family = (eax >> 20) & 0xff
  
  model = (ext_model << 4) | model
  family = (ext_family << 4) | family
  
  {:family => family, :model => model, :type => type, :step => step, :model_string => processor_type[type]}
end

#physical_address_sizeObject



57
58
59
# File 'lib/cpuid/cpuid.rb', line 57

def physical_address_size
  run_function(ADDRESS_SIZE_FN) & 0x00FF
end

#processor_serial_numberObject



37
38
39
40
# File 'lib/cpuid/cpuid.rb', line 37

def processor_serial_number
  eax, ebx, ecx, edx = run_function(SERIAL_NUMBER_FN)
  [signature, edx, ecx].map {|reg| register_to_hex_s(reg)}.join("-")
end

#reg_array_to_s(ary) ⇒ Object



106
107
108
# File 'lib/cpuid/cpuid.rb', line 106

def reg_array_to_s(ary)
  ary.map {|reg| register_to_s(reg)}.join
end

#register_to_hex_s(reg) ⇒ Object



99
100
101
102
103
104
# File 'lib/cpuid/cpuid.rb', line 99

def register_to_hex_s(reg)
  str = ""
  nibs = [0,1,2,3].map {|idx| get_byte(reg, idx).to_s(16).rjust(2,"0")}
  str = "#{nibs[0]}#{nibs[1]}-#{nibs[2]}#{nibs[3]}"
  str
end

#register_to_s(reg) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/cpuid/cpuid.rb', line 91

def register_to_s(reg)
  str = ""
  0.upto(3) do |idx|
    str << (get_byte(reg, idx)).chr
  end
  str
end

#run_function(fn) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/cpuid/cpuid.rb', line 67

def run_function(fn)
  if can_run(fn)
    run_cpuid(fn)
  else
    raise UnsupportedFunction.new("The requested CPUID function 0x#{fn.to_s(16).rjust(8,"0")} is unsupported by your CPU.")
  end
end

#signatureObject

private



63
64
65
# File 'lib/cpuid/cpuid.rb', line 63

def signature
  run_function(SIGNATURE_FEATURES_FN).first
end

#vendor_stringObject



42
43
44
45
# File 'lib/cpuid/cpuid.rb', line 42

def vendor_string
  eax, ebx, ecx, edx = run_function(VENDOR_ID_FN)
  register_to_s(ebx) + register_to_s(edx) + register_to_s(ecx)
end

#virtual_address_sizeObject



53
54
55
# File 'lib/cpuid/cpuid.rb', line 53

def virtual_address_size
  run_function(ADDRESS_SIZE_FN) & 0xFF00
end