CPU性能测试——CoreMark篇


在这里插入图片描述
本文将介绍使用CoreMark测试程序对我们小组自研芯片进行性能的测试,记录了CoreMarK工具的使用以及对其测试结果进行分析
测试环境:
PC OS: Ubuntu20.04 LTS
CPU: 自研CPU(ARCH=riscv64,ISA=rv64imafdc)
Kernel: CPU使用的内核为Linux4.15
CoreMark: CoreMark v1.01
GNU: riscv64-unknown-linux-gnu

1、CoreMark简介

??CoreMark是由EEMBC(Embedded Microprocessor Benchmark Consortium)Shay Gla-On2009年提出的一项基准测试程序,CoreMark的主要目标是简化操作,并提供一套测试单核处理器核心的方法。测试标准是在配置参数的组合下单位时间内运行的CoreMark程序次数(单位:CoreMark/MHz),该数字值越大则说明测试的性能越好。
??目前在嵌入式CPU行业中普遍公认的性能测试指标的标准主要使用以下三种,MIPSDhrystoneCoremark,而CoreMarkDhrystone一样,拥有体积小、方便移植、易于理解、免费并且显示单个数字基准分数。与Dhrystone不同的是,Dhrystone的主要部分实际上暴露了编译器优化工作负载的能力,而不是实际MCU或CPU的能力,的性能,而CoreMark具有特定的运行和报告规则,从而可以避免由于所使用的编译库不同而导致的测试结果难以比较。

2、获取源码

??EEMBC在将CoreMark源码托管在GitHub上可以访问github.com/eembc/coremark直接点击下载获得源码;也可以通过git命令下载到本地。

imaginemiracle@:Download$ git clone https://github.com/eembc/coremark.git
imaginemiracle@:Download$ cd coremark/
imaginemiracle@:coremark$ ls
barebones         core_main.c  coremark.md5   core_state.c  cygwin  freebsd     linux    macos     README.md  simple
core_list_join.c  coremark.h   core_matrix.c  core_util.c   docs    LICENSE.md  linux64  Makefile  rtems

CoreMark项目的详细介绍,可以查阅当前目录下“coremark/docs/html/index.html”。该项目以下是当前目录的个文件介绍:

#在tree命令的输出中,作了部分删除(不影响分析整个CoreMark工程)
imaginemiracle@:coremark$ tree
.
├── barebones	 --移植到裸机环境下需要修改的目录
│   ├── core_portme.c		--移植的目标平台配置信息
│   ├── core_portme.h		--计时以及板级初始化实现
│   ├── core_portme.mak		--该子目录的makefile
│   ├── cvt.c
│   └── ee_printf.c			--打印函数串口发送实现
├── core_list_join.c	--列表操作程序
├── core_main.c			--主程序
├── coremark.h			--项目配置与数据结构的定义头文件
├── coremark.md5		
├── core_matrix.c		--矩阵运算程序
├── core_state.c		--状态机控制程序
├── core_util.c			--CRC计算程序
├── cygwin				--x86 cygwin和gcc 3.4(四核,双核和单核系统)的测试代码
│   ├── core_portme.c
│   ├── core_portme.h
│   └── core_portme.mak
├── freebsd				--以下同理,是在不同操作系统下的测试代码
│   ├── ...
├── LICENSE.md
├── linux
│   ├── ...
├── linux64
│   ├── ...
├── macos
│   ├── ...
├── Makefile			
├── README.md			--自述文件,CoreMark项目的基本介绍
├── rtems
│   ├── ...
└── simple
    ├── ...
    └──

3、移植到riscv64架构

[注]:若移植的平台是ARM Cortex-M系列的裸机系统,只需要修改“coremark/barebones”目录下的文件即可。
将当前目录下linux64目录拷贝一份为riscv64分支:

imaginemiracle@:coremark$ cp -rf linux64/ riscv64
imaginemiracle@:coremark$ vim riscv64/core_portme.mak

