-
Notifications
You must be signed in to change notification settings - Fork 6
/
smt-manager.pl
executable file
·231 lines (165 loc) · 6.17 KB
/
smt-manager.pl
1
2
3
4
5
6
7
8
9
10
11
12
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env perl
# smt-manager.pl - a script for managing logical cores
# Copyright (C) 2015 Steven Barrett
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
use warnings;
use strict;
use v5.20;
use Getopt::Long;
use Pod::Usage;
# This is the top folder where CPUs can be enumerated and more detail retrieved.
use constant SYS_CPU => '/sys/devices/system/cpu';
use constant DEBUG => '0';
sub get_cpu_indexes {
opendir( my $dh, SYS_CPU ) or die "Cannot open folder: " . SYS_CPU;
my @cpu_indexes = map { s/cpu//r } grep { /^cpu[0-9]+/ } readdir($dh);
closedir $dh;
return \@cpu_indexes;
}
sub get_cpu_settings {
my $cpu_indexes = get_cpu_indexes();
my $cpus = {};
foreach my $cpu ( @$cpu_indexes ) {
my $siblings_file = SYS_CPU . "/cpu$cpu/topology/thread_siblings_list";
my $power_file = SYS_CPU . "/cpu$cpu/online";
my $cpu_settings = {
'core_type' => 'logical',
'power' => 'offline'
};
# Populate core topology, primary / logical
if ( open( my $fh, '<', $siblings_file ) ) {
my @siblings = split(/,|-/, <$fh>);
close $fh;
my $cpu_num = $cpu;
$cpu_num =~ s/cpu(\d+)/$1/;
if ( $cpu_num == $siblings[0] ) {
$cpu_settings->{'core_type'} = 'primary';
}
} else {
print "[ERROR] Could not open: $siblings_file\n" if DEBUG;
}
# Populate core status, online / offline
if ( open my $fh, '<', $power_file ) {
my $cpu_power = <$fh>;
close $fh;
chomp($cpu_power);
$cpu_settings->{'power'} = 'online' if ($cpu_power == 1);
} else {
print "[ERROR] Could not open: $power_file, assuming online\n" if DEBUG;
$cpu_settings->{'power'} = 'online';
}
$cpus->{$cpu} = $cpu_settings;
}
return $cpus;
}
sub set_logical_cpus {
my $power_state = shift;
my $cpus = get_cpu_settings();
my $state_changed = 0;
foreach my $cpu ( sort { $a <=> $b } keys %$cpus ) {
if ( ($cpus->{$cpu}->{'core_type'} eq 'logical' ||
$cpus->{$cpu}->{'core_type'} eq 'unknown') &&
$cpus->{$cpu}->{'power'} ne $power_state ) {
my $power_file = SYS_CPU . "/cpu$cpu/online";
if ( open( my $fh, '>', $power_file ) ) {
$state_changed = 1;
print "Setting $cpu to $power_state ... ";
print $fh '1' if $power_state eq 'online';
print $fh '0' if $power_state eq 'offline';
close $fh;
print "done!\n";
} else {
print "[ERROR] failed to open file for writing: $power_file. Are you root?\n"
}
}
}
if ( $state_changed ) {
# Lets rebalance the interrupts after power state changes to guarantee
# the system will remain stable.
unless ( system( 'irqbalance --oneshot' ) == 0 ) {
print STDERR "[ERROR] Failed to balance interrupts with 'irqbalance --oneshot', you may experience strange behavior.\n";
}
print "\n";
}
return $state_changed;
}
sub pretty_print_topology {
my $cpus = get_cpu_settings();
print "CPU topology:\n";
foreach my $cpu ( sort { $a <=> $b } keys %$cpus ) {
print "$cpu: " . $cpus->{$cpu}->{'core_type'} . " | " . $cpus->{$cpu}->{'power'} . "\n";
}
print "\n";
}
### EXECUTE ###
my $help;
my $online;
my $offline;
my $manual;
GetOptions (
'help' => \$help,
'online' => \$online,
'offline' => \$offline,
'manual' => \$manual
) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $manual;
if ( $online && $offline ) {
pod2usage("Invalid option combination: you cannot offline and online at the same time.\n");
}
my $power_state;
$power_state = 'online' if $online;
$power_state = 'offline' if $offline;
pretty_print_topology();
if ( defined $power_state && set_logical_cpus($power_state) ) {
# If there was a change, print the new state.
pretty_print_topology();
}
=pod
=head1 NAME
smt-manager.pl - View current status of CPU topology or set logical cores to
offline or online.
=head1 SYNOPSIS
smt-manager.pl [options]
--help Prints this help.
--online Enables all logical CPU cores.
--offline Disables all logical CPU cores.
--manual Display man page of this script.
=head1 OPTIONS
=over 8
=item B<--help>
Prints standard help information and options that are available.
=item B<--online>
Attempts to enable all logical cores if they are already offline. If they are
not offline, no changes will be made and no secondary topology print out will
occur.
=item B<--offline>
Performs the opposite action as --online.
=item B<--manual>
Displays help page as a man page and also displays description and summary.
=back
=head1 DESCRIPTION
This script provides the user details about whether each CPU is physical or
logical. When provided an optional parameter, the logical CPUs can be enabled
or disabled.
Why would one want to disable logical cores? Logical CPU cores with a high nice
value steal more CPU time than a physical core. This is noticeable when
multitasking heavily, for instance, building a kernel with 8 cores as a very
nice (+20) task while running a greedy single threaded application.
By disabling the logical cores, the behavior and responsiveness of the system
will be more predictable, but throughput will drop as a whole for heavy multi-
threaded batch jobs.
=cut