Wednesday, March 18, 2009

Can developers write green code?



Can developers write green code?


As a developer for years I believed that my profession is very environment friendly.
1> Except the code, I produce no other garbage.
2> I don't produce noise, except the sound of keystrokes
3> Smoke, ha ha ha... If I produce slightest of smoke the fire alarm will force everyone to go out of building
4> I am surrounded by group of people who are very much aware of environmental issues. I keep getting forwards of best practices to be followed to save water. I am reminded to turn off my monitor and cubicle lights while leaving the office by corporate heads etc…

But then some revelation changed my thinking. The hardware is made of materials that are not so environment friendly that one can ignore it. I don’t have the exact figures as how many tons of crap IT produces every year(citation needed), but I am sure it is enough to make me believe that I do produce garbage other than code ;)

The amount of data exchange happening on internet is so much that its certainly raises some concerns, though it may be unavoidable at this point of time.

The amount of energy consumed by the software development organizations and their data centers is some times more than the energy consumption of some countries. Even if I am not emitting any smoke in my cubicle, but somewhere a thermal power station may be doing it.

But these are some of the unavoidable stuff. I have to accept them as part of the business I do, i.e. writing code. I can’t do anything to make a difference….. Or can I?

I was amazed to discover how much R&D the hardware and processor companies have put in to go green. As I start learning about the hardware innovation from environment point of view, I find the features like Wake-on-LAN, battery power management features of laptop, the power modes, the power management features given by BIOS, are very impressive. The picture of computers have changed from that of a big machines that occupied half of your room and consumed tons of power to smart devices that can fit in your pockets.

All hardware are backed up by some software, so it goes without saying that OS and software development companies have put a matching effort to preserve the color of our planet. Below are some of the states that operating system can go in to save power:
· S1 - Stand By (Power On Suspend) - no system context is lost
· S2 - Stand By - CPU and system cache context is lost
· S3 - Stand By (Suspend to Ram) - system memory context is maintained, all other system context is lost
· S4 - Hibernate - Platform context is maintained.
· S5: Soft Off-- Some components remain powered so the computer can "wake" from input from the keyboard, LAN, or USB device.
All this is fine, but I have still not got the answer to the question, what a developer can do to conserve environment?

The research done by the hardware and software development companies have helped create a set of guidelines and best practices which developers can follow to contribute their two cents to environments. Below section focuses on this.
Energy-Efficient Software Guideline (http://software.intel.com/en-us/articles/energy-efficient-software-guidelines/)
1. Computation Efficiencies
· Make efficient use of multi-threading. On a multi core machine, the work will be finished faster, resulting in idling of CPU and saving energy. Try to balance workload on threads as imbalance workload may lead to inefficiencies)
· Minimize the use of tight loops
· Application energy efficiency can be improved by consuming libraries that are optimized for performance. The libraries could be doing work related to audio, video, cryptography, speech recognition and signal processing.
· User efficient algorithms, for example, a recursive algorithm may be inefficient as it may add overhead by using more stack space as compared to non recursive algorithm.
· Make use of compiler optimizations
· Programming language: Some programming language like C cause less wakeups as compared to other high level run-time language.
2. Data Efficiencies
· Use buffering/caching for transferring data to/from hard disc, optical disc, etc…
· The location of data should be as close as possible to the processing entity. For example if data is stored in cache then it will be efficient than if it is stored in RAM or hard disk.


3. Context/Power aware behavior: Applications can subscribe to sleep/hibernate/wake-up power transitions events. Applications can handle these events in a way that the application doesn’t need a restart, loss of data or state. Following can be considered by application to do sleep transitions
· Save state prior to sleep transition and restoring it on wake up
· Release all resources such as file, connections before sleeping
· Disconnect all communication link before sleeping and reconnecting on wake up.
· Stop any ongoing activities like streaming video etc.
· Save states on battery low or AC events

There are many more guidelines that one can find on internet. Some of them are mentioned below:
http://software.intel.com/en-us/articles/energy-efficient-software-guidelines/
http://software.intel.com/en-us/articles/the-art-of-graceful-application-suspension/
http://software.intel.com/en-us/articles/power-analysis-of-disk-io-methodologies/
http://software.intel.com/en-us/articles/intel-performance-tuning-utility-31-update-3

Some guidelines from “The Microsoft Architecture Journal” is given below
Application Patterns for Green IT
Profiling Energy Usage for Efficient Consumption
Green IT in Practice: SQL Server Consolidation in Microsoft IT

Conclusion
There hardware and platforms have evolved to save power and environment. There are small but significant way in which the way application writters can contribute to the purpose. Developers can define best practices and infact write Green Code.