Class: Hyperctl::Sysfs

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperctl/sysfs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSysfs

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Sysfs.



5
6
7
# File 'lib/hyperctl/sysfs.rb', line 5

def initialize
  @cpu_info = refresh
end

Instance Attribute Details

#cpu_infoObject

Returns the value of attribute cpu_info.



2
3
4
# File 'lib/hyperctl/sysfs.rb', line 2

def cpu_info
  @cpu_info
end

Class Method Details

.disable_core(core_id) ⇒ Object

Disable a CPU by numeric core id

Parameters:

  • core_id (Integer)


171
172
173
# File 'lib/hyperctl/sysfs.rb', line 171

def self.disable_core(core_id)
  set_core(core_id, '0')
end

.enable_core(core_id) ⇒ Object

Enable a CPU by numeric core id

Parameters:

  • core_id (Integer)


163
164
165
# File 'lib/hyperctl/sysfs.rb', line 163

def self.enable_core(core_id)
  set_core(core_id, '1')
end

Instance Method Details

#all_cores_enabled?Bool

Are all phyical/hyperthread/SMT/sibling cores enabled?

Returns:

  • (Bool)

    of status



147
148
149
# File 'lib/hyperctl/sysfs.rb', line 147

def all_cores_enabled?
  cores.count == online_cores.count
end

#all_siblings_disabled?Bool

Are all hyperthread/SMT/sibling cores Disabled?

Returns:

  • (Bool)

    of status



155
156
157
# File 'lib/hyperctl/sysfs.rb', line 155

def all_siblings_disabled?
  sibling_cores.empty?
end

#coresArray<Integer>

List of all CPUs in system by numeric core id

Returns:

  • (Array<Integer>)

    of core ids



76
77
78
79
80
81
82
83
# File 'lib/hyperctl/sysfs.rb', line 76

def cores
  cores = []
  cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
    cores << cpu_info[k][:core_id]
  end

  return cores
end

#offline_coresArray<Integer>

List of offline CPUs in system by numeric core id

Returns:

  • (Array<Integer>)

    of core ids



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hyperctl/sysfs.rb', line 105

def offline_cores
  cores = []
  cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
    core_id = cpu_info[k][:core_id]
    if cpu_info[k][:online] == false
      cores << core_id
    end
  end

  return cores
end

#online_coresArray<Integer>

List of online CPUs in system by numeric core id

Returns:

  • (Array<Integer>)

    of core ids



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hyperctl/sysfs.rb', line 89

def online_cores
  cores = []
  cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
    core_id = cpu_info[k][:core_id]
    if cpu_info[k][:online] == true
      cores << core_id
    end
  end

  return cores
end

#refreshHash

Refresh the ‘cpu_info` [Hash] of the current CPU states

Returns:

  • (Hash)

    of cpu status



13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hyperctl/sysfs.rb', line 13

def refresh
  info = {}

  # get a listing of all cpu cores
  cpu_dirs = Dir.glob('/sys/devices/system/cpu/cpu[0-9]*')

  cpu_dirs.each do |d|
    # find the "name" of the cpu based on the sysfs dir. Eg, cpu15
    cpu_name = File.basename d
    cpu_idx = cpu_name.to_sym
    info[cpu_idx] = { :name => cpu_name }

    # find the numeric core_id. Eg, 15 in sysfs as
    # /sys/devices/system/cpu/cpu3/topology/core_id but we can parse it from
    # the path
    core_id = cpu_name.match(/cpu(\d+)/)[1].to_i
    info[cpu_idx][:core_id] = core_id

    # is the cpu online?
    # if a CPU is online, /sys/devices/system/cpu/cpu1/online will be 1,
    # otherwise 0.  cpu0 appears to be special and does not have the online
    # sysfs entry on any of the systems I inspected. I suspect that it might
    # get this attribute if CONFIG_BOOTPARAM_HOTPLUG_CPU0 is enabled per
    # https://www.kernel.org/doc/Documentation/cpu-hotplug.txt
    path = File.join(d, 'online')
    online = false
    if File.exist?(path)
      online = to_bool(File.read(path).chomp)
    elsif core_id == 0
      # cpu0 gets a special pass if the online attr is missing
      online = true
    end
    info[cpu_idx][:online] = online

    next unless online

    # does the cpu have any [SMT] siblings?
    # The attr /sys/devices/system/cpu/cpu6/topology/thread_siblings_list
    # will list all siblings including the cpu's own core_id This attr is not
    # present if the cpu is offline This attr is not present under EL5.x
    # (2.6.18-164.el5PAE) on the one system I inspected that appears to have
    # HT disabled in the bios (/proc/cpuinfo shows the ht cpu flag but
    # there's no siblings list)
    path = File.join(d, 'topology/thread_siblings_list')
    if File.exist?(path)
      sibs = File.read(path).chomp.split(',')
      # convert core_id(s) to be numeric
      sibs.map! {|s| s.to_i }
      # remove the cpu's core_id from the list
      sibs = sibs - [ core_id ]
      unless sibs.empty?
        info[cpu_idx][:thread_siblings_list] = sibs
      end
    end
  end

  @cpu_info = info
end

#sibling_coresArray<Integer>

List of sibling (aka hyperthread/SMT) CPUs in system by numeric core id

Returns:

  • (Array<Integer>)

    of core ids



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hyperctl/sysfs.rb', line 121

def sibling_cores
  cores = []
  checked_cores = []
  cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
    cpu = cpu_info[k]
    checked_cores << cpu[:core_id]

    if cpu.has_key?(:thread_siblings_list)
      (cpu[:thread_siblings_list] - checked_cores).each do |core_id|
        # check to see if the core is already disabled
        # XXX this probably isn't nessicary as a disabled core appears to #
        # never be listed as a sibiling
        if cpu_info[k][:online] == true
          cores << core_id
        end
      end
    end
  end

  return cores
end