Lichee_RV学习系列---移植dhrystone

Lichee_RV学习系列文章目录

Lichee_RV学习系列—认识Lichee_RV、环境搭建和编译第一个程序


文章目录

  • Lichee_RV学习系列文章目录
  • 一、dhrystone简介
  • 二、dhrystone源码下载
  • 三、dhrystone移植
    • 1、移植官方源码
    • 2、移植GitHub开源代码
      • a:修改Makefile文件
      • b:编译
    • 3、执行dhrstone代码
  • 总结


一、dhrystone简介

dhrystone是测试CPU运算能力的一个用C编写的程序,检验CPU效率的计量单位是每秒计算多少次Dhrystone。

缺陷:

  • 该程序是用C编写的,容易受到编译器影响,不同优化选项下,测试的性能并不一致。
  • 代码量小,在现代CPU中,它能够被放进指令缓存中,所以它并不能严格的测量取指性能。

参考链接1
百度百科参考链接

二、dhrystone源码下载

dhrystone核心的文件就三个:dhry_1.c、dhry_2.c、dhry.h,只要把这三个文件修改一下就可以了

官方dhrystone 2.1源码
Github:iwannatto,开源的代码
之前写K210的文章有介绍一个Github加速器(往下拉就能找到了),访问不了github可以看看这个文章

三、dhrystone移植

1、移植官方源码

移植可以直接移植官方的,但是如果移植官方的会有比较多的错误。
我移植过程出现的错误就有,一个个修改起来比较麻烦:

  • 常见C库函数的重定义(malloc等)
  • 没有包含使用到的C库的头文件:如
  • 一些系统定义的变量,如系统中断HZ变量没有包含
  • 函数没有声明等

这个是我编译官方文件出现的问题:

dhry_1.c: In function 'main':
dhry_1.c:150:5: warning: implicit declaration of function 'Proc_5' [-Wimplicit-function-declaration]
    Proc_5();
    ^~~~~~
dhry_1.c:151:5: warning: implicit declaration of function 'Proc_4' [-Wimplicit-function-declaration]
    Proc_4();
    ^~~~~~
dhry_1.c:157:19: warning: implicit declaration of function 'Func_2'; did you mean 'Func_1'? [-Wimplicit-function-declaration]
    Bool_Glob = ! Func_2 (Str_1_Loc, Str_2_Loc);
                  ^~~~~~
                  Func_1
dhry_1.c:163:7: warning: implicit declaration of function 'Proc_7' [-Wimplicit-function-declaration]
      Proc_7 (Int_1_Loc, Int_2_Loc, &Int_3_Loc);
      ^~~~~~
