aboutsummaryrefslogtreecommitdiffstats
path: root/poc/cputemp2maxfreq.sh
blob: 027e05486e89e033eee8cea1d0c6312a43ca7f85 (plain)
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
#!/bin/sh
#
#   This file is part of cputemp2maxfreq.
#
#   Copyright (C) 2023, 2024 pa4wdh
#
#   cputemp2maxfreq 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 3 of the License, or
#   (at your option) any later version.
#
#   cputemp2maxfreq 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 cputemp2maxfreq.  If not, see <https://www.gnu.org/licenses/>.
#
TEMP_INPUT=/sys/devices/virtual/thermal/thermal_zone0/temp
TEMP_MAX=70000
FREQ_STEP=100000

read CPU_MIN < /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_min_freq
read CPU_MAX < /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq
read GOV_MAX < /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq

CPUS=`find /sys/devices/system/cpu -name "cpufreq" -type l`

set_max_freq()
{
 local NEWFREQ=$1

 for CPU in $CPUS
 do
  echo "$NEWFREQ" > "$CPU/scaling_max_freq"
 done

 sleep 0.1
 read GOV_MAX < /sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq
}

while true
do
 read TEMP < /sys/devices/virtual/thermal/thermal_zone0/temp
 read CPU_CUR < /sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq
 printf "$TEMP $TEMP_MAX $CPU_MAX $GOV_MAX $CPU_CUR"
 if [ "$TEMP" -lt "$TEMP_MAX" -a "$GOV_MAX" -lt "$CPU_MAX" ]
 then
  DIFF=$(((TEMP_MAX-TEMP)/1000))
  NEWFREQ=$((GOV_MAX+(FREQ_STEP*DIFF)))
  if [ "$NEWFREQ" -gt "$CPU_MAX" ]
  then
   NEWFREQ=$CPU_MAX
  fi
  printf " Increase GOV_MAX to $NEWFREQ"
  set_max_freq $NEWFREQ
 fi
 if [ "$TEMP" -gt "$TEMP_MAX" -a "$GOV_MAX" -gt "$CPU_MIN" ]
 then
  DIFF=$(((TEMP-TEMP_MAX)/1000))
  NEWFREQ=$((GOV_MAX-(FREQ_STEP*DIFF)))
  if [ "$NEWFREQ" -lt "$CPU_MIN" ]
  then
   NEWFREQ=$CPU_MIN
  fi
  printf " Decrease GOV_MAX to $NEWFREQ"
  set_max_freq $NEWFREQ
 fi
 echo ""
 sleep 10
done