You are not logged in.

#1 2012-07-25 11:30:01

kvashir
Member
Registered: 2012-07-25
Posts: 4

Cpufreq-Helper - Easy Python cpu governors switcher

Hi guys, i finally quitted windows and i was trying to configure my CPU governors under Arch, while surfing the web, i seen a lot of questions about that.

I started learning python 3 days ago and i tried to write a simple program (only shell for now) to easily set cpu governors: Cpufreq-Helper.

As you can see by its name, Cpufreq-Helper, started as root, uses cpufreq (cpufrequtils) to set governors. So, to use it, you need to install cpufrequtils then run it as root.

What is a cpu governor:
Governors can be thought of as pre-configured power schemes for the CPU. Some of the governors must be loaded as kernel modules in order to be seen by user space programs. One may load as many governors as desired (only one will be active on a CPU at any given time).
For Desktops and most systems, the ondemand governor can provide the best compromise between heat emission, power consumption, performance, and manageability. Since it is the default and built into the kernel, loading the CPU frequency driver should be sufficient to activate it. For Laptops or other mobile systems, the conservative governor can possibly provide significant savings in power consumption.
From:
https://wiki.archlinux.org/index.php/CP … cy_Scaling

How it works:
At the moment it is wrote to manage "ondemand, conservative, powersave, *userspace, performance" governors. It will verify which of those have been loaded in your system to let you easily switch through them.

Requirements:
- Python 2.7 (pacman -S python2)
- Cpufrequtils (pacman -S cpufrequtils)
- Root user

I will update this program with a graphical interface soon. I know there are a lot of programs like this but i have not found a good one for xfce so i started to write it by myself. My english is not so good so you can find error in this post and into the program. Please report to me any possible error.

Changelog:

2.1 - Added conservative governor

2.0 - Code changes no more "config" file needed

1.0 - Initial realease

Download:
Cpufreq-helper.py: https://docs.google.com/open?id=0B9VoI3 … WF0X2NtSTA

As Code:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""
	Program:		Cpufreq-Helper 2.1
	Release date:	25/07/2012
	Author:			Antonio Ciccia(Kvashir)
	Author email:	kv4sh1r@gmail.com
	
	License:
	
	This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
