powerstat: Power Consumption Calculator for Ubuntu Linux


Most users won’t be needing tools like these, but if you’re a bit of a power user, then having the ability to measure the real power consumption rate of your mobile computer (laptop, notebook etc) under Ubuntu Linux can be quite useful.

For instance, let’s say that you recently upgraded your Kernel (a part of your OS that communicates directly with the hardware), and the developers claimed that it will enhance the power consumption, then after the upgrade, how can you know if it has really decreased the power usage or not?

So if you had some sort of a tool that can measure the power consumption of your laptop, then you can use it before and after the upgrade, and then compare the results later and it should do the trick, right?

Now there aren’t that many power consumption monitoring tools in GNU/Linux yet, but in recent times, specially thanks to the upcoming 12.04 Precise Pangolin, Ubuntu developers are putting some efforts for enhancing the battery life. When comparing with Windows, GNU/Linux has not been managing power that efficiently and in the future mobile computers will eventually outrun desktops, so it is crucial for the survival of Ubuntu because and no one won’t bother installing an OS if has a bad reputation while managing power.

Thanks to those efforts, a Kernel developer of Ubuntu, called “Colin Ian” has written few tools that let you monitor the power consumption related things in Ubuntu Linux with ease. One in particular is called “powerstat”, and it is a pretty excellent little tool that calculates the power consumption of your Laptop/Notebook etc when running on your battery.

So if you’re interested in trying to improve your battery life under Ubuntu and searching for a tool to start measuring it to begin with, then powerstat is a must have.

Few main features …

*. Once run, it basically monitors your system for about 10 seconds and it is performed over and over again until 48 samples are collected over 480 seconds. While running, it shows information (fetched from your Kernel’s output) such as:

1. Time -- the startup time of each monitoring instance.

2. User -- CPU usage of processes initiated by the current user.

3. Nice -- a special value (Kernel function) that prioritize the CPU time for applications. Depending on the “importance” of the process, the “nice” value changes, giving more or less CPU time for the process.

So if you have some apps that require a lot of your CPU time while “powerstat” is running, then you’ll see those values under “Nice”.

4. Sys -- CPU usage for the system software, such as CPU time used by your Kernel for instance.

4. Idle -- in simple terms, “idle” values represent the “waiting seconds” of your CPU. In other words, it’s the only time your poor CPU gets to rest (well, other than when you turn off the computer :D).

For instance, if you see a value of 96%, then that simply means, for that period, 96% of the the CPU was at an idle state (low power mode, saving energy consumption by without doing any work). And only 4% of the CPU was consumed by applications.

5. IO  -- (This refers to “IO Wait” rather than I/O itself, I think)

“I/O” stand for the “input/output” protocol, which is the building block of inter device communication.

However an “I/O wait” is a bit more complicated term and many individuals have different opinions about it. So I won’t go into all the details (to be honest, I don’t have a thorough knowledge about it :/).

But as a simple explanation (so anyone can get a simple idea), an “I/O Wait” is the waiting time of your CPU, after sending a signal to a hardware asking to do something. Until the CPU gets a reply from that particular hardware, and if has nothing else to do, then it has to wait till the data arrives for processing.

It’s like sending a letter to someone asking whether they could help you out or not. Until they give you a replay, for that matter, even if you have fulfilled all the other necessities to solve it, you cannot do anything else but wait.

So your friend is acting as a “bottleneck”, which is the same with “I/O waits” (most of the time) because although the CPU has all the muscles it needs to process the heck out of it, until the hardware slowly (compared with the CPU) gathers the data, there’s nothing your CPU can do to improve the process.

Again, this is just a simple example, and in complicated technical terms, it might not be that accurate.

6. Run -- Under this it shows the currently running processes.

7. Ctxt/s (context switch rate) -- Let’s assume that you have a single CPU (no multiple cores) and three programs are trying to run simultaneously (let’s call them “A”, “B” and “C”). Now since it’s a single core CPU, the CPU will have to switch between each program really fast, (I meant super fast! :D) thus it would seem like those three programs were running simultaneously.

But in reality, the CPU executes each program for a short while (partially) and then moves over to the next one (B) run it for a bit,  and then goes to the other one (C), and so on. Then when it comes back again to the first process (A), it has to remember where it “left” it the last time, otherwise it’ll have to execute the program from the beginning and it is a waste of time.

So in order to help the CPU to resume programs (by storing data which contain details such as where it stopped the last time), a special method is used, and it’s called “Context switching”.

So under “Ctxt/s”, it shows how many times the CPU paused and resumed programs per second (again this is in really simple terms).

8. IRQ/s = IRQ per second -- IRQ is a especial signal (I/O technique) that hardware devices use to communicate with your CPU to get the work done. So under this field, it shows the amount of IRQ requests from your hardware.

9. Fork -- A method that a running process uses to create a copy of itself. The value under it’s name just initiates how many processes were forked.