修改CCriscv64-unknown-linux-gnu-gcc即可(默认已经配置好riscv64-linux-gnu工具链,有很多优秀的博客介绍了riscv toolchain的编译安装过程可以自行查阅参考):

#core_portme.mak文件

OUTFLAG= -o
# Flag: CC
#   Use this flag to define compiler to use
# ===========================Alter by me===========================
CC = riscv64-unknown-linux-gnu-gcc
# ============================End Alter============================
# Flag: CFLAGS
#   Use this flag to define compiler options. Note, you can add compiler options from the command line using XCFLAGS="other flags"
PORT_CFLAGS = -O2
FLAGS_STR = "$(PORT_CFLAGS) $(XCFLAGS) $(XLFLAGS) $(LFLAGS_END)"
CFLAGS = $(PORT_CFLAGS) -I$(PORT_DIR) -I. -DFLAGS_STR=\"$(FLAGS_STR)\"
#Flag: LFLAGS_END
#   Define any libraries needed for linking or other flags that should come at the end of the link line (e.g. linker scripts). 
#   Note: On certain platforms, the default clock_gettime implementation is supported but requires linking of librt.
LFLAGS_END += -lrt
# Flag: PORT_SRCS
# Port specific source files can be added here
PORT_SRCS = $(PORT_DIR)/core_portme.c

LOAD = echo Loading done
RUN = 

OEXT = .o
# ===========================Alter by me===========================
EXE = .rvexe
# ============================End Alter============================

4、编译生成coremark.rvexe

4.1、编译在单核上运行的coremark.rvexe

<1> 编译命令:

imaginemiracle@:coremark$ make PORT_DIR=riscv64

<2> 编译结果如下,会出现错误

./coremark.rvexe  0x0 0x0 0x66 0 7 1 2000 > ./run1.log
/bin/sh: 1: ./coremark.rvexe: Exec format error
make[1]: *** [Makefile:112: run1.log] Error 2
make[1]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/test/coremark'
make: *** [Makefile:99: rerun] Error 2
imaginemiracle@:coremark$ file coremark.rvexe
coremark.rvexe: ELF 64-bit LSB executable, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-riscv64-lp64d.so.1, for GNU/Linux 3.0.0, with debug_info, not stripped

??仔细查看,该错误并不是编译过程发生的错误,而是在运行编译出的coremark.rvexe报错,答案是肯定的,因为该可执行文件的架构是riscv64,而当前环境架构是x86

4.2、编译在多核上运行的coremark.rvexe

<1> 首先需要修改make文件(在LFLAGAS_END变量添加-lpthread``)

imaginemiracle@:coremark$ cd riscv64
imaginemriacle@:riscv64$ vim core_portme.mak
#coremark/riscv64/core_protme.mak
#File: core_portme.mak

# Flag: OUTFLAG
#   Use this flag to define how to to get an executable (e.g -o)
OUTFLAG= -o
# Flag: CC
#   Use this flag to define compiler to use
CC = riscv64-unknown-linux-gnu-gcc
#CC = gcc
# Flag: CFLAGS
#   Use this flag to define compiler options. Note, you can add compiler options from the command line using XCFLAGS="other flags"
PORT_CFLAGS = -O2 -fno-common -funroll-loops -finline-functions --param max-inline-insns-auto=20 -falign-functions=4 -falign-jumps=4 -falign-loops=4 -
FLAGS_STR = "$(PORT_CFLAGS) $(XCFLAGS) $(XLFLAGS) $(LFLAGS_END)"
CFLAGS = $(PORT_CFLAGS) -I$(PORT_DIR) -I. -DFLAGS_STR=\"$(FLAGS_STR)\"
#Flag: LFLAGS_END
#   Define any libraries needed for linking or other flags that should come at the end of the link line (e.g. linker scripts). 
#   Note: On certain platforms, the default clock_gettime implementation is supported but requires linking of librt.
#===============================Alter by me===========================
LFLAGS_END += -lrt -lpthread                                          
#===============================Alter by me===========================                                                                                
# Flag: PORT_SRCS
# Port specific source files can be added here
PORT_SRCS = $(PORT_DIR)/core_portme.c

