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 operating system 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, especially thanks to the upcoming 12.04 Precise Pangolin, Ubuntu developers are putting some effort into enhancing the battery life. When compared 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.

powerstat-running-in-Ubuntu-11.10

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 of powerstat

*. 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”.

individual-fields

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 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 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 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 wwere 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 that 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 special 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 its 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 the “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 in 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.

calculated-watts

*. 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.

summary

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 is 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 that 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

Simple Examples of Power Consumption Calculator for Ubuntu Linux

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 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

running-without-a-delay

However, as said before, by default it requires a 98% of CPU idle value for accepting that your system is in 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 advise you to read this Kernel power-management Ubuntu Wiki page as it will come in 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 some time now. A big thanks go 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.

An RHCE, 'Linux' user with 14+ years of experience. Extreme lover of Linux and FOSS. He is passionate to test every Linux distribution & compare with the previous release to write in-depth articles to help the FOSS community.

28 thoughts on “powerstat: Power Consumption Calculator for Ubuntu Linux”

  1. 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!

    Reply
  2. 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

    Reply
  3. 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 ???

    Reply
    • 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.

      Reply
    • Yes, it’s possible. For that you can use the below command:

      powerstat > power_usage_output.txt

      If you prefer ‘powerstat’ to start immediately, then it with the ‘-d’ option:

      powerstat -d 0 > power_usage_output.txt

      When it’s done running, you should be able to access the output through the ‘power_usage_output.txt’ file.

      Reply
  4. Thanks,but is it possibel to just see watts and save average watts in some variable so that i can access that variable in some other code to take some decision ?
    can you please tell me is there any documnet on powerstat which can help me see other things that can do with powerstat

    regards

    Reply
    • I don’t know if that’s possible. You can try asking in AskUbuntu website for that. However, if all you want is to print the average power consumption, then you can use the below command:

      powerstat -d0 > power_usage_output.txt && grep 'Watts on Average' < power_usage_output.txt > average_power_consumption.txt

      This will save the average power consumption on a file called ‘average_power_consumption.txt’ on your ‘Home’ folder. You can play with the ‘-d0’ or can remove it completely if you prefer as well. For learning more about ‘powerstat, you can refer to its manual using the below command.

      man powerstat

      Reply
  5. Hey,
    Powerstat take too much time in calculating average power. Is it possible to define less sample for its calculation so that time require to get power may get decrease

    Reply
  6. hello there…
    I am using Ubuntu 14.04 LTS and tried to install powerstat. The installation was successful but when i run “sudo powerstat -d 2”, it shows an error message as follows:
    “Machine is not discharging, cannot measure power usage.”
    Please note that my laptop was not on charging. If you could please help me with this, it will be very great…

    Reply
    • Hi Kartik,

      To measure power, your laptop should be running using the battery power. If this was indeed the case and didn’t work, then try rebooting the laptop…

      Reply
  7. hi
    I am using Ubuntu 12.04 on vmware and I install powerstat, when i want to use that it shows an error message as follows:
    “Machine is not discharging, cannot measure power usage.”
    can you help me please?

    Reply
    • Hi narges,

      First of all, ‘powerstat’ requires the presence of a battery. And secondly, make sure the virtual-machine has access to it (is the battery icon is shown in the notification area?). If they’re both true, then it could be something else…

      Reply
    • Thank you Colin. Since I got a chance, let me just say that my OS reviews wouldn’t have never felt ‘finished’ without this tool!, I love it.

      Oh and forgive me about the delay, my father got hospitalized a couple of days ago (he’s alright now, 🙂 ) so I couldn’t manage things out here on ‘HecticGeek’. I’ll come up with a separate article featuring the new features. Thanks again.

      Reply

Leave a Comment

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