-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpugov.pl
executable file
·79 lines (62 loc) · 1.96 KB
/
cpugov.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
#!/usr/bin/perl
# </> with <3, xxkfqz 2d24
use warnings;
use strict;
my $baseDir = '/sys/devices/system/cpu';
sub checkOs {
$^O eq 'linux'
|| die "Non-linux OSes are not supported\n";
}
sub getCurrentGovernor {
open(FH, '<', "$baseDir/cpu0/cpufreq/scaling_governor")
|| die "Could not read governor: $!\n";
(my $curGovernor = <FH>) =~ s/\R//g;
close FH;
$curGovernor;
}
sub getAvailabeGovernors {
my $govListFile = "$baseDir/cpu0/cpufreq/scaling_available_governors";
open(FH, '<', $govListFile)
|| die "Could not open '$govListFile': $!\n";
my @availableGovernors = split(' ', <FH>);
close FH;
@availableGovernors;
}
checkOs;
my $currentGovernor = getCurrentGovernor;
my @availableGovernors = getAvailabeGovernors;
if ($#ARGV < 0) {
# Just print available governors
print "Available governors:\n";
for my $index (0 .. $#availableGovernors) {
my $ch = ' ';
if ($availableGovernors[$index] eq $currentGovernor) {
$ch = '->';
}
print " [$index] $ch $availableGovernors[$index]\n";
}
} else {
# Set governor
my $newGovernor = shift;
# Is governor index?
if ($newGovernor =~ /^\d+$/) {
my $index = $newGovernor;
$newGovernor = $availableGovernors[$index];
unless ($newGovernor) {
die "Not found governor with index $index\n";
}
}
grep(/^$newGovernor$/, @availableGovernors)
|| die "Governor '$newGovernor' not available\n";
opendir(my $dirHandler, $baseDir)
|| die "Could not open directory '$baseDir': $!\n";
my @cpusList = sort(grep(/^cpu\d+$/, readdir($dirHandler)));
closedir $dirHandler;
print "Switching from '$currentGovernor' to '$newGovernor'\n";
for (@cpusList) {
open(FH, '>', "$baseDir/$_/cpufreq/scaling_governor")
|| die "Could not set governor '$newGovernor' on $_: $!\n";
print FH $newGovernor;
close FH;
}
}