CPU性能测试_Dhrystone基准测试

发布于:2024-05-08 ⋅ 阅读:(49) ⋅ 点赞:(0)

一、Dhrystone简介
Dhrystone是测量处理器运算能力的最常见基准程序之一,常用于处理器的整型运算性能的测量。程序是用C语言编写的,因此C编译器的编译效率对测试结果也有很大影响。

但其也有许多不足,Dhrystone不仅不适合于作为嵌入式系统的测试向量,甚至在其大多数场合下都不适合进行应用。Dhrystone还有许多漏洞,例如:易被非法利用、人为痕迹明显、代码长度太短、缺乏验证及标准的运行规则等。该程序是用C编写的,容易受到编译器影响,不同优化选项下,测试的性能并不一致。代码量小,在现代CPU中,它能够被放进指令缓存中,所以它并不能严格的测量取指性能。

Dhrystone标准的测试方法很简单,就是单位时间内跑了多少次Dhrystone程序,其指标单位为DMIPS/MHz。MIPS是Million Instructions Per Second的缩写,每秒处理的百万级的机器语言指令数。DMIPS中的D是Dhrystone的缩写,它表示了在Dhrystone标准的测试方法下的MIPS。

关于DMIPS有一个不得不注意的点,因为历史原因我们把在VAX-11/780机器上的测试结果1757 Dhrystones/s定义为1 DMIPS,因此在其他平台测试到的每秒Dhrystones数应除以1757,才是真正的DMIPS数值,故DMIPS其实表示的是一个相对值。

二、源码获取

http://www.roylongbottom.org.uk/classic_benchmarks.tar.gz

下载后解压

tar -xvf classic_benchmarks.tar.gz

三、操作使用

在\classic_benchmarks\source_code\dhrystone2\下可找到源代码,其中

dhry.h --关于兼容性的原型定义,

dhry_1.c --主程序入口,

dhry_2.c --算法子程序,

根据README文件的内容提示,


admin@ubuntu2004:~/localization_shared/tools/classic_benchmarks/source_code/dhrystone2$ cat README
 Instructions 32 Bit Version

 Copy dhry_1.c, dhry_2.c and dhry.h to a new folder and the
 following cpuida.o, cpuidc.o and cpuidh.h from common_32bit.

 Line 31 in dhry_1.c change options to "No Opt", "Opt 2" or "Opt 3".

 Compile and link using one or more of the following
 commands in Terminal pointing to the new folder.

 gcc dhry_1.c dhry_2.c cpuidc.o cpuida.o -lrt -lc -lm -o dhry2
 gcc dhry_1.c dhry_2.c cpuidc.o cpuida.o -lrt -lc -lm -O2 -o dhry22
 gcc dhry_1.c dhry_2.c cpuidc.o cpuida.o -lrt -lc -lm -O3 -o dhry23

 Execute using ./dhry2, ./dhry22 or ./dhryt23.
 Results should be displayed and saved in Dhry.txt.


 Instructions 64 Bit Version

 Copy dhry_1.c, dhry_2.c and dhry.h to a new folder and the following
 cpuida64.o, cpuidc64.o and cpuidh.h from common_64bit.

 Line 31 in dhry_1.c change options to "No Opt 64", "Opt 2 64" or "Opt 3 64".

 Compile and link using one or more of the following
 commands in Terminal pointing to the new folder.

 gcc dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -o dhry2_64
 gcc dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -O2 -o dhry22_64
 gcc dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -O3 -o dhry23_64

 Execute using ./dhry2_64, ./dhry22_64 or ./dhry23_64.
 Results should be displayed and saved in Dhry.txt.

 The execution files can be renamed later.

创建新的文件夹,例如/run,复制当前文件夹下的dhry_1.c,dhry_2.c,dhry.h到新创建的目录,复制common_64bit(64位系统)文件夹下的cpuida64.o,cpuidc64.o,cpuidh.h也到新创建的目录;

再切换到新创建的目录,修改dhry_1.c文件的第31行,修改引号中内容为"No Opt 64", 或者"Opt 2 64" ,或者 "Opt 3 64";

然后执行gcc dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -o dhry2_64,或者
 gcc dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -O2 -o dhry22_64,或者
 gcc dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -O3 -o dhry23_64,分别生成dhry2_64,dhry22_64,dhry23_64文件;

在这里我运行的时候报错,

admin@ubuntu2004:~/localization_shared/tools/classic_benchmarks/source_code/dhrystone2/run$ gcc dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -o dhry2
dhry_1.c: In function ‘main’:
dhry_1.c:158:5: warning: implicit declaration of function ‘getDetails’ [-Wimplicit-function-declaration]
  158 |     getDetails();
      |     ^~~~~~~~~~
dhry_1.c:184:27: warning: extra tokens at end of #endif directive [-Wendif-labels]
  184 |    #endif                 "Register option      Selected."
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dhry_1.c:222:8: warning: implicit declaration of function ‘start_time’; did you mean ‘strftime’? [-Wimplicit-function-declaration]
  222 |        start_time();
      |        ^~~~~~~~~~
      |        strftime
dhry_1.c:274:8: warning: implicit declaration of function ‘end_time’ [-Wimplicit-function-declaration]
  274 |        end_time();
      |        ^~~~~~~~
dhry_1.c:327:43: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  327 |    printf ("  Ptr_Comp:       *    %d\n", (int) Ptr_Glob->Ptr_Comp);
      |                                           ^
dhry_1.c:353:41: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  353 |    printf ("  Ptr_Comp:       *    %d", (int) Next_Ptr_Glob->Ptr_Comp);
      |                                         ^
