Sunday, 3 July 2016

Android Run Time(ART) - Comparison with Dalvik Virtual Machine

   ART is the application run time environment for the Android mobile operating system. It has been introduced experimentally along side of Dalvik Virtual Machine(Process virtual machine used in Android before Kitkat version) in Android 4.4 (Kitkat) but later replaced Dalvik Virtual Machine(DVM) to become only run time environment in Android 4.5 (Lollipop). Some of major changes introduced in ART compared to its predecessor DVM is Ahead of Time(AOT) compilation, improvements in garbage collection, development and debugging improvements and certain profiling improvements.

   All Android applications are available in APK(.apk) format. The byte code that Android environment uses is DEX(.dex called dalvik executable code). To maintain backward compatibility, ART uses the same input bytecode as DVM, supplied through standard DEX files, as part of APK files, while the .odex(Optimised DEX) files are replaced with Executable and Linkable Format (ELF) executables. Once an application is compiled by using ART’s on-device dex2oat utility, it run solely from the compiled ELF executable. Following Image shows the life of an APK file in Android environment(with ART) before it is getting installed. When you try to install an Android application on the device the .apk is first depackaged to the DEX files, resources(XMLfiles) and native code. The DEX files are fed to the dex2oat tool where it will compile from DEX to ELF(.elf). Then ELF files is fed to the ART along with the resources and native code which translates the code to the native instructions.