<2> 编译生成coremark.rvexe

可以使用XCFLAGS=-DMULTITHREAD=N其中的N是需要并行运行的线程数,如下使用的编译命令则是使用POSIX Threads API将源码编译在4个内核上运行的CoreMark测试:

imaginemiracle@:coremark$ make PORT_DIR=riscv64  XCFLAGS="-DMULTITHREAD=4 -DUSE_PTHREAD"

编译结果如下,会出现错误。

make XCFLAGS="-DMULTITHREAD=4 -DUSE_PTHREAD -DPERFORMANCE_RUN=1" load run1.log
make[1]: Entering directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make port_prebuild
make[2]: Entering directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make[2]: Nothing to be done for 'port_prebuild'.
make[2]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make link
make[2]: Entering directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
riscv64-unknown-linux-gnu-gcc -O2 -fno-common -funroll-loops -finline-functions --param max-inline-insns-auto=20 -falign-functions=4 -falign-jumps=4 -falign-loops=4 --param inline-min-speedup=10 -Iriscv64 -I. -DFLAGS_STR=\""-O2 -fno-common -funroll-loops -finline-functions --param max-inline-insns-auto=20 -falign-functions=4 -falign-jumps=4 -falign-loops=4 --param inline-min-speedup=10 -DMULTITHREAD=4 -DUSE_PTHREAD -DPERFORMANCE_RUN=1  -lrt -lpthread"\" -DITERATIONS=0 -DMULTITHREAD=4 -DUSE_PTHREAD -DPERFORMANCE_RUN=1 core_list_join.c core_main.c core_matrix.c core_state.c core_util.c riscv64/core_portme.c -o ./coremark.rvexe -lrt -lpthread
Link performed along with compile
make[2]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make port_postbuild
make[2]: Entering directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make[2]: Nothing to be done for 'port_postbuild'.
make[2]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make port_preload
make[2]: Entering directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make[2]: Nothing to be done for 'port_preload'.
make[2]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
echo Loading done ./coremark.rvexe
Loading done ./coremark.rvexe
make port_postload
make[2]: Entering directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make[2]: Nothing to be done for 'port_postload'.
make[2]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make port_prerun
make[2]: Entering directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make[2]: Nothing to be done for 'port_prerun'.
make[2]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
./coremark.rvexe  0x0 0x0 0x66 0 7 1 2000 > ./run1.log
/bin/sh: 1: ./coremark.rvexe: Exec format error
make[1]: *** [Makefile:112: run1.log] Error 2
make[1]: Leaving directory '/media/imaginemiracle/Disk_D/Linux_Workspace/riscv-project/File_System_test/sifive_coremark/coremark-riscv64'
make: *** [Makefile:99: rerun] Error 2

与上面的错误类似,都是在运行非X86架构的程序报的错误信息,因此不必担心,直接将编译好的coremark.rvexe拷贝到目标平台即可。

5、在目标平台上运行coremark.rvexe测试性能

下图是在我们自研CPU上CoreMark的测试结果,其中Iterations/Sec就是在跑分榜上CoreMark的值,根据该值和其它参数可以对比各微处理器的性能。

<1> 运行coremark.rvexe

执行命令:./coremark.rvexe
[注] 多核与单核coremark测试相同,直接运行coremark的可执行程序即可,下图是单核coremark的测试结果
在这里插入图片描述

6、CoreMark跑分榜

目前可以看到EEMBC已经上传了592款型号的微控制器CoreMark跑分结果,可以在EEMBC的coremark/scores里看到。(注:下图获取时间为2020-12-31
在这里插入图片描述