"""

import subprocess
import os
import multiprocessing

def is_int(n):
	try:
		int(n)
		return True
	except ValueError:
		return False

os.system('clear')

print("################################################################################")
print("#\n# Cpufreq-Helper 2.1 \t 25/07/2012")
print("#\n# Author:\t\t Antonio Ciccia - Kvashir (kv4sh1r@gmail.com)")
print("#\n# Info:\t\t\t Easily set cpu governor")
print("#\n################################################################################")

if os.geteuid() != 0:
	print("#\n# Warning, non-root user. No changes will be made to the system.")
else:
	print("#\n# Warning, root user. Continuing changes will be made to the system.")

cpuCores=multiprocessing.cpu_count()
		

while True:
	govFile = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")
	governors = govFile.readline().strip("\n")
	govList = governors.split(" ")
	del govList[-1]
	print("#\n# Loaded governors: %s" % governors)
	govNew = raw_input("#\n# Insert new governor: ")
	if govNew in govList:
		if govNew == "ondemand":
			for i in range(int(cpuCores)):
				cpuCmd = "cpufreq-set -c "+str(i)+" -g "+govNew
				if(subprocess.check_call(cpuCmd, shell=True)==0):
					successFlag=1
			break
		if govNew == "conservative":
			for i in range(int(cpuCores)):
				cpuCmd = "cpufreq-set -c "+str(i)+" -g "+govNew
				if(subprocess.check_call(cpuCmd, shell=True)==0):
					successFlag=1
			break	
		elif govNew == "performance":
			for i in range(int(cpuCores)):
				cpuCmd = "cpufreq-set -c "+str(i)+" -g "+govNew
				if(subprocess.check_call(cpuCmd, shell=True)==0):
					successFlag=1
			break
		elif govNew == "powersave":
			for i in range(int(cpuCores)):
				cpuCmd = "cpufreq-set -c "+str(i)+" -g "+govNew
				if(subprocess.check_call(cpuCmd, shell=True)==0):
					successFlag=1
			break
		elif govNew == "userspace":
			freqFile = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies")
			frequencies = freqFile.readline().strip("\n")
			freqFile.close()
			freqList = frequencies.split(" ")
			del freqList[-1]
			for i in range(len(freqList)):
				freqList[i]=int(freqList[i])/1000
			print("#\n# CPU Clocks: %s" % freqList)
			newFreq = raw_input("#\n# Insert new clock [copy from Cpu Clocks]: ")
			for i in range(int(cpuCores)):
				cpuCmd = "cpufreq-set -c "+str(i)+" -f "+newFreq+"Mhz"
				if(subprocess.check_call(cpuCmd, shell=True)==0):
					successFlag=1
			break
	else:
		print("#\n# Warning, wrong value, try again!")

if successFlag == 1:
	print("#\n# New governor set. Type cpufreq-info to verify.")
else:
	print("#\n# New governor not set. Non-root user / There may be a mistake.")
	
print("#\n#############################################################################\n")

Hoping to be useful,

Kvashir

Last edited by kvashir (2012-07-25 12:02:55)

Offline

#2 2012-07-25 11:42:05

SS4
Member
From: !Rochford, Essex
Registered: 2010-12-05
Posts: 699

Re: Cpufreq-Helper - Easy Python cpu governors switcher

It works for me. Any chance you can set it up to support the conservative governor? It's a pre-set mode like powersave and ondemand

[12:36:40] $  cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
conservative ondemand performance 

Rauchen verboten

Offline

#3 2012-07-25 11:52:21

kvashir
Member
Registered: 2012-07-25
Posts: 4

Re: Cpufreq-Helper - Easy Python cpu governors switcher

SS4 wrote:

It works for me. Any chance you can set it up to support the conservative governor? It's a pre-set mode like powersave and ondemand

[12:36:40] $  cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
conservative ondemand performance 

sure, i will smile

done! big_smile

Added to the first post!

Last edited by kvashir (2012-07-25 12:08:58)

Offline

#4 2012-07-25 12:33:01

keenerd
Trusted User (TU)
Registered: 2007-02-22
Posts: 647
Website

Re: Cpufreq-Helper - Easy Python cpu governors switcher

Way too much indentation!

# <snipped>
while True:
	govFile = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")
	governors = govFile.readline().strip("\n")
	govList = governors.split(" ")
	del govList[-1]
	print("#\n# Loaded governors: %s" % governors)
	govNew = raw_input("#\n# Insert new governor: ")
	if govNew not in govList:
		print("#\n# Warning, wrong value, try again!")
		continue
	if govNew == "ondemand":
		for i in range(int(cpuCores)):
			cpuCmd = "cpufreq-set -c "+str(i)+" -g "+govNew
			if(subprocess.check_call(cpuCmd, shell=True)==0):
				successFlag=1
		break
# <snipped>

And an overuse of break and a bunch of other stuff, but big and simple changes first.

Last edited by keenerd (2012-07-25 12:35:37)

Offline

#5 2012-07-25 12:42:54

kvashir
Member
Registered: 2012-07-25
Posts: 4

Re: Cpufreq-Helper - Easy Python cpu governors switcher

keenerd wrote:

Way too much indentation!

# <snipped>
while True:
	govFile = open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")
	governors = govFile.readline().strip("\n")
	govList = governors.split(" ")
	del govList[-1]
	print("#\n# Loaded governors: %s" % governors)
	govNew = raw_input("#\n# Insert new governor: ")
	if govNew not in govList:
		print("#\n# Warning, wrong value, try again!")
		continue
	if govNew == "ondemand":
		for i in range(int(cpuCores)):
			cpuCmd = "cpufreq-set -c "+str(i)+" -g "+govNew
			if(subprocess.check_call(cpuCmd, shell=True)==0):
				successFlag=1
		break
# <snipped>

And an overuse of break and a bunch of other stuff, but big and simple changes first.

Thanks i will look at this smile

Offline

#6 2012-07-25 16:32:07

dodo3773
Member
Registered: 2011-03-17
Posts: 801

Re: Cpufreq-Helper - Easy Python cpu governors switcher

Do you have any plans to support cpupower as well? Or only cpufrequtils. Pretty sure a lot of people switched to cpupower. I remember it being recommended a while back by someone.

Offline

#7 2012-07-25 16:50:23

kvashir
Member
Registered: 2012-07-25
Posts: 4

Re: Cpufreq-Helper - Easy Python cpu governors switcher

dodo3773 wrote:

Do you have any plans to support cpupower as well? Or only cpufrequtils. Pretty sure a lot of people switched to cpupower. I remember it being recommended a while back by someone.

I have never used cpupower, if you explain me how it works i can support it too smile

Offline

#8 2012-07-25 17:27:58

dodo3773
Member
Registered: 2011-03-17
Posts: 801

Re: Cpufreq-Helper - Easy Python cpu governors switcher

kvashir wrote:

I have never used cpupower, if you explain me how it works i can support it too smile

Pretty sure it's just a newer version of the same program with more features (a fork maybe?). The commands are a little different though. Instead of "cpufreq-set" it's "cpupower frequency-set" etc.. But, to answer your question, it works the same way I'm pretty sure (using a cpu scaling governor built into the kernel (or as a module)).

Offline

Board footer

Powered by FluxBB