hyperctl 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd42c9d0ce6995577336ac5d079c38b2dbcf5f0b
4
- data.tar.gz: d71520d05bb74c3bb584868fbdb9663be22b7b6c
3
+ metadata.gz: f0b2b0f0133da7e528821e2c7424a4fd5bfc4163
4
+ data.tar.gz: c77ebe50391c5405b75bfe8121f8edf040492576
5
5
  SHA512:
6
- metadata.gz: d90ca4e32248d536a58eb2a9f463e69caa9cbf8617d2d9e0b86358f38f95e2727292476c0ac94ba5acceadf373ee4c6db6075f7cacbd19acfbad78f63bc856d8
7
- data.tar.gz: f3fdfd9fbc3ee5c449efddcd8af2daf59ed74123efe78769add4bffd3d9e7ae87942133907d3504cc65c51fb7a8f4783f2313056f63eaa2d2e511b2c24d4d523
6
+ metadata.gz: bab6dea8336e756ffa21d23d5ff41410adcc56a13dea20bb5ff0eb1b25aaaebe1fc6d3eb58b24b1ecb80c949212a9ac7e17ca6a2639a3b09fca4a5a75269ca42
7
+ data.tar.gz: d8864acf582d1a736fc4112a005475b6097e5f2e80969350827f3e1d5423eabdf9e18aadee0b497b7b9d1e8a2e586a6e646e400a6cc799faeea250621941a646
@@ -1,4 +1,5 @@
1
1
  require 'hyperctl/sysfs'
2
+ require 'hyperctl/version'
2
3
 
3
4
  module Hyperctl
4
5
  # Enable all sibling cores
@@ -1,189 +1,193 @@
1
- class Hyperctl::Sysfs
2
- attr_accessor :cpu_info
3
-
4
- # @api private
5
- def initialize
6
- @cpu_info = refresh
7
- end
8
-
9
- # Refresh the `cpu_info` [Hash] of the current CPU states
10
- #
11
- # @return [Hash] of cpu status
12
- # @api public
13
- def refresh
14
- info = {}
15
-
16
- # get a listing of all cpu cores
17
- cpu_dirs = Dir.glob('/sys/devices/system/cpu/cpu[0-9]*')
18
-
19
- cpu_dirs.each do |d|
20
- # find the "name" of the cpu based on the sysfs dir. Eg, cpu15
21
- cpu_name = File.basename d
22
- cpu_idx = cpu_name.to_sym
23
- info[cpu_idx] = { :name => cpu_name }
24
-
25
- # find the numeric core_id. Eg, 15 in sysfs as
26
- # /sys/devices/system/cpu/cpu3/topology/core_id but we can parse it from
27
- # the path
28
- core_id = cpu_name.match(/cpu(\d+)/)[1].to_i
29
- info[cpu_idx][:core_id] = core_id
30
-
31
- # is the cpu online?
32
- # if a CPU is online, /sys/devices/system/cpu/cpu1/online will be 1,
33
- # otherwise 0. cpu0 appears to be special and does not have the online
34
- # sysfs entry on any of the systems I inspected. I suspect that it might
35
- # get this attribute if CONFIG_BOOTPARAM_HOTPLUG_CPU0 is enabled per
36
- # https://www.kernel.org/doc/Documentation/cpu-hotplug.txt
37
- path = File.join(d, 'online')
38
- online = false
39
- if File.exist?(path)
40
- online = to_bool(File.read(path).chomp)
41
- elsif core_id == 0
42
- # cpu0 gets a special pass if the online attr is missing
43
- online = true
44
- end
45
- info[cpu_idx][:online] = online
46
-
47
- next unless online
48
-
49
- # does the cpu have any [SMT] siblings?
50
- # The attr /sys/devices/system/cpu/cpu6/topology/thread_siblings_list
51
- # will list all siblings including the cpu's own core_id This attr is not
52
- # present if the cpu is offline This attr is not present under EL5.x
53
- # (2.6.18-164.el5PAE) on the one system I inspected that appears to have
54
- # HT disabled in the bios (/proc/cpuinfo shows the ht cpu flag but
55
- # there's no siblings list)
56
- path = File.join(d, 'topology/thread_siblings_list')
57
- if File.exist?(path)
58
- sibs = File.read(path).chomp.split(',')
59
- # convert core_id(s) to be numeric
60
- sibs.map! {|s| s.to_i }
61
- # remove the cpu's core_id from the list
62
- sibs = sibs - [ core_id ]
63
- unless sibs.empty?
64
- info[cpu_idx][:thread_siblings_list] = sibs
1
+ require 'hyperctl'
2
+
3
+ module Hyperctl
4
+ class Sysfs
5
+ attr_accessor :cpu_info
6
+
7
+ # @api private
8
+ def initialize
9
+ @cpu_info = refresh
10
+ end
11
+
12
+ # Refresh the `cpu_info` [Hash] of the current CPU states
13
+ #
14
+ # @return [Hash] of cpu status
15
+ # @api public
16
+ def refresh
17
+ info = {}
18
+
19
+ # get a listing of all cpu cores
20
+ cpu_dirs = Dir.glob('/sys/devices/system/cpu/cpu[0-9]*')
21
+
22
+ cpu_dirs.each do |d|
23
+ # find the "name" of the cpu based on the sysfs dir. Eg, cpu15
24
+ cpu_name = File.basename d
25
+ cpu_idx = cpu_name.to_sym
26
+ info[cpu_idx] = { :name => cpu_name }
27
+
28
+ # find the numeric core_id. Eg, 15 in sysfs as
29
+ # /sys/devices/system/cpu/cpu3/topology/core_id but we can parse it from
30
+ # the path
31
+ core_id = cpu_name.match(/cpu(\d+)/)[1].to_i
32
+ info[cpu_idx][:core_id] = core_id
33
+
34
+ # is the cpu online?
35
+ # if a CPU is online, /sys/devices/system/cpu/cpu1/online will be 1,
36
+ # otherwise 0. cpu0 appears to be special and does not have the online
37
+ # sysfs entry on any of the systems I inspected. I suspect that it might
38
+ # get this attribute if CONFIG_BOOTPARAM_HOTPLUG_CPU0 is enabled per
39
+ # https://www.kernel.org/doc/Documentation/cpu-hotplug.txt
40
+ path = File.join(d, 'online')
41
+ online = false
42
+ if File.exist?(path)
43
+ online = to_bool(File.read(path).chomp)
44
+ elsif core_id == 0
45
+ # cpu0 gets a special pass if the online attr is missing
46
+ online = true
47
+ end
48
+ info[cpu_idx][:online] = online
49
+
50
+ next unless online
51
+
52
+ # does the cpu have any [SMT] siblings?
53
+ # The attr /sys/devices/system/cpu/cpu6/topology/thread_siblings_list
54
+ # will list all siblings including the cpu's own core_id This attr is not
55
+ # present if the cpu is offline This attr is not present under EL5.x
56
+ # (2.6.18-164.el5PAE) on the one system I inspected that appears to have
57
+ # HT disabled in the bios (/proc/cpuinfo shows the ht cpu flag but
58
+ # there's no siblings list)
59
+ path = File.join(d, 'topology/thread_siblings_list')
60
+ if File.exist?(path)
61
+ sibs = File.read(path).chomp.split(',')
62
+ # convert core_id(s) to be numeric
63
+ sibs.map! {|s| s.to_i }
64
+ # remove the cpu's core_id from the list
65
+ sibs = sibs - [ core_id ]
66
+ unless sibs.empty?
67
+ info[cpu_idx][:thread_siblings_list] = sibs
68
+ end
65
69
  end