10. Exec -- A function of the Kernel that replaces a currently running process with a new one. Although the process is totally different from the old one, unlike starting a totally new process (from scratch, with a new “id” etc), the newly executed process will have the same “id” and some of the other attributes from the now old, terminated process.

In this instance, in the output if it says “4”, then that means, for that period, 4 processes were replaced with new ones.

11. Exit -- After doing  its job, each process exits or terminates all by itself and notifies the OS by using a special notification called “exit” command. Again, under this value, you’ll see how many processes exited during that time.

12. Watts -- Oh well, that’s pretty apparent :D.

Anyhow, this is the value that we’re quite interested as we can get almost all the above mentioned info by default with somewhat ease. Under this field, it shows the current power consumption rate (energy units  per second). So as you can see from the below screenshot, depending on your system’s load, the power consumption will give different values.

*. After collecting the samples, it’ll display, “Average”, “Minimum” and “Maximum” values under each field. And at the very end, it shows the “Average” power consumption rate plus a “Deviation” value for the whole 480 seconds.

Please remember that, if all you want to know is the average power consumption rate of your PC in the “idle” state (by default powerstat assumes having 98% of a CPU idle value as it being in an”idle” state), then don’t run any apps while “powerstat” is running.

*. Of course, you can run some power intensive programs, if you want to measure the Maximum power consumption of your mobile PC.

Now I know no one really needs all these ramblings. But the reason I thought I’d put a simple description under each field is because, it kinda helps to get a simple idea of what the output says and how it effects the system’s performance (power usage).

If interested, you can install “powerstat” in Ubuntu 12.04 Precise Pangolin, 11.10 Oneiric Ocelot, 11.04 Natty Narwhal, 10.10 and 10.04 by simply using its PPA, again, provided by Colin (thanks mate! :D).

For that, open your Terminal and enter the below commands.

sudo apt-add-repository ppa:colin-king/powermanagement

sudo apt-get update

sudo apt-get install powerstat

Two simple examples …

You can run this program with normal user privileges or as the administrator (which would give you slightly more details in the output). And as a way of letting your PC to drop into a “balanced” state (say the system was a bit busy when you first ran powerstat), “powerstat” waits for about 3 minutes before start monitoring.

But if you don’t want to wait that long, then you can use the “-d” argument to change the execution time. For that, you’ll need to run it using the “sudo” privileges.

For instance, if you want to execute the program as soon as you type its command, without any delays, then you can use something like the below one.

sudo powerstat -d 0

However, as said before, by default it requires a 98% of CPU idle value for accepting that your system is an idle state. But, if your PC usually has a slightly higher CPU idle value (due to some heavy system services for instance), then you can manually bypass the default idle value with the “-i” argument, to get the power usage of your PC’s actual idle value.

So let’s say I want to use 90% as the default CPU idle value in powerstat, then I’d use something like the below command.

sudo powerstat -i 90

It has few other additional commands. For that, please refer to its manual, you can use the below command to start reading it.

man powerstat

Now as said in the beginning, the upcoming Ubuntu 12.04 is undergoing some nice changes (improvements), especially concerning the power consumption related ones. So the developers have set up an excellently written Wiki page where you can find tips & various other ways of improving the battery life (some are hardware specific, for instance, some optimizations are only valid for Intel Sandy Bridge chips etc).

So I humbly advice you to read this Kernel power-management Ubuntu Wiki page as it will come handy for some. And if you’re a developer, then you can acquire the source code by visiting this page as well.

As an ending note, as an experienced Ubuntu Linux user, if you’re looking for an easy to use, powerful tool to measure the power consumption rate of your mobile computer, then “powerstat” is an awesome tool that fills a “gap” (quite elegantly! :D) that had been there for sometime now. A big thanks goes to “Colin King” for creating it.

You might also wanna read Colin’s Blog as he often updates you on the latest power-management related issues/improvements in Ubuntu. Good luck.

10 thoughts on “powerstat: Power Consumption Calculator for Ubuntu Linux

    1. brijesh

      hi colin is there any tool that can actually monitor the power consumption per each process that is actually running in the linux system

  1. Necolas

    Wow, that's an awesome tool!

    by the time i downloaded it, it had a much better GUI interface which is stunning!

    Let the experiments start!

    Nice one!

  2. Danish

    Hi! Colin
    I am setting a headless server. Wanted to measeure the server’s power consumptiona dn hence ran the powerstat program on a laptop only connected to DC without a battery. The program is giving a message saying that since the battery is not discharging power consumption cannot be measured.
    Is there any option or tweak reqired to monitor sch consumption?
    Thanks in advance.

    Dan

  3. A K Saha

    Nice explanation …. Let me know, whether possible to know only the power consumption by CPU rather than the whole system.. Means say Power consumption is 18 watt. Out of this, how much is consumed by only CPU or Microprocessor rather than the whole system ???

    1. Gayan Post author

      You’re welcome. As for the question, no, ‘powerstat’ cannot measure the power usage of different hardware as it simply displays the overall power usage readings, taken directly from the battery.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>