Module: CPUID

Extended by:
CPUID
Includes:
Features, Processor
Included in:
CPUID
Defined in:
lib/cpuid/cpuid.rb,
lib/cpuid/features.rb,
lib/cpuid/processor.rb

Overview

CPUID

Module that accesses CPUID information available on x86 processors. Currently mostly supports only Intel meanings behind certain values.

Defined Under Namespace

Modules: Features, Processor Classes: UnsupportedFunction

Constant Summary

Constants included from Processor

Processor::ADDRESS_SIZE_FN, Processor::BRAND_STR_FN_1, Processor::BRAND_STR_FN_2, Processor::BRAND_STR_FN_3, Processor::EXT_L2_FN, Processor::MAX_EXT_FN, Processor::SERIAL_NUMBER_FN, Processor::SIGNATURE_FEATURES_FN, Processor::VENDOR_ID_FN

Constants included from Features

Features::EXT_FEATURE_FN, Features::INTEL_64_CHECK_BIT, Features::INTEL_LAHF_CHECK_BIT, Features::INTEL_SYSCALL_CHECK_BIT, Features::INTEL_TSC_INVARIANCE_CHECK_BIT, Features::INTEL_XD_CHECK_BIT, Features::POWER_MANAGEMENT_FN, Features::SIGNATURE_FEATURES_FN

Instance Method Summary collapse

Methods included from Processor

#amd?, #brand_string, #easy_L2_info, #family, #intel?, #model, #model_information, #physical_address_size, #processor_serial_number, #stepping, #type, #vendor_string, #virtual_address_size

Methods included from Features

#features

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CPUID::Features

Instance Method Details

#can_run?(fn) ⇒ Boolean

Checks if the processor supports the given function, by using the processor’s maximum parameter functions (also a part of the CPUID information)

Parameters:

  • fn (Fixnum)

    the function to check for availability

Returns:

  • (Boolean)

    does the processor support the function?



51
52
53
# File 'lib/cpuid/cpuid.rb', line 51

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



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

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

#max_basic_paramObject



55
56
57
# File 'lib/cpuid/cpuid.rb', line 55

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

#max_extended_paramObject



59
60
61
# File 'lib/cpuid/cpuid.rb', line 59

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

#reg_array_to_s(ary) ⇒ Object



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

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

#register_to_hex_s(reg) ⇒ Object



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

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) ⇒ String

Converts 4 bytes to 4 characters, reversing the order due to little-endian.

Parameters:

  • reg (Fixnum)

    the register value to convert to a string

Returns:

  • (String)

    a 4-character string converted from the register



72
73
74
75
76
77
78
# File 'lib/cpuid/cpuid.rb', line 72

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

#run_function(fn) ⇒ Array<Fixnum>

Only runs the given CPUID function if the processor supports it, by checking the processor’s maximum parameter values

Parameters:

  • fn (Fixnum)

    the function to check and run

Returns:

  • (Array<Fixnum>)

    an array of 4 values: eax, ebx, ecx, edx, returned by the processor.

Raises:

  • UnsupportedFunction raised if the requested function is not supported by the processor



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

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



25
26
27
# File 'lib/cpuid/cpuid.rb', line 25

def signature
  @signature ||= run_function(SIGNATURE_FEATURES_FN).first
end

#supports_easy_L2?Boolean

private

Returns:

  • (Boolean)


21
22
23
# File 'lib/cpuid/cpuid.rb', line 21

def supports_easy_L2?
  EXT_L2_FN <= max_extended_param
end