This wiki will shut down!

Please note that this wiki will be made read-only and eventually be taken offline.

A replacement is being discussed at We'd be happy to have more input for that discussion and volunteers who help us migrate the content of this wiki to its replacement.

CPU Governor/Freq. Widget for Linux (Pure Lua)

From awesome
(Redirected from Linux CPU Governor Widget)
Jump to: navigation, search

by Fredrik Ax <mailto:[email protected]>

This widget is written entirely in LUA for awesome 3.2.

It depends on sudo, cpufreq-set and Linux CPU_FREQ_GOV and SYSFS (/sys/devices/system/cpu/cpu0/cpufreq/scaling_*).

It provides a textbox widget that shows your current governor and what frequence your cpu currently is using.

When left-clicked it opens a menu that allow you to select another governor (if you have the appropriate line in sudoers). Right-clicked it simply updates it self.


sudo and cpufreq-set must be in your path e.g. in Debian, as root do:

apt-get install sudo cpufrequtils

You must be allowed to run the command "sudo cpufreq-set ..." passwordless, e.g. by adding the following line to your /etc/sudoers:

username	ALL=(ALL) NOPASSWD:/usr/bin/cpufreq-set

where username is ... guess what ... your user name.

Code to be inserted into your rc.lua[edit]

The widget must be defined before it is added to the wibox where it is going to be used. So using the mywibox as defined in the default rc.lua the code should be inserted before the loop (for s = 1, screen.count() do) containing the statement starting with:

mywibox[s].widgets = {

This is the code to be inserted:

-- Create fraxcpumenu, and add all available governors to it
fraxcpumenu = {}
local fh="/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", "r")
if fh ~= nil then 
   govstr= fh:read()
   local i= 1
   for w in string.gmatch(govstr, "%a+") do
      fraxcpumenu[i]= { w, "sudo cpufreq-set  --governor "..w}
      i= i + 1
fraxcpumenu = { items= fraxcpumenu }  )

-- Create fraxcpu widget
fraxcpu=  widget({ type = "textbox", name = "fraxcpu", align = "right" })
fraxcpu.text= 'fraxcpu'

-- Function for updating the fraxcpu widget
function hook_fraxcpu (tbw)
   if not fraxcpuupd then return(nil) end
   local freq= 
   local gov= 
   local fh="/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", "r")
   if fh then
      gov= fh:read()..':'
   fh="/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r")
   if fh then
      freq= fh:read()
      freq= tostring(math.ceil(tonumber(freq)/1000))
      fraxcpuupd= nil
      fh="/proc/cpuinfo", "r")
      if fh then
	 for l in fh:lines() do
	    freq= string.match(l, '^%s*cpu MHz%s*:%s*([0-9]+)')
	    if freq ~= nil then break end
   tbw.text= gov..freq

-- Mouse button bindings for fraxcpu widget
fraxcpu.buttons(fraxcpu,{ button({ }, 1, function () end),
			  button({ }, 2, function () hook_fraxcpu(fraxcpu) end),
			  button({ }, 3, function () hook_fraxcpu(fraxcpu) end) })

Don't forget to make the widget visible, e.g. by adding it to the mywibox as defined by the default rc.lua, look for the line starting with

mywibox[s].widgets = {

and insert "fraxcpu," into the list of widgets.

Finally you need to register a timer that keeps the widget updated. For it to be informational in environments where the freq changes dynamically (e.g. when using the ondemand governor) it needs to be updated often.

To update it every second add the following at the end of your rc.lua:

-- Update the fraxcpu widget every second
awful.hooks.timer.register(1, function ()