Coscup2011: porting android to brand-new cpu architecture

31
新規CPU アーキテクチャにアンドロイドの移植 PORTING ANDROID TO BRAND-NEW CPU ARCHITECTURE Luse Cheng <[email protected]> August 21, 2011 @ COSCUP

Transcript of Coscup2011: porting android to brand-new cpu architecture

Page 1: Coscup2011: porting android to brand-new cpu architecture

新規CPUアーキテクチャにアンドロイドの移植

PORTING ANDROID TO BRAND-NEW CPU ARCHITECTURE

Luse Cheng <[email protected]> August 21, 2011 @ COSCUP

Page 2: Coscup2011: porting android to brand-new cpu architecture

自我介紹

Experience Compiler Lead at An Embedded CPU IP Company

Compiler Engineer at An Embedded CPU IP Company

Quick summary Full-time Open-Source Developer

Part-time Open-Source Contributor

國民革命軍軍人(DIRDS:國四無雙)

專長: 屠龍之技 (Compiler)

Page 3: Coscup2011: porting android to brand-new cpu architecture

An Embedded CPU IP Company

Andes Technology Corporation

32-bit Embedded CPU IP Company

Brand-new 32/16 mixed ISA

本土 32-bit 嵌入式處理系智財

簡單的說就是台灣的 ARM

或者說 ARM 就是英國 Andes (笑)

EEtimes 的 scouting report

Taiwan nurtures the next ARM

Page 4: Coscup2011: porting android to brand-new cpu architecture

About This Session

Android = Armdroid

Android is arm-awareness product

Porting to non-ARM platform

x86 in not included: Android-x86 is very good project

x86 is a faster CPU than ARM

It’s good reference for us

Reference platform at this session

Andes architecture

Page 5: Coscup2011: porting android to brand-new cpu architecture

Agenda

Brand new CPU architecture?

Platform quick-review

OK, let go !

Pre-requirement, bring up

Life is not fair, get used to it

Android porting guideline

How do you do your best?

Optimization

Page 6: Coscup2011: porting android to brand-new cpu architecture

Brand new CPU architecture?

Andes ADP-AG102

N1233-F * 2 (533 MHz)

With Single/Double FPU

With SW cache-coherence

RAM: DDR-II 512 MB

GPU: XG22 2D-Only GPU

Output: D-SUB

800x600

ROOTFS: SDCARD

USB Keyboard, Mouse

Page 7: Coscup2011: porting android to brand-new cpu architecture

Target Android Version

We use Froyo with tag 2.2.1

Why Froyo?

Andes BSP: Linux kernel 2.6.32

ADP-AG102 lack of 3D GPU

Can use over 80% Android AP

Why tag 2.2.1?

Can use 32-bit host

From: Wikipedia (Android)

Version API  level Distribution

3.x.x Honeycomb 11-13 0.9%

2.3.x Gingerbread 9-10 18.6%

2.2.x Froyo 8 59.4%

2.0.x /2.1.x Eclair 5-7 17.5%

1.6 Donut 4 2.2%

1.5 Cupcake 3 1.4%

Page 8: Coscup2011: porting android to brand-new cpu architecture

Agenda

Brand new CPU architecture?

Platform quick-review

OK, let go !

Pre-requirement, bring up

Life is not fair, get used to it

Android porting guideline

How do you do your best?

Optimization

Page 9: Coscup2011: porting android to brand-new cpu architecture

OK, let go !

bring up 的最高指導原則: 先求有再求好

Pre-requirement 1. A Workable Environment (Real-Board or Virtual-Board)

至少要有開發板或模擬器

2. A Workable GNU-Toolchain Froyo need : GCC 4.4 + Binutils 2.19

如果沒有怎麼辦? 如果有別的版本 (like GCC-4.2), 就使用別的版本

3. A Workable BSP (Board support package) 最好的情況: BSP 也是使用 2.6.32 系列的 Linux Kernel

事實上能找到一個動得很好的 BSP 其實不是簡單的事

Page 10: Coscup2011: porting android to brand-new cpu architecture

A Workable BSP

Make sure BSP is workable on Linux environment

From Linux to Android-Linux We must think then as two similar platform, but different

Reference change Linux kernel

TLS system call

Integrate Android driver (No-ASM)

