Android System Programming Tips and Tricks系统编程技巧(46P).ppt
4/11/11,Android System ProgrammingTips and Tricks,4/11/11,Overview,Intro to AndroidWorking with SourceInteracting with the TargetDebug and Trace toolsPerformance toolsRandom thoughts on AndroidResources,4/11/11,Intro to Android,Google runtime on top of Linux,ObligatoryArchitecturediagram:,4/11/11,Android device proliferation,4/11/11,Working With Source,4/11/11,Working with Source,GitRepoBuild SystemBuilding fastAdding a program to the build,4/11/11,Git,Android open source project uses gitYou need to learn to use git well,reallyNeed to know how to do a git rebase especially for kernel patchesgit rebase-iKernel patch rebase needed for moving kernel versions,porting work,etc.Lots of online resourcesRecommended online book:http:/progit.org/book/,4/11/11,Repo,Export REPO_TRACE=1 is handy to see what git commands are happeningRepo tricksRepo forall-c git diff Repo forall-c echo$REPO_PATH;git remote-vUse to see upstream remotes from which to compare and merge with.Repo manifest-r-o my-tag-file.xmlMake a repository snapshot manifest,4/11/11,Build system,Lots of interesting stuff in build/envsetup.shhelpchoosecombo/lunchjgrep/cgrepgodirInteresting make targets:showcommands psuedo-target to show build commandssdk can build the SDK from scratch,4/11/11,Fast building,Parallel make threadsmake-j6Use 2 more than your number of CPUs(include hyperthreaded CPUs)Compiled output cacheccache is in/prebuilt areaexport USE_CCACHE=1Great for rebuilds(21 minutes on my desktop)Make only a specific modulemm build only the module(s)in the current directory(and below)I usually combine with a custom install script,which copies from out/target/product/,4/11/11,Adding a program to the build,Make a directory under externale.g./external/myprogramCreate your C/cpp files.Create Android.mk as clone of external/ping/Android.mkChange the names ping.c and ping to match your C/cpp files and program nameAdd the directory name in ANDROID/build/core/main.mk after external/zlib as external/myprogramMake from the root of the source tree,4/11/11,Interacting with the Target,4/11/11,Interacting with the Target,Android has some very nice integration engineeringTools discussedFastbootADBUseful development configurations,4/11/11,Fastboot,“fastboot”is a both tool and a bootloader protocolRequired by Google for certified devicesWould be really nice to adopt as an industry standarde.g.maybe support fastboot in U-BootFastboot operationsInstall kernelInstall new flash imageBoot directly from hostVery useful for test automation,4/11/11,ADB,Android Debug BridgeTool for all kinds of target interactions(install,logging,remote shell,file copy)shell push/pullLogcatInstall/uninstallPrint this and keep it under your pillow.http:/,4/11/11,ADB(cont.),Can work over network,instead of USBUseful if you run build inside virtual machine on hoste.g.I build on Ubuntu 8.04 on Fedora 12(64-bit)hostIts simple:export ADBHOST=192.168.2.1For some reason,I have to kill the server after rebooting targetadb kill-serverCalling adb will re-spawn the server automatically,4/11/11,Development configurations,Power control,Target,Host,USB,Network,kernel,root fs,data,flash,Functionality testing,Target,Integration and Performance testing,4/11/11,Trace and Debug Tools,4/11/11,Trace and Debug tools,LoggingKernel log(dmesg)LogcatStdio redirectionStraceBootchartDumpsysDDMSgdb,4/11/11,Kernel log,Its there,use dmesg to access after bootTurn on PRINTK_TIMES for timestampsIncrease buffer size:CONFIG_LOG_BUF_SHIFTCan emit messages into log by writing to/dev/kmsgVery handy to synchronize with kernel messages,4/11/11,Logcat,Logging system in kernelIntegrated throughout Android system(C+and Java access)Increase logging levelsFlags to control logging level in code(DEBUG emits more?)Different logs(main,event,etc.)Event is funky,is coded for sizeSee jamboree presentation on log info http:/(Presentation by Tetsuyuki Kobayashi),4/11/11,Logcat,Use from host to redirect to a fileTo get main log info,use:e.g.adb logcat v time d*:V test.logTo get info from events log,use-b:e.g.adb logcat b events v time d|grep bootFilter using:Can use ANDROID_LOG_TAGS environment variableI wrote my own logdelta tool,to see time between eventsSee http:/elinux.org/Improving_Android_Boot_Time#logdelta,4/11/11,Logging system overview diagram,*Shameless ripoff of Tetsuyuki Kobayashi,4/11/11,Logcat results(events),I/boot_progress_start(754):12559I/boot_progress_preload_start(754):17879I/boot_progress_preload_end(754):28546I/boot_progress_system_run(768):29230I/boot_progress_pms_start(768):29697I/boot_progress_pms_system_scan_start(768):30117I/boot_progress_pms_data_scan_start(768):44171I/boot_progress_pms_scan_end(768):50006I/boot_progress_pms_ready(768):50505I/boot_progress_ams_ready(768):53166I/boot_progress_enable_screen(768):56793,4/11/11,Stdio redirection,You can send stdout and stderr to the log:logwrapper myprogramRedirecting Dalvik output:Redirecting C/cpp output:Myprogram|xargs logAssumes you have busybox xargs installed,#stop#setprop log.redirect-stdio true#start,4/11/11,Strace,Shows system calls for a process(or set of processes)Is part of AOSP since EclairCan add to init.rc to trace initialization.For example,to trace zygote startup,in/init.rc change:to,service zygote/system/xbin/strace-tt-o/data/boot.strace/system/bin/app_process-Xzygote/system/bin-zygote-start-system-server,service zygote/system/bin/app_process-Xzygote/system/bin-zygote-start-system-server,4/11/11,Bootchart,init gathers data on startupMust re-compile init with support for bootchart data collectionA tool on the host produces a nice graphicSee http:/elinux.org/Bootchart and http:/elinux.org/Using_Bootchart_on_Android,4/11/11,Bootchart output,4/11/11,Dumpstate/dumpsys,Dumps huge amounts of information about the system,including status,counts and statisticsDumpstate reproduces lots of stuff from/procDoes a dumpsys as wellDumpsys show status information from Androidservicese.g.dumpsys alarmFirst part of dump has list of services you can dump,4/11/11,DDMS,Dalvik Debug Monitor Servicehttp:/of features,controllable via eclipseTo watch allocations in C/c+code,try:Set“native=true”in/.android/ddms.cfgUse standalone ddms programOn target do:,#setprop libc.debug.malloc 1#stop#start,4/11/11,GDB,How to invoke:Note that gdbclient is a function in build/envsetup.shFiles are stripped in output dirUnstripped files are at;./out/target/product/generic/obj/EXECUTABLES/_intermediates/LINKED/,1.adb forward tcp:5039 tcp:50392.adb shell gdbserver:5039 3.In another shell,gdbclient Or,manually:$arm-eabi-gdb#file./out/target/product/generic/symbols/system/bin/app_process#set solib-search-path./out/target/product/generic/symbols/system/lib#target remote localhost:5039,4/11/11,More debug tips,See http:/omappedia.org/wiki/Android_DebuggingTons of tips,including:How to debug a native program segfaultHow to use kernel profiler and oprofileHow to use gdb and DDDInfo is for Zoom2 board,but some things should work on your board also,4/11/11,Performance Tools,4/11/11,Performance Tools,SmemTraceview0 xbenchPerf?,4/11/11,Smem,Tools for analyzing system-wide memory usageCan slice,dice,and visualize memory info snapshotRun smemcap on target,grab data with adb,then analyze on hostSee http:/elinux.org/Using_smem_on_Android,4/11/11,Traceview,Shows trace of Java methodsAlso shows profile informationUser can start and stop tracing either using DDMSApp can start and stop tracing programmaticallyGoogle:“android traceview”,4/11/11,0 xbench,Has several built-in benchmarks,such as Linpack,Scimark2,and LibMicroProject page at:http:/available in Android MarketSome tests require root privileges,4/11/11,Perf,Standard kernel tool for performance analysisNow that Android is on more recent kernels,it should work out of the boxHave to admit that I havent done it yetAnyone here used it?,4/11/11,Miscellaneous tools,procranksetprop/getpropsqlite(command line)start/stopCan stop/start whole system,4/11/11,Procrank,Shows a quick summary of processes,sorted by VSS,RSS,PSS or USSSee http:/elinux.org/Android_Memory_UsageOutput:,#procrank PID Vss Rss Pss Uss cmdline 1217 36848K 35648K 17983K 13956K system_server 1276 32200K 32200K 14048K 10116K android.process.acore 1189 26920K 26920K 9293K 5500K zygote 1321 20328K 20328K 4743K 2344K android.process.media 1356 20360K 20360K 4621K 2148K com.android.email 1303 20184K 20184K 4381K 1724K com.android.settings 1271 19888K 19888K 4297K 1764K com.android.inputmethod.latin 1332 19560K 19560K 3993K 1620K com.android.alarmclock 1187 5068K 5068K 2119K 1476K/system/bin/mediaserver 1384 436K 436K 248K 236K procrank 1 212K 212K 200K 200K/init 753 572K 572K 171K 136K/system/bin/rild 748 340K 340K 163K 152K/system/bin/sh 751 388K 388K 156K 140K/system/bin/vold 1215 148K 148K 136K 136K/sbin/adbd 757 352K 352K 117K 92K/system/bin/dbus-daemon 760 404K 404K 104K 80K/system/bin/keystore 759 312K 312K 102K 88K/system/bin/installd 749 288K 288K 96K 84K/system/bin/servicemanager 752 244K 244K 71K 60K/system/bin/debuggerd,4/11/11,setprop/getprop,Many services have debug elements controlled by propertiesMany properties are set in/init.rcYou can also query and set properties on the command lineUse getprop(with no args)to see list of propertiesHave to examine source for properties with special effects(or see some tip on a mailing list)Example:setting the DNS server address manually:setprop net.nds1 xx.yy.zz.aa,4/11/11,Sqlite,You can inspect and modify sqlite data directly from the command line(or over adb)Heres an example of setting the http_proxy for a development boardMost databases are under a directory called databases,and end in.db,#cd/data/data/com.android.providers.settings/databases#sqlite3 settings.dbSQLite version 3.5.9Enter.help for instructionssqlite insert into system values(99,http_proxy,192.168.1.1:80);sqlite.exit#,4/11/11,Wrapup,4/11/11,Random thoughts on Android,Throws POSIX out the windowThis is refreshingHas some downsides,but most things are OKCan finally jettison some legacy baggageSeems destined to be a major embedded Linux platformOnly drawback(?)is non-native appsBut even this has pros and consWould like to see consolidationYou can use an Android kernel for“regular”embedded Linux work,4/11/11,Resources,eLinux wiki Android portal:http:/elinux.org/Android_PortalUse android-porting,android-platform,and android-kernel mailing lists,depending on where your issue isSeehttp:/elinux.org/Android_Web_Resources#Mailing_ListsMy e-mail:tim.bird(at),4/11/11,Thanks for your time,Questions and Answers,