
What is energy efficiency as applied to mobile platforms? Simple words is the ability to do more, while spending less energy.
Each user would like as little as possible to charge your mobile device, be it a smartphone, netbook, or ultrabook. Maybe someday there will come a time when the device will need to be charged only once, after its purchase and use until it becomes tired or morally obsolete.
If we consider the enlarged model of any mobile platform, then it consists of 3 main parts.
')
Battery
It is a storage device for mobile power. Battery manufacturers each year try to increase capacity, reduce the time to full charge.
Iron
It is the main direct consumer of energy. Here progress also does not stand still. Manufacturers of "iron" create more and more energy-efficient chips that produce greater performance per watt of energy consumed, add different power modes, allowing you to turn off unused iron, translate into low power modes, thereby saving the battery.
Soft
It is an indirect consumer of energy. Directly software does not consume anything, it forces iron to consume energy. Here, too, has its own methods to extend the life of the battery. I would like to talk about the problem of energy efficiency of software in this article.
How exactly does software affect energy consumption? In a nutshell - it does not allow the gland to "sleep."
Consider one of the largest consumers of energy in the system - the processor.
A processor can control its power consumption with the help of the so-called
C-State . For those who are not familiar with these modes, here is a short reference:
C0 is the operating state of the processor, divided into various P-States.
C1 - a state when the processor does nothing, but is ready to start work, albeit with a slight delay. Many processors have different variations of this state.
C2 is almost the same as C1, but in this state, the processor consumes less energy and has a longer delay for the transition to a working state.
C3 - the state of "sleep", moving into this state, the processor clears the second level cache. It is characterized by lower power consumption and a longer transition time to working condition.
... and so on, depending on the processor.
In order to be more visual I will give an illustration:
The most energy-efficient option - the processor is always asleep. So the most effective program in terms of energy costs is the program that is not running and does not “wake up” it. It does not produce any action, and does not consume anything at all. But no one needs such software, the program should do something useful. A compromise solution is a program that does not do anything then when it does not have to do anything (“wakes up” only because of need), and if it does something, then it does it as quickly as possible.
This is especially true of programs that perform any actions in the background. These programs should always sleep and wake up only when an event occurs.
Events rule or event-driven approach
I will give an example of a “wrong” code (unfortunately, this approach to writing code is used much more often than you think). This code example serves to get data from a socket, for example, in a server application.
while(true) {
What is “wrong” here? There is data or no data, the code will “wake up” the processor every 1000 ms. The behavior of the code resembles an ass from Shrek: “Have you arrived already? Now come? Now come? ”.
The “correct” code, for this task, will not ask anyone, he will fall asleep and will have to wait for him to wake him up. For this, in many operating systems, there are synchronization objects, such as events. With that said, the code should look like this (code is not complete, error handling and return codes are omitted, my task is to simply illustrate the principle):
WSANETWORKEVENTS NetworkEvents; WSAEVENT wsaSocketEvent; wsaSocketEvent = WSACreateEvent(); WSAEventSelect(serverSocket, wsaSocketEvent, FD_READ|FD_CLOSE); while(true) {
What is the beauty of the example above? He will sleep when he has nothing to do.
Timers, alarms of our code
Sometimes you can't do without timers, mass examples - playing audio, video, animation.
A little bit about timers. The Windows system timer interval, by default, is 15.6 ms. What does this mean for programs? Suppose you want the above application to perform some kind of action every 40 ms. The first interval of 15.6 ms passes, too little, the second passes 31.1, again early, the third 46.8 - hit, the timer will work. In most cases, the extra 6.8 ms are irrelevant.
Just a direct effect on
Sleep , if you call Sleep (1), at a set interval of 15.6 ms, then the code will not sleep 1 ms, but 15.6 ms all.
But if it comes to playing a video, then this behavior is not acceptable. In these cases, the developer can change the discreteness of the system timer by calling a function from the Windows Multimedia API - timeBeginPeriod. This function allows you to change the timer period up to 1ms. For code, this is good, but it greatly reduces battery life (up to 25%).
How to find a compromise? All just change the period of the system timer only when it is really necessary. For example, if you are developing an application that uses animation and you need a smaller timer resolution, change the timer when the animation is displayed and occurs, and return if, for example, the window is minimized or the animation is stopped.
From the user's point of view, sometimes
Powercfg will be interesting to understand how to extend battery life. With its help, you can find out some application has changed the period of the system timer, the value of the period of the system timer, information about driver problems that do not allow transferring "iron" in the low power consumption mode, etc.
Combining Timers
In Windows 7, there was a great opportunity to combine timers. What it is and how it works is presented in the figure below:
Those. Windows "adjusts" the application timers so that they coincide with the timer triggers of the operating system itself.
In order to use this feature you must call
BOOL WINAPI SetWaitableTimerEx( __in HANDLE hTimer, __in const LARGE_INTEGER *lpDueTime, __in LONG lPeriod, __in_opt PTIMERAPCROUTINE pfnCompletionRoutine, __in_opt LPVOID lpArgToCompletionRoutine, __in_opt PREASON_CONTEXT WakeContext, __in ULONG TolerableDelay );
You can find the full description of the function in
MSDN . In this article, we are only interested in the TolerableDelay parameter, which defines the maximum allowable disconnect from a given interval.
More information about timers in Windows can be found in the article
Timers, Timer Resolution, and Development of Efficient Code.Do it fast
Another way to make the program more energy efficient is to teach it to do the right things as quickly as possible. This can be achieved, for example, by optimizing the code by using SSE, AVX and other hardware capabilities of the platform. As an example, I would like to use Quick Sync in Sandy Bridge to encode and decode video. On the
Tom's Hardware site you can see the results.
Suppose we optimized our program, but how much more energy efficient is it now, how to evaluate it? Very simple - with the help of special programs and tools.
Energy Efficiency Analysis Tools
1.
Intel Power Checker . Perhaps the easiest and fastest way to assess the energy-efficiency of its program.
Review and description of the program can be found in the blog
ISN2.
Intel Battery Life analyzerA more complex, but at the same time more informative tool, serves to track various activities of hardware and software that affect the battery life.
3. Microsoft's
joulemeterAlso quite an interesting tool that determines the power consumption of the various components of the platform. Can work in conjunction with a
WattsUp wattmeter.
Where to find out more
Intel Power Efficiency Community articles, best practices, and tips for creating energy-efficient software.
Battery Life and Energy Efficiency collection of articles and recipes from Microsoft
Timers, Timer Resolution, and Development of Efficient Code link is already given above, for those who start reading the article from the end.
If you have questions - ask in the comments. You can also ask me about the development of "green" software on the
webinar , which will be held tomorrow, December 15 at 11 am.