Android Multi-Core   Funky Android Ltd.     http://www.funkyandroid.com/
Why Bother?• Potential for more performance• Increased battery life• More cores in future devices• It’s not difficult once...
Performance           Balls and Slides• Multi-core friendly apps are not guaranteed to be faster• Think of a each task you...
Performance           Balls and Slides• It will, however, allow a box of balls to be emptied faster• More small balls work...
Battery Life• The more tasks running on a single core, the less likely the on-chip cache will have the necessary instructi...
Battery life • Twice the cores != Twice the power consumption • ARM Cortex A9 figures shown a single 830 Mhz   core ~= 0.4...
NVIDIA Kal-El• 5 Cores - 4 High Speed, 1 Low Power• Removes the need to choose speed or power consumption at the hardware ...
More cores in future             devices• It’s now nearly impossible to buy a single core PC.• Multi-core hardware experim...
It’s not difficult once you      know how......                       www.funkyandroid.com
First: What is a core?• It’s a component capable of doing one piece of work at any point in time• It can switch between ta...
First: What is a core?• A system with 4 cores can be doing 4 things at the same point in time• This doesn’t mean everythin...
Android multi-core• < 2.2 - No multi-core devices• 2.2 - Not designed for Multi-Core, but used anyway (e.g. LG Optimus 3D,...
Think before you code...                     www.funkyandroid.com
Threads and processes• Processes contain one or more active Threads• In Android; Processes can’t share memory, but Threads...
Context switching• It’s the overhead changing from doing one task to doing another• It’s the reason more threads does not ...
Resource contention• If you only have one physical component, then it can only do one thing at a time• Having multiple tas...
Deadlock• Three people ready to eat a meal, each with one chopstick. Everyone tries to eat at once but nobody can because ...
Coding for the cores                   www.funkyandroid.com
The building blocks• Thread & Runnable : Parts of “core” Java. Quick, Dirty, Not “Android”• AsyncTask : A more Android fri...
AsyncTask• By default there’s no change between Gingerbread and Honeycomb• In Honeycomb you can use executeOnExecutor and ...
IntentService Example• SyncService in the Google IO 2011 app•Declare IntentService in the manifest;<service android:name="...
Putting it into practice                      www.funkyandroid.com
Noughts & Crosses          (a.k.a. Tic-Tac-Toe)   Image from Wikipedia (http://goo.gl/W02Jp)   www.funkyandroid.com
Devices used• Nexus One (Qualcomm Snapdragon - 1Ghz Single Core)• HTC Flyer (Qualcomm Snapdragon - 1.5 Ghz Single Core)• L...
Noughts & crosses• Simple game.• 3x3 grid, 2 placers (O & X)• Winner is the one who gets three in a row first.• Limited nu...
CPU BoundThreads can’t give you any more CPU power, and the      OS will always take up some CPU time.                    ...
Real world applications• Tend involve interaction with another slower component (Network, Storage)• Don’t be tempted by Th...
Not CPU bound  Using threads improves performance on multi-coredevices without a significant impact on single core ones.  ...
What just happened?• While one Thread waits for a slower component the other Threads get more CPU time. Too many threads a...
Things to be aware of                   www.funkyandroid.com
Devices are different• Not all components in all devices are the same• Internal storage differences can give some wildly d...
Using "synchronised"• Makes sure that Threads don’t clash• Use it sparingly, if you overuse it you’ll end up with worse pe...
Thread.yield()• If you’re using it you’re not doing yourself any favours• In the Noughts and Crosses example performance d...
Counting the cores• Runtime.getRuntime().availableProcessors()• Cores + a few is a good place to start when looking for a ...
Questions ?              www.funkyandroid.com
Upcoming SlideShare
Loading in...5
×

Droidcon London 2011 - Android Multi-core

336

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
336
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Droidcon London 2011 - Android Multi-core"

  1. 1. Android Multi-Core Funky Android Ltd. http://www.funkyandroid.com/
  2. 2. Why Bother?• Potential for more performance• Increased battery life• More cores in future devices• It’s not difficult once you know how www.funkyandroid.com
  3. 3. Performance Balls and Slides• Multi-core friendly apps are not guaranteed to be faster• Think of a each task your application performs as a football travelling down a slide• More slides wont help a single ball travel faster down a slide. www.funkyandroid.com
  4. 4. Performance Balls and Slides• It will, however, allow a box of balls to be emptied faster• More small balls work better on multi-core devices than a few large ones. www.funkyandroid.com
  5. 5. Battery Life• The more tasks running on a single core, the less likely the on-chip cache will have the necessary instructions/data.• Power saving measures can kick-in faster if all the balls have gone. www.funkyandroid.com
  6. 6. Battery life • Twice the cores != Twice the power consumption • ARM Cortex A9 figures shown a single 830 Mhz core ~= 0.4W, dual 800 Mhz cores ~= 0.5w* • Cortex A9 is the core of the NVidia Tegra 2, OMAP 4xxx series, Samsung Exynos, and so is a good representation of shipping devices.* - http://www.arm.com/products/processors/cortex-a/cortex-a9.php www.funkyandroid.com
  7. 7. NVIDIA Kal-El• 5 Cores - 4 High Speed, 1 Low Power• Removes the need to choose speed or power consumption at the hardware level• You can still drain the battery pretty quickly if your app has a heavy workload. www.funkyandroid.com
  8. 8. More cores in future devices• It’s now nearly impossible to buy a single core PC.• Multi-core hardware experimentation already performed on servers and desktops.• On-chip multi-core is over a decade old (e.g IBMs’ Power4), multi-chip multi-core architectures are several decades older than that (first ones in the 1960s). www.funkyandroid.com
  9. 9. It’s not difficult once you know how...... www.funkyandroid.com
  10. 10. First: What is a core?• It’s a component capable of doing one piece of work at any point in time• It can switch between tasks, but can only work on them one at a time Portions of this page are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. www.funkyandroid.com
  11. 11. First: What is a core?• A system with 4 cores can be doing 4 things at the same point in time• This doesn’t mean everything gets 4x faster though. Portions of this page are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. www.funkyandroid.com
  12. 12. Android multi-core• < 2.2 - No multi-core devices• 2.2 - Not designed for Multi-Core, but used anyway (e.g. LG Optimus 3D, LG Optimus 2X)• 2.3 - Used on Multi-Core phones, has tweaks which work well with Multi-Core devices (concurrent garbage collector)• 3.x - Multi-core devices, but not fully multi-core aware. www.funkyandroid.com
  13. 13. Think before you code... www.funkyandroid.com
  14. 14. Threads and processes• Processes contain one or more active Threads• In Android; Processes can’t share memory, but Threads can.• Processes can talk to each other, but it’s far slower than the methods available to Threads. www.funkyandroid.com
  15. 15. Context switching• It’s the overhead changing from doing one task to doing another• It’s the reason more threads does not always mean better performance• If you’ve got 10 emails to write you wouldn’t spend a minute on each in strict rotation. www.funkyandroid.com
  16. 16. Resource contention• If you only have one physical component, then it can only do one thing at a time• Having multiple tasks trying to use the same component will slow things down for all of them.• If you only have one hammer you wouldn’t try to hammer in 10 nails at the same time... well... most of you wouldn’t. www.funkyandroid.com
  17. 17. Deadlock• Three people ready to eat a meal, each with one chopstick. Everyone tries to eat at once but nobody can because nobody has two chopsticks.• Think carefully about what your task needs to succeed• If task A needs task B to finish, make sure task A isn’t stopping task B completing. www.funkyandroid.com
  18. 18. Coding for the cores www.funkyandroid.com
  19. 19. The building blocks• Thread & Runnable : Parts of “core” Java. Quick, Dirty, Not “Android”• AsyncTask : A more Android friendly Thread. Allows IO Interaction• Service : Good for long running tasks which don’t need the UI. Has it’s own lifecycle.• IntentService : Builds on Service to create a long running background task that’s Intent driven www.funkyandroid.com
  20. 20. AsyncTask• By default there’s no change between Gingerbread and Honeycomb• In Honeycomb you can use executeOnExecutor and AsyncTask.THREAD_POOL_EXECUTOR to run multiple tasks in parallel* *Thanks to Richard Hyndman at Google for this tip www.funkyandroid.com
  21. 21. IntentService Example• SyncService in the Google IO 2011 app•Declare IntentService in the manifest;<service android:name="com.google.android.apps.iosched.service.SyncService" />•Extend android.app.IntentService•Implement onHandleIntent(Intent) SyncService can be found at http://goo.gl/h7s9U www.funkyandroid.com
  22. 22. Putting it into practice www.funkyandroid.com
  23. 23. Noughts & Crosses (a.k.a. Tic-Tac-Toe) Image from Wikipedia (http://goo.gl/W02Jp) www.funkyandroid.com
  24. 24. Devices used• Nexus One (Qualcomm Snapdragon - 1Ghz Single Core)• HTC Flyer (Qualcomm Snapdragon - 1.5 Ghz Single Core)• LG Optimus 3D (TI OMAP 4430 - 1 Ghz Dual Core)• Samsung Tab 10.1 (Nvidia Tegra 2 - 1 Ghz Dual Core) www.funkyandroid.com
  25. 25. Noughts & crosses• Simple game.• 3x3 grid, 2 placers (O & X)• Winner is the one who gets three in a row first.• Limited number of possible moves makes it a good candidate for a benchmark. www.funkyandroid.com
  26. 26. CPU BoundThreads can’t give you any more CPU power, and the OS will always take up some CPU time. www.funkyandroid.com
  27. 27. Real world applications• Tend involve interaction with another slower component (Network, Storage)• Don’t be tempted by Thread.yield(), it’s not going to help if your CPU is maxed out (it’ll actually slow it down) www.funkyandroid.com
  28. 28. Not CPU bound Using threads improves performance on multi-coredevices without a significant impact on single core ones. www.funkyandroid.com
  29. 29. What just happened?• While one Thread waits for a slower component the other Threads get more CPU time. Too many threads an context switching is an issue.• The “sweet spot” for the number of Threads depends on the processing/waiting ratio.• More threads if they spend more time waiting, Less threads if they spend more time calculating. www.funkyandroid.com
  30. 30. Things to be aware of www.funkyandroid.com
  31. 31. Devices are different• Not all components in all devices are the same• Internal storage differences can give some wildly different results• Don’t assume the Thread sweet-spot is tied to the number of cores. Nvidia, Texas Instruments, Qualcomm, Samsung, and others all have their own chip design. www.funkyandroid.com
  32. 32. Using "synchronised"• Makes sure that Threads don’t clash• Use it sparingly, if you overuse it you’ll end up with worse performance than not Threading• If you think you need it then think about your design. There may be a more Thread-wise way of doing things. www.funkyandroid.com
  33. 33. Thread.yield()• If you’re using it you’re not doing yourself any favours• In the Noughts and Crosses example performance dropped by around 10% when it was introduced• The OS now handles Thread scheduling well enough to make it redundant. www.funkyandroid.com
  34. 34. Counting the cores• Runtime.getRuntime().availableProcessors()• Cores + a few is a good place to start when looking for a sweet spot for non CPU-bound threads. www.funkyandroid.com
  35. 35. Questions ? www.funkyandroid.com

×