交叉編譯和使用HTOP

一、什麼是htopphp

 htop來源於top,top是Unix/linux下功能強大的性能檢測工具之一,用於實時檢測並統計進程的屬性和狀態,基於ncurses庫,可上顯示文字界面。可是top已經很是陳舊,不支持鼠標點擊操做,不支持查看進程的各個子線程的信息(對於我來講,這個很是重要,有助於分析代碼的效率並優化),不支持color顯示,易用性較差(曾有人提出改進top但未被接受)。htop官網對htop和top的比較,以下示例圖爲個人ubuntu12.4的htop效果:html

  • In 'htop' you can scroll the list vertically and horizontally to see all processes and complete command lines.
  • In 'top' you are subject to a delay for each unassigned key you press (especially annoying when multi-key escape sequences are triggered by accident).
  • 'htop' starts faster ('top' seems to collect data for a while before displaying anything).
  • In 'htop' you don't need to type the process number to kill a process, in 'top' you do.
  • In 'htop' you don't need to type the process number or the priority value to renice a process, in 'top' you do.
  • 'htop' supports mouse operation, 'top' doesn't
  • 'top' is older, hence, more used and tested.

 

 

二、下載htoplinux

 htop官網提供兩種下載:部分Linux發行版本的預編譯二進制文件,以及源代碼方式。git

htop官網:http://hisham.hm/htop/index.php?page=downloads
 github上的源碼:https://github.com/hishamhm/htop (git clone獲取)github

 

三、解決htop的編譯依賴ubuntu

 htop編譯時,必須有ncurses庫的支持,因此編譯htop以前,必須確保已存在ncurses庫,交叉編譯前需編譯對應的ncurses庫。ide

ncurses最新版本源碼:ftp://invisible-island.net/ncurses/ncurses.tar.gz工具

 

 1  $  tar  -zxvf ncurses- 5.9 . tar .gz

 2 $ cd ncurses-5.9
 3 #配置前,可以使用./configure --help查看配置幫助
 4 $ ./configure CC=arm-hisiv200-linux-gnueabi-gcc --prefix=$PWD/_install --host=arm-linux --with-shared #編譯爲共享庫
 5 $ make && make install
 6 $ ls -lh _install/lib 
 7 total 4.5M
 8 -rw-r--r-- 1 root root  93K  5月  6 15:04 libform.a
 9 -rw-r--r-- 1 root root 529K  5月  6 15:04 libform_g.a
10 lrwxrwxrwx 1 root root   12  5月  6 15:04 libform.so -> libform.so.5
11 lrwxrwxrwx 1 root root   14  5月  6 15:04 libform.so.5 -> libform.so.5.9
12 -rwxr-xr-x 1 root root  68K  5月  6 15:04 libform.so.5.9
13 -rw-r--r-- 1 root root  51K  5月  6 15:04 libmenu.a
14 -rw-r--r-- 1 root root 321K  5月  6 15:04 libmenu_g.a
15 lrwxrwxrwx 1 root root   12  5月  6 15:04 libmenu.so -> libmenu.so.5
16 lrwxrwxrwx 1 root root   14  5月  6 15:04 libmenu.so.5 -> libmenu.so.5.9
17 -rwxr-xr-x 1 root root  36K  5月  6 15:04 libmenu.so.5.9
18 -rw-r--r-- 1 root root 446K  5月  6 15:04 libncurses.a
19 -rw-r--r-- 1 root root 129K  5月  6 15:04 libncurses++.a
20 -rw-r--r-- 1 root root 2.3M  5月  6 15:04 libncurses_g.a
21 lrwxrwxrwx 1 root root   15  5月  6 15:04 libncurses.so -> libncurses.so.5
22 lrwxrwxrwx 1 root root   17  5月  6 15:04 libncurses.so.5 -> libncurses.so.5.9
23 -rwxr-xr-x 1 root root 327K  5月  6 15:04 libncurses.so.5.9
24 -rw-r--r-- 1 root root  23K  5月  6 15:04 libpanel.a
25 -rw-r--r-- 1 root root 131K  5月  6 15:04 libpanel_g.a
26 lrwxrwxrwx 1 root root   13  5月  6 15:04 libpanel.so -> libpanel.so.5
27 lrwxrwxrwx 1 root root   15  5月  6 15:04 libpanel.so.5 -> libpanel.so.5.9
28 -rwxr-xr-x 1 root root  15K  5月  6 15:04 libpanel.so.5.9
29 lrwxrwxrwx 1 root root   17  5月  6 15:04 terminfo -> ../share/terminfo性能

 

    Libncurses庫用來在顯示器上顯示文本界面。典型例子:Linux內核的make menuconfig配置界面,busybox的make menuconfig,fdisk磁盤分區工具界面,甚至open suse的yasT2。優化

    Libform庫用於 在ncurses中使用表格。

    Libmenu庫用於在ncurses中使用菜單。

    Libpanel庫用於在ncurses中使用面板。

 將編譯好的這些動態庫拷貝到htop的源碼目錄下。

 htop的交叉編譯流程相似:

 1 $ ./configure CC=arm-hisiv200-linux-gnueabi-gcc  --prefix=$PWD/_install

2 $ make && make install 

3 $ ls -lh _install

 

四、 命令的掛載和執行

htop程序的執行須要libcurses和term的支持,執行以前請檢查TERM環境變量的值(echo $TERM),以及TERMINFO的值(echo $TERMINFO,爲空則必須保證/usr/share/terminfo存在對應的term程序)。 

理想的狀態是,將libcurses(放在/lib、/usr/lib 或/usr/local/lib)和htop直接拷貝到開發板上直接就能運行,但若是嵌入式Linux裁剪的太厲害,將/usr/share/terminfo目錄裁剪了,htop將沒法正常啓動。此時可從別處拷貝terminfo目錄。

 最不理想的狀態是板子將全部程序編譯成了靜態程序,同時裁撤了全部lib和/usr/share目錄,且此時文件系統爲只讀,此時能夠將機型對應的linux lib目錄mount到板子上的/lib目錄,同時將設置TERMINFO的環境變量爲對應的terminfo目錄(好比:export TERMINFO="/var/nfs/terminfo")。

 舉例:如$TERM=xterm,則/usr/share/terminfo/x/xterm必須存在。

 

五、htop的應用舉例 

詳細應用請man htop查閱。 

選擇某一進程,按s:用strace追蹤進程的系統調用

P、T、M和top下是同樣的,按CPU,Time+、Memory使用排序。
下面有F1~F10的功能和對應的字母快捷鍵。
Shortcut Key Function Key Description
h F1 Invoke htop Help
S F2 Htop Setup Menu
/ F3 Search for a Process
I F4 Invert Sort Order
t F5 Tree View
> F6 Sort by a column
[ F7 Nice - (change priority)
] F8 Nice + (change priority)
k F9 Kill a Process
q F10 Quit htop

 此段落來自:http://www.lylinux.org/linux-top%E5%91%BD%E4%BB%A4%E7%9A%84%E6%9B%BF%E4%BB%A3%E8%80%85htop.html

相關文章
相關標籤/搜索