Class: OsCtl::Lib::CpuTopology

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/cpu_topology.rb

Defined Under Namespace

Classes: Cpu, Package

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCpuTopology

Returns a new instance of CpuTopology.



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

def initialize
  parse
end

Instance Attribute Details

#cpusHash<Integer, Cpu> (readonly)

Returns:

  • (Hash<Integer, Cpu>)


19
20
21
# File 'lib/libosctl/cpu_topology.rb', line 19

def cpus
  @cpus
end

#packagesHash<Integer, Package> (readonly)

Returns:



16
17
18
# File 'lib/libosctl/cpu_topology.rb', line 16

def packages
  @packages
end

Instance Method Details

#parseObject (protected)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/libosctl/cpu_topology.rb', line 27

def parse
  @packages = {}
  @cpus = {}

  sys_dir = '/sys/devices/system/cpu'
  cpu_list = []

  Dir.glob(File.join(sys_dir, 'cpu*')).each do |f|
    next unless %r{/cpu(\d+)$} =~ f

    cpu_list << ::Regexp.last_match(1).to_i
  end

  cpu_list.sort.each do |cpu_id|
    begin
      online = File.read(File.join(sys_dir, "cpu#{cpu_id}", 'online')).strip
      next if online != '1'
    rescue Errno::ENOENT
      # If online file does not exist, we assume it is online
    end

    pkg_id = File.read(File.join(sys_dir, "cpu#{cpu_id}", 'topology/physical_package_id')).strip.to_i

    cpu = Cpu.new(id: cpu_id, package_id: pkg_id)

    @cpus[cpu.id] = cpu

    @packages[pkg_id] ||= Package.new(id: pkg_id, cpus: {})
    @packages[pkg_id].cpus[cpu.id] = cpu
  end
end