devterm-a06-gearbox 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/usr/bin/python3
  2. import glob
  3. import os
  4. import sys,getopt
  5. import subprocess
  6. import time
  7. gear_m = []
  8. gear_m.append(" +-----------------------------------+-----------------+-----------+") #0
  9. gear_m.append(" | Cortex-A53 | Cortex-A72 | Mali-T860 |") #1
  10. gear_m.append(" +--------+--------+--------+--------+--------+--------+-----------+") #2
  11. gear_m.append(" | CPU 0 | CPU 1 | CPU 2 | CPU 3 | CPU 4 | CPU 5 | GPU |") #3
  12. gear_m.append("+---+--------+--------+--------+--------+--------+--------+-----------+") #4
  13. gear_m.append("| 1 | 600 MHz| OFF | |") #5
  14. gear_m.append("+---+--------+--------+-----------------------------------+ 200 MHz +") #6
  15. gear_m.append("| 2 | 800 MHz | OFF | |") #7
  16. gear_m.append("+---+-----------------+-----------------+-----------------+-----------+") #8
  17. gear_m.append("| 3 | 1008 MHz | OFF | |") #9
  18. gear_m.append("+---+-----------------------------------+-----------------+ +") #10
  19. gear_m.append("| 4 | OFF | 1008 MHz | 400 MHz |") #11
  20. gear_m.append("+---+-----------------------------------+-----------------+ +") #12
  21. gear_m.append("| 5 | OFF | 1200 MHz | |") #13
  22. gear_m.append("+---+-----------------------------------+-----------------+-----------+") #14
  23. gear_idx = []
  24. gear_idx.append("|*1*| 600 MHz| OFF | | <===") #0 => 5
  25. gear_idx.append("|*2*| 800 MHz | OFF | | <===") #1 => 7
  26. gear_idx.append("|*3*| 1008 MHz | OFF | | <===") #9
  27. gear_idx.append("|*4*| OFF | 1008 MHz | 400 MHz | <===") #11
  28. gear_idx.append("|*5*| OFF | 1200 MHz | | <===") #13
  29. cur_stat = []
  30. cur_stat.append("+-----------------------------------+-----------------+-----------+")
  31. cur_stat.append("| Cortex-A53 | Cortex-A72 | Mali-T860 |")
  32. cur_stat.append("+--------+--------+--------+--------+--------+--------+-----------+")
  33. cur_stat.append("| CPU 0 | CPU 1 | CPU 2 | CPU 3 | CPU 4 | CPU 5 | GPU |")
  34. cur_stat.append("+--------+--------+--------+--------+--------+--------+-----------+")
  35. cur_stat.append("| 600MHz | OFF | OFF | OFF | OFF | OFF | 400MHz |") #5
  36. cur_stat.append("+--------+--------+--------+--------+--------+--------+-----------+")
  37. def isDigit(x):
  38. try:
  39. float(x)
  40. return True
  41. except ValueError:
  42. return False
  43. class A06:
  44. cpus = []
  45. cpu_scaling_governor= "schedutil"
  46. gpu_governor="simple_ondemand"
  47. gpu_max_freq=200000000
  48. gear = 1 # 1-5
  49. null_out = "2>/dev/null"
  50. def __init__(self):
  51. self.cpus = []
  52. self.init_cpu_infos()
  53. self.cpu_total_count = len(self.cpus)
  54. def init_cpu_infos(self):
  55. self.cpus = glob.glob('/sys/devices/system/cpu/cpu[0-9]')
  56. self.cpus.sort()
  57. def get_cpu_gov(self):
  58. if self.gear < 4:
  59. cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
  60. else:
  61. cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy4/scaling_governor"
  62. gov = ""
  63. with open(cpu_gov_path,"r") as f: gov = f.read().strip()
  64. return gov
  65. def set_cpu_gov0( self,gov):
  66. cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
  67. try:
  68. subprocess.run( "echo %s | sudo tee %s " %(gov,cpu_gov_path),shell=True,stdout=subprocess.DEVNULL)
  69. except:
  70. print("set cpu governor failed")
  71. def set_cpu_gov4( self,gov):
  72. cpu_gov_path = "/sys/devices/system/cpu/cpufreq/policy4/scaling_governor"
  73. try:
  74. subprocess.run( "echo %s | sudo tee %s" %(gov,cpu_gov_path),shell=True,stdout=subprocess.DEVNULL)
  75. except:
  76. print("set cpu governor failed")
  77. def get_cpu_on_off(self,cpu_num):
  78. cpu_onoff_file = "/sys/devices/system/cpu/cpu%d/online" % cpu_num
  79. onoff = "0"
  80. max_freq = "0"
  81. with open(cpu_onoff_file,"r") as f: onoff = f.read().strip()
  82. if onoff == "1":
  83. cpu_max_freq_file = "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq" % cpu_num
  84. with open(cpu_max_freq_file,"r") as f: max_freq = f.read().strip()
  85. mhz = int(max_freq)/1000
  86. return "%dMhz" % mhz
  87. return "OFF"
  88. def set_cpu_on_off(self,cpu_num,onoff):
  89. cpu_onoff_file = "/sys/devices/system/cpu/cpu%d/online" % cpu_num
  90. try:
  91. #print("echo %d | sudo tee %s" %(onoff,cpu_onoff_file) )
  92. subprocess.run( "echo %d | sudo tee %s" %(onoff,cpu_onoff_file),shell=True,stdout=subprocess.DEVNULL)
  93. except:
  94. print("set cpu %d on off failed" % cpu_num)
  95. def set_cpu_max_freq(self,cpu_num,max_freq):
  96. cpu_max_freq_file = "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq" % cpu_num
  97. try:
  98. subprocess.run( "echo %d | sudo tee %s" %(max_freq,cpu_max_freq_file),shell=True,stdout=subprocess.DEVNULL)
  99. except:
  100. print("set cpu %d max freq failed" % cpu_num)
  101. def get_gpu_freq(self):
  102. gpu_sys_path = "/sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu"
  103. gpu_freq_path = os.path.join(gpu_sys_path,"max_freq")
  104. freq = ""
  105. with open(gpu_freq_path,"r") as f: freq = f.read().strip()
  106. mhz = int(freq)/1000000
  107. return "%dMhz" % mhz
  108. def set_gpu(self,gov,hz):
  109. gpu_sys_path = "/sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu"
  110. gpu_gov_path = os.path.join(gpu_sys_path,"governor")
  111. gpu_freq_path = os.path.join(gpu_sys_path,"max_freq")
  112. try:
  113. subprocess.run("echo %s | sudo tee %s" %(gov,gpu_gov_path),shell=True,stdout=subprocess.DEVNULL)
  114. subprocess.run("echo %d | sudo tee %s" %(hz, gpu_freq_path),shell=True,stdout=subprocess.DEVNULL)
  115. except:
  116. print("set gpu failed")
  117. def print_cpu_gpu_gov(self):
  118. print("CPU Governor: %s GPU Governor: %s" % (self.get_cpu_gov(),self.gpu_governor))
  119. def print_cur_status(self):
  120. global cur_stat
  121. stat_str = "|%s|%s|%s|%s|%s|%s|%s|"
  122. cpu0 = self.get_cpu_on_off(0).center(8)[:8]
  123. cpu1 = self.get_cpu_on_off(1).center(8)[:8]
  124. cpu2 = self.get_cpu_on_off(2).center(8)[:8]
  125. cpu3 = self.get_cpu_on_off(3).center(8)[:8]
  126. cpu4 = self.get_cpu_on_off(4).center(8)[:8]
  127. cpu5 = self.get_cpu_on_off(5).center(8)[:8]
  128. gpu = self.get_gpu_freq().center(11)[:11]
  129. table_str = stat_str %(cpu0,cpu1,cpu2,cpu3,cpu4,cpu5,gpu)
  130. print("\nCurrent Status:")
  131. for idx,val in enumerate(cur_stat):
  132. if idx == 5:
  133. print(table_str)
  134. else:
  135. print(val)
  136. self.print_cpu_gpu_gov()
  137. def set_gear(self,g):
  138. self.gear = g
  139. if self.gear == 1:
  140. self.set_cpu_on_off(0,1)
  141. self.set_cpu_on_off(1,0)
  142. self.set_cpu_on_off(2,0)
  143. self.set_cpu_on_off(3,0)
  144. self.set_cpu_on_off(4,0)
  145. self.set_cpu_on_off(5,0)
  146. self.set_cpu_max_freq(0,600000)
  147. self.set_gpu(self.gpu_governor,self.gpu_max_freq)
  148. self.set_cpu_gov0(self.cpu_scaling_governor)
  149. if self.gear == 2:
  150. self.set_cpu_on_off(0,1)
  151. self.set_cpu_on_off(1,1)
  152. self.set_cpu_on_off(2,0)
  153. self.set_cpu_on_off(3,0)
  154. self.set_cpu_on_off(4,0)
  155. self.set_cpu_on_off(5,0)
  156. self.set_cpu_max_freq(0,800000)
  157. self.set_cpu_max_freq(1,800000)
  158. self.set_gpu(self.gpu_governor,self.gpu_max_freq)
  159. self.set_cpu_gov0(self.cpu_scaling_governor)
  160. if self.gear == 3:
  161. self.set_cpu_on_off(0,1)
  162. self.set_cpu_on_off(1,1)
  163. self.set_cpu_on_off(2,1)
  164. self.set_cpu_on_off(3,1)
  165. self.set_cpu_on_off(4,0)
  166. self.set_cpu_on_off(5,0)
  167. self.set_cpu_max_freq(0,1008000)
  168. self.set_cpu_max_freq(1,1008000)
  169. self.set_cpu_max_freq(2,1008000)
  170. self.set_cpu_max_freq(3,1008000)
  171. self.set_gpu(self.gpu_governor,self.gpu_max_freq*2)
  172. self.set_cpu_gov0(self.cpu_scaling_governor)
  173. if self.gear == 4:
  174. self.set_cpu_on_off(4,1)
  175. self.set_cpu_on_off(5,1)
  176. self.set_cpu_on_off(0,0)
  177. self.set_cpu_on_off(1,0)
  178. self.set_cpu_on_off(2,0)
  179. self.set_cpu_on_off(3,0)
  180. self.set_cpu_max_freq(4,600000)
  181. self.set_cpu_max_freq(5,600000)
  182. self.set_gpu(self.gpu_governor,self.gpu_max_freq*2)
  183. self.set_cpu_gov4(self.cpu_scaling_governor)
  184. if self.gear == 5:
  185. self.set_cpu_on_off(4,1)
  186. self.set_cpu_on_off(5,1)
  187. self.set_cpu_on_off(0,0)
  188. self.set_cpu_on_off(1,0)
  189. self.set_cpu_on_off(2,0)
  190. self.set_cpu_on_off(3,0)
  191. self.set_cpu_max_freq(4,1200000)
  192. self.set_cpu_max_freq(5,1200000)
  193. self.set_gpu(self.gpu_governor,self.gpu_max_freq*2)
  194. self.set_cpu_gov4(self.cpu_scaling_governor)
  195. def print_gear_map(gear):
  196. global gear_m
  197. rp = -1
  198. if gear > 0:
  199. rp = 5 + (gear -1)*2
  200. for idx, val in enumerate(gear_m):
  201. if idx == rp:
  202. print(gear_idx[gear-1])
  203. else:
  204. print(val)
  205. def print_help_msg():
  206. print("Useage: devterm-a06-gearbox [OPTION]...")
  207. print("Show or set the CPU operating frequency,online status and GPU operating frequency for DevTerm A06.")
  208. print()
  209. print(" -s, --set [n] set a speed mode between the number 1-5:")
  210. print(" 1 for simple writing tasks with long battery life.")
  211. print(" 2 for browsing most websites with long battery life.")
  212. print(" 3 for most 2D games and emulators.")
  213. print(" 4 for playing videos and 3D games.")
  214. print(" 5 for performance-first tasks.")
  215. print()
  216. print("Examples:")
  217. print("Set to mode 1, single LITTLE core @600MHz(max), GPU@200MHz.")
  218. print(" $ devterm-a06-gearbox -s 1")
  219. def is_root():
  220. return os.geteuid() == 0
  221. def main(argv):
  222. gear = 1
  223. try:
  224. opts, args = getopt.getopt(argv,"hs:",["set="])
  225. except getopt.GetoptError:
  226. print_help_msg()
  227. sys.exit(2)
  228. for opt, arg in opts:
  229. if opt == '-h':
  230. print_help_msg()
  231. sys.exit()
  232. elif opt in ("-s","--set"):
  233. if(isDigit(arg)):
  234. gear = int(arg)
  235. if gear > 5 or gear < 1:
  236. print("illegal input: mode range 1-5")
  237. sys.exit(-1)
  238. DT = A06()
  239. if len(argv) == 0:
  240. DT.print_cur_status()
  241. sys.exit(0)
  242. DT = A06()
  243. if is_root():
  244. DT.set_gear(gear)
  245. print_gear_map(gear)
  246. DT.print_cpu_gpu_gov()
  247. else:
  248. print("Require super user privilege to set mode,try run it with sudo")
  249. sys.exit(1)
  250. if __name__ == "__main__":
  251. main(sys.argv[1:])