I program my home computer, beam myself into the future

At some point, the Linux kernel decided that the maximum CPU usage (derived from “/proc/<PID>/stat”) for my FX-8320E CPU should be 400% instead of 800%. Poppycock, I say! This Piledriver beauty has 8 (integer) cores and Conky divides a process’ CPU usage to the number of cores, ending up with a maximum value of 50% instead of 100%.

So it should be as simple as doubling the “CPU%” value for each ${top cpu <#>} in ~/.conkyrc, right? Unfortunately, Conky’s configuration file lacks support for even the simplest math operation. The only way I could double those value was with a Lua script:

function conky_double(...)
        return string.format('%6.2f', 2 * tonumber(conky_parse('${' .. table.concat({...}, ' ') .. '}')));

This Lua function is called “double” inside the config file and it’s used to replace something like “${top cpu 1}” with “${lua double top cpu 1}”. So the function takes an arbitrary number of parameters  (passed as strings), insert spaces between them, places the result between “${” and “}”, gets conky to parse this as if it were a regular config command, converts the resulting string in a number, doubles it, formats it and returns it.

Now all that’s left to do is tell Conky to load this Lua script, by adding this inside conky.config:

lua_load = '/path/to/conky.lua'

and, of course, put “lua double” in front of those “top cpu” calls so processes can be shown with the right CPU usage percentage.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.