Green Coding Techniques in Ultrabook App Development

 

Introduction

Devices and their respective batteries have improved over the years and have extended battery lives now. But as a software engineer I always think if I can write better and energy efficient software. With advent of new devices like Ultrabooks having processors which are capable of moving between states, the developer has the chance to detect the states and make the app go idle or be active and hence reduce energy consumption in a significant way.

Solution

1. User Present : S0

2. Unattended and Active : S0 ISCT

3. Standby : S3 ISCT

The transition between the states is given in the below diagram.

If the app connects to a socket or a database it would hinder the processor from going to sleep. In a sense it means that the app is active though the system might be trying to move to a Standby state.

The fact that the system is moving to a standby state means it is unattended and hence there is no need for the app to be in the active state.

It can relinquish the socket, network or database connections and move to the idle state thereby reducing power consumption. Once the system moves from the standby to unattended and active or user present states the connections can be reestablished.

The way we can do it is by detecting the Smart Connect power states through Windows events and making the app event-driven rather than hard coded flow driven. So the app would not mandate the socket, network or database connection. Rather it would respond to the events and adapt its behavior accordingly. This helps to move the app to idle state as well when the processor moves to idle state thereby reducing power consumption.

The way we can do it is by detecting the Smart Connect power states through Windows events and making the app event-driven rather than hard coded flow driven.

This savings might not be huge but consider if every developer in the world implements this in their code and every device offers these states and notifications we would get significant results. But that is something that only time will confirm. For now we have tried to implement this in our code but we would be testing all these extensively only in 4th week while we profile and test the app for performance and other parameters.

Sample code 1

This is a code we wrote for a Twitter client on Ultrabook. This is an event driven model where the network status will be intimated to the code (announceStatus method) and then the app can take necessary action based on the network status.

varmonitor:URLMonitor = newURLMonitor(new URLRequest('http://www.twitter.com'));


monitor.addEventListener(StatusEvent.STATUS, announceStatus); monitor.start();

public function announceStatus(e:StatusEvent):void {

if(monitor.available)

{

this.connected = true;

//Network available.Fetch tweets or email. refresh();

} else {

this.connected = false;

//Network not available. Close all connections closeConnection();

}

}

Sample code 2

This is a more standard method where we directly monitor the network change event and based on that trigger appropirate actions in the app.

NetworkInfo.networkInfo.addEventListener(Event.NETWORK_CHANGE, onNetworkChange);

public function onNetworkChange(event:Event) {


//Network status has changed. Check the event and take corresponding action.

// If network becomes available enable all connections and activate the app.




// If network becomes unavailable close all connections and make the app idle.

}

Both of the above scenarios are event driven coding and hence the app can be activated or moved to idle state based on the state of the processor. If the processor moves to idle state and the network status is disconnected then the app will be notified and it will also go to the idle state.

These are just a couple of techniques and there are more methods available to change the app state based on processor state. But these will be a good starting point in that learning process.

Ultrabook Developer Take Away.

1. Ultrabook processors have have different states and the idle time states can be used to notify the app to go to idle state when the processor goes idle, thereby saving power.

2. There are many ways of identifying an idle state from directly checking out the native SDK or using the APIs provided in the programming language to check network or resource availability. A twitter client for example cannot function without a network connection and in an idle state of the processor it would not be available. Hence the availability of network connection can be a factor for activating or deactivating an app.

3. The app should be written to be event driven rather than a hard coded flow especially in the power management areas since going back and forth between idle and active states is possible in Ultrabooks.

4. You will not save MegaWatts of power by taking this extra step but it feels good to know that you have done your bit for the environment.

Authors

George and suresh

www.theblueinnovations.com

For more complete information about compiler optimizations, see our Optimization Notice.