Writing Zippy Android Apps Brad Fitzpatrick May 20th, 2010 Live Wave:

Slides:



Advertisements
Similar presentations
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Advertisements

The Path to Multi-core Tools Paul Petersen. Multi-coreToolsThePathTo 2 Outline Motivation Where are we now What is easy to do next What is missing.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
6/13/20151 CS 160: Lecture 13 Professor John Canny Fall 2004.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Computer Science 1620 Programming & Problem Solving.
Cosc 4730 Brief return Sockets And HttpClient And AsyncTask.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Android Tutorial Android Written in Java Utilizes Dalvik VM – Just in time (JIT) compilation since Android 2.2.
1 Android: Event Handler Blocking, Android Inter-Thread, Process Communications 10/11/2012 Y. Richard Yang.
CSE 486/586 CSE 486/586 Distributed Systems PA Best Practices Steve Ko Computer Sciences and Engineering University at Buffalo.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
UI Design Patterns & Best Practices Mike Wolfson July 22, 2010.
Map Reduce: Simplified Data Processing On Large Clusters Jeffery Dean and Sanjay Ghemawat (Google Inc.) OSDI 2004 (Operating Systems Design and Implementation)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Designing For Testability. Incorporate design features that facilitate testing Include features to: –Support test automation at all levels (unit, integration,
1 Mobile Software Development Framework: Android 2/23/2011 Y. Richard Yang.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
ANDROID Presented By Mastan Vali.SK. © artesis 2008 | 2 1. Introduction 2. Platform 3. Software development 4. Advantages Main topics.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
DUE Hello World on the Android Platform.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
PHP meets MySQL.
1 Mobile Software Development Framework: Android 2/28/2011 Y. Richard Yang.
Ideas to Improve SharePoint Usage 4. What are these 4 Ideas? 1. 7 Steps to check SharePoint Health 2. Avoid common Deployment Mistakes 3. Analyze SharePoint.
Android for Java Developers Denver Java Users Group Jan 11, Mike
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Games Development 2 Concurrent Programming CO3301 Week 9.
The Alternative Larry Moore. 5 Nodes and Variant Input File Sizes Hadoop Alternative.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Deadlock Detection and Recovery
(a) What is the output generated by this program? In fact the output is not uniquely defined, i.e., it is not necessarily the same in each execution. What.
Laser-Guided PerformanceManagement MXUG #5 An introduction to Parfait Paul Cowan Aconex July 2009.
Lecture 4 Page 1 CS 111 Online Modularity and Virtualization CS 111 On-Line MS Program Operating Systems Peter Reiher.
Chapter 7 - Interprocess Communication Patterns
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Services Background operating component without a visual interface Running in the background indefinitely Differently from Activity, Service in Android.
Lecture 12 Page 1 CS 111 Online Using Devices and Their Drivers Practical use issues Achieving good performance in driver use.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
CHAPTER 9 File Storage Shared Preferences SQLite.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
Beyond Application Profiling to System Aware Analysis Elena Laskavaia, QNX Bill Graham, QNX.
USING ANDROID WITH THE INTERNET. Slide 2 Lecture Summary Getting network permissions Working with the HTTP protocol Sending HTTP requests Getting results.
Multithreading Chapter 6. Objectives Understand the benefits of multithreading on Android Understand multi-threading fundamentals Know the Thread class.
COMP 430 Intro. to Database Systems
Small talk with the UI thread
UI Design Patterns & Best Practices
Asynchronous Task (AsyncTask) in Android
Reactive Android Development
Instructor: Mazhar Hussain
Async or Parallel? No they aren’t the same thing!
MapReduce Simplied Data Processing on Large Clusters
12 Asynchronous Programming
Mobile Software Development Framework: Android
CS371m - Mobile Computing Responsiveness.
Fundamentals of Databases
Multithreaded Programming
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
Android Developer Fundamentals V2
Hold up, wait a minute, let me put some async in it
Threads, Handlers, and AsyncTasks
CSE 153 Design of Operating Systems Winter 19
CSE 451 Section 1/27/2000.
Presentation transcript:

Writing Zippy Android Apps Brad Fitzpatrick May 20th, 2010 Live Wave:

3 Outline Me & why I care What is an ANR? Why do you see them? Quantifying responsiveness: “jank” Android SDK features to use to avoid jankiness & ANRs Numbers to know War stories from optimizing Froyo (2.2) New performance instrumentation available in Froyo Q&A and/or tell me what you want Note: not a talk on the Dalvik JIT (which is cool when CPU- bound, but often not the problem)

4 About Me Brad Fitzpatrick Pre-Google: –Danga.com / LiveJournal / Six Apart memcached, OpenID, MogileFS, Gearman, perlbal, djabberd,.... … mix of Social + infrastructure <3 Open Source Google: –Social Graph API, ~Google Profiles –PubSubHubbub, WebFinger Now: –Android performance –working on Open Source again!

Jank

6 Chrome team's term for stalling the event loop –Chrome's fanatically anti-jank –“Janky”: not being immediately responsive to input “Eliminating jank” –Reacting to events quickly, –Don't hog the event loop (“main” / UI) thread! –Getting back into the select() / epoll_wait() call ASAP, so... –… you can react to future events quickly (touches, drags) Else,...

ANR!

8 ANRs (“Application Not Responding”) ANRs happen when, –main thread (“event thread” / “UI thread”) doesn't respond to input event in 5 seconds, –a BroadcastReceiver doesn't finish in 10 seconds –typically, when doing network operations on main thread, doing slow 'disk' operations (un-optimized SQL) on the main thread Less than 5 or 10 seconds, though... –Users: “This app feels janky.” (or “sluggish”, “slow”, …)

Numbers

10 Numbers (Nexus One) ~0.04 ms – writing a byte on pipe process A->B, B->A –or reading simple /proc files (from dalvik) ~0.12 ms – void/void Binder RPC call A->B, B->A ~5-25 ms – uncached flash reading a byte ~5-200+(!) ms – uncached flash writing tiny amount (next...) 16 ms – one frame of 60 fps video 41 ms – one frame of 24 fps video ms – human perception of slow action 108/350/500/800 ms – ping over 3G. varies! ~1-6+ seconds – TCP setup + HTTP fetch of 6k over 3G

11 Writing to flash (yaffs2) Create file, 512 byte write, delete –ala sqlite.journal in transaction Flash is … different than disks you're likely used to –read, write, erase, wear-leveling, GC, … nutshell: write performance varies a lot Source: empirical samples over Google employee phones (Mar 2010)

12 sqlite performance previous writes were what sqlite does on ~each transaction, –even no-op transactions (since reported & fixed upstream) use indexes –EXPLAIN vs. EXPLAIN QUERY PLAN log files: often much cheaper to append to a file than use a database –look at “adb shell cat /proc/yaffs” and look at writes/erases –sqlite can be pretty write-happy sqlite-wrapper.pl tool –

13 Lessons so far Writing to disk is “slow” Using the network is “slow” Be paranoid. Always assume the worst. –given enough users, usage, time... –users will find your ANRs –bad Market reviews, uninstalls, –me filing bugs on your app :-)

