PORTING ANDROID TO
BRAND-NEW CPU ARCHITECTURE
新規CPUアーキテクチャにアンドロイドの移植




          Luse Cheng <lusecheng@gmail.com>
          August 21, 2011 @ COSCUP
自我介紹
 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)
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
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
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
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
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

                                     Version               API level   Distribution
 Why tag 2.2.1?                     3.x.x Honeycomb       11-13       0.9%
                                     2.3.x Gingerbread     9-10        18.6%
   Can use 32-bit host              2.2.x Froyo
                                     2.0.x /2.1.x Eclair
                                                           8
                                                           5-7
                                                                       59.4%
                                                                       17.5%
                                     1.6 Donut             4           2.2%
                                     1.5 Cupcake           3           1.4%
                                                            From: Wikipedia (Android)
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
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 其實不是簡單的事
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
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
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)
Porting Dalvik VM
 非常幸運, 如果只是要能動, 幾乎不用 Porting
   Dalvik VM 已經包含了一個 generic 用 c 寫的直譯器
   非得需要 Porting 的部分 : JNI
     使用組合語言來實作
       需要再寫一個 Hint 的產生方式
     Alternative: Porting libffi (foreign function interface library)
   以上兩種方法其實都需要寫組合語言
 DVM Optimization
   Fast Interpreter: 使用組語來處理 Dalvik 指令
   JITTER: 使用動態編譯器來最佳化熱區 (快取機制)
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
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
Configuration !
 Configuration! You can porting android to
 brand-new CPU architecture now!

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


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

 あなたがエンジニアである場合, すべてはただ今始
 まったばかりです
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
身為一個 non-ARM 的 Android Porter …




         +           =?

                         ・´
                          ェ`・
Life is not fair, get used to it
 Battery Problem
   基本上是一個 Porting 到 non-Phone 的platform 最容易
    遇到也必須要解決的一個問題
   Google 上答案很多, 挑一個喜歡的帶回家 (!?)


 10 秒鐘的幸福
   Android Activity Manager Service 預設的 Timeout
   很可惜台灣的CPU 不能享受這個奢華的幸福
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
使用者介面
 這些問題都解決後, 大概就可以正常運作了
  Android 要跑起來很簡單, 可是要跑的順很困難
  這都是使用者介面的陰謀 !
    因為 Android App 開發者很愛 OpenGL
    這是一個3D的時代,開發者很愛 OpenGL不為過


 我們台灣人一向勤儉, IC裡面有個殘廢的GPU也是
很正常的事情
  或者GPU只有2D engine, 沒有 OpenGL 也不意外
  很剛好, 我們的開發版也是這樣
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
Why Don't You Do Your Best?




                    Source: http://yoake-kibo.jugem.jp/?eid=1671
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
   能不能做到最好還要問有沒有時間做到最好


 所以我們在這邊探討不探討”為何不”,只探討”如何”
   身為產品洪流下, 工程師的宿命
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會動是一回事, 可是沒有錯又是另一回事
How do you do your best?
 修身齊家治國平天下
   欲爭服 Android 必先征服 GUI


 Optimization Guideline
   Q: 如果不要 Porting Pixelflinger 中的 Dynamic Compiler,
    那還能取得讓使用者可以接受的加速嗎?
   A: 雖然Google的字典裡只有放棄, 可是我們的字典裡
    沒有放棄, 確實還是有辦法的
Optimization Guideline: Library
 在圖形系統當中,複製是非常常見的事情,而他們通
 常也是一個最簡單可以效果很好的施力點
  memcpy … etc


 參考一下ARM平台中,那些其他的 Library Function
 有使用組合語言
  通常要把那些 Library Function 用組合語言重寫並不太
   困難, 這也是一個不用自己去 profile 出瓶頸點的簡單
   解法
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
Experimental result of Scanline shortcut
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
Any Question ?

Coscup2011: porting android to brand-new cpu architecture

  • 1.
    PORTING ANDROID TO BRAND-NEWCPU ARCHITECTURE 新規CPUアーキテクチャにアンドロイドの移植 Luse Cheng <lusecheng@gmail.com> August 21, 2011 @ COSCUP
  • 2.
    自我介紹  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)
  • 3.
    An Embedded CPUIP 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
  • 4.
    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
  • 5.
    Agenda  Brand newCPU 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
  • 6.
    Brand new CPUarchitecture?  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
  • 7.
    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 Version API level Distribution  Why tag 2.2.1? 3.x.x Honeycomb 11-13 0.9% 2.3.x Gingerbread 9-10 18.6%  Can use 32-bit host 2.2.x Froyo 2.0.x /2.1.x Eclair 8 5-7 59.4% 17.5% 1.6 Donut 4 2.2% 1.5 Cupcake 3 1.4% From: Wikipedia (Android)
  • 8.
    Agenda  Brand newCPU 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
  • 9.
    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 其實不是簡單的事
  • 10.
    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
  • 11.
    Porting Android BuildSystem  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
  • 12.
    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)
  • 13.
    Porting Dalvik VM 非常幸運, 如果只是要能動, 幾乎不用 Porting  Dalvik VM 已經包含了一個 generic 用 c 寫的直譯器  非得需要 Porting 的部分 : JNI  使用組合語言來實作  需要再寫一個 Hint 的產生方式  Alternative: Porting libffi (foreign function interface library)  以上兩種方法其實都需要寫組合語言  DVM Optimization  Fast Interpreter: 使用組語來處理 Dalvik 指令  JITTER: 使用動態編譯器來最佳化熱區 (快取機制)
  • 14.
    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
  • 15.
    Build Whole AndroidImage  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
  • 16.
    Configuration !  Configuration!You can porting android to brand-new CPU architecture now!  不過如果你是工程師, 就會知道這一切才剛開始  If you are an engineer, you know that everything is just beginning  あなたがエンジニアである場合, すべてはただ今始 まったばかりです
  • 17.
    Agenda  Brand newCPU 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
  • 18.
    身為一個 non-ARM 的Android Porter … + =? ・´ ェ`・
  • 19.
    Life is notfair, get used to it  Battery Problem  基本上是一個 Porting 到 non-Phone 的platform 最容易 遇到也必須要解決的一個問題  Google 上答案很多, 挑一個喜歡的帶回家 (!?)  10 秒鐘的幸福  Android Activity Manager Service 預設的 Timeout  很可惜台灣的CPU 不能享受這個奢華的幸福
  • 20.
    Life is notfair, get used to it  電源管理  一看就知道很手機, 不改就會死人  Hack 掉一些很手機的 code  我很低調, 我不愛GPS !  老是喜歡讓大家使用beta產品的某公司首頁改版後, 沒 有GPS 的Android上某公司首頁後瀏覽器會 crash!  只好使用 那家公司唯一不是 beta 的產品 : Search  改 framework/base 的 location/java/android/location/LocationManager.java
  • 21.
    使用者介面  這些問題都解決後, 大概就可以正常運作了  Android 要跑起來很簡單, 可是要跑的順很困難  這都是使用者介面的陰謀 !  因為 Android App 開發者很愛 OpenGL  這是一個3D的時代,開發者很愛 OpenGL不為過  我們台灣人一向勤儉, IC裡面有個殘廢的GPU也是 很正常的事情  或者GPU只有2D engine, 沒有 OpenGL 也不意外  很剛好, 我們的開發版也是這樣
  • 22.
    Agenda  Brand newCPU 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
  • 23.
    Why Don't YouDo Your Best? Source: http://yoake-kibo.jugem.jp/?eid=1671
  • 24.
    How do youdo your best?  Q:Why Don't You Do Your Best?  A: Life is not fair, get used to it  Era of Time-to-Market  能不能做到最好還要問有沒有時間做到最好  所以我們在這邊探討不探討”為何不”,只探討”如何”  身為產品洪流下, 工程師的宿命
  • 25.
    Compile once, CompilerEverywhere!  這是一個無所不在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會動是一回事, 可是沒有錯又是另一回事
  • 26.
    How do youdo your best?  修身齊家治國平天下  欲爭服 Android 必先征服 GUI  Optimization Guideline  Q: 如果不要 Porting Pixelflinger 中的 Dynamic Compiler, 那還能取得讓使用者可以接受的加速嗎?  A: 雖然Google的字典裡只有放棄, 可是我們的字典裡 沒有放棄, 確實還是有辦法的
  • 27.
    Optimization Guideline: Library 在圖形系統當中,複製是非常常見的事情,而他們通 常也是一個最簡單可以效果很好的施力點  memcpy … etc  參考一下ARM平台中,那些其他的 Library Function 有使用組合語言  通常要把那些 Library Function 用組合語言重寫並不太 困難, 這也是一個不用自己去 profile 出瓶頸點的簡單 解法
  • 28.
    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
  • 29.
    Experimental result ofScanline shortcut
  • 30.
    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
  • 31.