66
70
  end
71
+
72
+ @cpu_info = info
67
73
  end
68
74
 
69
- @cpu_info = info
70
- end
71
-
72
- # List of all CPUs in system by numeric core id
73
- #
74
- # @return [Array<Integer>] of core ids
75
- # @api public
76
- def cores
77
- cores = []
78
- cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
79
- cores << cpu_info[k][:core_id]
75
+ # List of all CPUs in system by numeric core id
76
+ #
77
+ # @return [Array<Integer>] of core ids
78
+ # @api public
79
+ def cores
80
+ cores = []
81
+ cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
82
+ cores << cpu_info[k][:core_id]
83
+ end
84
+
85
+ return cores
80
86
  end
81
87
 
82
- return cores
83
- end
84
-
85
- # List of online CPUs in system by numeric core id
86
- #
87
- # @return [Array<Integer>] of core ids
88
- # @api public
89
- def online_cores
90
- cores = []
91
- cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
92
- core_id = cpu_info[k][:core_id]
93
- if cpu_info[k][:online] == true
94
- cores << core_id
88
+ # List of online CPUs in system by numeric core id
89
+ #
90
+ # @return [Array<Integer>] of core ids
91
+ # @api public
92
+ def online_cores
93
+ cores = []
94
+ cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
95
+ core_id = cpu_info[k][:core_id]
96
+ if cpu_info[k][:online] == true
97
+ cores << core_id
98
+ end
95
99
  end
100
+
101
+ return cores
96
102
  end
97
103
 
98
- return cores
99
- end
100
-
101
- # List of offline CPUs in system by numeric core id
102
- #
103
- # @return [Array<Integer>] of core ids
104
- # @api public
105
- def offline_cores
106
- cores = []
107
- cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
108
- core_id = cpu_info[k][:core_id]
109
- if cpu_info[k][:online] == false
110
- cores << core_id
104
+ # List of offline CPUs in system by numeric core id
105
+ #
106
+ # @return [Array<Integer>] of core ids
107
+ # @api public
108
+ def offline_cores
109
+ cores = []
110
+ cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
111
+ core_id = cpu_info[k][:core_id]
112
+ if cpu_info[k][:online] == false
113
+ cores << core_id
114
+ end
111
115
  end