Tools

15 android.os.AsyncTask “AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.”

private class DownloadFilesTask extends AsyncTask { protected Long doInBackground(URL... urls) { // on some background thread int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); } return totalSize; } protected void onProgressUpdate(Integer... progress) { // on UI thread! setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { // on UI thread! showDialog("Downloaded " + result + " bytes"); } new DownloadFilesTask().execute(url1, url2, url3); // call from UI thread!

private boolean handleWebSearchRequest(final ContentResolver cr) {... new AsyncTask () { protected Void doInBackground(Void... unused) { Browser.updateVisitedHistory(cr, newUrl, false); Browser.addSearchUrl(cr, newUrl); return null; } }.execute()... return true; } Fire-and-forget style Source: Froyo's src/com/android/browser/BrowserActivity.java, roughly

18 AsyncTask Caveats Must be called from a main thread –rather, a thread with a Handler/Looper around –don't use AsyncTask in a library where caller could call it from their own AsyncTask. or, check first. If called from an activity, the activity process may exit before your AsyncTask completes –user leaves activity, –system is low on RAM, –system serializes activity's state for later, –system kills your process (“just a replaceable activity!”) –if work is critical, use...

19 android.app.IntentService Eclair (2.0, 2.1) docs: –“An abstract Service that serializes the handling of the Intents passed upon service start and handles them on a handler thread. To use this class extend it and implement onHandleIntent(Intent). The Service will automatically be stopped when the last enqueued Intent is handled.” –little confusing, thus... –nobody really used it Froyo (2.2) docs, clarified....

20 android.app.IntentService “IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. This 'work queue processor' pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate. All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.”

21 IntentService benefits your activity's process, while processing that Intent, now has a Service running the Android process killer will try really hard not to kill you now but once you're done handling your piece of work, you're now properly disposable again very easy way to use a Service

public class DismissAllAlarmsService extends IntentService public void onHandleIntent(Intent unusedIntent) { ContentResolver resolver = getContentResolver();... resolver.update(uri, values, selection, null); } in AlertReceiver extends BroadcastReceiver, onReceive(): (main thread) Intent intent = new Intent(context, DismissAllAlarmsService.class); context.startService(intent); Calendar's use of IntentService Source: Froyo's src/com/android/calendar/DismissAllAlarmsService.java, roughly

23 Other tips disable UI elements immediately, before kicking off your AsyncTask to finish the task do some animation –e.g. spinner in title bar ProgressDialog –for long-ish operations –use sparingly. can be jarring / interrupting. Combination of above, –not sure how long it will take? –start with disabling UI elements, animating something, –start timer, –show a ProgressDialog if it's taking over 200 ms. –in AsyncTask onPostExecute, cancel alarm timer

Non-jank related tools...

25 Traceview when CPU-bound –trace file format supports walltime clocksource, but GUI traceview tool ignores it, assumes per-thread CPU time toggle in code: –dalvik.system.VMDebug#{start,stop}MethodTracing() toggle at runtime: –adb shell am profile start –adb shell am profile stop

26 print “here” class android.os.PerformanceCollector –I've seen lots of one-off variants log occasionally: –trace point name {start,stop} –android.os.SystemClock#uptimeMillis() –dalvik.system.VMDebug#threadCpuTimeNanos() watch your own device, upload, aggregate, and analyze from users? –often overkill

Froyo (2.2)

Performance Instrumentation in Froyo (2.2) We've instrumented, –Database queries, –RPC (Binder) calls, –ContentResolver, –Mutex lock contention in Dalvik, How long? Main thread or not? Which thread/process? Which build? Log a small percentage to in-memory ring-buffer Service (on Google employee dogfooder phones) uploading a few MB/day/phone for analysis, report generation Found & fixed many things, often surpising

Performance Instrumentation in Froyo (2.2) Not a generic debugging feature yet, but somewhat usable... Log vs EventLog –Log: text ring-buffer (adb logcat), apps use this. –EventLog: binary structured ring-buffer (adb logcat -b events), intended for low-level platform development work only EventLog records: –db_sample –binder_sample –content_query_sample –content_update_sample –dvm_lock_sample

Coming later

31 After Froyo more instrumentation –more granular db stats, mutex lock->unlock duration stats... easier to use during app development API additions to let you force “strict mode” in your apps, –prevent read and/or write I/O on your main thread –prevent slow Binder calls to other processes doing I/O –prevent network I/O on the main thread let users opt-in to performance collection (system-wide or app-by-app? separate app?) –surface empirical performance data to developers through Android Market? sqlite WAL maybe? speculative...

Summary

Get off the main thread! Disks & networks aren't instantaneous Know what sqlite is doing Instrumentation is fun :-) More speed goodness coming post-Froyo... Code, tools from this talk: Q&A? Live Wave: