Commit d5528cc1 authored by Geert Uytterhoeven's avatar Geert Uytterhoeven Committed by Andrew Morton

lib: add Dhrystone benchmark test

When working on SoC bring-up, (a full) userspace may not be available,
making it hard to benchmark the CPU performance of the system under
development.  Still, one may want to have a rough idea of the (relative)
performance of one or more CPU cores, especially when working on e.g.  the
clock driver that controls the CPU core clock(s).

Hence make the classical Dhrystone 2.1 benchmark available as a Linux
kernel test module, based on[1].

When built-in, this benchmark can be run without any userspace present.

Parallel runs (run on multiple CPU cores) are supported, just kick the
"run" file multiple times.

Note that the actual figures depend on the configuration options that
control compiler optimization (e.g.  CONFIG_CC_OPTIMIZE_FOR_SIZE vs. 
CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE), and on the compiler options used when
building the kernel in general.  Hence numbers may differ from those
obtained by running similar benchmarks in userspace.

[1] https://github.com/qris/dhrystone-deb.gitSigned-off-by: default avatarGeert Uytterhoeven <geert+renesas@glider.be>
Link: https://lkml.kernel.org/r/4d07ad990740a5f1e426ce4566fb514f60ec9bdd.1670509558.git.geert+renesas@glider.beSigned-off-by: default avatarGeert Uytterhoeven <geert+renesas@glider.be>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Brendan Higgins <brendanhiggins@google.com>
Cc: David Gow <davidgow@google.com>
[geert+renesas@glider.be: fix uninitialized use of ret]
 Link: https://lkml.kernel.org/r/alpine.DEB.2.22.394.2212190857310.137329@ramsan.of.borgSigned-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 88ad32a7
