📜 ⬆️ ⬇️

Windows 7: Developing energy-efficient applications

image

None of us likes it when the battery of a mobile phone or computer sits down on time. It's no secret that the new version of Windows 7, which has recently become available for download by MSDN subscribers, allows you to create energy-efficient applications. The article will allow Windows developers to learn new useful methods, techniques and tools that will help increase the operating time of mobile devices.

power usage

image
')
Let's first take a look at the trend of modern computing processors. Researchers at Microsoft Research have analyzed many of the parameters of modern processors (the number of transistors, power, performance, etc.). Special attention was paid to the power consumption of the processors. Below is an overview of Intel processors over the past 20 years.

image

As we see, according to research by Microsoft Research, the amount of energy consumed by processors is growing rapidly. The Intel 80386SX processor consumes almost no power, but it also does not have high performance. If we look at the top processors of recent years with high performance, Core 2 Extreme QX6700 and others, then it becomes obvious how much energy these processors consume (about 100W). This trend is observed not only in modern processors, but also in many other components of modern computers (GPU, Ethernet, etc.), which is becoming a serious problem.

If you study the problem more deeply, the cause of high energy consumption is not only the components (Processor, GPU, Ethernet) and the operating system, but also additional software. The Windows 7 development team compared the battery life of a computer with a clean installation and the pre-installation of OEM software manufacturers.

image

There is a large scatter of values, but in most cases a clean installation allows the computer to run on battery longer. From here we can conclude that the applications we develop have a strong influence on the power consumption of our device. Energy-efficient application will increase the operating time of a mobile device, such as a laptop, and reduce energy costs in the event of a server station.

Developments in the field of energy efficient solutions

Energy consumption is growing rapidly, which causes active development in this direction. One of them is the concept of “Battery for the whole day”, which will allow you to use mobile devices for 6.9 hours or more. Thus, today we are talking about the opportunities that we can use to increase the battery life of our mobile device. Many OEM manufacturers are developing in the field of energy-efficient devices. We can observe trends in optimizing solutions not only for mobile devices, but also for desktop and server stations. Companies want to reduce the power consumption of their offices and data centers. Today I wanted to talk about the methods of developing such energy-efficient applications and about the tools for diagnosing solutions that will help make these applications better.

Energy Efficient Application Development Scheme

1) Understand the causes of high power consumption

2) Reduce the use of resources

3) Pay attention to simple

4) Adapt software for system environment

5) Use the right tools and technology.

6) Pay attention in the application to change the state of the OS (restart, shutdown, sleep, etc.)

7) Check using diagnostic tools

8) Repeat everything anew (back to step 2)

Pay attention to the simple

image

When we talk about the battery life of a mobile device, it is important to pay attention to the type of work. 2 hours watching a DVD? 2 hours of office work or 2 hours of downtime? In each case, the device consumes a different amount of energy. But the important thing is that they are all associated with downtime. In idle mode, the processor consumes a small amount of energy. Therefore, the main task is the greatest reduction in energy consumption in the base layer “Simple”.

image

You can reduce energy consumption by performing some operations less often or by drawing fewer graphics. A good example is the color scheme in Windows Vista. When switching to the “Power saver” mode, Windows changes the color scheme to a simpler (non-transparent) one, allowing the computer to work a little longer. Another example is how the team optimized DVD playback in Windows 7. The playback speed was reduced from 60fps to 30fps. For the user, this change is not noticeable, but imagine how much less our system now consumes.

Another method to reduce energy consumption is code optimization, which allows you to add extra battery life at each stage. All these techniques are a tool to reduce the amount of energy consumed in the mode of the processor.

image

Another trick to reducing energy consumption is the method in which we try to do our work as quickly as possible, thereby falling into idle states for the maximum possible amount of time. Those. we try to be idle, as often as possible and as long as possible. Sometimes it is more profitable to rise to a higher level of energy consumption, complete the task as quickly as possible and return to an idle state. A good example is the processor power manager in Windows, which is very pulsating.

Optimization techniques

image

If we look at modern mobile processors, we see that they have a large dynamic range of power consumption. They consume almost nothing in idle mode; on the other hand, they eat up a large amount of energy with maximum productivity. Therefore, it is very important to focus on the simple!

CPU usage

Exclude polls in your programs, use events! In Vista and newer versions of the OS, there is a special API (RegisterPowerSettingNotification), built on events that allow you to track the power status of your device (See the code below). In the example I am using the Windows API Code Pack for .Net Framework. An example of a working application can be downloaded from the link at the end of the post.

public partial class Window1: Window

{

public Window1 ()

{

Initializecomponent ();

PowerManager.PowerSourceChanged + = new EventHandler (PowerManager_PowerSourceChanged);

}

private void PowerManager_PowerSourceChanged (object sender, EventArgs e)

{

lab1.Content = PowerManager.PowerSource;

}

}

If the survey in your program is still necessary, try to do it as rarely as possible. I recommend that you take advantage of the new Unifying Programming Interface (Coalescing API), which allows you to increase downtime intervals.

