Top Porting and User Guide
1. Porting¶
top is a command line tool that displays real-time system status in the terminal. It provides a dynamic, interactive system monitor for viewing various performance indicators and process information of the system.
The default top of the public version is integrated into busybox. In actual scenarios, users may need to compile the top tool separately. It can be generated through the procps-ng toolset. The versions are as follows:
- procps-ng: 3.3.12
- ncurses: 6.1
1.1. Traditional method¶
1.1.1. ncurses cross-compilation¶
ncurses is an open source library for controlling terminal screen output and responding to user input. It provides a set of functions and data structures for creating terminal interfaces and handling text-based user interface (TUI) applications.
1.1.1.1. Source code download
Version: 6.1
1.1.1.2. Cross-compilation
After the resource package is downloaded, unzip it and enter the directory. Execute the configuration file to configure the compilation tool chain and specify the corresponding installation path, as follows:
tar -zxvf ncurses-6.1.tar.gz cd ncurses-6.1/ export PATH=/tools/toolchain/gcc-10.2.1-20210303-sigmastar-glibc-x86_64_aarch64-linux-gnu/bin:$PATH ; export ARCH=arm64 ; export CROSS_COMPILE=aarch64-linux-gnu- ./configure --host=aarch64-linux-gnu CPPFLAGS="-P" --prefix=$PWD/install --disable-shared --enable-static --disable-stripping --without-debug make clean -j8 make -j8 make install
Note that the export command in the above example steps specifies the compilation chain address. In actual operation, please declare it according to the actual compilation chain path. At the same time, use --disable-shared --enable-static to configure the generation of static libraries. After executing the above steps, the generated header files and library files will be found in the ncurses-6.1/install/ path.
1.1.2. procps-ng cross-compilation¶
procps-ng is an open source toolset for monitoring system processes and providing process-related information. It is the next generation version of the procps toolset, designed to provide better stability, portability, and functional extensibility.
1.1.2.1. Source code download
Version: 3.3.12
1.1.2.2. Cross-compilation
After the resource package is downloaded, unzip it and enter the directory, and execute the configuration file to configure the installation directory as follows:
tar -xvf procps-ng-3.3.12.tar.xz cd procps-ng-3.3.12/ export PATH= /tools/toolchain/gcc-10.2.1-20210303-sigmastar-glibc-x86_64_aarch64-linux-gnu/bin: $PATH ; export ARCH= arm64 ; export CROSS_COMPILE= aarch64-linux-gnu- echo "ac_cv_func_realloc_0_nonnull=yes" > arm-linux.cache echo "ac_cv_func_malloc_0_nonnull=yes" >> arm-linux.cache ./configure --disable-shared --enable-static --host=aarch64-linux-gnu NCURSES_CFLAGS="-I/home/veahow.chen/opensource/procps-ng-3.3.12/../ncurses-6.1/install/include/ncurses/" NCURSES_LIBS="-L/home/veahow.chen/opensource/procps-ng-3.3.12/../ncurses-6.1/install/lib -lncurses" --prefix=$PWD/install --with-ncurses --cache-file=arm-linux.cache
Note that the export command in the above example steps specifies the compilation chain address. In actual operation, please declare it according to the actual compilation chain path. Note that in order not to rely on dynamic libraries, the static compilation configuration --disable-shared --enable-static is used here.
After executing the above steps, you will find the generated Makefile file in the procps-ng-3.3.12/ path. After generating the Makefile file, you also need to modify CFLAGS and LDFLAGS:
-CFLAGS=-g -O2 +CFLAGS = -g -O2 -I/home/veahow.chen/opensource/procps-ng-3.3.12/../ncurses-6.1/install/include/ncurses/ -I/home/veahow.chen/opensource/procps-ng-3.3.12/../ncurses-6.1/install/include/ ... -LDFLAGS= +LDFLAGS = -L/home/veahow.chen/opensource/procps-ng-3.3.12/../ncurses-6.1/install/lib
After modification, execute the following commands to compile and install:
make clean -j8 make -j8 make install
Finally, you will find the generated top program in procps-ng-3.3.12/install/bin. The original generated program is large in size and can be trimmed using the following command:
cd install/bin/
aarch64-linux-gnu-strip top --strip-unneeded
1.2. Based on Buildroot¶
Top belongs to the procps-ng package, version 3.3.12
export PATH= /tools/toolchain/gcc-10.2.1-20210303-sigmastar-glibc-x86_64_aarch64-linux-gnu/bin: $PATH ; export ARCH= arm64 ; export CROSS_COMPILE= aarch64-linux-gnu- cd 3 rdparty/buildroot-masters make procps-ng
The top built in this way depends on the dynamic libraries libprocps.so.6 and libncurses.so.6. The related lib libraries are integrated in the 3rdparty/buildroot-master/output/target/usr/lib/ folder.
2. Environment Setup¶
2.1. Copying Files¶
Copy the program to the directory project/release/chip/pcupid/dispcam/common/glibc/10.2.1/release/bin/debug and add executable permissions using the chmod +x command.

2.2. Image packaging¶
Executing make image_install under project will package the program to project/image/output/customer.

3. Test verification¶
The board runs an application with a specified thread name. The test code is as follows:
#define _GNU_SOURCE #include <stdio.h> #include <pthread.h> // Thread function void* threadFunc(void* arg) { const char* threadName = (const char*) arg; printf("Thread %s is running\n", threadName); // Do some work here for (;;) ; // ... return NULL; } int main() { pthread_t thread; const char* threadName = "WorkerThread"; // Create thread pthread_create(&thread, NULL, threadFunc, (void*) threadName); pthread_setname_np(thread, threadName); // Wait for thread to finish pthread_join(thread, NULL); printf("Thread finished\n"); return 0; }
Use the following command to generate the test program:
export PATH= /tools/toolchain/gcc-10.2.1-20210303-sigmastar-glibc-x86_64_aarch64-linux-gnu/bin: $PATH ; export ARCH= arm64 ; export CROSS_COMPILE= aarch64-linux-gnu- aarch64-linux-gnu-gcc -lpthread pthread.c -o pthread
Copy the test program to the board and test the Top command.
3.1. top verification¶

3.2. top -H verification¶

3.3. top -Hbp verification¶

You can see that the thread name of the test program is displayed normally.
3.4. Verify with top after adb shell¶

4. Common Q&A¶
Q1: adb shell uses top garbled characters
top source code has some string processing. You can modify procps-ng-3.3.12/top/top.c to return the following processing code.

Q2: When running top -Hbp xx, 'dumb': unknown terminal type appears.
You can enter the board and use the following command to solve it: This error means that there is no scp in the remote sh command, not the local one. scp will send scp cmd to the remote end through ssh. From the code, you can see the command is as follows:
cd /config/terminfo
mkdir d
cp v/vt100 d/dumb
Q3: top thread name is not fully displayed
This problem is related to the version. Please use procps-3.1.12 to solve it.