...@@ -2079,6 +2079,41 @@ menuconfig RUNTIME_TESTING_MENU ...@@ -2079,6 +2079,41 @@ menuconfig RUNTIME_TESTING_MENU
if RUNTIME_TESTING_MENU if RUNTIME_TESTING_MENU
config TEST_DHRY
tristate "Dhrystone benchmark test"
help
Enable this to include the Dhrystone 2.1 benchmark. This test
calculates the number of Dhrystones per second, and the number of
DMIPS (Dhrystone MIPS) obtained when the Dhrystone score is divided
by 1757 (the number of Dhrystones per second obtained on the VAX
11/780, nominally a 1 MIPS machine).
To run the benchmark, it needs to be enabled explicitly, either from
the kernel command line (when built-in), or from userspace (when
built-in or modular.
Run once during kernel boot:
test_dhry.run
Set number of iterations from kernel command line:
test_dhry.iterations=<n>
Set number of iterations from userspace:
echo <n> > /sys/module/test_dhry/parameters/iterations
Trigger manual run from userspace:
echo y > /sys/module/test_dhry/parameters/run
If the number of iterations is <= 0, the test will devise a suitable
number of iterations (test runs for at least 2s) automatically.
This process takes ca. 4s.
If unsure, say N.
config LKDTM config LKDTM
tristate "Linux Kernel Dump Test Tool Module" tristate "Linux Kernel Dump Test Tool Module"
depends on DEBUG_FS depends on DEBUG_FS
......
...@@ -57,6 +57,8 @@ obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o ...@@ -57,6 +57,8 @@ obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
obj-y += kstrtox.o obj-y += kstrtox.o
obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o obj-$(CONFIG_FIND_BIT_BENCHMARK) += find_bit_benchmark.o
obj-$(CONFIG_TEST_BPF) += test_bpf.o obj-$(CONFIG_TEST_BPF) += test_bpf.o
test_dhry-objs := dhry_1.o dhry_2.o dhry_run.o
obj-$(CONFIG_TEST_DHRY) += test_dhry.o
obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
obj-$(CONFIG_TEST_BITOPS) += test_bitops.o obj-$(CONFIG_TEST_BITOPS) += test_bitops.o
CFLAGS_test_bitops.o += -Werror CFLAGS_test_bitops.o += -Werror
......
This diff is collapsed.
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/*
****************************************************************************
*
* "DHRYSTONE" Benchmark Program
* -----------------------------
*
* Version: C, Version 2.1
*
* File: dhry_1.c (part 2 of 3)
*
* Date: May 25, 1988
*
* Author: Reinhold P. Weicker
*
****************************************************************************
*/
#include "dhry.h"
#include <linux/ktime.h>
#include <linux/slab.h>
#include <linux/string.h>
/* Global Variables: */
int Int_Glob;
char Ch_1_Glob;
static Rec_Pointer Ptr_Glob, Next_Ptr_Glob;
static Boolean Bool_Glob;
static char Ch_2_Glob;
static int Arr_1_Glob[50];
static int Arr_2_Glob[50][50];
static void Proc_3(Rec_Pointer *Ptr_Ref_Par)
/******************/
/* executed once */
/* Ptr_Ref_Par becomes Ptr_Glob */
{
if (Ptr_Glob) {
/* then, executed */
*Ptr_Ref_Par = Ptr_Glob->Ptr_Comp;
}
Proc_7(10, Int_Glob, &Ptr_Glob->variant.var_1.Int_Comp);
} /* Proc_3 */
static void Proc_1(Rec_Pointer Ptr_Val_Par)
/******************/
/* executed once */
{
Rec_Pointer Next_Record = Ptr_Val_Par->Ptr_Comp;
/* == Ptr_Glob_Next */
/* Local variable, initialized with Ptr_Val_Par->Ptr_Comp, */
/* corresponds to "rename" in Ada, "with" in Pascal */
*Ptr_Val_Par->Ptr_Comp = *Ptr_Glob;
Ptr_Val_Par->variant.var_1.Int_Comp = 5;
Next_Record->variant.var_1.Int_Comp =
Ptr_Val_Par->variant.var_1.Int_Comp;
Next_Record->Ptr_Comp = Ptr_Val_Par->Ptr_Comp;
Proc_3(&Next_Record->Ptr_Comp);
/* Ptr_Val_Par->Ptr_Comp->Ptr_Comp == Ptr_Glob->Ptr_Comp */
if (Next_Record->Discr == Ident_1) {
/* then, executed */
Next_Record->variant.var_1.Int_Comp = 6;
Proc_6(Ptr_Val_Par->variant.var_1.Enum_Comp,
&Next_Record->variant.var_1.Enum_Comp);
Next_Record->Ptr_Comp = Ptr_Glob->Ptr_Comp;
Proc_7(Next_Record->variant.var_1.Int_Comp, 10,
&Next_Record->variant.var_1.Int_Comp);
} else {
/* not executed */
*Ptr_Val_Par = *Ptr_Val_Par->Ptr_Comp;
}
} /* Proc_1 */
static void Proc_2(One_Fifty *Int_Par_Ref)
/******************/
/* executed once */
/* *Int_Par_Ref == 1, becomes 4 */
{
One_Fifty Int_Loc;
Enumeration Enum_Loc;
Int_Loc = *Int_Par_Ref + 10;
do {
/* executed once */
if (Ch_1_Glob == 'A') {
/* then, executed */
Int_Loc -= 1;
*Int_Par_Ref = Int_Loc - Int_Glob;
Enum_Loc = Ident_1;
} /* if */
} while (Enum_Loc != Ident_1); /* true */
} /* Proc_2 */
static void Proc_4(void)
/*******/
/* executed once */
{
Boolean Bool_Loc;
Bool_Loc = Ch_1_Glob == 'A';
Bool_Glob = Bool_Loc | Bool_Glob;
Ch_2_Glob = 'B';
} /* Proc_4 */
static void Proc_5(void)
/*******/
/* executed once */
{
Ch_1_Glob = 'A';
Bool_Glob = false;
} /* Proc_5 */
int dhry(int n)
/*****/
/* main program, corresponds to procedures */
/* Main and Proc_0 in the Ada version */
{
One_Fifty Int_1_Loc;
One_Fifty Int_2_Loc;
One_Fifty Int_3_Loc;
char Ch_Index;
Enumeration Enum_Loc;
Str_30 Str_1_Loc;
Str_30 Str_2_Loc;
int Run_Index;
int Number_Of_Runs;
ktime_t Begin_Time, End_Time;
u32 User_Time;
/* Initializations */
Next_Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_KERNEL);
Ptr_Glob = (Rec_Pointer)kzalloc(sizeof(Rec_Type), GFP_KERNEL);
Ptr_Glob->Ptr_Comp = Next_Ptr_Glob;
Ptr_Glob->Discr = Ident_1;
Ptr_Glob->variant.var_1.Enum_Comp = Ident_3;
Ptr_Glob->variant.var_1.Int_Comp = 40;
strcpy(Ptr_Glob->variant.var_1.Str_Comp,
"DHRYSTONE PROGRAM, SOME STRING");
strcpy(Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
Arr_2_Glob[8][7] = 10;
/* Was missing in published program. Without this statement, */
/* Arr_2_Glob[8][7] would have an undefined value. */
/* Warning: With 16-Bit processors and Number_Of_Runs > 32000, */
/* overflow may occur for this array element. */
pr_debug("Dhrystone Benchmark, Version 2.1 (Language: C)\n");
Number_Of_Runs = n;
pr_debug("Execution starts, %d runs through Dhrystone\n",
Number_Of_Runs);
/***************/
/* Start timer */
/***************/
Begin_Time = ktime_get();
for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index) {
Proc_5();
Proc_4();
/* Ch_1_Glob == 'A', Ch_2_Glob == 'B', Bool_Glob == true */
Int_1_Loc = 2;
Int_2_Loc = 3;
strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
Enum_Loc = Ident_2;
Bool_Glob = !Func_2(Str_1_Loc, Str_2_Loc);
/* Bool_Glob == 1 */
while (Int_1_Loc < Int_2_Loc) {
/* loop body executed once */
Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
/* Int_3_Loc == 7 */
Proc_7(Int_1_Loc, Int_2_Loc, &Int_3_Loc);
/* Int_3_Loc == 7 */
Int_1_Loc += 1;
} /* while */
/* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
Proc_8(Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
/* Int_Glob == 5 */
Proc_1(Ptr_Glob);
for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index) {
/* loop body executed twice */
if (Enum_Loc == Func_1(Ch_Index, 'C')) {
/* then, not executed */
Proc_6(Ident_1, &Enum_Loc);
strcpy(Str_2_Loc, "DHRYSTONE PROGRAM, 3'RD STRING");
Int_2_Loc = Run_Index;
Int_Glob = Run_Index;
}
}
/* Int_1_Loc == 3, Int_2_Loc == 3, Int_3_Loc == 7 */
Int_2_Loc = Int_2_Loc * Int_1_Loc;
Int_1_Loc = Int_2_Loc / Int_3_Loc;
Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
/* Int_1_Loc == 1, Int_2_Loc == 13, Int_3_Loc == 7 */
Proc_2(&Int_1_Loc);
/* Int_1_Loc == 5 */
} /* loop "for Run_Index" */
/**************/
/* Stop timer */
/**************/
End_Time = ktime_get();
#define dhry_assert_int_eq(val, expected) \
if (val != expected) \
pr_err("%s: %d (FAIL, expected %d)\n", #val, val, \
expected); \
else \
pr_debug("%s: %d (OK)\n", #val, val)
#define dhry_assert_char_eq(val, expected) \
if (val != expected) \
pr_err("%s: %c (FAIL, expected %c)\n", #val, val, \
expected); \
else \
pr_debug("%s: %c (OK)\n", #val, val)
#define dhry_assert_string_eq(val, expected) \
if (strcmp(val, expected)) \
pr_err("%s: %s (FAIL, expected %s)\n", #val, val, \
expected); \
else \
pr_debug("%s: %s (OK)\n", #val, val)
pr_debug("Execution ends\n");
pr_debug("Final values of the variables used in the benchmark:\n");
dhry_assert_int_eq(Int_Glob, 5);
dhry_assert_int_eq(Bool_Glob, 1);
dhry_assert_char_eq(Ch_1_Glob, 'A');
dhry_assert_char_eq(Ch_2_Glob, 'B');
dhry_assert_int_eq(Arr_1_Glob[8], 7);
dhry_assert_int_eq(Arr_2_Glob[8][7], Number_Of_Runs + 10);
pr_debug("Ptr_Comp: %px\n", Ptr_Glob->Ptr_Comp);
dhry_assert_int_eq(Ptr_Glob->Discr, 0);
dhry_assert_int_eq(Ptr_Glob->variant.var_1.Enum_Comp, 2);
dhry_assert_int_eq(Ptr_Glob->variant.var_1.Int_Comp, 17);
dhry_assert_string_eq(Ptr_Glob->variant.var_1.Str_Comp,
"DHRYSTONE PROGRAM, SOME STRING");
if (Next_Ptr_Glob->Ptr_Comp != Ptr_Glob->Ptr_Comp)
pr_err("Next_Ptr_Glob->Ptr_Comp: %px (expected %px)\n",
Next_Ptr_Glob->Ptr_Comp, Ptr_Glob->Ptr_Comp);
else
pr_debug("Next_Ptr_Glob->Ptr_Comp: %px\n",
Next_Ptr_Glob->Ptr_Comp);
dhry_assert_int_eq(Next_Ptr_Glob->Discr, 0);
dhry_assert_int_eq(Next_Ptr_Glob->variant.var_1.Enum_Comp, 1);
dhry_assert_int_eq(Next_Ptr_Glob->variant.var_1.Int_Comp, 18);
dhry_assert_string_eq(Next_Ptr_Glob->variant.var_1.Str_Comp,
"DHRYSTONE PROGRAM, SOME STRING");
dhry_assert_int_eq(Int_1_Loc, 5);
dhry_assert_int_eq(Int_2_Loc, 13);
dhry_assert_int_eq(Int_3_Loc, 7);
dhry_assert_int_eq(Enum_Loc, 1);
dhry_assert_string_eq(Str_1_Loc, "DHRYSTONE PROGRAM, 1'ST STRING");
dhry_assert_string_eq(Str_2_Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
User_Time = ktime_to_ms(ktime_sub(End_Time, Begin_Time));
kfree(Ptr_Glob);
kfree(Next_Ptr_Glob);
/* Measurements should last at least 2 seconds */
if (User_Time < 2 * MSEC_PER_SEC)
return -EAGAIN;
return div_u64(mul_u32_u32(MSEC_PER_SEC, Number_Of_Runs), User_Time);
}
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/*
****************************************************************************
*
* "DHRYSTONE" Benchmark Program
* -----------------------------
*
* Version: C, Version 2.1
*
* File: dhry_2.c (part 3 of 3)
*
* Date: May 25, 1988
*
* Author: Reinhold P. Weicker
*
****************************************************************************
*/
#include "dhry.h"
#include <linux/string.h>
static Boolean Func_3(Enumeration Enum_Par_Val)
/***************************/
/* executed once */
/* Enum_Par_Val == Ident_3 */
{
Enumeration Enum_Loc;
Enum_Loc = Enum_Par_Val;
if (Enum_Loc == Ident_3) {
/* then, executed */
return true;
} else {
/* not executed */
return false;
}
} /* Func_3 */
void Proc_6(Enumeration Enum_Val_Par, Enumeration *Enum_Ref_Par)
/*********************************/
/* executed once */
/* Enum_Val_Par == Ident_3, Enum_Ref_Par becomes Ident_2 */
{
*Enum_Ref_Par = Enum_Val_Par;
if (!Func_3(Enum_Val_Par)) {
/* then, not executed */
*Enum_Ref_Par = Ident_4;
}
switch (Enum_Val_Par) {
case Ident_1:
*Enum_Ref_Par = Ident_1;
break;
case Ident_2:
if (Int_Glob > 100) {
/* then */
*Enum_Ref_Par = Ident_1;
} else {
*Enum_Ref_Par = Ident_4;
}
break;
case Ident_3: /* executed */
*Enum_Ref_Par = Ident_2;
break;
case Ident_4:
break;
case Ident_5:
*Enum_Ref_Par = Ident_3;
break;
} /* switch */
} /* Proc_6 */
void Proc_7(One_Fifty Int_1_Par_Val, One_Fifty Int_2_Par_Val, One_Fifty *Int_Par_Ref)
/**********************************************/
/* executed three times */
/* first call: Int_1_Par_Val == 2, Int_2_Par_Val == 3, */
/* Int_Par_Ref becomes 7 */
/* second call: Int_1_Par_Val == 10, Int_2_Par_Val == 5, */
/* Int_Par_Ref becomes 17 */
/* third call: Int_1_Par_Val == 6, Int_2_Par_Val == 10, */
/* Int_Par_Ref becomes 18 */
{
One_Fifty Int_Loc;
Int_Loc = Int_1_Par_Val + 2;
*Int_Par_Ref = Int_2_Par_Val + Int_Loc;
} /* Proc_7 */
void Proc_8(Arr_1_Dim Arr_1_Par_Ref, Arr_2_Dim Arr_2_Par_Ref, int Int_1_Par_Val, int Int_2_Par_Val)
/*********************************************************************/
/* executed once */
/* Int_Par_Val_1 == 3 */
/* Int_Par_Val_2 == 7 */
{
One_Fifty Int_Index;
One_Fifty Int_Loc;
Int_Loc = Int_1_Par_Val + 5;
Arr_1_Par_Ref[Int_Loc] = Int_2_Par_Val;
Arr_1_Par_Ref[Int_Loc+1] = Arr_1_Par_Ref[Int_Loc];
Arr_1_Par_Ref[Int_Loc+30] = Int_Loc;
for (Int_Index = Int_Loc; Int_Index <= Int_Loc+1; ++Int_Index)
Arr_2_Par_Ref[Int_Loc][Int_Index] = Int_Loc;
Arr_2_Par_Ref[Int_Loc][Int_Loc-1] += 1;
Arr_2_Par_Ref[Int_Loc+20][Int_Loc] = Arr_1_Par_Ref[Int_Loc];
Int_Glob = 5;
} /* Proc_8 */
Enumeration Func_1(Capital_Letter Ch_1_Par_Val, Capital_Letter Ch_2_Par_Val)
/*************************************************/
/* executed three times */
/* first call: Ch_1_Par_Val == 'H', Ch_2_Par_Val == 'R' */
/* second call: Ch_1_Par_Val == 'A', Ch_2_Par_Val == 'C' */
/* third call: Ch_1_Par_Val == 'B', Ch_2_Par_Val == 'C' */
{
Capital_Letter Ch_1_Loc;
Capital_Letter Ch_2_Loc;
Ch_1_Loc = Ch_1_Par_Val;
Ch_2_Loc = Ch_1_Loc;
if (Ch_2_Loc != Ch_2_Par_Val) {
/* then, executed */
return Ident_1;
} else {
/* not executed */
Ch_1_Glob = Ch_1_Loc;
return Ident_2;
}
} /* Func_1 */
Boolean Func_2(Str_30 Str_1_Par_Ref, Str_30 Str_2_Par_Ref)
/*************************************************/
/* executed once */
/* Str_1_Par_Ref == "DHRYSTONE PROGRAM, 1'ST STRING" */
/* Str_2_Par_Ref == "DHRYSTONE PROGRAM, 2'ND STRING" */
{
One_Thirty Int_Loc;
Capital_Letter Ch_Loc;
Int_Loc = 2;
while (Int_Loc <= 2) {
/* loop body executed once */
if (Func_1(Str_1_Par_Ref[Int_Loc],
Str_2_Par_Ref[Int_Loc+1]) == Ident_1) {
/* then, executed */
Ch_Loc = 'A';
Int_Loc += 1;
}
} /* if, while */
if (Ch_Loc >= 'W' && Ch_Loc < 'Z') {
/* then, not executed */
Int_Loc = 7;
}
if (Ch_Loc == 'R') {
/* then, not executed */
return true;
} else {
/* executed */
if (strcmp(Str_1_Par_Ref, Str_2_Par_Ref) > 0) {
/* then, not executed */
Int_Loc += 7;
Int_Glob = Int_Loc;
return true;
} else {
/* executed */
return false;
}
} /* if Ch_Loc */
} /* Func_2 */
// SPDX-License-Identifier: GPL-2.0-only
/*
* Dhrystone benchmark test module
*
* Copyright (C) 2022 Glider bv
*/
#include "dhry.h"
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/smp.h>
#define DHRY_VAX 1757
static int dhry_run_set(const char *val, const struct kernel_param *kp);
static const struct kernel_param_ops run_ops = {
.flags = KERNEL_PARAM_OPS_FL_NOARG,
.set = dhry_run_set,
};
static bool dhry_run;
module_param_cb(run, &run_ops, &dhry_run, 0200);
MODULE_PARM_DESC(run, "Run the test (default: false)");
static int iterations = -1;
module_param(iterations, int, 0644);
MODULE_PARM_DESC(iterations,
"Number of iterations through the benchmark (default: auto)");
static void dhry_benchmark(void)
{
int i, n;
if (iterations > 0) {
n = dhry(iterations);
goto report;
}
for (i = DHRY_VAX; i > 0; i <<= 1) {
n = dhry(i);
if (n != -EAGAIN)
break;
}
report:
if (n >= 0)
pr_info("CPU%u: Dhrystones per Second: %d (%d DMIPS)\n",
smp_processor_id(), n, n / DHRY_VAX);
else if (n == -EAGAIN)
pr_err("Please increase the number of iterations\n");
else
pr_err("Dhrystone benchmark failed error %pe\n", ERR_PTR(n));
}
static int dhry_run_set(const char *val, const struct kernel_param *kp)
{
int ret;
if (val) {
ret = param_set_bool(val, kp);
if (ret)
return ret;
} else {
dhry_run = true;
}
if (dhry_run && system_state == SYSTEM_RUNNING)
dhry_benchmark();
return 0;
}
static int __init dhry_init(void)
{
if (dhry_run)
dhry_benchmark();
return 0;
}
module_init(dhry_init);
MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
MODULE_LICENSE("GPL");
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment