Bläddra i källkod

add devterm-a06-gearbox

cuu 2 år sedan
förälder
incheckning
1335b269b7
1 ändrade filer med 296 tillägg och 0 borttagningar
  1. 296 0
      Code/A06/devterm-a06-gearbox

+ 296 - 0
Code/A06/devterm-a06-gearbox

@@ -0,0 +1,296 @@
+#!/usr/bin/python3
+
+import glob
+import os
+import sys,getopt
+import subprocess
+import time
+
+gear_m =  []
+gear_m.append("    +-----------------------------------+-----------------+-----------+") #0
+gear_m.append("    |            Cortex-A53             |   Cortex-A72    | Mali-T860 |") #1
+gear_m.append("    +--------+--------+--------+--------+--------+--------+-----------+") #2
+gear_m.append("    | CPU 0  | CPU 1  | CPU 2  | CPU 3  | CPU 4  | CPU 5  |   GPU     |") #3
+gear_m.append("+---+--------+--------+--------+--------+--------+--------+-----------+") #4
+gear_m.append("| 1 | 600 MHz|                  OFF                       |           |") #5
+gear_m.append("+---+--------+--------+-----------------------------------+  200 MHz  +") #6
+gear_m.append("| 2 |     800 MHz     |                OFF                |           |") #7
+gear_m.append("+---+-----------------+-----------------+-----------------+-----------+") #8
+gear_m.append("| 3 |              1008 MHz             |       OFF       |           |") #9
+gear_m.append("+---+-----------------------------------+-----------------+           +") #10
+gear_m.append("| 4 |                OFF                |     1008 MHz    |  400 MHz  |") #11
+gear_m.append("+---+-----------------------------------+-----------------+           +") #12
+gear_m.append("| 5 |                OFF                |     1200 MHz    |           |") #13
+gear_m.append("+---+-----------------------------------+-----------------+-----------+") #14
+
+
+gear_idx = []
+gear_idx.append("|*1*| 600 MHz|                  OFF                       |           | <===") #0 => 5
+gear_idx.append("|*2*|     800 MHz     |                OFF                |           | <===") #1 => 7
+gear_idx.append("|*3*|              1008 MHz             |       OFF       |           | <===") #9
+gear_idx.append("|*4*|                OFF                |     1008 MHz    |  400 MHz  | <===") #11
+gear_idx.append("|*5*|                OFF                |     1200 MHz    |           | <===") #13
+
+
+
+cur_stat = []
+cur_stat.append("+-----------------------------------+-----------------+-----------+")
+cur_stat.append("|            Cortex-A53             |   Cortex-A72    | Mali-T860 |")
+cur_stat.append("+--------+--------+--------+--------+--------+--------+-----------+")
+cur_stat.append("| CPU 0  | CPU 1  | CPU 2  | CPU 3  | CPU 4  | CPU 5  |    GPU    |")
+cur_stat.append("+--------+--------+--------+--------+--------+--------+-----------+")
+cur_stat.append("| 600MHz | OFF    | OFF    | OFF    | OFF    | OFF    | 400MHz    |") #5
+cur_stat.append("+--------+--------+--------+--------+--------+--------+-----------+")
+
+
+def isDigit(x):
+    try:
+        float(x)
+        return True
+    except ValueError:
+        return False
+
+
+class A06:
+    cpus = []
+    cpu_scaling_governor= "schedutil"
+    gpu_governor="simple_ondemand"
+    gpu_max_freq=200000000
+    gear       = 1 # 1-5
+    null_out = "2>/dev/null"
+    def __init__(self):
+      self.cpus = []
+      self.init_cpu_infos()
+      self.cpu_total_count = len(self.cpus)
+
+    def init_cpu_infos(self):
+        self.cpus = glob.glob('/sys/devices/system/cpu/cpu[0-9]')
+        self.cpus.sort()
+    
+    def get_cpu_gov(self):
+        if self.gear < 4:
+            cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
+        else:
+            cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy4/scaling_governor"
+        gov = ""
+        with open(cpu_gov_path,"r") as f: gov = f.read().strip()
+        return gov
+
+    def set_cpu_gov0( self,gov):
+        cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
+        try:
+            subprocess.run( "echo %s | sudo tee  %s " %(gov,cpu_gov_path),shell=True,stdout=subprocess.DEVNULL)
+        except:
+            print("set cpu governor failed")
+
+    def set_cpu_gov4( self,gov):
+        cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy4/scaling_governor"
+        try:
+            subprocess.run( "echo %s | sudo tee  %s" %(gov,cpu_gov_path),shell=True,stdout=subprocess.DEVNULL)
+        except:
+            print("set cpu governor failed")
+
+    
+    def get_cpu_on_off(self,cpu_num):
+        cpu_onoff_file = "/sys/devices/system/cpu/cpu%d/online" % cpu_num
+        onoff = "0"
+        max_freq = "0"
+        with open(cpu_onoff_file,"r") as f: onoff = f.read().strip()
+        if onoff == "1":
+            cpu_max_freq_file = "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq" % cpu_num
+            with open(cpu_max_freq_file,"r") as f: max_freq = f.read().strip()
+            mhz = int(max_freq)/1000
+            return "%dMhz" % mhz
+        
+        return "OFF"
+            
+         
+    def set_cpu_on_off(self,cpu_num,onoff):
+        cpu_onoff_file = "/sys/devices/system/cpu/cpu%d/online" % cpu_num
+        try:
+            #print("echo %d | sudo tee  %s" %(onoff,cpu_onoff_file) )
+            subprocess.run( "echo %d | sudo tee  %s" %(onoff,cpu_onoff_file),shell=True,stdout=subprocess.DEVNULL)
+        except:
+            print("set cpu %d on off failed" % cpu_num)
+
+    def set_cpu_max_freq(self,cpu_num,max_freq):
+        cpu_max_freq_file = "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq" % cpu_num
+        try:
+            subprocess.run( "echo %d | sudo tee  %s" %(max_freq,cpu_max_freq_file),shell=True,stdout=subprocess.DEVNULL)
+        except:
+            print("set cpu %d max freq failed" % cpu_num)
+
+    def get_gpu_freq(self):
+        gpu_sys_path = "/sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu"
+        gpu_freq_path = os.path.join(gpu_sys_path,"max_freq")
+        freq = ""
+        with open(gpu_freq_path,"r") as f: freq = f.read().strip()
+        mhz = int(freq)/1000000
+        return "%dMhz" % mhz
+
+    def set_gpu(self,gov,hz):
+        gpu_sys_path = "/sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu"
+        gpu_gov_path = os.path.join(gpu_sys_path,"governor")
+        gpu_freq_path = os.path.join(gpu_sys_path,"max_freq")
+        try:
+            subprocess.run("echo %s | sudo tee %s" %(gov,gpu_gov_path),shell=True,stdout=subprocess.DEVNULL)
+            subprocess.run("echo %d | sudo tee %s" %(hz, gpu_freq_path),shell=True,stdout=subprocess.DEVNULL)
+        except:
+            print("set gpu failed")
+
+
+    def print_cpu_gpu_gov(self):
+        print("CPU Governor: %s    GPU Governor: %s" % (self.get_cpu_gov(),self.gpu_governor))
+    
+    def print_cur_status(self):
+        global  cur_stat
+        
+        stat_str = "|%s|%s|%s|%s|%s|%s|%s|"
+ 
+        cpu0 = self.get_cpu_on_off(0).center(8)[:8]
+        cpu1 = self.get_cpu_on_off(1).center(8)[:8]
+        cpu2 = self.get_cpu_on_off(2).center(8)[:8]
+        cpu3 = self.get_cpu_on_off(3).center(8)[:8]
+        cpu4 = self.get_cpu_on_off(4).center(8)[:8]
+        cpu5 = self.get_cpu_on_off(5).center(8)[:8]
+        gpu  = self.get_gpu_freq().center(11)[:11]
+        
+        table_str = stat_str %(cpu0,cpu1,cpu2,cpu3,cpu4,cpu5,gpu)
+        print("\nCurrent Status:") 
+        for idx,val in enumerate(cur_stat):
+            if idx == 5:
+                print(table_str)
+            else:
+                print(val)
+        
+        self.print_cpu_gpu_gov()
+         
+    def set_gear(self,g):
+        self.gear = g
+        if self.gear == 1:
+            self.set_cpu_on_off(0,1)
+            self.set_cpu_on_off(1,0)
+            self.set_cpu_on_off(2,0)
+            self.set_cpu_on_off(3,0)
+            self.set_cpu_on_off(4,0)        
+            self.set_cpu_on_off(5,0)
+            self.set_cpu_max_freq(0,600000)
+            self.set_gpu(self.gpu_governor,self.gpu_max_freq)
+            self.set_cpu_gov0(self.cpu_scaling_governor)
+        if self.gear == 2:
+            self.set_cpu_on_off(0,1)
+            self.set_cpu_on_off(1,1)
+            self.set_cpu_on_off(2,0)
+            self.set_cpu_on_off(3,0)
+            self.set_cpu_on_off(4,0)        
+            self.set_cpu_on_off(5,0)
+            self.set_cpu_max_freq(0,800000)
+            self.set_cpu_max_freq(1,800000)
+            self.set_gpu(self.gpu_governor,self.gpu_max_freq)
+            self.set_cpu_gov0(self.cpu_scaling_governor)
+        if self.gear == 3:
+            self.set_cpu_on_off(0,1)
+            self.set_cpu_on_off(1,1)
+            self.set_cpu_on_off(2,1)
+            self.set_cpu_on_off(3,1)
+            self.set_cpu_on_off(4,0)
+            self.set_cpu_on_off(5,0)
+            self.set_cpu_max_freq(0,1008000)
+            self.set_cpu_max_freq(1,1008000)
+            self.set_cpu_max_freq(2,1008000)
+            self.set_cpu_max_freq(3,1008000)
+            self.set_gpu(self.gpu_governor,self.gpu_max_freq*2)
+            self.set_cpu_gov0(self.cpu_scaling_governor)
+        if self.gear == 4:
+            self.set_cpu_on_off(4,1)
+            self.set_cpu_on_off(5,1)
+            self.set_cpu_on_off(0,0)
+            self.set_cpu_on_off(1,0)
+            self.set_cpu_on_off(2,0)
+            self.set_cpu_on_off(3,0)
+            self.set_cpu_max_freq(4,600000)
+            self.set_cpu_max_freq(5,600000)
+            self.set_gpu(self.gpu_governor,self.gpu_max_freq*2)
+            self.set_cpu_gov4(self.cpu_scaling_governor)
+        if self.gear == 5:
+            self.set_cpu_on_off(4,1)
+            self.set_cpu_on_off(5,1)
+            self.set_cpu_on_off(0,0)
+            self.set_cpu_on_off(1,0)
+            self.set_cpu_on_off(2,0)
+            self.set_cpu_on_off(3,0)
+            self.set_cpu_max_freq(4,1200000)
+            self.set_cpu_max_freq(5,1200000)
+            self.set_gpu(self.gpu_governor,self.gpu_max_freq*2)
+            self.set_cpu_gov4(self.cpu_scaling_governor)
+
+
+
+def print_gear_map(gear):
+    global gear_m
+    rp = -1
+
+    if gear > 0:
+        rp = 5 + (gear -1)*2
+
+    for idx, val in enumerate(gear_m):
+        if idx == rp:
+            print(gear_idx[gear-1])
+        else:
+            print(val) 
+      
+def print_help_msg():
+    print("Useage: devterm-a06-gearbox [OPTION]...")
+    print("Show or set the CPU operating frequency,online status and GPU operating frequency for DevTerm A06.")
+    print()
+    print(" -s, --set [n] set a speed mode between the number 1-5:")
+    print("               1 for simple writing tasks with long battery life.")
+    print("               2 for browsing most websites with long battery life.")
+    print("               3 for most 2D games and emulators.")
+    print("               4 for playing videos and 3D games.")
+    print("               5 for performance-first tasks.")
+    print()
+    print("Examples:")
+    print("Set to mode 1, single LITTLE core @600MHz(max), GPU@200MHz.")
+    print("   $ devterm-a06-gearbox -s 1")
+
+def is_root():
+    return os.geteuid() == 0
+
+def main(argv):
+    gear   = 1
+    try:
+        opts, args = getopt.getopt(argv,"hs:",["set="])
+    except getopt.GetoptError:
+        print_help_msg()
+        sys.exit(2)
+    for opt, arg in opts:
+        if opt == '-h':
+            print_help_msg()
+            sys.exit()
+        elif opt in ("-s","--set"):
+            if(isDigit(arg)):
+                gear = int(arg)
+                if gear > 5 or gear < 1:
+                    print("illegal input: mode range 1-5")
+                    sys.exit(-1)
+           
+    
+    DT = A06()
+ 
+    if len(argv) == 0:
+        DT.print_cur_status()
+        sys.exit(0)
+
+    DT = A06()
+    if is_root():
+        DT.set_gear(gear)
+        print_gear_map(gear)
+        DT.print_cpu_gpu_gov()
+    else:
+        print("Require super user privilege to set mode,try run it with sudo")
+        sys.exit(1)
+
+if __name__ == "__main__":
+    main(sys.argv[1:])
+