Linux kernel Bug Fix Deadlock in page fault handling

arch/<arch-name>/kernel/time.c Add function save_time_delta to support android wakelock

Make sure Android-Linux BSP is workable

Page 11: Coscup2011: porting android to brand-new cpu architecture

Porting Android Build System

Folder <ROOT>/build Arm-awareness !!

EX: core/binary.mk

Reference change Add TARGET_linux-<arch>.mk

Add Linker scirpt <arch>elf.x and <arch>elf.xsc

Toolchain path in core/envsetup.mk

MISC Change: Fix build message with hard-coding “ARM” or “Thumb”

Add TARGET_ARCH variable

Use <Vendor-board>.mk

Page 12: Coscup2011: porting android to brand-new cpu architecture

Porting Android Libc (bionic)

Porting Bionic 如果已經有 uclibc 或 glibc 的 port, 應該不是件難事 非得需要使用組合語言的地方

System call Wrapper, Startup code Setjmp / Longjmp, Atomic operation

Reference change Libc : Syscall, Kernel Header

libc/tools/bionic_utils.py libc/tools/gensyscalls.py MISC change

Libm : fenv.h Libdl: Add Architecture define (#if defined(<arch_name>)) Linker : Reference to ld.so (glibc)

Page 13: Coscup2011: porting android to brand-new cpu architecture

Porting Dalvik VM

非常幸運, 如果只是要能動, 幾乎不用 Porting

Dalvik VM 已經包含了一個 generic 用 c 寫的直譯器

非得需要 Porting 的部分 : JNI

使用組合語言來實作

需要再寫一個 Hint 的產生方式

Alternative: Porting libffi (foreign function interface library)

以上兩種方法其實都需要寫組合語言

DVM Optimization

Fast Interpreter: 使用組語來處理 Dalvik 指令

JITTER: 使用動態編譯器來最佳化熱區 (快取機制)

Page 14: Coscup2011: porting android to brand-new cpu architecture

Porting External Package

<ROOT>/external

其實不太需要 Porting

Fix misc compilation error and makefile change

Like external/webkit

Elfutils :

Sync EM number and RELOC type of Architecture

Page 15: Coscup2011: porting android to brand-new cpu architecture

Build Whole Android Image

make TARGET_ARCH=nds32 -j4

Waiting for build complete ….

Finding NOTICE files: out/target/product/generic/obj/NOTICE_FILES/hash-timestamp Combining NOTICE files: out/target/product/generic/obj/NOTICE.html Target system fs image: out/target/product/generic/obj/PACKAGING/systemimage_unopt_intermediates/system.img Install system fs image: out/target/product/generic/system.img Installed file list: out/target/product/generic/installed-files.txt

OK, We can compile whole android for brand-new CPU Architecture now

Page 16: Coscup2011: porting android to brand-new cpu architecture

Configuration !

Configuration! You can porting android to brand-new CPU architecture now!

不過如果你是工程師, 就會知道這一切才剛開始

If you are an engineer, you know that everything is just beginning

あなたがエンジニアである場合, すべてはただ今始まったばかりです

Page 17: Coscup2011: porting android to brand-new cpu architecture

Agenda

Brand new CPU architecture?

Platform quick-review

OK, let go !

Pre-requirement, bring up

Life is not fair, get used to it

Android porting guideline

How do you do your best?

Optimization

Page 18: Coscup2011: porting android to brand-new cpu architecture

+ = ?

身為一個 non-ARM 的 Android Porter …

・ ェ́`・

Page 19: Coscup2011: porting android to brand-new cpu architecture

Life is not fair, get used to it

Battery Problem

基本上是一個 Porting 到 non-Phone 的platform 最容易 遇到也必須要解決的一個問題

Google 上答案很多, 挑一個喜歡的帶回家 (!?)

10 秒鐘的幸福

Android Activity Manager Service 預設的 Timeout

很可惜台灣的CPU 不能享受這個奢華的幸福

Page 20: Coscup2011: porting android to brand-new cpu architecture

Life is not fair, get used to it

電源管理

一看就知道很手機, 不改就會死人

Hack 掉一些很手機的 code

我很低調, 我不愛GPS !

老是喜歡讓大家使用beta產品的某公司首頁改版後, 沒有GPS 的Android上某公司首頁後瀏覽器會 crash!

只好使用 那家公司唯一不是 beta 的產品 : Search

改 framework/base 的location/java/android/location/LocationManager.java

Page 21: Coscup2011: porting android to brand-new cpu architecture

使用者介面

這些問題都解決後, 大概就可以正常運作了 Android 要跑起來很簡單, 可是要跑的順很困難

這都是使用者介面的陰謀 ! 因為 Android App 開發者很愛 OpenGL

這是一個3D的時代,開發者很愛 OpenGL不為過

我們台灣人一向勤儉, IC裡面有個殘廢的GPU也是很正常的事情 或者GPU只有2D engine, 沒有 OpenGL 也不意外

很剛好, 我們的開發版也是這樣

Page 22: Coscup2011: porting android to brand-new cpu architecture

Agenda

Brand new CPU architecture?

Platform quick-review

OK, let go !

Pre-requirement, bring up

Life is not fair, get used to it

Android porting guideline

How do you do your best?

Optimization

Page 24: Coscup2011: porting android to brand-new cpu architecture

How do you do your best?

Q:Why Don't You Do Your Best?

A: Life is not fair, get used to it

Era of Time-to-Market

能不能做到最好還要問有沒有時間做到最好

所以我們在這邊探討不探討”為何不”,只探討”如何”

身為產品洪流下, 工程師的宿命

Page 25: Coscup2011: porting android to brand-new cpu architecture

Compile once, Compiler Everywhere!

這是一個無所不在Compiler的時代

Android 裡面存在多個 Dynamic Compiler (JITTER) Dalvik VM Pixelflinger : CodeflingerJIT (Graphics 相關) WebKit (V8) Render Script

Dynamic Compiler 都需要 Porting, 而且要真的能動的是需要蠻大的功夫的 Porting Dynamic Compiler 需要的背景和熟悉度很高 所以要 Porting 到 brand-new CPU 越來越不利了 雖然我的專長是 Compiler 領域, 可是我選擇不弄這些東西 因為Dynamic Compiler會動是一回事, 可是沒有錯又是另一回事

Page 26: Coscup2011: porting android to brand-new cpu architecture

How do you do your best?

修身齊家治國平天下

欲爭服 Android 必先征服 GUI

Optimization Guideline

Q: 如果不要 Porting Pixelflinger 中的 Dynamic Compiler, 那還能取得讓使用者可以接受的加速嗎?

A: 雖然Google的字典裡只有放棄, 可是我們的字典裡沒有放棄, 確實還是有辦法的

Page 27: Coscup2011: porting android to brand-new cpu architecture

Optimization Guideline: Library

在圖形系統當中,複製是非常常見的事情,而他們通常也是一個最簡單可以效果很好的施力點

memcpy … etc

參考一下ARM平台中,那些其他的 Library Function有使用組合語言

通常要把那些 Library Function 用組合語言重寫並不太困難, 這也是一個不用自己去 profile 出瓶頸點的簡單解法

Page 28: Coscup2011: porting android to brand-new cpu architecture

Optimization Guideline: GUI

從 Surfaceflinger 下手 Surfaceflinger 還有一些最佳化的空間 可以參考其他Project的修正 (android-x86 and 0xdroid)

動不動就刷新整個畫面是有問題的 不過 Android 常常這麼做 可以去找出那些元凶

從 Scanline shortcut 下手 打開 LOGW("using generic (slow) pixel-pipeline"); 觀察為什麼會使用到 generic pixel-pipeline 新增新的 shortcut

Tips: Back port 新的 shortcut

Page 29: Coscup2011: porting android to brand-new cpu architecture

Experimental result of Scanline shortcut

Page 30: Coscup2011: porting android to brand-new cpu architecture

Summary

Andes Android 的 Demo Video http://www.youtube.com/watch?v=-XxlP55v2is&feature=related

Special Thanks 0xlab and Andorid-x86 project

Summary (Porting android to brand-new CPU architecture) Bring-up

BSP (Kernel & Driver)

Android System

Optimization GUI Optimization with Surfaceflinger

GUI Optimization with Scanline shortcut

Page 31: Coscup2011: porting android to brand-new cpu architecture

Any Question ?