dhry_1.c:168:5: warning: implicit declaration of function 'Proc_8' [-Wimplicit-function-declaration]
    Proc_8 (Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
    ^~~~~~
dhry_1.c:170:5: warning: implicit declaration of function 'Proc_1'; did you mean 'Func_1'? [-Wimplicit-function-declaration]
    Proc_1 (Ptr_Glob);
    ^~~~~~
    Func_1
dhry_1.c:177:9: warning: implicit declaration of function 'Proc_6' [-Wimplicit-function-declaration]
        Proc_6 (Ident_1, &Enum_Loc);
        ^~~~~~
dhry_1.c:188:5: warning: implicit declaration of function 'Proc_2' [-Wimplicit-function-declaration]
    Proc_2 (&Int_1_Loc);
    ^~~~~~
dhry_1.c:225:40: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  printf ("  Ptr_Comp:          %d\n", (int) Ptr_Glob->Ptr_Comp);
                                       ^
dhry_1.c:236:40: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  printf ("  Ptr_Comp:          %d\n", (int) Next_Ptr_Glob->Ptr_Comp);
                                       ^
dhry_1.c:50:27: error: 'HZ' undeclared (first use in this function)
#define Too_Small_Time (2*HZ)
                          ^~
dhry_1.c:263:19: note: in expansion of macro 'Too_Small_Time'
  if (User_Time < Too_Small_Time)
                  ^~~~~~~~~~~~~~
dhry_1.c:50:27: note: each undeclared identifier is reported only once for each function it appears in
#define Too_Small_Time (2*HZ)
                          ^~
dhry_1.c:263:19: note: in expansion of macro 'Too_Small_Time'
  if (User_Time < Too_Small_Time)
                  ^~~~~~~~~~~~~~
dhry_1.c: At top level:
dhry_1.c:291:1: warning: return type defaults to 'int' [-Wimplicit-int]
Proc_1 (Ptr_Val_Par)
^~~~~~
dhry_1.c: In function 'Proc_1':
dhry_1.c:307:3: warning: implicit declaration of function 'Proc_3'; did you mean 'Proc_1'? [-Wimplicit-function-declaration]
  Proc_3 (&Next_Record->Ptr_Comp);
  ^~~~~~
  Proc_1
dhry_1.c: At top level:
dhry_1.c:325:1: warning: return type defaults to 'int' [-Wimplicit-int]
Proc_2 (Int_Par_Ref)
^~~~~~
dhry_1.c:348:1: warning: return type defaults to 'int' [-Wimplicit-int]
Proc_3 (Ptr_Ref_Par)
^~~~~~
dhry_1.c:363:1: warning: return type defaults to 'int' [-Wimplicit-int]
Proc_4 () /* without parameters */
^~~~~~
dhry_1.c:375:1: warning: return type defaults to 'int' [-Wimplicit-int]
Proc_5 () /* without parameters */
^~~~~~
make: *** [dhry_1.o] Error 1

2、移植GitHub开源代码

我找到github开源的代码,简单修改Makefile文件就能跑的GitHub开源代码,作者是基于dhrystone2.2版本的。

由于iwannatto,它已经把一些警告,他如我上面说到的这些警告和错误已经解决掉了。添加了Linux相关的头文件,比如#include 、#include 、#include (这些在dry.h头文件都有定义)、正确的声明了Proc_1、Proc_2等函数。

a:修改Makefile文件

需要做的就是简单修改Makefile文件即可编译代码:设置交叉工具链、把一些没有要的代码注释掉,下面是我简单修改后的Makefile文件。

.PHONY: run clean
# Set compilation tool chain
CTOOL:=riscv64-unknown-linux-gnu-
CCL:=/home/allwinner/workspace/tina-D1-H/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702
CC:=${CCL}/bin/${CTOOL}gcc
CFLAGS=-O3


run: dry2 dry2nr dry2o
#	./dry2 ${1-50000} 2>/dev/null
#	./dry2nr ${1-50000} 2>/dev/null
#	./dry2o ${1-50000} 2>/dev/null

dry2: dry.h dry1.c dry2.c
	${CC} -c ${CFLAGS} dry1.c -o dry1.o
	${CC} ${CFLAGS} dry2.c dry1.o ${LFLAGS} -o dry2 

dry2nr: dry.h dry1.c dry2.c
	${CC} -c -DREG ${CFLAGS} dry1.c -o dry1.o
	${CC} -DREG ${CFLAGS} dry2.c dry1.o ${LFLAGS} -o dry2nr

dry2o: dry.h dry1.c dry2.c
	${CC} -c -O ${CFLAGS} dry1.c -o dry1.o
	${CC} -O ${CFLAGS} dry2.c dry1.o ${LFLAGS} -o dry2o

clean:
	rm -f dry1.o dry2 dry2nr dry2o

b:编译

编译文件会有三个可执行文件,分别是dry2、dry2nr、dry2o
在这里插入图片描述

这三个文件的区别就是编译的时候使用了不同的参数。
dry2:普通的编译
dry2nr:编译加了-DREG
dry2o:编译添加了-O,优化了代码

3、执行dhrstone代码

执行先需要先将整个目录放到开发板,直接使用adb push windows目录 Linux目录,将文件送到开发板(ADB使用可以看我这个系列的第一篇文章)
在这里插入图片描述
修改文件权限:chmod 777 dry2 dry2nr dry2o

下面是我执行dry2o后的结果:在我这块Lichee Rv Docx CPU运行的效率为:每秒运行Dhrystones的次数为:3003003

root@MaixLinux:/mnt/dhrystone# ./dry2o

Dhrystone Benchmark, Version C, Version 2.2
Program compiled without 'register' attribute
Using times(), HZ=100

Trying 50000 runs through Dhrystone:
Measured time too small to obtain meaningful results

Trying 500000 runs through Dhrystone:
Measured time too small to obtain meaningful results

Trying 5000000 runs through Dhrystone:
Measured time too small to obtain meaningful results

Trying 50000000 runs through Dhrystone:
Final values of the variables used in the benchmark:

Int_Glob:            5
        should be:   5
Bool_Glob:           1
        should be:   1
Ch_1_Glob:           A
        should be:   A
Ch_2_Glob:           B
        should be:   B
Arr_1_Glob[8]:       0
        should be:   7
Arr_2_Glob[8][7]:    50000010
        should be:   Number_Of_Runs + 10
Ptr_Glob->
  Ptr_Comp:          0x6417260
        should be:   (implementation-dependent)
  Discr:             0
        should be:   0
  Enum_Comp:         2
        should be:   2
  Int_Comp:          17
        should be:   17
  Str_Comp:          DHRYSTONE PROGRAM, SOME STRING
        should be:   DHRYSTONE PROGRAM, SOME STRING
Next_Ptr_Glob->
  Ptr_Comp:          0x6417260
        should be:   (implementation-dependent), same as above
  Discr:             0
        should be:   0
  Enum_Comp:         1
        should be:   1
  Int_Comp:          18
        should be:   18
  Str_Comp:          DHRYSTONE PROGRAM, SOME STRING
        should be:   DHRYSTONE PROGRAM, SOME STRING
Int_1_Loc:           5
        should be:   5
Int_2_Loc:           13
        should be:   13
Int_3_Loc:           7
        should be:   7
Enum_Loc:            1
        should be:   1
Str_1_Loc:           DHRYSTONE PROGRAM, 1'ST STRING
        should be:   DHRYSTONE PROGRAM, 1'ST STRING
Str_2_Loc:           DHRYSTONE PROGRAM, 2'ND STRING
        should be:   DHRYSTONE PROGRAM, 2'ND STRING

Microseconds for one run through Dhrystone:        0.3
Dhrystones per Second:                         3003003

这是我运行dry2执行文件的结果:
每秒运行Dhrystones的次数为:1126126

root@MaixLinux:/mnt/dhrystone# ./dry2

Dhrystone Benchmark, Version C, Version 2.2
Program compiled without 'register' attribute
Using times(), HZ=100

Trying 50000 runs through Dhrystone:
Measured time too small to obtain meaningful results

Trying 500000 runs through Dhrystone:
Measured time too small to obtain meaningful results

Trying 5000000 runs through Dhrystone:
Final values of the variables used in the benchmark:

Int_Glob:            5
        should be:   5
Bool_Glob:           1
        should be:   1
Ch_1_Glob:           A
        should be:   A
Ch_2_Glob:           B
        should be:   B
Arr_1_Glob[8]:       7
        should be:   7
Arr_2_Glob[8][7]:    5000010
        should be:   Number_Of_Runs + 10
Ptr_Glob->
  Ptr_Comp:          0x2db27260
        should be:   (implementation-dependent)
  Discr:             0
        should be:   0
  Enum_Comp:         2
        should be:   2
  Int_Comp:          17
        should be:   17
  Str_Comp:          DHRYSTONE PROGRAM, SOME STRING
        should be:   DHRYSTONE PROGRAM, SOME STRING
Next_Ptr_Glob->
  Ptr_Comp:          0x2db27260
        should be:   (implementation-dependent), same as above
  Discr:             0
        should be:   0
  Enum_Comp:         1
        should be:   1
  Int_Comp:          18
        should be:   18
  Str_Comp:          DHRYSTONE PROGRAM, SOME STRING
        should be:   DHRYSTONE PROGRAM, SOME STRING
Int_1_Loc:           5
        should be:   5
Int_2_Loc:           13
        should be:   13
Int_3_Loc:           7
        should be:   7
Enum_Loc:            1
        should be:   1
Str_1_Loc:           DHRYSTONE PROGRAM, 1'ST STRING
        should be:   DHRYSTONE PROGRAM, 1'ST STRING
Str_2_Loc:           DHRYSTONE PROGRAM, 2'ND STRING
        should be:   DHRYSTONE PROGRAM, 2'ND STRING

Microseconds for one run through Dhrystone:        0.9
Dhrystones per Second:                         1126126

总结

dhrystone运行的结果我觉得不是特别有参考价值,不太优化结果下,运行的结果都不一样。而且数据相差挺大的,每秒运行Dhrystones次数差不多三倍。所以dhrystone得到的运行的结果看看得了,感觉并不是特别能反映CPU的运行效率。

你可能感兴趣的:(Lichee,Rv,Dock学习系列,学习,linux)