-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu-freq-plugin
168 lines (168 loc) · 5.19 KB
/
cpu-freq-plugin
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
#!/bin/bash
# add this to startup
# NAME_OF_THIS_FILE --set-permissions
##### The below script is to fix a permission preservation bug with the intel_pstate module
## #!/bin/sh
## # This file is: /etc/pm/sleep.d/intel_pstate
## # /usr/local/bin/cpu-freq-plugin is the full path to the genmon applet's script, see line 7 (or line 11 if this line is line 7)
## case "$1" in
## thaw|resume)
## if [ $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_driver) = "intel_pstate" ];then
## /usr/local/bin/cpu-freq-plugin --set-permissions
## fi
## ;;
## esac
##### END intel_pstate bug workaround script
if [ "$1" == "--set-permissions" ];then
LOC="/sys/devices/system/cpu"
chmod 777 $LOC/cpu*/cpufreq/{scaling_setspeed,scaling_governor} 2> /dev/null
if [ $? -eq 1 ];then
echo "You should know you need root access to do this"
exit
fi
if [ $(ls $LOC/cpu*/cpufreq/cpuinfo_cur_freq 2> /dev/null | wc -l) -gt 0 ];then
chmod 444 $LOC/cpu*/cpufreq/cpuinfo_cur_freq
fi
exit
fi
ICONS="/usr/local/share/pixmaps/cpufreq-applet"
if [ -z "$1" ];then
echo "<img>$ICONS/cpufreq-na.png</img><txt>Which Core?</txt>"
echo -e "<tool>$(basename $0) [Core Number]\n$(basename $0) 0</tool>"
exit
fi
LOC="/sys/devices/system/cpu/cpu$1/cpufreq"
if [ ! -d "$LOC" ];then
echo "<img>$ICONS/cpufreq-100.png</img>"
Spd=$(cat /proc/cpuinfo | grep 'cpu MHz' | awk "NR==$(($1+1))" | sed 's/.*: //')
Spd=$(perl -e "printf(\"%.0f\", $Spd)")
if [ $Spd -gt 999 ];then
Spd=$(perl -e "print $Spd/1000")
t="GHz"
else
t="MHz"
fi
echo -e "<tool>Core $1 is at $Spd $t\nCPU scaling is not supported</tool>"
echo "<txt>100%</txt>"
exit
fi
GOV=$(cat $LOC/scaling_governor)
if [ "$2" == "--set" ];then
GOVS=$(cat $LOC/scaling_available_governors)
NOTIFY_PRA1="Access Denied"
NOTIFY_PRA2="Run this to fix it:\n$(basename $0) --set-permissions"
if [ "${GOVS:$((${#GOVS}-1))}" != " " ];then
GOVS="$GOVS "
fi
LIST=$(echo ${GOVS:0:-1} | sed 's/.*/\L&/;s/[a-z]*/\u&/g;s/ / FALSE /g')
NewGOV=$(zenity --list --title 'Choose a Governor' --text 'Select one' --radiolist --column 'Pick' --column 'Opinion' TRUE $LIST)
if [ -n "$NewGOV" ];then
if [ "$NewGOV" == "Userspace" ];then
SPD=$(cat $LOC/scaling_available_frequencies)
LIST=$(echo ${SPD:0:-1} | sed 's/ / FALSE /g')
NewSPD=$(zenity --list --title 'Choose a Frequency' --text 'Select one' --radiolist --column 'Pick' --column 'Opinion' TRUE $LIST)
if [ -z "$NewSPD" ];then
exit
fi
fi
CoreCt=$(ls /sys/devices/system/cpu/ | grep 'cpu[0-9]' | wc -l)
if [ $CoreCt -gt 1 ];then
if [ $(zenity --question --text="Apply this to all cores?";echo $?) -eq 0 ];then
Core=0
while [ $Core -lt $CoreCt ];do
echo ${NewGOV,} > /sys/devices/system/cpu/cpu$Core/cpufreq/scaling_governor
#cpufreq-set -c $Core -g ${NewGOV,}
if [ $? -eq 1 ];then
notify-send --icon=error "$NOTIFY_PRA1" "$NOTIFY_PRA2"
exit
fi
if [ -n "$NewSPD" ];then
echo $NewSPD > /sys/devices/system/cpu/cpu$Core/cpufreq/scaling_setspeed
#cpufreq-set -c $Core -f $NewSPD
fi
Core=$(($Core+1))
done
exit
fi
fi
echo ${NewGOV,} > $LOC/scaling_governor
#cpufreq-set -c $1 -g ${NewGOV,}
if [ $? -eq 1 ];then
notify-send --icon=error "$NOTIFY_PRA1" "$NOTIFY_PRA2"
exit
fi
if [ -n "$NewSPD" ];then
echo $NewSPD > $LOC/scaling_setspeed
#cpufreq-set -c $1 -f $NewSPD
fi
fi
exit
fi
if [ -f $LOC/scaling_cur_freq ];then
CUR=$(cat $LOC/scaling_cur_freq)
elif [ -f $LOC/cpuinfo_cur_freq ];then
CUR=$(cat $LOC/cpuinfo_cur_freq 2> /dev/null)
if [ $? -eq 1 ];then
echo "<txt>ERR</txt>"
echo -e "<tool>Permission Error, to fix run:\n$(basename $0) --set-permissions</tool>"
exit
fi
else
echo "<txt>ERR</txt>"
echo "<tool>No idea how the power management works on this cpu @.@</tool>"
exit
fi
DRV=$(cat $LOC/scaling_driver)
if [ ! -f "/tmp/.CPU-Frequency" ];then
# This makes turbo frequencies read as over 100%
if [ "$DRV" == "intel_pstate" ];then
MAX=$(dmesg | grep 'tsc: Det' | awk '{print int($5*1000)}')
if [ -n "$MAX" ];then
if [ $MAX -gt 0 ];then
MAX2=$(cat $LOC/scaling_max_freq)
if [ $MAX2 -gt $MAX ];then
echo $MAX > /tmp/.CPU-Frequency
fi
fi
fi
fi
if [ ! -f "/tmp/.CPU-Frequency" ];then
echo '-1' > /tmp/.CPU-Frequency
fi
else
MAX=$(cat /tmp/.CPU-Frequency)
if [ $MAX -lt 0 ];then
MAX=$(cat $LOC/scaling_max_freq)
fi
fi
USE=$(perl -e "print int($CUR/$MAX*1000)")
GHz=$(perl -e "print $CUR/1000000")
if [ "$2" == "--bar" ];then
echo "<img>$ICONS/cpufreq-na.png</img>"
echo "<bar>$(($USE/10))</bar>"
elif [ $USE -lt 125 ];then
echo "<img>$ICONS/cpufreq-0.png</img>"
elif [ $USE -lt 375 ];then
echo "<img>$ICONS/cpufreq-25.png</img>"
elif [ $USE -lt 625 ];then
echo "<img>$ICONS/cpufreq-50.png</img>"
elif [ $USE -lt 875 ];then
echo "<img>$ICONS/cpufreq-75.png</img>"
else
echo "<img>$ICONS/cpufreq-100.png</img>"
fi
if [ ${GHz:0:1} -gt 0 ];then
Spd="$GHz GHz"
else
MHz=$(perl -e "print $GHz*1000")
Spd="$MHz MHz"
fi
USE="$(($USE/10))"
if [ $USE -lt 100 ];then
echo "<txt>$USE%<span fgcolor=\"#18191A\">0</span></txt>" # 18191A is the panel background color
else
echo "<txt>$USE%</txt>"
fi
echo -e "<tool>Core $1 is at $Spd\nGovernor is ${GOV^}\nScaling driver is $DRV</tool>"
echo "<click>$(basename $0) $1 --set</click>"
exit