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.



8
9
10
# File 'lib/hyperctl/sysfs.rb', line 8

def initialize
  @cpu_info = refresh
end

Instance Attribute Details

#cpu_infoObject

Returns the value of attribute cpu_info.



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

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)


174
175
176
# File 'lib/hyperctl/sysfs.rb', line 174

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)


166
167
168
# File 'lib/hyperctl/sysfs.rb', line 166

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



150
151
152
# File 'lib/hyperctl/sysfs.rb', line 150

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



158
159
160
# File 'lib/hyperctl/sysfs.rb', line 158

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



79
80
81
82
83
84
85
86
# File 'lib/hyperctl/sysfs.rb', line 79

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



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hyperctl/sysfs.rb', line 108

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/hyperctl/sysfs.rb', line 92

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



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
71
72
73
# File 'lib/hyperctl/sysfs.rb', line 16

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



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

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