Guest

blogsportgruppe

By: a guest on Mar 22nd, 2009  |  syntax: Python  |  size: 1.16 KB  |  hits: 795  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. def _get_file_content(file_path):
  2.     try:
  3.         f = open(file_path, 'r')
  4.         content = f.read()
  5.         f.close()
  6.         return content
  7.     except:
  8.         pass
  9.  
  10. def get_current_freq():
  11.     content = _get_file_content('/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq')
  12.     return int(content)
  13.  
  14. def get_min_freq():
  15.     content = _get_file_content('/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq')
  16.     return int(content)
  17.        
  18. def get_max_freq():
  19.     content = _get_file_content('/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq')
  20.     return int(content)
  21.    
  22. def get_lastminutes_loadavg():
  23.     content = _get_file_content('/proc/loadavg')
  24.     return float(content.split()[0])
  25.    
  26. def set_freq(freq):
  27.     import subprocess
  28.     p = subprocess.Popen(['cpufreq-set', '-f', '%d' % freq])
  29.     p.wait()
  30.  
  31. load_avg = get_lastminutes_loadavg()
  32. cur_freq = get_current_freq()
  33. min_freq = get_min_freq()
  34. max_freq = get_max_freq()
  35. min_max_ratio = float(min_freq) / float(max_freq)
  36.  
  37. if cur_freq == min_freq and load_avg > 1:
  38.     set_freq(max_freq)
  39.        
  40. if cur_freq == max_freq and load_avg < min_max_ratio:
  41.     set_freq(min_freq)