dhry_1.c:448:4: warning: implicit declaration of function ‘local_time’; did you mean ‘localtime’? [-Wimplicit-function-declaration]
  448 |    local_time();
      |    ^~~~~~~~~~
      |    localtime
/usr/bin/ld: cpuidc64.o: relocation R_X86_64_32 against symbol `timeday' can not be used when making a PIE object; recompile with -fPIE
/usr/bin/ld: cpuida64.o: relocation R_X86_64_32 against symbol `eaxCode1' can not be used when making a PIE object; recompile with -fPIE
collect2: error: ld returned 1 exit status

需要改成gcc -no-pie dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -o dhry2_64执行;

admin@ubuntu2004:~/localization_shared/tools/classic_benchmarks/source_code/dhrystone2/run$ gcc -no-pie dhry_1.c dhry_2.c cpuidc64.o cpuida64.o -m64 -lrt -lc -lm -o dhry2_64
dhry_1.c: In function ‘main’:
dhry_1.c:158:5: warning: implicit declaration of function ‘getDetails’ [-Wimplicit-function-declaration]
  158 |     getDetails();
      |     ^~~~~~~~~~
dhry_1.c:184:27: warning: extra tokens at end of #endif directive [-Wendif-labels]
  184 |    #endif                 "Register option      Selected."
      |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dhry_1.c:222:8: warning: implicit declaration of function ‘start_time’; did you mean ‘strftime’? [-Wimplicit-function-declaration]
  222 |        start_time();
      |        ^~~~~~~~~~
      |        strftime
dhry_1.c:274:8: warning: implicit declaration of function ‘end_time’ [-Wimplicit-function-declaration]
  274 |        end_time();
      |        ^~~~~~~~
dhry_1.c:327:43: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  327 |    printf ("  Ptr_Comp:       *    %d\n", (int) Ptr_Glob->Ptr_Comp);
      |                                           ^
dhry_1.c:353:41: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
  353 |    printf ("  Ptr_Comp:       *    %d", (int) Next_Ptr_Glob->Ptr_Comp);
      |                                         ^
dhry_1.c:448:4: warning: implicit declaration of function ‘local_time’; did you mean ‘localtime’? [-Wimplicit-function-declaration]
  448 |    local_time();
      |    ^~~~~~~~~~
      |    localtime
admin@ubuntu2004:~/localization_shared/tools/classic_benchmarks/source_code/dhrystone2/run$ ls
cpuida64.asm  cpuida64.o  cpuidc64.c  cpuidc64.o  cpuidh.h  dhry_1.c  dhry2_64  dhry_2.c  dhry.h

最后执行./dhry2_64即可;

admin@ubuntu2004:~/localization_shared/tools/classic_benchmarks/source_code/dhrystone2/run$ ./dhry2_64
  ####################################################
  getDetails and MHz

  Assembler CPUID and RDTSC
  CPU GenuineIntel, Features Code 0F8BFBFF, Model Code 00050657
  Montage Jintide(R) C5218R
  Measured - Minimum 2100 MHz, Maximum 2100 MHz
  Linux Functions
  get_nprocs() - CPUs 8, Configured CPUs 8
  get_phys_pages() and size - RAM Size 15.58 GB, Page Size 4096 Bytes
  uname() - Linux, ubuntu2004, 5.8.0-43-generic
  #49~20.04.1-Ubuntu SMP Fri Feb 5 09:57:56 UTC 2021, x86_64

##########################################

Dhrystone Benchmark, Version 2.1 (Language: C or C++)

Optimisation    No Opt 64
Register option not selected

       10000 runs   0.00 seconds
      100000 runs   0.01 seconds
     1000000 runs   0.08 seconds
     2000000 runs   0.18 seconds
     4000000 runs   0.33 seconds
     8000000 runs   0.67 seconds
    16000000 runs   1.36 seconds
    32000000 runs   2.62 seconds

Final values (* implementation-dependent):

Int_Glob:      O.K.  5  Bool_Glob:     O.K.  1
Ch_1_Glob:     O.K.  A  Ch_2_Glob:     O.K.  B
Arr_1_Glob[8]: O.K.  7  Arr_2_Glob8/7: O.K.    32000010
Ptr_Glob->              Ptr_Comp:       *    21476480
  Discr:       O.K.  0  Enum_Comp:     O.K.  2
  Int_Comp:    O.K.  17 Str_Comp:      O.K.  DHRYSTONE PROGRAM, SOME STRING
Next_Ptr_Glob->         Ptr_Comp:       *    21476480 same as above
  Discr:       O.K.  0  Enum_Comp:     O.K.  1
  Int_Comp:    O.K.  18 Str_Comp:      O.K.  DHRYSTONE PROGRAM, SOME STRING
Int_1_Loc:     O.K.  5  Int_2_Loc:     O.K.  13
Int_3_Loc:     O.K.  7  Enum_Loc:      O.K.  1
Str_1_Loc:                             O.K.  DHRYSTONE PROGRAM, 1'ST STRING
Str_2_Loc:                             O.K.  DHRYSTONE PROGRAM, 2'ND STRING

Microseconds for one run through Dhrystone:         0.08
Dhrystones per Second:                        12202113
VAX  MIPS rating =                               6944.86

 Press Enter


admin@ubuntu2004:~/localization_shared/tools/classic_benchmarks/source_code/dhrystone2/run$

最后两行的值即为测算的Dhrystones/s和DMIPS,注意1757 Dhrystones/s定义为1 Dhrystone MIPS(百万条指令每秒)。

Dhrystones per Second:                        12202113
VAX  MIPS rating =                               6944.86

四、压缩包

附件中的_x86是在x86系统编译的,可以在run目录下直接运行./dhry2_64