Image: Life of Android Application file(APK) in Android environment during installation time. (Image courtesy:http://anandtech.com/show/8231/a-closer-look-at-android-runtime-art-in-android-l)

   Major improvement in ART in relative to Dalvik Virtual Machine(DVM) is the introduction of Ahead Of Time(AOT) compilation. With Dalvik’s JIT(Just-In-Time) compilation, each time when an application will run, it dynamically translates a part of dalvik code in to machine code. As the execution progress more byte code is compiled and cached. That is, whenever an app is relaunched with DVM you have to compile the entire code again in order to run the application. The involvement of CPU in each time compilation will increase the app launch time and decreases the battery life time. So to increase the performance and battery time ART comes with Ahead of Time(AOT) Compilation. With AOT compilation in ART, during the app installation phase, it statically translates the DEX code into machine code and stores it on the device storage. Thus, launching of application requires only to bring the binary code to memory with out the need of recompilation. Thus, ART increases the performance and battery life. Though AOT compilation increases the performance and battery life, it increases the installation time of application and demands the more storage on system.

Android Architecture - High level components

      I have worked on a project as part of my Masters thesis work that requires decent level of study on Android Operating System. As part this, I did a detailed study on Android OS internals. This post talks about the high level architecture of Android OS.

   Android is an open source operating system for mobile devices, originally developed by Android Inc., currently under development by Google Inc. along with Open Handset Alliance. Android is intended to be a complete software stack that includes every thing from the operating system through middleware layer and finally the applications. Android uses the Linux kernel at the lowest level though its not the standard Linux kernel.

Software layers of Android
Android architecture can be described as five layers
1. Androidized Linux kernel
2. Native Libraries
3. Android run time(ART)
4. Application framework
5. Applications

1. Androidized Linux kernel
   The lowest layer that performs all the basic system functionality like process management, memory management and device management is the Linux kernel. Android is based on Linux kernel but the Linux kernel used here is not the standard one. The Linux kernel in Android contain several hundred patches over the standard kernel, often to provide certain device-specific functionality, fixes, and enhancements to above layers. Thus some people call it as androidized Linux kernel. There is no glibc, instead it uses its own C library called Bionic libc. It does not support windowing system, bash shell and busy box. Significant enhancements in this context includes wavelock mechanism(a memory management mechanism that is more aggressive in preserving memory), low memory killer, binder IPC mechanism(an IPC mechanism that allows the processes to communicate with one another), ashmem(anonymous shared memory), logger. Most of these enhancements are extended through drivers. The security model of android will heavily depends on the security model followed at this kernel level.










Image: Android architecture - The purple colored layer is written in Java and green colored layer is written C and C++.

2. Native libraries and daemons
   The next layer above the kernel is the Android native libraries, daemons and services. This layer enables the device to handle different types of data. These libraries are written in C or C++ language and are specific for a particular hardware. It provides different abstractions to the layer above it. To provide uniform interface for all the devices, Android system expects the device vendors to implement certain hardware abstractions over the device drivers that collectively form the hardware abstraction layer. This layer includes many of the open source project libraries such as Bionic libc, OpenGL, webkit browser, SQLite Database mechanism, native servers like surface flinger, audio flinger and LLVM tools etc.

3. Run Time Environment
   It is the application run time environment where the compilation and execution of applications happens. It contains all compilation and optimization tools such as java compiler, dex tool, dex2opt tool and java libraries. I will present more detailed explanation about the android run time(ART) in my next blog.

4. Application Framework
   This acts as an API to the Android application developers so that applications can interact directly with the API. This framework will take care of the launching and shutting down the applications. The framework contains the managers and libraries that are high level abstractions above the native library. These programs manage the basic functions like resource management, voice call management etc. Important blocks of this framework includes content manager, activity manager, resource manager and location manager.

5. Applications
   The highest layer of the Android stack is Android applications. Android applications are available in APK(.apk) files. APK is a package of DEX files, XML files and some AIDL fies. Here, DEX is the byte code that ART will interpret and execute, XML files like Manifest.xml describe the starting point of the application and provides the permission details needed to interact with the other applications, and AIDL is the interface description language through which developer can define the programming interface so that both the client and service agree upon the communication between the two using IPC. The programming language used for android application development is the java language. These java source files are given to the java compiler to generate the .class files and these .class files will be merged and translated to DEX code by dex tool of run time. And finally these DEX files along with some application resources will be bundled to form an Android Application Package(APK) file.

Saturday, 2 July 2016

Thinking about Masters

“You can’t connect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future. You have to trust in something — your gut, destiny, life, karma, whatever. This approach has never let me down, and it has made all the difference in my life.”                    - Steve Jobs

   It was 2011 when I was first thinking of GATE as an option to getting into IIT's. As like many other engineers in our country, I did come from a tier-3 engineering college where getting a job means getting into TCS/Infosys/Wipro.

   I had  never tried for IIT during my 12th standard, but based on the reputation of IIT's I always had a dream of stepping into IIT's, not to study, but atleast to see how to study. For me, it looks like IIT's are in a different world where every student tries to work hard and tries their best, if not, tries to be of themselves.   

   It was my brother who encouraged me to go for Masters.When I started thinking about the Masters first, I had three options in my mind.

1.  Crack CAT exam and get into IIM or any other well known management school.
2.  Crack GRE and go for MS in USA.
3.  Crack GATE exam and get into IIT/NIT's.

   I did a lot of survey from my fellow mates and seniors who had gone through this phase before making up my mind. Cracking the CAT exam needs alot of verbal communication skills. Being a Telugu medium student till my Xth standard, never had confidence of cracking exams that requires good english communication skills. So, I eliminated the option of preparing for CAT from my list since I thought it never suits me :P

   Cracking GRE requires less effort than CAT exam. But, it also needs some sort of verbal communication skills which am badly lacking at the moment. Basically, I'm from a poor family.  So, my family cannot afford the expenses to go for MS in foreign university. So, there was no reason for me to think about MS ;)

   Finally, I had left with the option of cracking GATE exam. GATE needs basic computer science fundamentals along with bit of logical reasoning. From the initial days, I was very strong in my CS fundamentals and I had a gut feeling that I can be better if I can work on something I was good at. And at the time, I was also thinking that GATE is the only short cut way of getting a better product based company job. Considering all the ways, I have choosen GATE as my future, career and started my steps towards it from my second year of under-graduation.

   I'm aware of the fact that there will be alot of competition for GATE. But, competition will come into the picture only when you are bad at it or feel it ;) I know I have to be a little bit smart to get good rank/score in GATE. When you choose something that you are left with, you have to give your 100%, if not, more than that ;)