Intel® System Studio Developer Story : How to configure, build and profile the Linux Kernel of Android by using the VTune

Intel® System Studio Developer Story: How to configure, build, debug and optimize key parts of your mobile software stack for Android*

1. Set-up and configure a development environment.

(1) The target HW environment

   In this article, a Minnow board MAX is used for a HW platform. The MinnowBoard is an Intel® Atom™ processor based board which introduces Intel® Architecture to the small and low cost embedded market for the developer and maker community. It has exceptional performance, flexibility, openness and standards.

Specification Minnow Board Max
CPU64-bit Intel® Atom™ E3815 (Single Core 1.46 GHz or E3825 Dual Core 1.33 GHz
GraphicsIntegrated Intel® HD Graphics
Memory1~2 GB DDR3 RAM
IO

Video : micro HDMI  / Micro SD / SATA2 / USB 3.0 (Host) / USB 2.0 (Host) /  Serial / Ethernet

OSLinux / Yocto Linux / Windows 8.1 / Android 4.4

* Please find more details in officially Minnow homepage : http://www.minnowboard.org/

(2) Software environment

 Host OS : Ubuntu 14.04 LTS / Window 7 64 bits

 IDE : Android Developer Tools / eclipse KEPLER

 Tools : Intel® System Studio 2015

 Target OS: Android 4.2.2

(3) Set up the Android SW development environment

  • Configure a workstation (Ubuntu Linux)    
sudo dpkg --assert-multi-arch
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java6-installer

sudo apt-get install git git-core gnupg flex bison gperf build-essential ccache squashfs-tools zip curl libc6-dev libncurses5-dev x11proto-core-dev g++-multilib mingw32 tofrodos  python-markdown libxml2-utils zlib1g-dev:i386 libx11-dev libreadline6-dev xsltproc

echo 'export USE_CCACHE=1' >> ~/.bashrc
ccache -M 16

mkdir ~ / bin
PATH=~/bin:$PATH
curl http://commondatastorage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
  • Download the code
repo init -u https://github.com/android-ia/platform_manifest.git
repo sync -j4 -q -c --no-clone-bundle
  • Build 
source build/envsetup.sh
lunch
<select minnow-eng>
make -j4

 

  • Make an installer USB memory stick and install it to a micro SD card in the Minnow board

Copy android-ia/out/target/product/minnowboard_max/live.img and make a installer image to an USB memory stick.  In Windows you can make an android installer image by Win32diskimager to an USB memory stick.  And then  Insert an USB memory stick which has an Android installer image into the Minnow board which has a micro SD card. After boot up you can select the installing Android.

  • Connect the Android on the Minnow board to a host machine

After successful boot-up, connect the target via an Ethernet cable. And configure a connection.

adb connect 192.168.42.1
adb shell

 

2. Using a VTune.

The VTune is the performance analysis tool to find a hotspot which may utilize CPUs' inefficiency way both in applications and a system wide.

Links following are the useful documents you can refer for how to find out a hotspot in Android. 

In this article, we are focusing on the profiling an Android system. To profile a system - including Linux Kernel by using the system wide profiling feature of a VTune, some functions which can stop the VTune profiling will be added in the Linux Kernel layer of Android and the user-defined custom analysis feature of a VTune will be introduced as well. For the prerequisite of this kind of analysis,  the compiling an Android and updating a boot image by the fastboot and the logging and some ADB command of an Android are also explained.

(1) The example function of stopping VTune profiling

During poring or working on the Linux kernel layer of an Android, sometimes we need to stop the profiling at the time of any specific event or signal or exceptional cases such as a kernel panic. And we can start analysis the time which is just right after the specific debug point you want to check. And you can accomplish it by sending the QUIT signal to the VTune process which is started when you start analysis in a VTune GUI or a command line. The example function following is to find the VTune process and send QUIT signal to the process to stop profiling.

void stop_vtune_process (void)
{
	struct task_struct *p;
	int j;
	int flasg_to_skip_sh = 0;
	
	for_each_process(p) 	{
		for (j=0;j< (TASK_COMM_LEN-4);j++) {
			if (p->comm[j] == 'a' && p->comm[j+1] == 'm' && p->comm[j+2] == 'p' \
				&& p->comm[j+3] == 'l' && p->comm[j+4] == 'x') {
                                /* found the amplx ... in the process name. */ 
				printk ("[vtune] %d %s \n",task_pid_nr(p),p->comm);
				if (flasg_to_skip_sh) {
					task_lock(p);	
					printk("[vtune]Kill %d(%s)\n",task_pid_nr(p), p->comm);
					task_unlock(p);
					do_send_sig_info(SIGQUIT, SEND_SIG_PRIV, p, false);
					break;
				}
				else	{
					printk("[vtune] skip sh for amplxe\n");
					flasg_to_skip_sh++;
				}
			}
		}
	}
}

You can add and call this kind of function into your Linux kernel source codes such as kernel panic, an USB event function, a Key event function etc., whichever you want to stop profiling and start the analysis by a VTune.

(2) Some useful things - fastboot, ADB, kernel log (Minnow board MAX)

<build source codes>
make -j4

<download  kernel image by fastboot>
adb reboot bootloader
fastboot -t 192.168.42.1 flash boot boot.img
fastboot -t 192.168.42.1 continue

<logging kernel via adb>
adb shell cat /proc/kmsg | grep vtune

(3) Setting and using the VTune for the system profiling

To get the details of system data during profiling, we'd better use an Advanced hotspot with the system wide profile even it has little call stack information, but we can see a system wide processes and functions which is called during profiling.  

  • Project properties - Target type : Profile System 
  • New Analysis - Choose analysis type - advanced hotspot  , collection level : hotspot

<The screen shot of the result - example : Advanced hot spot>

advanced hotspot - system wide profileThis advanced hot spot is using 3 high frequent basic HW events - CPU_CLK_UNHALTED.CORE / CPU_CLK_UNHALTED_REF_TSC / INST_RETIRED_ANY. If your system codes which you want profiling are more delay-critical-codes or you want to use specific HW PMU event, use the custom analysis. Next example is the custom analysis.

  • Project properties - Attach process -select process
  • New Analysis - Custom Analysis - New Hardware Event-based sampling analysis
  • New Hardware Event-based sampling analysis- Edit - add events you want
  • New Hardware Event-based sampling analysis- Edit - Check the collect stacks or Analyze system-wide context switches

<The screen shot of result - example : custom analysis>

Hardware Event-based sampling analysis

 

You can analysis the process working in the time line as the picture above. And if you found any suspicious process which needs more investigation. Change the Vtune - Project  Properties - Target Type - Attach to Process and repeat testing above to do narrow down the issues.

 

For more complete information about compiler optimizations, see our Optimization Notice.