gem5 初体验 --- 以全系统运行 arm64 为例

gem5 基本介绍

关于  gem5 的介绍,网上资料也比较多,这里不再重复赘述了,这里推荐一些入门学习资料(https://blog.csdn.net/qq_43381135/article/details/104371236)。

目前本人对 gem5 的原理不是很了解,仍在学习当中。这篇文章只是小试牛刀,记录一些实操的过程,体验一下gem5。

 

下载与编译

源码获取:关于源码这个问题,可能涉及到了,因为代码托管在 google 上,为了省去麻烦以及速度,可以从 gitee 上下载:git clone https://gitee.com/minh95/gem5.git 

编译依赖:编译会依赖一些工具,本人编译的时候是根据错误提示一个个安装的,因为网上的资料一般以 ubuntu 为例的,本人用的是 majaro ,对应的工具肯定是有的,只是名字可能略有区别。如果是用的 ubuntu,可以一次性都安装:

sudo apt-get install build-essential apt-get install git m4  scons zlib1g zlib1g-dev libprotobuf-dev protobuf-compiler libprotoc-dev libgoogle-perftools-dev  python-dev python python-protobuf

其他 linux 发行版,可以参考 ubuntu 的,这里不再详述。

编译 gem5:源码提供了 5  种二进制程序: debug, opt, fast, prof, and perf.

这5种类型区别如下:

debug

Built with no optimizations and debug symbols. This binary is useful when using a debugger to debug if the variables you need to view are optimized out in the opt version of gem5. Running with debug is slow compared to the other binaries.

opt

This binary is build with most optimizations on (e.g., -O3), but with debug symbols included. This binary is much faster than debug, but still contains enough debug information to be able to debug most problems.

fast

Built with all optimizations on (including link-time optimizations on supported platforms) and with no debug symbols. Additionally, any asserts are removed, but panics and fatals are still included. fast is the highest performing binary, and is much smaller than opt. However, fast is only appropriate when you feel that it is unlikely your code has major bugs.

prof and perf

These two binaries are build for profiling gem5. prof includes profiling information for the GNU profiler (gprof), and perf includes profiling information for the Google performance tools (gperftools).

根据需要可以编译初不同的二进制,这里以 debug 为例:scons build/ARM/gem5.debug -j4

由于本人机器太菜了,导致空间不足:

gem5 初体验 --- 以全系统运行 arm64 为例_第1张图片

此时需要扩大 swap 的空间即可:

dd if=/dev/zero of=swapfile bs=1024 count=3145728
mkswap swapfile 
swapon swapfile 

不出意外,应可以编译出对应的二进制版本:build/ARM/gem5.debug

 

运行 arm64 linux 系统

运行  arm64 linux 一般来说需要提供 kernel,rootfs,dtb,但是 gem5 把 bootloader 也单独整出来了,因此我们需要至少提供这四个文件。

其实相应的文件,gem5 中已经提供了,只是需要单独编译,其中 bootloader 在预编译好的内核里也带了,可以直接使用:

make -C system/arm/bootloader/arm
make -C system/arm/bootloader/arm64
make -C system/arm/dt

而 kernel 则放在另一个仓库里,可以选择自己编译,或者直接下载已经编译好的内核,文件系统也是如此:

wget http://dist.gem5.org/dist/v21-0/arm/aarch-system-20210904.tar.bz2
wget http://dist.gem5.org/dist/current/arm/disks/ubuntu-18.04-arm64-docker.img.bz2

这里只是为了体验,就直接使用预编译好的文件了。

运行 gem5:

./build/ARM/gem5.debug configs/example/fs.py  --kernel ~/data/binaries/vmlinux.arm64 --disk ~/data/ubuntu-18.04-arm64-docker.img --dtb ./system/arm/dt/armv8_gem5_v1_1cpu.dtb --bootloader ~/data/binaries/boot.arm64

在 gem5 的运行窗口看到如下打印,证明 linux 已经运行起来了。但是这里不会有 linux 内核的打印,根据提示,用工具连接 3456 端口即可看到 linux 的输出内容。

gem5 初体验 --- 以全系统运行 arm64 为例_第2张图片

获取输出:

cd util/term; make
./m5term 3456

gem5 初体验 --- 以全系统运行 arm64 为例_第3张图片

 

总结

这篇文章参考了 gem5 的文档,体验了下 gem5 运行 arm64 linux 的效果。总体感受是,这也太他么慢了。编译慢也就算了,毕竟源码多(50w?),模拟是真的慢,从启动到进入 shell 花了差不多一个小时(心累)。本人猜测有三点因素:

1、很大一部分是模拟器的因素

2、x86 上运行 arm64,没有用到硬件虚拟化加速? 因为本人跑 x86 系统的时候挺快的,这个因素待后续验证。

3、使用了 debug 二进制?经验证,果然是,使用 opt 后,速度明显提升了很多,这里有点出乎意料,差异太大了。

总的来说,gem5 能够正常运行,debug 速度堪忧,为了节省时间,还是选择 opt  或 fast 吧。如果是为了调试,感觉使用 printf 也会比 debug 要快 :)。好了,接下来就是一段奇妙的探索之旅了,加油!!!

 

参考链接:

https://www.gem5.org/documentation/general_docs/fullsystem/building_arm_kernel

你可能感兴趣的:(linux)