Friday, October 27, 2017

Build OpenCV for Android with LLVM C++ Runtime

According to the Android NDK C++ Library Support, LLVM's libc++ will be the only STL:
LLVM's libc++ is the C++ standard library that has been used by the Android OS since Lollipop, and in the future will be the only STL available in the NDK.Until NDK r16, the NDK's libc++ is only of beta quality. Beginning with NDK r16, libc++ will be the preferred STL. A future NDK release will remove the other options.
If you are working with libraries linked with gnustl, you may have big problems.
In this post, I introduce how I compile the OpenCV for Android with LLVM libc++ runtime.

Install the cmake and cmake-gui

1. Dont install by sudo apt-get install (or update) cmake. Depends on the different Ubuntu version, you may not get the latest cmake installed.
2. Uninstall the existing cmake: sudo apt-get purge cmake
3. Download pre-compiled cmake package: Latest Release (3.9.4)
4. Install the cmake (cmake-gui is also included)

Download the OpenCV source

OpenCV is available from github.
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git (Optional)

Cmake configuration

1. Start cmake-gui
2. Click the "Browse Source" button and give the OpenCV source folder
3. Click the "Browse Build" button and give the build folder (e.g. create a folder: opencv_android_build)
4. Click "Configure" button, select "Specify toolchain file for cross-compiling", in the next prompt window, make sure to select the "android.toolchain.cmake" under HOME_NDK/build/cmake/. Make sure not to use the file under opencv folder!!!
5. Click "Add Entry" button, add ANDROID_PLATFORM as String with value android-21
6. Click "Add Entry" button, add ANDROID_STL as String with value c++_static
7. Change the existing item CMAKE_BUILD_TYPE to Release
8. Change the existing item CMAKE_CXX_STANDARD_LIBRARIES to libc++_static.a from  libgnustl_static.a (under HOME_NDK/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/)
9. Click "Configure" button to configure again
10. Click "Generate" button to generate configuration
11. Go to the build folder (e.g. opencv_android_build), run make -j to make the targets
12. Run make install, the final files will be found under install folder.

Good luck!

Monday, November 30, 2015

Android smart phone with longer battery life (2)

