Optimization In Mobile Systems

1,075 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,075
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Optimization In Mobile Systems

  1. 1. Optimization & Performance Tuning in Mobile Systems, Advanced Level Raghu Sesha Iyengar
  2. 2. Agenda <ul><li>Optimization </li></ul><ul><ul><li>What is optimization </li></ul></ul><ul><ul><li>Why optimize </li></ul></ul><ul><ul><li>When to optimize </li></ul></ul><ul><li>Performance parameters </li></ul><ul><li>Profilers </li></ul><ul><li>Benchmark </li></ul><ul><li>References </li></ul>
  3. 3. What is Optimization <ul><li>“ The design and operation of a system or process to make it as good as possible in some defined sense”. -- Wiktionary </li></ul><ul><li>“ an act, process, or methodology of making something (as a design, system, or decision) as fully perfect, functional, or effective as possible ; specifically : the mathematical procedures (as finding the maximum of a function) involved in this” – Merriam-Webster’s Online dictionary </li></ul><ul><li>Finding “ best ” route to Orchid – our definition </li></ul><ul><li>For software optimization, </li></ul><ul><li>“ as good as possible” generally means achieving a preset benchmark </li></ul><ul><li>“ defined” generally means parameters used to measure how close we are to the most optimized state </li></ul>
  4. 4. What is Optimization Optimization applies to all aspects of mobile system Typical software architecture of a mobile phone (Android as an example) Source:http://developer.android.com Processor RAM Peripherals HARDWARE
  5. 5. Why to optimize <ul><li>Major considerations of mobile phone software are: </li></ul><ul><li>Portability </li></ul><ul><ul><li>Multitude of hardware choices </li></ul></ul><ul><ul><li>API, Protocols and Specifications </li></ul></ul><ul><li>User experience </li></ul><ul><ul><li>Usability </li></ul></ul><ul><ul><li>Performance (speed, memory, responsiveness) </li></ul></ul><ul><ul><li>Look and feel </li></ul></ul><ul><li>Functionality </li></ul><ul><ul><li>Phone or Supercomputer </li></ul></ul><ul><ul><li>Reliability </li></ul></ul><ul><li>Current drain </li></ul><ul><ul><li>Battery life </li></ul></ul><ul><ul><li>Battery technology not catching up with growth in processor power </li></ul></ul><ul><li>Security </li></ul><ul><li>Software development time </li></ul><ul><ul><li>Open Source </li></ul></ul><ul><li>Create differentiator software </li></ul>
  6. 6. Pitfalls of optimization <ul><li>Portability </li></ul><ul><li>Repetitive process </li></ul><ul><ul><li>Optimization is rarely a one-time process </li></ul></ul><ul><ul><li>Change in hardware platform or Software framework can cause optimization cycle to start </li></ul></ul><ul><li>Increasing processor power </li></ul><ul><ul><li>Faster processors </li></ul></ul><ul><ul><li>Coprocessors </li></ul></ul><ul><ul><li>Hardware Accelerators </li></ul></ul><ul><li>Unplanned time and effort </li></ul><ul><li>Set your target </li></ul><ul><ul><li>What is the optimal time to calculate the next move in a chess game? </li></ul></ul><ul><li>Open source or 3rd party code </li></ul><ul><ul><li>A new drop may invalidate all your effort </li></ul></ul>
  7. 7. When to optimize <ul><li>Measured parameter values acceptable </li></ul><ul><li>Measured parameter values not acceptable </li></ul>Platform and framework Decisions Compiler/Assembler Decisions Design Optimize Code Code Optimize Design Measure Parameters Compare with Benchmark Start Stop The Optimization Cycle
  8. 8. When to optimize Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Source: http://www.phonegg.com Effort Returns Low Low High High 5MP MeeGo 480x1024px - - - LG Gw990 5MP Bada 480x800px 2GB 512MB 1GHz Samsung S8500 Wave 5MP Linux 800x480px 32GB 256MB 600MHz Nokia N900 3MP MAC OS X 320x480px 16GB 256MB 600MHz iPhone 3GS 5MP Android 480x800px 512MB 512MB 1GHZ HTC Nexus One 5MP Android 480x854px 16GB 256MB 550MHZ Droid Camera OS Display Internal Memory Ram Processor Speed
  9. 9. When to optimize <ul><li>Factors affecting CPU performance: </li></ul><ul><ul><li>Co-processors & hardware accelerators </li></ul></ul><ul><ul><li>Advanced micro architecture (for same ISA) </li></ul></ul><ul><ul><li>Vector Registers </li></ul></ul><ul><ul><li>Pipeline Optimizations </li></ul></ul><ul><ul><ul><li>Data Forwarding </li></ul></ul></ul><ul><ul><ul><li>Branch Prediction </li></ul></ul></ul><ul><ul><ul><li>Predicate Register </li></ul></ul></ul><ul><ul><ul><li>Delayed Branch </li></ul></ul></ul><ul><ul><li>Cache </li></ul></ul><ul><li>Factors affecting Platform choice </li></ul><ul><ul><li>Dev tools, IDE </li></ul></ul><ul><ul><li>Target applications </li></ul></ul><ul><ul><li>Support from vendor </li></ul></ul>Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  10. 10. When to optimize <ul><li>Know Thy compiler </li></ul><ul><li>Optimization types </li></ul><ul><ul><li>Programming language dependant Vs Programming language independent </li></ul></ul><ul><ul><li>Machine dependant Vs Machine independent </li></ul></ul><ul><li>Optimization levels </li></ul><ul><li>Dynamic and Static linking </li></ul><ul><li>Prelinking </li></ul><ul><li>Pragma </li></ul><ul><li>Compiler options </li></ul><ul><li>JIT </li></ul>Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  11. 11. When to optimize <ul><li>Know Thy Framework </li></ul><ul><li>Threading </li></ul><ul><ul><li>UI interactions </li></ul></ul><ul><ul><li>Background computations </li></ul></ul><ul><li>File operation choices. </li></ul><ul><ul><li>System calls for file op </li></ul></ul><ul><ul><ul><li>fopen Vs open </li></ul></ul></ul><ul><ul><li>Write in standard chunks </li></ul></ul><ul><ul><ul><li>Background writes </li></ul></ul></ul><ul><li>Algorithm choices </li></ul><ul><ul><li>Speed Vs Memory </li></ul></ul><ul><ul><ul><li>Hash Vs Binary </li></ul></ul></ul><ul><ul><li>Analyze choices before optimizing </li></ul></ul><ul><ul><ul><li>Spend enough time before picking </li></ul></ul></ul><ul><li>Create/destroy resources </li></ul><ul><ul><li>Memory </li></ul></ul><ul><ul><li>peripherals </li></ul></ul>Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  12. 12. When to optimize <ul><li>Other decisions </li></ul><ul><ul><li>Database decisions </li></ul></ul><ul><ul><ul><li>Do we need database </li></ul></ul></ul><ul><ul><ul><li>Which data base to use </li></ul></ul></ul><ul><ul><li>Fixed Vs Floating point </li></ul></ul><ul><ul><li>Coding language decisions </li></ul></ul><ul><ul><ul><li>Application code </li></ul></ul></ul><ul><ul><ul><li>Native libraries </li></ul></ul></ul><ul><ul><li>API definitions </li></ul></ul><ul><ul><ul><li>Extensibility </li></ul></ul></ul><ul><ul><li>Avoid costly Message passing </li></ul></ul><ul><ul><ul><li>Sockets Vs Intents </li></ul></ul></ul><ul><ul><li>XML parser </li></ul></ul><ul><ul><ul><li>SAX Vs DOM </li></ul></ul></ul>Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  13. 13. When to optimize <ul><li>Know Thy language </li></ul><ul><li>Strive for good programs not fast ones </li></ul><ul><li>Document optimizations </li></ul><ul><ul><li>Optimization Vs Readability </li></ul></ul><ul><li>80/20 rule. </li></ul><ul><li>Area for compiler research </li></ul><ul><li>Control flow optimizations </li></ul><ul><ul><li>Implement state machines </li></ul></ul><ul><li>Optimization techniques for </li></ul><ul><ul><li>Assembly </li></ul></ul><ul><ul><ul><li>Highly ISA dependent </li></ul></ul></ul><ul><ul><ul><li>Group instructions </li></ul></ul></ul><ul><ul><ul><li>Efficient loops </li></ul></ul></ul><ul><ul><ul><li>Use MAC/FMA </li></ul></ul></ul>Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  14. 14. When to optimize <ul><ul><li>High level language </li></ul></ul><ul><ul><ul><li>Language dependent </li></ul></ul></ul><ul><ul><ul><li>Lookup table </li></ul></ul></ul><ul><ul><ul><li>Bounds checking </li></ul></ul></ul><ul><ul><ul><li>Integer multiplication </li></ul></ul></ul><ul><ul><ul><li>Dead code elimination </li></ul></ul></ul><ul><ul><ul><li>Common sub expression elimination </li></ul></ul></ul><ul><ul><ul><li>Loop unrolling </li></ul></ul></ul>Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  15. 15. When to optimize <ul><li>Measured parameter values acceptable </li></ul><ul><li>Measured parameter values not acceptable </li></ul>Platform and framework Decisions Compiler/Assembler Decisions Design Optimize Code Code Optimize Design Measure Parameters Compare with Benchmark Start Stop The Optimization Cycle
  16. 16. Parameters for Optimization <ul><li>Speed </li></ul><ul><li>Memory </li></ul><ul><ul><li>Heap & Stack </li></ul></ul><ul><ul><li>Image size </li></ul></ul><ul><li>Response Time </li></ul><ul><ul><li>Launch time </li></ul></ul><ul><ul><li>Response to user input </li></ul></ul><ul><li>Current Drain </li></ul><ul><ul><li>Active Current drain </li></ul></ul><ul><ul><li>Idle Current drain </li></ul></ul><ul><li>Security </li></ul><ul><ul><li>Application Security </li></ul></ul><ul><ul><li>Device Security </li></ul></ul><ul><ul><li>Network Security </li></ul></ul>
  17. 17. Profilers <ul><li>Speed Profilers </li></ul><ul><ul><li>Based on how data is collected </li></ul></ul><ul><ul><ul><li>Event based (e.g., gcov, gprof’s number of calls etc, codewarrior, eclipse profiler) </li></ul></ul></ul><ul><ul><ul><li>Sampling based (e.g., gprof’s runtime figures) </li></ul></ul></ul><ul><ul><li>Based on amount of information provided </li></ul></ul><ul><ul><ul><li>Trace based (e.g., gcov) </li></ul></ul></ul><ul><ul><ul><li>Statistical (e.g., AMD CodeAnalyst) </li></ul></ul></ul><ul><ul><li>Based on instrumentation of code </li></ul></ul><ul><ul><ul><li>Manual (e.g., Eclipse TraceView) </li></ul></ul></ul><ul><ul><ul><li>Automatic source level (e.g., CodeWarrior) </li></ul></ul></ul><ul><ul><ul><li>Compiler assisted </li></ul></ul></ul><ul><ul><ul><li>Binary instrumented </li></ul></ul></ul><ul><li>Memory Profilers </li></ul><ul><ul><li>Heap and Stack usage (e.g., Valgrind) </li></ul></ul>
  18. 18. Profilers <ul><li>Response Time Profiler </li></ul><ul><ul><li>Manual </li></ul></ul><ul><ul><li>High Speed Camera setup </li></ul></ul><ul><li>Current Drain Profiler </li></ul><ul><ul><li>Customized setup </li></ul></ul><ul><li>Security Profiler </li></ul><ul><ul><li>Application security </li></ul></ul><ul><ul><ul><li>FindBugs, Klocworks </li></ul></ul></ul><ul><ul><li>Device Security </li></ul></ul><ul><ul><ul><li>Anti virus </li></ul></ul></ul><ul><ul><li>Network security </li></ul></ul><ul><ul><ul><li>Netfilter </li></ul></ul></ul><ul><li>Appropriate IDEs </li></ul><ul><ul><li>From Vendor (e.g., CodeWarrior) </li></ul></ul><ul><ul><li>Open Source (e.g., Eclipse, MOTODEV Studio, Carbide.C++) </li></ul></ul>
  19. 19. Benchmarks <ul><li>Internal Benchmarks </li></ul><ul><ul><li>Benchmark against another phone </li></ul></ul><ul><ul><li>Benchmark against another similar application </li></ul></ul><ul><li>External Benchmarks </li></ul><ul><ul><li>Industry Standard </li></ul></ul><ul><ul><ul><li>Embedded Microprocessor Benchmark Consortium (EEMBC) </li></ul></ul></ul><ul><ul><ul><li>Standard Performance Evaluation Corporation (SPEC) </li></ul></ul></ul><ul><ul><li>Open Source </li></ul></ul><ul><ul><ul><li>I/O intensive (IOMETER) </li></ul></ul></ul><ul><ul><ul><li>CPU intensive (PI calculator, LINPACK) </li></ul></ul></ul>
  20. 20. References <ul><li>Various Processors power dissipation details </li></ul><ul><li>http://en.wikipedia.org/wiki/List_of_CPU_power_dissipation </li></ul><ul><li>Summary of various processors based on different architectures: </li></ul><ul><li>http://www.linuxfordevices.com/c/a/Linux-For-Devices-Articles/Embedded-Processor-and-SystemonChip-Quick-Reference-Guide/ </li></ul><ul><li>openMP </li></ul><ul><li>http://openmp.org/wp/ </li></ul><ul><li>Boostpro libaries: </li></ul><ul><li>http://www.boostpro.com </li></ul><ul><li>Vector signal processing library </li></ul><ul><li>http://gpu-vsipl.gtri.gatech.edu/ </li></ul><ul><li>Assembly and high level optimization techniques </li></ul><ul><li>http://www.agner.org/optimize/ </li></ul><ul><li>Optimizing Java code </li></ul><ul><li>http://www.glenmccl.com/jperf/index.htm </li></ul><ul><li>Optimization levels in GCC: </li></ul><ul><li>http://www.linuxjournal.com/article/7269 </li></ul><ul><li>Optimization levels in CodeWarrior </li></ul><ul><li>http://www.freescale.com/infocenter/Codewarrior/index.jsp?topic=/com.freescale.doc.microcontrollers.targeting.manual/050_CWBuildProps.ColdFire_Compiler__Optimization.html </li></ul><ul><li>Profiler basics </li></ul><ul><li>http://en.wikipedia.org/wiki/Profiling_%28computer_programming%29 </li></ul><ul><li>Summary of profiling tools: </li></ul><ul><li>http://ktown.kde.org/~seli/memory/analysis.html </li></ul><ul><li>Valgrind: </li></ul><ul><li>http://valgrind.org/ </li></ul><ul><li>Example codewarrior profiler </li></ul><ul><li>http://www.freescale.com/files/soft_dev_tools/doc/user_guide/IDE_5.5_UG_Profiler.pdf </li></ul>
  21. 21. References <ul><li>Kernel profile and OProfile </li></ul><ul><li>http://omappedia.org/wiki/Android_Debugging </li></ul><ul><li>WinCE kernel profiler: </li></ul><ul><li>http://discovertheexperience.blogspot.com/2009/02/windows-ce-kernel-profiler.html </li></ul><ul><li>iPhone Vs Android Development: </li></ul><ul><li>http:// blogs.zdnet.com/Burnette/?p =682&tag=col1;post-682 </li></ul><ul><li>Low level optimization resources </li></ul><ul><li>http:// www.agner.org /optimize/ </li></ul><ul><li>Iphone security </li></ul><ul><li>http:// www.itsecurity.com/features/iphone -security-threat/ </li></ul><ul><li>Email security </li></ul><ul><li>http:// www.itsecurity.com /email-security/ </li></ul><ul><li>Jit: http:// www.answers.com /topic/just-in-time-compilation </li></ul><ul><li>Assembly optimization tips: </li></ul><ul><li>http://www.mark.masmcode.com/ </li></ul><ul><li>Compiler, C++ optimization: </li></ul><ul><li>http://www.agner.org/optimize/ </li></ul><ul><li>Book on Software optimization: </li></ul><ul><li>Software Optimization for High-Performance Computing By Kevin R. Wadleigh, Isom L. Crawford </li></ul><ul><li>Java benchmarks </li></ul><ul><li>http://java-phones.com/tools/fpc-bench-303 </li></ul><ul><li>Android benchmarks: </li></ul><ul><li>http://mobileswdev.wordpress.com/2009/10/27/android-benchmarks/ </li></ul>
  22. 22. Thank You Raghu Sesha Iyengar [email_address] Mobile Developer Conference by MoMoB http://momob.in
  23. 23. Additional Slides
  24. 24. When to optimize Source: Software Optimization for high performance computing By Kevin R. Wadleigh, Isom L. Crawford Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  25. 25. When to optimize Source: Software Optimization for high performance computing By Kevin R. Wadleigh, Isom L. Crawford Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  26. 26. When to optimize Source: Optimizing Software in C++ Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  27. 27. When to optimize Source:http://www.linuxjournal.com/article/7269 Optimization Levels in GCC Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  28. 28. When to optimize <ul><li>Optimization Levels (gcc) </li></ul><ul><li>-O0 </li></ul><ul><ul><li>No optimization </li></ul></ul><ul><li>-O1 </li></ul><ul><ul><li>Produce optimized image in short time </li></ul></ul><ul><li>-O2 </li></ul><ul><ul><li>All optimizations that do not cause space-seed tradeoff </li></ul></ul><ul><li>-Os </li></ul><ul><ul><li>Size over speed optimization </li></ul></ul><ul><li>-O3 </li></ul><ul><ul><li>Optimize for best speed. Global optimization. Size increase expected. </li></ul></ul><ul><ul><li>Can have adverse effect if this size increase is more than cache size and leads to more memory access. </li></ul></ul><ul><li>Use –f to use any option (which may not be included in the –ox) e.g., gcc finline-functions. </li></ul><ul><li>Use –fno to exclude. E.g., fcc –o1 –fno-defer-pop </li></ul>Source:http://www.linuxjournal.com/article/7269 Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  29. 29. When to optimize Source:http://reallylongword.org/prelink-2/ 700MHz Athlon, 768MB RAM, Linux 2.4.21 Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  30. 30. When to optimize <ul><ul><ul><li>A 100 MB file was read sequentially starting from the beginning of the file to produce these times. The file system buffer cache was 500MB </li></ul></ul></ul>Source: Software Optimization for high performance computing By Kevin R. Wadleigh, Isom L. Crawford Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High
  31. 31. When to optimize Lookup Tables Boundary checking Source: Software Optimization for high performance computing By Kevin R. Wadleigh, Isom L. Crawford Platform and framework Decisions Compiler Decisions Design Optimization Code Optimization Effort Returns Low Low High High

×