Saturday, January 12, 2013

Using Kernel-based Virtual Machine For Android AVD Development.

*Using KVM Virtualizations to speed Android AVDs with host GPU support!*

How to considerable speed up Android Virtual Devices-AVD performance in a Linux environment, for my case using Fedora 17. However these methods should translate to any Linux distro, in fact most of the instructions below were taken from https://www.youtube.com/watch?v=AZrdBIrRnsQ with some slight changes to support Fedora caveats.

So let get to it, anybody who has ever done some Android development using the SDK software for creating and running Android Virtual Devices-"AVD" for some purpose or another is painfully aware that the AVD devices are not fast as physical Android devices.  Here are just some of the reasons why this is so, but first some basic information/overview of what is happening when an application is run in an android emulator. Lets investigate the reason why despite running the AVD on your high performance computer, its still is well quite slow. All programs running in Android run in a virtual machine*(Not quite true some code compile using the NDK does run nativaley on the ARM or Atom CPU but I will address this later on in the article.) Android apps in a virtual machine-VM, called Dalvik, Java source code is compiled to Java Virtual Machine-JVM class files(bytecode files), which are then optimized even more for execution in Dalvik, these files are called dex files. OK so initially android SDK supported only one type CPU, the ARM CPU later addition support for ARM-7 was added, now recently support for the Intel Atom was added. The Intel Atom CPU support is key for this performance enhancement, for we will run the Dalvik machine VM natively on our computers CPU(s) in addition to this the host GPU will be used/available to the AVD.

Summary of steps to perform to virtualization  AVD
(1) First lets determine if your computer have the right type of CPU(s) to even do KVM. If not, then your done now that was easy. ;-)

(2) Enabling your system BIOS to enable virtualization this usually means setting two options. Note: Since this is BIOS specific you are own your own. But I'll still try to help you anyway.

(3) Using yum to install/configure the appropriate virtualization software packages. You do know how to use yum? Will have to reboot your Linux box after this step.

(4) Checking that you've installed/configured virtualization. Basically did you mess up step 3.

(5) Downloading the proper Android packages, using Android SDK Manager.

(6) Creating an AVD using the Intel atom CPU and host GPU support and how to launch the device.

(7) Lets take this baby for a test ride, and start up the stock browser viewing HD videos in an AVD, no its not madness, at least not now.

(8) Some obvious thoughts, that during debugging delirium might be forgotten/overlooked.


Step 1:
     Open up a shell window and type the following command.  egrep -c '(vmx|svm)' /proc/cpuinfo  If you get a number that is not 0 then your CPU(s) support virtualization and your good to go.

Step 2:
     Reboot your computer and go into the BIOS menu, this usually mean pressing one of the appropriate F-keys, for example F2 or F7 of F11 or something along those lines, during the Power on self test sequence. If you don't know which
     key to press then check your computer user manual. *smirk* Once in the BIOS menu, your looking for two options to enable-, they are usually located in the security group of the BIOS section. If you see the following options.

  • Virtualization 
  • VT for Direct I/O

then enable both options.

Step 3: 
          Perform the following commands to install the require packages.
            su -c 'yum install  qemu-kvm libvirt-daemon' 
           yum should then pull in and install the necessary additional required packages.
           restart your Linux box.

Step 4:
          Perform the following command to see if the libvirt daemon has started up.
              sudo virsh -c qemu:///system list

If you see something like this then so far everything is ok.

 Id    Name                           State
----------------------------------------------------

Step 5: 

You want to download the Intel x86 Atom System image, so if the Status of this Packages are Not installed then click on the installation box and install them, both for API 16 and API 10.
   
      Step 6:
               Creating  an appropriate AVD, start the Android SDK manager, and select  Tools -> Manage  AVDs -> Device Definitions-> Nexus 7  by Google.
 Click on  Create AVD. Use the default name or give it what ever name you want to, for this test case  I am going to set the name to WMD (done for convince of using shorter name for easier command line launch.),  on the CPU/ABI: option select  Intel Atom x86, For the Emulator Options: Enable Use Host GPU. Select OK button and create the AVD.  The creation page should look something like this.


     Step 7:
To start the AVD open up a terminal window and type the following command.

emulator-x86  -avd WMD -gpu on -partition-size 1027  -qemu -m  1027  -enable-kvm 

Who might get the following error messages, depending on your system setup.


emulator: ERROR: Could not load OpenGLES emulation library: libOpenglRender.so: cannot open shared object file: No such file or directory
emulator: WARNING: Could not initialize OpenglES emulation, using software renderer.

Basically the emulator is trying to libOpenglRender.so shared library that is on the host system. 
Use the command locate OpenglRender to find where these files are located. Then create the appropriate config file in the /etc/ld.so.conf.d/ directory, for example my config file is named android.conf  looks something like this

# Note: location of libOpenglRender.so and some other shared libraries
/opt/android-sdk-linux_x86/tools/lib

After you create this config file you have to run the ldconfig command, don't forget the config file has to have the .conf extension in order to work. After performing these actions start up the AVD emulator again, everything should work fine now. For comparisons sake feel free to view a video using the stock android browser, the performance difference between your kvm gpu supported AVD and a regular AVD is so very much apparent
Step 8: 
        If you are developing using NDK then obviously your installed shared libraries on your emulator has to be targeted for the Intel Atom processor, when you release your app then you would had to also compiled an ARM version of the libraries.

Perhaps as a follow up to this blog I will post some profiling data between a kvm AVD and a regular AVD. I'm thinking that maybe the fairest way to perform this test, is to have both AVDs simultaneously running the app "System Test" by the developer TACOTY CN.  If anybody has any advice,hints about the best way to go about device comparison testing for Android devices any constructive feed back would be greatly appreciated.