This time, let's start from the healthd battery health monitor service start entrance. It is the healthd.cpp file from https://github.com/android/platform_system_core/blob/master/healthd/healthd.cpp, here is the code snippets of main(...):
int main(int argc, char **argv) {
    int ch;
    int ret;

    klog_set_level(KLOG_LEVEL);  // logging into kernel
    healthd_mode_ops = &android_ops;  // setup the callback

    if (!strcmp(basename(argv[0]), "charger")) {
        healthd_mode_ops = &charger_ops;   // setup the charger only callback
    } else {
        while ((ch = getopt(argc, argv, "cr")) != -1) {
            switch (ch) {
            case 'c':
                healthd_mode_ops = &charger_ops;
                break;
            case 'r':
                healthd_mode_ops = &recovery_ops; // setup recovery mode callback
The global variable healthd_mode_ops is an instance of struct healthd_mode_ops. It is to register the callbacks of battery monitor initialization and operations of update. The service has three different callbacks for normal bootup, recovery mode, and charger only mode respectively.
struct healthd_mode_ops {
    void (*init)(struct healthd_config *config);
    int (*preparetowait)(void);
    void (*heartbeat)(void);
    void (*battery_update)(struct android::BatteryProperties *props);
};
Here is the normal mode callbacks:
static struct healthd_mode_ops android_ops = {
    .init = healthd_mode_android_init,
    .preparetowait = healthd_mode_android_preparetowait,
    .heartbeat = healthd_mode_nop_heartbeat,
    .battery_update = healthd_mode_android_battery_update,
};
The callback functions are implemented in file: https://github.com/android/platform_system_core/blob/2655256570b7c1c5af6d886735835eecb99f45f2/healthd/healthd_mode_android.cpp. We will re-visit the file in details a little later.
After the callback installation, healthd_init() and an endless loop healthd_mainloop() is followed.
    ret = healthd_init();
    if (ret) {
        KLOG_ERROR("Initialization failed, exiting\n");
        exit(2);
    }

    healthd_mainloop();
And here is the healthd_init(), which finishes the following:
  • create an epoll instance, it is a very scalable I/O event notification mechanism which is in here to monitor the kernel uevent updates, binder object, wakeup alarm timer etc.
  • call the registered init callback of healthd_mode_ops. We will re-visit later.
  • healthd_board_init(...), which actually does nothing here.
  • setup a wakeup alarm timer, add the timer file descriptor to be monitored by the epoll object. By default the fast timer internal is set.
  • setup a uevent, and add the uevent file descriptor to be monitored by the epoll object. This is here to get the battery subsystem updates from kernel.
  • create the BatteryMonitor object, and get BatteryMonitor initialized. The BatteryMonitor initialization has been visited in part I.
static int healthd_init() {
    epollfd = epoll_create(MAX_EPOLL_EVENTS);
    if (epollfd == -1) {
        KLOG_ERROR(LOG_TAG,
                   "epoll_create failed; errno=%d\n",
                   errno);
        return -1;
    }

    healthd_mode_ops->init(&healthd_config);
    healthd_board_init(&healthd_config);
    wakealarm_init();
    uevent_init();
    gBatteryMonitor = new BatteryMonitor();
    gBatteryMonitor->init(&healthd_config);
Then it is an endless loop healthd_mainloop(). That is how the battery health state is get monitored continuously and endlessly. It waits on the epoll object, once any event happens the event callback will be called, which could be one or several of the registered events.
static void healthd_mainloop(void) {
    while (1) {
        struct epoll_event events[eventct];
        int nevents;
        int timeout = awake_poll_interval;
        int mode_timeout;

        mode_timeout = healthd_mode_ops->preparetowait();
        if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout))
            timeout = mode_timeout;
        nevents = epoll_wait(epollfd, events, eventct, timeout);

        if (nevents == -1) {
            if (errno == EINTR)
                continue;
            KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
            break;
        }

        for (int n = 0; n < nevents; ++n) {
            if (events[n].data.ptr)
                (*(void (*)(int))events[n].data.ptr)(events[n].events);
        }

        if (!nevents)
            periodic_chores();

        healthd_mode_ops->heartbeat();
    }

More details about callbacks of healthd_mode_android.cpp

Let's look into more details about the callbacks of file: https://github.com/android/platform_system_core/blob/2655256570b7c1c5af6d886735835eecb99f45f2/healthd/healthd_mode_android.cpp.
void healthd_mode_android_init(struct healthd_config* /*config*/) is the initialize function, it creates a binder object, add the binder object into the epoll polling list and install the callback for the operation when the binder object is polled by the epoll.
void healthd_mode_android_init(struct healthd_config* /*config*/) {
    ProcessState::self()->setThreadPoolMaxThreadCount(0);
    IPCThreadState::self()->disableBackgroundScheduling(true);
    IPCThreadState::self()->setupPolling(&gBinderFd);

    if (gBinderFd >= 0) {
        if (healthd_register_event(gBinderFd, binder_event))
            KLOG_ERROR(LOG_TAG,
                       "Register for binder events failed\n");
    }
The installed epoll event callback for the binder is to process the binder commands:
static void binder_event(uint32_t /*epevents*/) {
    IPCThreadState::self()->handlePolledCommands();
}
Finally the initialize register self as a service called "batteryproperties" to the android service manager. The android Java space can listen to the service to get the battery health state updates. Use the command: service list, you shall see the service "batteryproperties" listed.
    gBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar();
    gBatteryPropertiesRegistrar->publish();
}
void healthd_mode_android_battery_update(...) is the update callback. It is to notify all the registered service clients when the battery health state gets updated. It is the service listener's implementation of how to handle the battery state updates.
void healthd_mode_android_battery_update(
    struct android::BatteryProperties *props) {
    if (gBatteryPropertiesRegistrar != NULL)
        gBatteryPropertiesRegistrar->notifyListeners(*props);

    return;
}

Callbacks of other two epoll objects

The uevent callback receives the kernel uevent updates, then parse the power_supply events, and reads the sysfs power_supply updates.
static void uevent_event(uint32_t /*epevents*/) {
    char msg[UEVENT_MSG_LEN+2];
    char *cp;
    int n;

    n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
    if (n <= 0)
        return;
    if (n >= UEVENT_MSG_LEN)   /* overflow -- discard */
        return;

    msg[n] = '\0';
    msg[n+1] = '\0';
    cp = msg;

    while (*cp) {
        if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) { // power_supply only
            healthd_battery_update(); // reads the power_supply sysfs into memory
            break;
        }

        /* advance to after the next \0 */
        while (*cp++)
            ;
    }
}
The wakeup timer object setup the timer to wakeup from kernel sleep and gets the power_supply updates.
static void wakealarm_event(uint32_t /*epevents*/) {
    ... ...
    periodic_chores();
}

static void periodic_chores() {
    healthd_battery_update(); // reads the power_supply sysfs into memory
}
Will illustrate how to get the battery state updates from Java with an sample app in the next article.

Android smart phone with longer battery life

Smart phones have been developed a lot with more cpu cores, more computation capabilities, with larger and brighter screens, with higher resolution cameras and etc. But battery is one of the areas have no breakthrough. Talking to the battery life, because of the faster cpu/gpu, bigger screen, it even gets worse. Do you still remember the last time you got so mad to find a power supply outlet?
Smart phones use Li-ion batteries. Before Li-ion batteries improve the capacity with the technology breakthrough, how to save the battery life becomes very important as we dont have too much to waste. To save the battery life keeping closely monitoring the battery life is the first step.
Start from Android KK (Kitkat) Android adds the system/core/healthd, which is built into part of Android system image as /sbin/healthd, to monitor the battery state. In this article let's take a close look into healthd to understand how can we monitor the battery state.

What does healthd monitor and tell?

First let's take a look at the healthd logs:
<14>[21867.192464] healthd: battery l=78 v=4024 t=18.8 h=2 st=4 c=-239 chg=a
<14>[21867.421852] healthd: battery l=78 v=4067 t=18.8 h=2 st=2 c=5 chg=a
<14>[21867.448320] healthd: battery l=78 v=4067 t=18.8 h=2 st=2 c=5 chg=a
The healthd monitors the battery state of charge and reports 7 outputs. They are battery level (l=78), voltage (v=4024), battery temperature (t=18.8), health state (h=2), battery status (st=4), charging/discharging current (c=-239), and charging status/charger type (chg=a).

  • Battery level: values in 0 ~ 100, means no battery to full battery.
  • Battery voltage: voltage in mV, lower battery level, less voltage.
  • Battery temperature: temperature in Celsius (°C).
  • Health state: values in 1 ~ 7. It is a enumeration with following health state:
    • 1 -> Unknown
    • 2 -> Good
    • 3 -> Overheat
    • 4 -> Dead
    • 5 -> Over voltage
    • 6 -> Unspecified failure
    • 7 -> Cold
  • Battery status: values in 1 ~ 5. It is a enumeration with following battery status:
    • 1 -> Unknown
    • 2 -> Charging
    • 3 -> Discharging
    • 4 -> Not charging
    • 5 -> Full
  • Charging/discharging current: positive value means discharging current in mV, negative value means charging current in mV.
  • Charging status: empty if not on charging. a/u/w otherwise:
    • a: charger AC online
    • u: charger USB online
    • w: charger wireless online

How does healthd read the battery health data?

The battery driver exports the virtual sysfs under /sys/class/power_supply for upper layer to get the battery health data. Let's review the code https://github.com/android/platform_system_core/blob/master/healthd/BatteryMonitor.cpp to understand how healthd gets the battery health data.

BatteryMonitor::init(...) function: open /sys/class/power_supply directory, read and traverse
void BatteryMonitor::init(struct healthd_config *hc) {
    String8 path;
    char pval[PROPERTY_VALUE_MAX];

    mHealthdConfig = hc;
    DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH); /* open folder /sys/class/power_supply */
    if (dir == NULL) {
        KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
    } else {
        struct dirent* entry;

        while ((entry = readdir(dir))) { /* loop and read files in /sys/class/power_supply */
In sysfs folder /sys/class/power_supply, there is a list of power_supply supported by the given smart phone. In each power_supply folder, there are some attributes. One of the attributes every power_supply shall have is the type. Following code snippets check the type of each power_supply to determine the next things to check:
394            // Look for "type" file in each subdirectory
395            path.clear();
396            path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
397            switch(readPowerSupplyType(path)) {
398            case ANDROID_POWER_SUPPLY_TYPE_AC:
399            case ANDROID_POWER_SUPPLY_TYPE_USB:
400            case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
... ...
407            case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
... ...
506            case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
... ...
If power_supply type is other than Battery or Unknown, then power_supply type should be one of the following:
  • ANDROID_POWER_SUPPLY_TYPE_AC
  • ANDROID_POWER_SUPPLY_TYPE_USB
  • ANDROID_POWER_SUPPLY_TYPE_WIRELESS
and /sys/class/power_supply/%power_supply_type%/online will be further checked to see if such power_supply is plugged to charge the given smart phone.
401                path.clear();
402                path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
403                if (access(path.string(), R_OK) == 0)
404                    mChargerNames.add(String8(name));
405                break;
... ...
215    for (i = 0; i < mChargerNames.size(); i++) {
216        String8 path;
217        path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
218                          mChargerNames[i].string());
219
220        if (readFromFile(path, buf, SIZE) > 0) { /* online */
221            if (buf[0] != '0') {
222                path.clear();
223                path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
224                                  mChargerNames[i].string());
225                switch(readPowerSupplyType(path)) {
226                case ANDROID_POWER_SUPPLY_TYPE_AC:
227                    props.chargerAcOnline = true;
228                    break;
229                case ANDROID_POWER_SUPPLY_TYPE_USB:
230                    props.chargerUsbOnline = true;
231                    break;
232                case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
233                    props.chargerWirelessOnline = true;
234                    break;
235                default:
236                    KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
237                                 mChargerNames[i].string());
238                }
239            }
240        }
241    }
If power_supply is Battery, it checks more attributes under sysfs folder /sys/class/power_supply/battery, such as status, health, present, capacity, voltage_now/batt_vol, current_now, current_avg, charge_counter, temp/batt_temp, technology.
Read battery level in percentages and battery voltage in mV:
191    props.batteryLevel = mBatteryFixedCapacity ?
192        mBatteryFixedCapacity :
193        getIntField(mHealthdConfig->batteryCapacityPath);
194    props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Read battery temperature in Celsius (°C):
196    props.batteryTemperature = mBatteryFixedTemperature ?
197        mBatteryFixedTemperature :
198        getIntField(mHealthdConfig->batteryTemperaturePath);
Read battery status:
204    if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
205        props.batteryStatus = getBatteryStatus(buf);
57int BatteryMonitor::getBatteryStatus(const char* status) {
58    int ret;
59    struct sysfsStringEnumMap batteryStatusMap[] = {
60        { "Unknown", BATTERY_STATUS_UNKNOWN },
61        { "Charging", BATTERY_STATUS_CHARGING },
62        { "Discharging", BATTERY_STATUS_DISCHARGING },
63        { "Not charging", BATTERY_STATUS_NOT_CHARGING },
64        { "Full", BATTERY_STATUS_FULL },
65        { NULL, 0 },
66    };
67
68    ret = mapSysfsString(status, batteryStatusMap);
Read battery health state:
207    if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
208        props.batteryHealth = getBatteryHealth(buf);
77int BatteryMonitor::getBatteryHealth(const char* status) {
78    int ret;
79    struct sysfsStringEnumMap batteryHealthMap[] = {
80        { "Unknown", BATTERY_HEALTH_UNKNOWN },
81        { "Good", BATTERY_HEALTH_GOOD },
82        { "Overheat", BATTERY_HEALTH_OVERHEAT },
83        { "Dead", BATTERY_HEALTH_DEAD },
84        { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
85        { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
86        { "Cold", BATTERY_HEALTH_COLD },
87        { NULL, 0 },
88    };
89
90    ret = mapSysfsString(status, batteryHealthMap);
After BatteryMonitor.cpp traverse the sysfs folder /sys/class/power_supply and update the attributes, the following code snippets is to notify the applications who concern the battery health state.
275    healthd_mode_ops->battery_update(&props);
It is a callback function registered at the beginning of the health monitoring service. Have to cut here. Will continue with a new article.

Wednesday, March 28, 2012

memory leak tracking on android, cont

You have Android version >= 2.2.
Make sure you have libc_malloc_debug_leak.so:
  • > adb shell ls /system/lib/libc_malloc_debug_leak.so
Enable the memory leak check:
  • > adb shell setprop libc.debug.malloc 1
Restart the Android framework, make sure the memory leak check is functional:
  • > adb shell stop
  • > adb shell start
  • > adb logcat -b main -v threadtime (you see this line in the printed log: 03-29 03:10:52.942 4200 4200 I libc : /system/bin/logcat using MALLOC_DEBUG = 1 (leak checker))
In your DDMS configuration file (e.g. ~/.android/ddms.cfg on Linux), add "native=true". This enables the Native Heap tab. Now you can use the "Native Heap" tab features to grab snapshots of the heap memory usage. Group by library to view the memory usage in libraries. Show new allocations comparing to the previous snapshot to monitor the memory allocation increments. If you are using the images built by your own, fill the "Symbol Search Path" to get more details information.

More options:
  • > adb shell setprop libc.debug.malloc 5
# For filling allocated /freed memory with patterns defined by CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros.
  • > adb shell setprop libc.debug.malloc 10
# For adding pre-, and post- allocation stubs in order to detect buffer overruns.

Monday, September 26, 2011

Android sh script, why failed with [ not found

A simple sh script, but run failure with error: [ not found.
#!/system/bin/sh
if [ $? eq 0 ]; then
    echo "Command run successful"
fi
The same script can run well on Linux.
I dont know why, but I finally got a solution.
Upload a busybox, change it to executable permissions, create a soft link "[" to busybox. Like the following:
-rwxr-xr-x 500      500       1877260 2011-09-22 02:42 busybox
lrwxrwxrwx root     root              2011-09-26 08:15 [ -> /sbin/busybox
Run the sh script again, which shall give no error any more.
Finally, make sure the searching path $PATH can find command "busybox", and "[".

Thursday, July 14, 2011

Disable Linux kernel version magic check

Linux kernel will refuse to load kernel module if the version magic is not match. It can be easily disabled for some ease.
Find linux kernel kernel/module.c, find "check_modinfo" function, comment out "return -ENOEXEC;" for same_magic(...) check function.

How to get a console with root if Motorola XOOM is working as USB Host and connected with USB devices

If Motorola XOOM is connected with USB devices and working as USB Host mode, you will not be able to have adb shell work on PC. The only USB connection is used for other purpose. Here is a tip to have a console with root even the USB connection is used for USB device.
  • Motorola XOOM is rooted
  • Push a busybox to Motorola XOOM, make it executable
  • Connect Motorola XOOM with PC, in a PC cmd window, adb shell to the device, the shell must be a root shell.
  • Start telnetd: busybox telnetd -l /system/bin/sh
  • Download a telnet APP for Motorola XOOM, e.g. BlowTorch. Launch telnet client (BlowTorch), telnet to device, a root console is served.

Wednesday, July 13, 2011

USB Host Mode on Motorola XOOM

Motorola XOOM gets USB host functionality. No software changes, this article shows you how to enable Motorola XOOM the ability of USB host functionality, which grant you the potential to have the ability to read media from external SD card reader, USB HD, USB DVD adapter, USB LAN adapter, and etc. Potentially, Motorola XOOM can work as a PC and access all kinds of external USB devices. Will introduce the software level changes later to enable reading from SD card reader, connect USB LAN, etc.
Motorola XOOM works as a portable device (just like the mobile phone), provides a micro-usb to connect to PC. In this way, PC can access media in the Motorola XOOM. Motorola XOOM works as a USB client.
Motorola XOOM can also work in Host mode, because it has the USB OTG (on-the-go). How to switch the USB OTG on the Motorola XOOM to USB Host mode? It is very simple. You just need need a USB toggle cable. The USB toggle cable looks like the following:

After the Motorola XOOM plugs with this USB toggle cable, it can connect with all kinds of USB devices. To make the USB devices work on Motorola XOOM, linux drivers change may be needed. Will introduce some later.
In the kernel log, following message will be shown after USB toggle cable connected:

Tuesday, July 12, 2011

Step by step to enable LAN access for Motorola XOOM

With the USB toggle cable connected with Motorola XOOM, Motorola XOOM can work with all kinds of USB devices, such as USB LAN, so to connect with Local Area Network.
Pre-requisite:
  • A rooted Motorola XOOM
  • USB toggle cable
  • USB LAN adapter
  • Networking Cat5 cable
USB LAN adapter:
Google USB LAN adapter, there are many USB LAN adapter. A typical USB LAN adapter is shown below.
Networking Cat5 cable:
Not sure why, but looks a straight through Networking Cat5 cable does not work with the above USB LAN adapter. Make sure the cable has one end terminated with T568A scheme, the other end terminated with T568B scheme. Google them for more details.
Kernel driver changes:
  • Get to know the USB LAN adapter device ID and vendor ID. With USB LAN adapter connected with Motorola XOOM through USB toggle cable, find dmesg kernel log with following information. Write down the device ID and vendor ID.
  • Add "CONFIG_USB_NET_DM9601=y" into kernel config file to have drivers/net/usb/dm9601.c compiled into kernel object.
  • Add vendor ID and device ID into drivers/net/usb/dm9601.c, struct usb_device_id structure, referenced by variable products[]. e.g.
  • + {
  • + USB_DEVICE(0x0fe6, 0x9700), /* XXXY */
  • + .driver_info = (unsigned long)&dm9601_info,
  • + },
  • Compile the kernel, and apply new kernel to Motorola XOOM. New kernel will fail to load Wifi module because of un-matched kernel version magic, disable kernel version magic matching will resolve this issue.
DHCP client for USB LAN adapter interface:
Power up Motorola XOOM with Network Cat5 cable connected with Motorola XOOM through USB LAN adapter and USB toggle cable. The connection looks like the following. After XOOM is up, run "dhcpcd eth0" in a console with root. After dhcpcd return, eth0 shall get an IP. Ping command shall work. But browser still not work.
Make XOOM browser work in LAN:
In a console with root, run following commands:
  • getprop dhcp.eth0.dns1 # get dns1
  • getprop dhcp.eth0.dns2 # get dns2
  • setprop net.dns1 dns1
  • setprop net.dns2 dns2
  • Browser shall work now
How to have USB LAN work automatically:
Write a NETLINK socket program to listen all NETLINK socket event, if eth0 interface is UP, run dhcpcd on eth0 and set net.dns1, net.dns2 with dhcp.eth0.dns1 and dhcp.eth0.dns2. If eth0 interface is DOWN, reset those properties.

Friday, August 27, 2010

patch the kernel image boot.img

Tools list:
  1. split_bootimg.pl
  2. mkbootimg
  3. mkbootfs
boot.img contains the linux kernel and the ramdisk, while the default.prop in ramdisk can enable/disable the adb root permission. following is the default.prop contents for a droid product device:
  • ro.secure=1
  • ro.allow.mock.location=0
  • ro.debuggable=0
  • persist.service.adb.enable=0
droid product device with above default.prop has no adb root permissions if the device is not hacked. the default.prop with the following contents has the adb root permissions by default:
  • ro.secure=0
  • ro.allow.mock.location=0
  • ro.debuggable=1
  • persist.service.adb.enable=1
to create a boot.img with such default.prop, here is the steps:
  1. split the boot.img: split_bootimg.pl boot.img, after running the command, two files are generated: boot.img-kernel and boot.img-ramdisk.gz.
  2. create a temp folder (e.g. ramdisk), change to that folder, run the following command to extract the boot.img-ramdisk.gz: cat ../boot.img-ramdisk.gz | gzip -d | cpio -idm.
  3. make the changes to the ramdisk file system in ramdisk folder, e.g. change the default.prop file to get adb root permissions.
  4. return the parent folder, and run the following command to recreate the ramdisk package: mkbootfs ./ramdisk | gzip > ramdisk-new.gz
  5. recreate the boot image: mkbootimg --kernel boot.img-kernel --ramdisk ramdisk-new.gz -o boot-new.img
done. flash the boot-new.img to the device with fastboot.
more things can be done with the above steps, such as change the boot up sequence by modify the init.rc.

Tuesday, August 24, 2010

unyaffs the android image

unyaffs is a tool to extract files from a yaffs file system image. It supports extract images created by mkyaffs2image. It is useful to patch the system.img and re-generate the system.img.
How to get it:
  1. download a pre-build binary for linux: http://unyaffs.googlecode.com/files/unyaffs
  2. download the source and compile with gcc -o unyaffs unyaffs.c:
  • http://unyaffs.googlecode.com/files/unyaffs.c
  • http://unyaffs.googlecode.com/files/unyaffs.h

Thursday, August 6, 2009

Automatic Memory Leak Tracking on Android

Android provides a mechanism to track memory leakage.
In folder bionic/libc/bionic/malloc_leak.c is compiled into /system/lib/libc_debug.so, it is a memory leakage tracking version of libc.so. To enable the memory leakage tracking, replace the /system/lib/libc.so with the /system/lib/libc_debug.so.
> adb shell mv /system/lib/libc.so /system/lib/libc_original.so
> adb shell mv /system/lib/libc_debug.so /system/lib/libc.so
Before doing that, be sure to remount the system partition to 'rw'.
Use 'mv' command may have problem without libc.so, in that case use 'cp' command instead.
With the memory leak tracking, you shall see something like "MALLOC_DEBUG = 1 (leak checker)" in your logcat output.

Thursday, June 11, 2009

android memory usage with hprof

Android provides some mechanisms to generate hprof data:
  1. Dalvik VM will dump hprof file within /data/misc folder if SIGUSR1 and/or SIGQUIT signal is received. Manually send SIGUSR1 to process: kill -10 pid.
  2. The android provides monkey tool (random testing tool), with --hprof option will generate hprof file within /data/misc folder.
  3. In cupcake release, a new API has been introduced to generate a dump programmatically, the static method dumpHprofData(String fileName) of the Debug class. Example: Debug.dumpHprofData("xxx.hprof").
Some tips:
  1. The folder /data/misc shall have 777 permissions.
  2. The hprof file format is heap-dump-tm-pid.hprof-head and heap-dump-tm-pid.hprof if android 1.1 release is used.
  3. The hprof file format is heap-dump-tm-pid.hprof if android 1.5 release is used.
  4. If two files are generated, you need to join the two files into one: type heap-dump-tm-pid.hprof-head > dump.hprof; type heap-dump-tm-pid.hprof >> dump.hprof.
  5. Convert the hprof to standard hprof format: hprofconv dump.hprof out_dump.hprof
  6. Use JProfiler, Eclipse MAT, or other tools to open out_dump.hprof. I tried JProfiler and got the same problems as indicated in http://osdir.com/ml/android-porting/2009-04/msg00597.html.
Some resources:
  1. Eclipse MAT: http://www.eclipse.org/mat/
  2. MAT standalone: MemoryAnalyzer-Incubation-0.7.0.20081210-win32.win32.x86.zip
  3. Hprofconv: http://bigflake.com/HprofConv.c.txt

Monday, June 8, 2009

Android Java Space OOM

The default Android java space heap maximum limitation is 16M. If you are running multimedia applications on android, such as jpg file decoding, video decoding, you may run OOM (Out Of Memory). If memory tuning can not help you on the OOM, you can go to the file: frameworks/base/core/jni/AndroidRuntime.cpp, line 607, change opt.optionString = "-Xmx16m" to opt.optionString = "-Xmx20m" for 20M heap size or even bigger.
The limitation is the upward limitation, it will not actually allocate 20M memory every time in every java application.
Take this solution as the last try, focus on your coding quality first.

Tools to monitor your android system memory and performance

Tools to monitor your android/linux system memory and performance:
  • ps -x
  • top
  • cat /proc/meminfo
  • dumpsys meminfo proc-id
  • procrank
  • DDMS (Android SDK tool)
  • time command-line
  • vmstat

Android java space profiling tool - traceview

Android provides a good java space profiling tool: traceview. Traceview can be found in the same folder as adb in Android SDK package. Usage is quite simple and straightforward:
  1. import android.os.Debug;
  2. Debug.startMethodTracing("tag-name");
  3. Debug.stopMethodTracing();
  4. Run the Java application
  5. Fetch out the profiling output file on SD card (SD card is needed) root folder: tag-name.trace
  6. traceview tag-name.trace to open the traceview window
Insert items 1, 2, 3 into your java source code where you want to do profiling.
For more information about traceview window, refer to http://developer.android.com/guide/developing/tools/traceview.html

Tuesday, May 19, 2009

fdatasync not found in bionic libc

fdatasync(int) declare is found in bionic/libc/include/unistd.h, but it is not actually implemented in libc. I can NOT find the fdatasync symbol in /system/lib/libc.so.

Thursday, February 26, 2009

UI/Application Exerciser Monkey

The Monkey is a program that runs on your emulator or device and generates pseudo-random streams of user events such as clicks, touches, or gestures, as well as a number of system-level events. You can use the Monkey to stress-test applications that you are developing, in a random yet repeatable manner.
Basic usage of monkey:
usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]
[-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]
[--ignore-crashes] [--ignore-timeouts]
[--ignore-security-exceptions] [--monitor-native-crashes]
[--kill-process-after-error] [--hprof]
[--pct-touch PERCENT] [--pct-motion PERCENT]
[--pct-trackball PERCENT] [--pct-syskeys PERCENT]
[--pct-nav PERCENT] [--pct-majornav PERCENT]
[--pct-appswitch PERCENT] [--pct-anyevent PERCENT]
[--wait-dbg] [--dbg-no-events]
[-s SEED] [-v [-v] ...] [--throttle MILLISEC]
COUNT
Example: adb shell monkey 1000
#send 1000 events to all applications

Wednesday, December 10, 2008

Tips to import sample Android projects to Eclipse

I am a newbie to Eclipse and Android applications development. I installed the Eclipse yesterday and tried to take a look at the Android sample to start with. I had problems to import sample project into Eclipse. Google is always the one I first tried. I found this article: How to import sample Android projects to Eclipse. It is helpful to me. I think it will also be helpful to others.
Here is the tips: Simply create project from existing source (then use "Browse" to select sample shipped with SDK).