交叉编译和使用HTOP

时间:2023-03-09 23:29:56
交叉编译和使用HTOP

1、什么是htop

htop来源于top,top是Unix/linux下功能强大的性能检测工具之一,用于实时检测并统计进程的属性和状态,基于ncurses库,可上显示文字界面。但是top已经非常陈旧,不支持鼠标点击操作,不支持查看进程的各个子线程的信息(对于我来说,这个非常重要,有助于分析代码的效率并优化),不支持color显示,易用性较差(曾有人提出改进top但未被接受)。htop官网对htop和top的比较,如下示例图为我的ubuntu12.4的htop效果:

  • 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.

交叉编译和使用HTOP

2、下载htop

htop官网提供两种下载:部分Linux发行版本的预编译二进制文件,以及源代码方式。

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

3、解决htop的编译依赖

htop编译时,必须有ncurses库的支持,所以编译htop之前,必须确保已存在ncurses库,交叉编译前需编译对应的ncurses库。

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

 1 $ tar -zxvf ncurses-5.9.tar.gz 2 $ cd ncurses-5.9
  #配置前,可使用./configure --help查看配置帮助
  $ ./configure CC=arm-hisiv200-linux-gnueabi-gcc --prefix=$PWD/_install --host=arm-linux --with-shared #编译为共享库
  $ make && make install
  $ ls -lh _install/lib 
  total 4.5M
  -rw-r--r--  root root  93K  5月   : libform.a
  -rw-r--r--  root root 529K  5月   : libform_g.a
 lrwxrwxrwx  root root     5月   : libform.so -> libform.so.
 lrwxrwxrwx  root root     5月   : libform.so. -> libform.so.5.9
 -rwxr-xr-x  root root  68K  5月   : libform.so.5.9
 -rw-r--r--  root root  51K  5月   : libmenu.a
 -rw-r--r--  root root 321K  5月   : libmenu_g.a
 lrwxrwxrwx  root root     5月   : libmenu.so -> libmenu.so.
 lrwxrwxrwx  root root     5月   : libmenu.so. -> libmenu.so.5.9
 -rwxr-xr-x  root root  36K  5月   : libmenu.so.5.9
 -rw-r--r--  root root 446K  5月   : libncurses.a
 -rw-r--r--  root root 129K  5月   : libncurses++.a
 -rw-r--r--  root root 2.3M  5月   : libncurses_g.a
 lrwxrwxrwx  root root     5月   : libncurses.so -> libncurses.so.
 lrwxrwxrwx  root root     5月   : libncurses.so. -> libncurses.so.5.9
 -rwxr-xr-x  root root 327K  5月   : libncurses.so.5.9
 -rw-r--r--  root root  23K  5月   : libpanel.a
 -rw-r--r--  root root 131K  5月   : libpanel_g.a
 lrwxrwxrwx  root root     5月   : libpanel.so -> libpanel.so.
 lrwxrwxrwx  root root     5月   : libpanel.so. -> libpanel.so.5.9
 -rwxr-xr-x  root root  15K  5月   : libpanel.so.5.9
 lrwxrwxrwx  root root     5月   : 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

4、 命令的挂载和执行

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必须存在。

5、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