2012年4月18日 星期三

Save power through dynamic frequency

the program split in 2 part:
a program set something and call the script to manage the frequency

/etc/local.d/powerSave.start

#!/bin/bash

#Runtime Power Management
for i in `find /sys/devices/*/power/control`; do echo auto > $i; done;

#USB Autosuspend
for i in `find /sys/bus/usb/devices/*/power/level`; do echo auto > $i; done;
for i in `find /sys/bus/usb/devices/*/power/autosuspend`; do echo 2 > $i; done;

#Use script to mgmt the frequency
cpufreq-set -r -g userspace
echo `date +"%a %b %d %T"` dynamicFrequency start >> /var/log/dynamicFrequency.log
/sbin/dynamicFrequency.py



/etc/local.d/powerSave.stop

#!/bin/bash
kill $(cat /var/run/dynamicFrequency.pid)
cpufreq-set -r -g performance
echo `date +"%a %b %d %T"` dynamicFrequency Stop >> /var/log/dynamicFrequency.log


/sbin/dynamicFrequency.py

#!/usr/bin/python3.2
"""\
This program select a scaling available frequencies based on loadavg
for example the available frequencies:
350000 700000 1050000 1400000 1750000 2100000 2450000 2800000
the current frequencies is 175000
if load >= 80% the frequencies jump 2 step (245000)
if load >= 60% the frequencies jump 1 step (210000)
if load < 20% the frequencies jump -2 step (1050000)
if load < 40% the frequencies jump -1 step (1400000)


"""

import os

# Write the pid into
pid_path = '/var/run/dynamicFrequency.pid'

pidFile=None
if os.path.exists(pid_path):
pidFile = open(pid_path, 'w')
else:
pidFile = open(pid_path, 'a')

pidFile.write(str(os.getpid()))
pidFile.close()

scaling_available_frequencies = None
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies', 'r') as file:
scaling_available_frequencies = file.read().strip().split()

current_frequencies = None
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq', 'r') as file:
raw_current_frequencies = file.read().strip()
if raw_current_frequencies in scaling_available_frequencies:
current_frequencies = scaling_available_frequencies.index(raw_current_frequencies)
else:
current_frequencies = len(scaling_available_frequencies)

from decimal import *
from time import sleep

max = len(scaling_available_frequencies) - 1

while True:
loadavg = None
with open('/proc/loadavg', 'r') as file:
loadavg = Decimal(file.read().strip()[0])

change = 0
if loadavg >= Decimal(0.80):
change = 2
elif loadavg >= Decimal(0.60):
change = 1
elif loadavg < Decimal(0.20):
change = -2
elif loadavg < Decimal(0.40):
change = -1

plan=None
if current_frequencies + change >= max:
plan = max
elif current_frequencies + change < 0:
plan = 0
else:
plan = current_frequencies + change

if plan != current_frequencies:
current_frequencies = plan
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed', 'w') as file:
file.write(scaling_available_frequencies[current_frequencies])
os.system('echo `date +"%a %b %d %T"` dynamicFrequency' + ' loadavg:' + str(loadavg) + ' change frequency to:' + \
scaling_available_frequencies[plan] + ' >> /var/log/dynamicFrequency.log')

sleep(5)

Reference:

沒有留言:

張貼留言