Difference between revisions of "Android Notes"

From eLinux.org
Jump to: navigation, search
m
m (Copy edited (e.g. ref. <https://en.wikipedia.org/wiki/Out_of_memory#Out_of_memory_management> and <https://en.wikipedia.org/wiki/BusyBox>).)
 
Line 5: Line 5:
  
  
== Technical Info about system components ==
+
== Technical information about system components ==
=== oom killer info ===
+
=== OOM Killer information ===
  
Google (Android) developer Arve Hjonevag added a lowmemorykiller feature to the staging area of the
+
Google (Android) developer Arve Hjonevag added a lowmemorykiller feature to the staging area of the Linux kernel in January of 2009. This feature tries to reclaim memory before the system runs out (acting as a kind of cache manager, according to Arve). In Linus' 2.6.28-rc tree this appears in  
Linux kernel in January of 2009.   This feature tries to reclaim memory before the system
 
runs out (acting as a kind of cache manager, according to Arve). In Linus' 2.6.28-rc tree this
 
appears in  
 
  
 
Application lifecycle (and activity states) can be found here:
 
Application lifecycle (and activity states) can be found here:
 
http://code.google.com/android/intro/lifecycle.html
 
http://code.google.com/android/intro/lifecycle.html
  
Applications in different stages of their lifecycle receive a different oom_adj value, which affects
+
Applications in different stages of their lifecycle receive a different oom_adj value, which affects the probability of their being reaped.
the probability of their being reaped.
 
  
 
Initial lowmemorykiller thresholds are set by /etc/init.rc (with the following lines):
 
Initial lowmemorykiller thresholds are set by /etc/init.rc (with the following lines):
  
  # Define the oom_adj values for the classes of processes that can be
+
  # Define the oom_adj values for the classes of processes that can be killed by the kernel. These are used in ActivityManagerService.
# killed by the kernel. These are used in ActivityManagerService.
 
 
     setprop ro.FOREGROUND_APP_ADJ 0
 
     setprop ro.FOREGROUND_APP_ADJ 0
 
     setprop ro.VISIBLE_APP_ADJ 1
 
     setprop ro.VISIBLE_APP_ADJ 1
Line 30: Line 25:
 
     setprop ro.EMPTY_APP_ADJ 15
 
     setprop ro.EMPTY_APP_ADJ 15
 
   
 
   
  # Define the memory thresholds at which the above process classes will
+
  # Define the memory thresholds at which the above process classes will be killed. These numbers are in pages (4k).
# be killed. These numbers are in pages (4k).
 
 
     setprop ro.FOREGROUND_APP_MEM 1536
 
     setprop ro.FOREGROUND_APP_MEM 1536
 
     setprop ro.VISIBLE_APP_MEM 2048
 
     setprop ro.VISIBLE_APP_MEM 2048
Line 51: Line 45:
  
 
Routines to actually calculate the oom_adj value, based on application state
 
Routines to actually calculate the oom_adj value, based on application state
are in the ActivityManager. See frameworks/base/services/java/com/android/server/am/ActivityManagerService.java:UpdateOomAdjLocked(...)
+
are in the ActivityManager. See frameworks/base/services/java/com/android/server/am/ActivityManagerService.java:UpdateOomAdjLocked(...) and frameworks/base/core/java/android/os/Process.java:setOomAdj() => frameworks/base/core/jni/android_util_Process.cpp:android_os_Process_setOomAdj()
  
and
+
=== Init sequence ===
frameworks/base/core/java/android/os/Process.java:setOomAdj() =>
 
frameworks/base/core/jni/android_util_Process.cpp:android_os_Process_setOomAdj()
 
 
 
=== init sequence ===
 
 
See this blog entry for an overview of the sequence of operations performed by the 'init' program: http://blog.csdn.net/loughsky/archive/2008/11/13/3293922.aspx
 
See this blog entry for an overview of the sequence of operations performed by the 'init' program: http://blog.csdn.net/loughsky/archive/2008/11/13/3293922.aspx
  
=== kernel power management ===
+
=== Kernel power management ===
 
See http://mjg59.livejournal.com/100221.html for a discussion of kernel modifications to support power management.
 
See http://mjg59.livejournal.com/100221.html for a discussion of kernel modifications to support power management.
  
(Also, the comments are interesting, in that they delve into (and speculate about) the possible history of
+
(Also, the comments are interesting, in that they delve into (and speculate about) the possible history of certain components of the Android system.)
certain components of the Android system.)
 
  
=== C Library (bionic) info ===
+
=== C Library (bionic) information ===
See the excellent article: http://codingrelic.geekhold.com/2008/11/six-million-dollar-libc.html
+
See the excellent article: http://codingrelic.geekhold.com/2008/11/six-million-dollar-libc.html for an overview of bionic.
for an overview of bionic.
 
  
 
From: http://discuz-android.blogspot.com/2008/10/google-android-native-libc-bionic.html
 
From: http://discuz-android.blogspot.com/2008/10/google-android-native-libc-bionic.html
Line 77: Line 65:
 
* Speed: limited CPU power means it needs to be fast. Bionic has a small size and fast code paths, including a very fast and small custom pthread implementation.
 
* Speed: limited CPU power means it needs to be fast. Bionic has a small size and fast code paths, including a very fast and small custom pthread implementation.
  
Bionic has built-in support for important Android-specific services such as system properties and logging. It doesn’t support certain POSIX features, like C++ exceptions and wide chars, which were not needed on Android. Thus it’s not quite compatible with the gnu libc. All native code must be compiled against bionic, not glibc.
+
Bionic has built-in support for important Android-specific services such as system properties and logging. It doesn’t support certain POSIX features, like C++ exceptions and wide chars, which were not needed on Android. Thus it’s not quite compatible with the GNU libc. All native code must be compiled against bionic, not glibc.
  
==== threads ====
+
==== Threads ====
 
The bionic C library has its own thread API, not the same as either original LinuxThreads or
 
The bionic C library has its own thread API, not the same as either original LinuxThreads or
 
NPTL.
 
NPTL.
Line 85: Line 73:
 
From: http://www.mail-archive.com/uclibc@uclibc.org/msg02787.html
 
From: http://www.mail-archive.com/uclibc@uclibc.org/msg02787.html
  
  android thread library (from bionic libc) is minimalist :
+
  Android thread library (from bionic libc) is minimalist:
   - use most of the linux kernel thread features (futex, CLONE_THREAD)
+
   - Uses most of the Linux kernel thread features (futex, CLONE_THREAD)
 
   unlike old linuxpthread.
 
   unlike old linuxpthread.
  - it doesn't support thread cancellation (see CAVEATS file)
+
  - It doesn't support thread cancellation (see CAVEATS file)
  - it misses some pthread functions : for example no barrier, missing  
+
  - It misses some pthread functions: for example, no barrier, missing  
  *timedwait variant, ...
+
  *timedwait variant, etc.
  - very basic gdb support that only work through gdbserver
+
  - Very basic GDB support that only works through gdbserver
  - some theoretic race
+
  - Some theoretic race
  
 
== Development Tools ==
 
== Development Tools ==
=== Busybox ===
+
=== BusyBox ===
Android ships with a utility suite (called 'toolbox') that is not busybox.
+
Android ships with a utility suite (called 'toolbox') that is not BusyBox.
  
You can get a binary busybox for Android [http://benno.id.au/blog/2007/11/14/android-busybox here]
+
You can get a binary BusyBox for Android [http://benno.id.au/blog/2007/11/14/android-busybox here]. The site includes instructions for easy installation on your device.
The site includes instructions for easy installation on your device.
 
  
 
=== Bootchart ===
 
=== Bootchart ===
 
The 'init' program in Android has built-in capability to gather the data needed to produce a
 
The 'init' program in Android has built-in capability to gather the data needed to produce a
bootchart image. See README.BOOTCHART in the directory system/core/init directory of the Android
+
bootchart image. See README.BOOTCHART in the directory system/core/init directory of the Android source repository for details.
source repository for details.
 
  
 
See [[Using Bootchart on Android]]
 
See [[Using Bootchart on Android]]
  
 
=== Benchmarks ===
 
=== Benchmarks ===
Apparently, [http://code.google.com/p/droidbench/ droidbench] is an integrated benchmark program
+
Apparently, [http://code.google.com/p/droidbench/ droidbench] is an integrated benchmark program for Android.
for Android.
 
  
 
== Development Tips ==
 
== Development Tips ==
=== debugging tips ===
+
=== Debugging tips ===
Quick stack dump - "kill -3 <pid>" will put a stack trace in the log file. (Use logcat to view)
+
Quick stack dump - "kill -3 <pid>" will put a stack trace in the log file. (Use logcat to view.)
  
 
=== Using 'fastboot' ===
 
=== Using 'fastboot' ===
Line 127: Line 112:
  
 
==== Tutorial on Android flash partitions ====
 
==== Tutorial on Android flash partitions ====
[http://forum.xda-developers.com/showthread.php?t=443994 HOWTO: Unpack, Edit, and Repack Boot Images] is a
+
[http://forum.xda-developers.com/showthread.php?t=443994 HOWTO: Unpack, Edit, and Repack Boot Images] is a good tutorial on Android Flash partitions.  
good tutorial on Android Flash partitions.  
 
  
 
==== Nandroid (NAND flash backup tool) ====
 
==== Nandroid (NAND flash backup tool) ====
[http://www.gotontheinter.net/content/nandroid-v20-full-nand-backup-and-restore-tool Nandroid] is a tool for
+
[http://www.gotontheinter.net/content/nandroid-v20-full-nand-backup-and-restore-tool Nandroid] is a tool for backing up your flash partitions. It is highly recommended if you plan to experiment with customizations of your system software. It requires BusyBox (not in regular Android images).
backing up your flash partitions. It is highly recommended if you plan to experiment with customizations
 
of your system software. It requires busybox (not in regular Android images).
 
  
 
=== Compiling native applications ===
 
=== Compiling native applications ===
Because Android uses its own C library (not glibc), it is tricky to compile native applications
+
Because Android uses its own C library (not glibc), it is tricky to compile native applications for.
for.
 
  
 
Here are some instructions for compiling "hello world" on the android phone:
 
Here are some instructions for compiling "hello world" on the android phone:
Line 152: Line 133:
  
 
== Android taxonomy ==
 
== Android taxonomy ==
The information in this wiki needs to be better organized. So I'm creating a taxonomy of topics for Android system
+
The information in this wiki needs to be better organized. So I'm creating a taxonomy of topics for Android system developers. ''NOTE: This section of the page is a work-in-progress.''
developers. ''NOTE: This section of the page is a work-in-progress.''
 
  
 
Categories of information:
 
Categories of information:
* kernel
+
* Kernel
** where to obtain
+
** Where to obtain
** how to install on phone, on emulator
+
** How to install on phone, on emulator
** what version to use
+
** What version to use
** kernel features unique to android (wakelocks, binder)
+
** Kernel features unique to android (wakelocks, binder)
** code names for boards/hardware
+
** Code names for boards/hardware
 
** msm bsp highlights
 
** msm bsp highlights
 
* libc (bionic)
 
* libc (bionic)
** where to obtain
+
** Where to obtain
** license
+
** License
** how to install on phone, on emulator
+
** How to install on phone, on emulator
** comparison with glibc, uclibc
+
** Comparison with glibc, uclibc
* development tools
+
* Development tools
** eclipse (I still can't use it)
+
** Eclipse (I still can't use it)
 
** traceview
 
** traceview
** logs
+
** Logs
** toolchains
+
** Toolchains
** repo
+
** Repository
** adb, android, emulator
+
** ADB, Android, emulator
* booting
+
* Booting
** boot sequences (firmware, partitions, images)
+
** Boot sequences (firmware, partitions, images)
** bootchart
+
** Bootchart
** init
+
** Init
* power management
+
* Power management
** wakelocks
+
** Wakelocks
** application life-cycle
+
** Application life-cycle
* memory usage
+
* Memory usage
** oom killer, app life-cycle
+
** OOM Killer, app life-cycle
** smem reports
+
** Smem reports
** dalvik memory usage
+
** Dalvik memory usage
* security
+
* Security
* programs (outside scope of this wiki - many other resources available)
+
* Programs (outside scope of this wiki - many other resources available)
 
** API
 
** API
** toolchains
+
** Toolchains
** dalvik
+
** Dalvik
 
** DEX
 
** DEX
 
** JNI
 
** JNI
* scripts
+
* Scripts
 
** ASE
 
** ASE
* hardware
+
* Hardware
* distros
+
* Distributions

Latest revision as of 13:25, 21 January 2017


Here are some miscellaneous notes on Android. See Android on OMAP, too.


Technical information about system components

OOM Killer information

Google (Android) developer Arve Hjonevag added a lowmemorykiller feature to the staging area of the Linux kernel in January of 2009. This feature tries to reclaim memory before the system runs out (acting as a kind of cache manager, according to Arve). In Linus' 2.6.28-rc tree this appears in

Application lifecycle (and activity states) can be found here: http://code.google.com/android/intro/lifecycle.html

Applications in different stages of their lifecycle receive a different oom_adj value, which affects the probability of their being reaped.

Initial lowmemorykiller thresholds are set by /etc/init.rc (with the following lines):

# Define the oom_adj values for the classes of processes that can be killed by the kernel. These are used in ActivityManagerService.
   setprop ro.FOREGROUND_APP_ADJ 0
   setprop ro.VISIBLE_APP_ADJ 1
   setprop ro.SECONDARY_SERVER_ADJ 2
   setprop ro.HIDDEN_APP_MIN_ADJ 7
   setprop ro.CONTENT_PROVIDER_ADJ 14
   setprop ro.EMPTY_APP_ADJ 15

# Define the memory thresholds at which the above process classes will be killed. These numbers are in pages (4k).
   setprop ro.FOREGROUND_APP_MEM 1536
   setprop ro.VISIBLE_APP_MEM 2048
   setprop ro.SECONDARY_SERVER_MEM 4096
   setprop ro.HIDDEN_APP_MEM 5120
   setprop ro.CONTENT_PROVIDER_MEM 5632
   setprop ro.EMPTY_APP_MEM 6144

# Write value must be consistent with the above properties.
   write /sys/module/lowmemorykiller/parameters/adj 0,1,2,7,14,15

   write /proc/sys/vm/overcommit_memory 1
   write /sys/module/lowmemorykiller/parameters/minfree 1536,2048,4096,5120,5632,6144

   class_start default

   # Set init its forked children's oom_adj.
   write /proc/1/oom_adj -16

Routines to actually calculate the oom_adj value, based on application state are in the ActivityManager. See frameworks/base/services/java/com/android/server/am/ActivityManagerService.java:UpdateOomAdjLocked(...) and frameworks/base/core/java/android/os/Process.java:setOomAdj() => frameworks/base/core/jni/android_util_Process.cpp:android_os_Process_setOomAdj()

Init sequence

See this blog entry for an overview of the sequence of operations performed by the 'init' program: http://blog.csdn.net/loughsky/archive/2008/11/13/3293922.aspx

Kernel power management

See http://mjg59.livejournal.com/100221.html for a discussion of kernel modifications to support power management.

(Also, the comments are interesting, in that they delve into (and speculate about) the possible history of certain components of the Android system.)

C Library (bionic) information

See the excellent article: http://codingrelic.geekhold.com/2008/11/six-million-dollar-libc.html for an overview of bionic.

From: http://discuz-android.blogspot.com/2008/10/google-android-native-libc-bionic.html

Google developed a custom library for the C compiler (libc) called Bionic. This was necessary for three main reasons:

  • License: they wanted to keep GPL out of user-space. Bionic code uses the BSD license.
  • Size: the library has to be loaded in each process, so it needs to be small. Bionic is about 200K, or half the size of glibc (the GNU version of libc).
  • Speed: limited CPU power means it needs to be fast. Bionic has a small size and fast code paths, including a very fast and small custom pthread implementation.

Bionic has built-in support for important Android-specific services such as system properties and logging. It doesn’t support certain POSIX features, like C++ exceptions and wide chars, which were not needed on Android. Thus it’s not quite compatible with the GNU libc. All native code must be compiled against bionic, not glibc.

Threads

The bionic C library has its own thread API, not the same as either original LinuxThreads or NPTL.

From: http://www.mail-archive.com/uclibc@uclibc.org/msg02787.html

Android thread library (from bionic libc) is minimalist:
 - Uses most of the Linux kernel thread features (futex, CLONE_THREAD)
 unlike old linuxpthread.
- It doesn't support thread cancellation (see CAVEATS file)
- It misses some pthread functions: for example, no barrier, missing 
*timedwait variant, etc.
- Very basic GDB support that only works through gdbserver
- Some theoretic race

Development Tools

BusyBox

Android ships with a utility suite (called 'toolbox') that is not BusyBox.

You can get a binary BusyBox for Android here. The site includes instructions for easy installation on your device.

Bootchart

The 'init' program in Android has built-in capability to gather the data needed to produce a bootchart image. See README.BOOTCHART in the directory system/core/init directory of the Android source repository for details.

See Using Bootchart on Android

Benchmarks

Apparently, droidbench is an integrated benchmark program for Android.

Development Tips

Debugging tips

Quick stack dump - "kill -3 <pid>" will put a stack trace in the log file. (Use logcat to view.)

Using 'fastboot'

Re-flashing a device

See How (not) to brick the Android Developer Phone, Feb. 2009, LWN.net

Android 101 by Haykuro - describes basic re-flashing of a G1 (so has some non-ADP1 information).

Tutorial on Android flash partitions

HOWTO: Unpack, Edit, and Repack Boot Images is a good tutorial on Android Flash partitions.

Nandroid (NAND flash backup tool)

Nandroid is a tool for backing up your flash partitions. It is highly recommended if you plan to experiment with customizations of your system software. It requires BusyBox (not in regular Android images).

Compiling native applications

Because Android uses its own C library (not glibc), it is tricky to compile native applications for.

Here are some instructions for compiling "hello world" on the android phone: http://android-tricks.blogspot.com/2009/02/hello-world-c-program-on-using-android.html

Building

See Updating and rebuilding Android LWN.net, May 2009

Building the kernel from scratch

See http://honeypod.blogspot.com/2007/12/compile-android-kernel-from-source.html

Organizations

Android taxonomy

The information in this wiki needs to be better organized. So I'm creating a taxonomy of topics for Android system developers. NOTE: This section of the page is a work-in-progress.

Categories of information:

  • Kernel
    • Where to obtain
    • How to install on phone, on emulator
    • What version to use
    • Kernel features unique to android (wakelocks, binder)
    • Code names for boards/hardware
    • msm bsp highlights
  • libc (bionic)
    • Where to obtain
    • License
    • How to install on phone, on emulator
    • Comparison with glibc, uclibc
  • Development tools
    • Eclipse (I still can't use it)
    • traceview
    • Logs
    • Toolchains
    • Repository
    • ADB, Android, emulator
  • Booting
    • Boot sequences (firmware, partitions, images)
    • Bootchart
    • Init
  • Power management
    • Wakelocks
    • Application life-cycle
  • Memory usage
    • OOM Killer, app life-cycle
    • Smem reports
    • Dalvik memory usage
  • Security
  • Programs (outside scope of this wiki - many other resources available)
    • API
    • Toolchains
    • Dalvik
    • DEX
    • JNI
  • Scripts
    • ASE
  • Hardware
  • Distributions