When the team developed Windows 7, it noticed that many system processes are periodic. A good example is the processor power controller, which changes the voltage on the processor, thereby adjusting the processing power. It changes the voltage of the processor based on the previous state, and for this it uses periodic polling.

Once again, modern processors consume an incredibly small amount of energy in idle mode. And our main task is to make the processor stay in this state as long as possible. I want to draw your attention to the fact that the processor consumes additional energy to change the mode. Therefore, it is possible that you will spend more energy on switching than save in idle mode. In this regard, we need to be in idle mode, as long as possible.
Windows timer coalescing API

The idea of ​​Coalescing API in combining repetitive activities. The graph below shows an example of activities in the system (unrealistic data). As we can see, the Windows graph is controlled by a periodic timer, which runs every 15.6 ms +. In our system, there are periodic events on a timer, there are many of these events, and they are scattered randomly along the time axis. The new API allows you to group them, providing a state of downtime for the longest possible period of time.

image

You can apply this feature in applications and services using the user-level function SetWaitableTimerEx . However, I recommend using events. If you cannot opt ​​out of periodic timers, the “Windows timer coalescing API” will help you do it most effectively.

The prototype of the SetWaitableTimerEx function:

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

);

The SetWaitableTimerEx function is very similar to the SetWaitableTimer function, which is used to determine the period for which the timer should expire. In many situations, you can simply replace the SetWaitableTimer function with SetWaitableTimerEx . SetWaitableTimerEx has two new parameters: WakeContext and TolerableDelay. The WakeContext parameter is used only if you want to set a timer that can take the system out of sleep. The TolerableDelay parameter defines the permissible deviation from the specified time interval in ms.

You must use a value not less than 32 ms (2 platform timer interrupt intervals) for the TolerableDelay parameter. Optimally, when the tolerance value increases along with the interval value. For example, if the period between interrupts is 1 second, then the appropriate deviation value will be 50 ms. However, if the period length is 30 seconds, the deviation value must be at least 1,000 ms.

SetWaitableTimerEx function requirements table .

HeaderWinbase.h
LibraryKernel32.lib
DllKernel32.dll
Supported OSWindows 7 or Windows Server 2008 R2 or newer


Practice use

You should use the “Windows Timer Coalescing API” to optimize the power consumption of your applications and device drivers. However, activity grouping cannot replace a reasonable use of resources. First you need to try to limit the periodic activity in the application and try to use the event model.

When you use the “Windows Timer Coalescing API”, note:

· The period between interruptions is not guaranteed. However, it is always executed subject to error (TolerableDelay).

· Use the TolerableDelay parameter value of at least 32 ms, which corresponds to two interrupts of the platform timer, which occurs every 15.6 ms.

· Use as a value of the TolerableDelay parameter a multiple of 50, for example 50,100, 250, 500 ms, etc.

[DllImport ("kernel32.dll")]

static extern bool SetWaitableTimerEx (IntPtr hTimer, [In] ref long ft, int lPeriod, TimerCompleteDelegate pfnCompletionRoutine, IntPtr pArgToCompletionRoutine, bool fResume, REASON_CONTEXT wakeContext, ulong tolerableDelay);

An example using this technology can be downloaded at the end of the post.

HDD

image

Modern hard drives consume about 8% of the total energy consumption, and therefore it is very important to pay attention to all disk activity in your system. As we can see on the graph, the most resource-intensive are read and write operations, to achieve which the disk needs to spin up and spend a lot of energy. Use the hard disk only when it is really required, try to exclude all periodic disk activities.

State change

In Windows 7, the development team made a new API that allows them not only to maintain the state of the computer (an example of Media Center, which allows you to play videos for hours without going to sleep; for this, he constantly informs the system that he needs to be in working condition), but and report which application, and why it saves or changes the current state.

/// Create a context

var powerRequestContext = new POWER_REQUEST_CONTEXT ();

powerRequestContext.Version = POWER_REQUEST_CONTEXT_VERSION;

powerRequestContext.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;

powerRequestContext.SimpleReasonString = "App is doing job";

/// Create a query

IntPtr powerRequest = PowerCreateRequest (ref powerRequestContext);

/// Install the request

PowerSetRequest (powerRequest, PowerRequestType.PowerRequestSystemRequired);

/// Perform work

Console.WriteLine ("Press any key");

Console.ReadLine ();

/// Clearing the query

PowerClearRequest (powerRequest, PowerRequestType.PowerRequestSystemRequired);

Diagnostic tools

The Windows 7 development team has added new tools to diagnose your application in the new version of the OS. One such utility is PowerCFG, which you can call from the console and get a small report on the current state of power consumption.

image

Let's now see the resulting HTML.

image

This document provides quick information and recommendations for optimizing the processes in the system.

For more detailed analysis, you should use the Windows Performance Tools Kit (XPerf) , which allows you to get a full range of data on all parameters and processes.

image

Summary

Modern software has a huge impact on energy consumption. Being as often as possible and longer in idle time, we can save a significant amount of energy. I hope this short article will help you better understand the techniques and methods for optimizing your application in terms of energy consumption.

Download examples

Source: https://habr.com/ru/post/68145/


All Articles