116
+
117
+ return cores
112
118
  end
113
119
 
114
- return cores
115
- end
116
-
117
- # List of sibling (aka hyperthread/SMT) CPUs in system by numeric core id
118
- #
119
- # @return [Array<Integer>] of core ids
120
- # @api public
121
- def sibling_cores
122
- cores = []
123
- checked_cores = []
124
- cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
125
- cpu = cpu_info[k]
126
- checked_cores << cpu[:core_id]
127
-
128
- if cpu.has_key?(:thread_siblings_list)
129
- (cpu[:thread_siblings_list] - checked_cores).each do |core_id|
130
- # check to see if the core is already disabled
131
- # XXX this probably isn't nessicary as a disabled core appears to #
132
- # never be listed as a sibiling
133
- if cpu_info[k][:online] == true
134
- cores << core_id
120
+ # List of sibling (aka hyperthread/SMT) CPUs in system by numeric core id
121
+ #
122
+ # @return [Array<Integer>] of core ids
123
+ # @api public
124
+ def sibling_cores
125
+ cores = []
126
+ checked_cores = []
127
+ cpu_info.each_key.sort_by {|k| cpu_info[k][:core_id] }.each do |k|
128
+ cpu = cpu_info[k]
129
+ checked_cores << cpu[:core_id]
130
+
131
+ if cpu.has_key?(:thread_siblings_list)
132
+ (cpu[:thread_siblings_list] - checked_cores).each do |core_id|
133
+ # check to see if the core is already disabled
134
+ # XXX this probably isn't nessicary as a disabled core appears to #
135
+ # never be listed as a sibiling
136
+ if cpu_info[k][:online] == true
137
+ cores << core_id
138
+ end
135
139
  end
136
140
  end
137
141
  end
142
+
143
+ return cores
144
+ end
145
+
146
+ # Are all phyical/hyperthread/SMT/sibling cores enabled?
147
+ #
148
+ # @return [Bool] of status
149
+ # @api public
150
+ def all_cores_enabled?
151
+ cores.count == online_cores.count
152
+ end
153
+
154
+ # Are all hyperthread/SMT/sibling cores Disabled?
155
+ #
156
+ # @return [Bool] of status
157
+ # @api public
158
+ def all_siblings_disabled?
159
+ sibling_cores.empty?
160
+ end
161
+
162
+ # Enable a CPU by numeric core id
163
+ #
164
+ # @param core_id [Integer]
165
+ # @api public
166
+ def self.enable_core(core_id)
167
+ set_core(core_id, '1')
138
168
  end
139
169
 
140
- return cores
141
- end
142
-
143
- # Are all phyical/hyperthread/SMT/sibling cores enabled?
144
- #
145
- # @return [Bool] of status
146
- # @api public
147
- def all_cores_enabled?
148
- cores.count == online_cores.count
149
- end
150
-
151
- # Are all hyperthread/SMT/sibling cores Disabled?
152
- #
153
- # @return [Bool] of status
154
- # @api public
155
- def all_siblings_disabled?
156
- sibling_cores.empty?
157
- end
158
-
159
- # Enable a CPU by numeric core id
160
- #
161
- # @param core_id [Integer]
162
- # @api public
163
- def self.enable_core(core_id)
164
- set_core(core_id, '1')
165
- end
166
-
167
- # Disable a CPU by numeric core id
168
- #
169
- # @param core_id [Integer]
170
- # @api public
171
- def self.disable_core(core_id)
172
- set_core(core_id, '0')
173
- end
174
-
175
- private
176
-
177
- def to_bool(s)
178
- return true if s =~ /^1$/
179
- return false
180
- end
181
-
182
- def self.set_core(core_id, state)
183
- path = File.join('/sys/devices/system/cpu', "cpu#{core_id.to_s}", 'online')
184
- # doesn't work in ruby 1.8.7: File.write(path, '0')
185
- File.open(path, 'w') do |f|
186
- f.write(state)
170
+ # Disable a CPU by numeric core id
171
+ #
172
+ # @param core_id [Integer]
173
+ # @api public
174
+ def self.disable_core(core_id)
175
+ set_core(core_id, '0')
176
+ end
177
+
178
+ private
179
+
180
+ def to_bool(s)
181
+ return true if s =~ /^1$/
182
+ return false
183
+ end
184
+
185
+ def self.set_core(core_id, state)
186
+ path = File.join('/sys/devices/system/cpu', "cpu#{core_id.to_s}", 'online')
187
+ # doesn't work in ruby 1.8.7: File.write(path, '0')
188
+ File.open(path, 'w') do |f|
189
+ f.write(state)
190
+ end
187
191
  end
188
- end
189
- end # class Hyperctl::Sysfs
192
+ end # class Sysfs
193
+ end # module Hyperctl
@@ -1,3 +1,3 @@
1
1
  module Hyperctl
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperctl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hoblitt