From ee8274a645738f77e5c80d20f38c01a5cc2ab001 Mon Sep 17 00:00:00 2001
From: Tsutomu Muroya <tsutomu.muroya.jy@bp.renesas.com>
Date: Mon, 31 Jul 2023 12:29:59 +0900
Subject: [PATCH] Add benchmark project

Signed-off-by: Tsutomu Muroya <tsutomu.muroya.jy@bp.renesas.com>
---
 cmake/compiler/gcc/compiler_flags.cmake |   2 +-
 samples/basic/benchmark/CMakeLists.txt  |  31 ++++++
 samples/basic/benchmark/prj.conf        |   3 +
 samples/basic/benchmark/sample.yaml     |  10 ++
 samples/basic/benchmark/src/benchmark.c | 124 ++++++++++++++++++++++++
 5 files changed, 169 insertions(+), 1 deletion(-)
 create mode 100644 samples/basic/benchmark/CMakeLists.txt
 create mode 100644 samples/basic/benchmark/prj.conf
 create mode 100644 samples/basic/benchmark/sample.yaml
 create mode 100644 samples/basic/benchmark/src/benchmark.c

diff --git a/cmake/compiler/gcc/compiler_flags.cmake b/cmake/compiler/gcc/compiler_flags.cmake
index 2dbb1e3e08..a6d0acb496 100644
--- a/cmake/compiler/gcc/compiler_flags.cmake
+++ b/cmake/compiler/gcc/compiler_flags.cmake
@@ -85,7 +85,7 @@ check_set_compiler_property(APPEND PROPERTY warning_dw_3
 
 check_set_compiler_property(PROPERTY warning_extended -Wno-unused-but-set-variable)
 
-check_set_compiler_property(PROPERTY warning_error_implicit_int -Werror=implicit-int)
+## check_set_compiler_property(PROPERTY warning_error_implicit_int -Werror=implicit-int)
 
 set_compiler_property(PROPERTY warning_error_misra_sane -Werror=vla)
 
diff --git a/samples/basic/benchmark/CMakeLists.txt b/samples/basic/benchmark/CMakeLists.txt
new file mode 100644
index 0000000000..acfa3db718
--- /dev/null
+++ b/samples/basic/benchmark/CMakeLists.txt
@@ -0,0 +1,31 @@
+# SPDX-License-Identifier: Apache-2.0
+
+cmake_minimum_required(VERSION 3.20.0)
+find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
+project(benchmark LANGUAGES C)
+
+
+target_compile_options(app PRIVATE -Wno-return-type -Wno-implicit-int -Wno-implicit-function-declaration)
+target_compile_options(app PRIVATE -Wno-error=implicit-int -Wno-old-style-definition)
+target_compile_options(app PRIVATE -include zephyr/kernel.h)
+
+# Dhrystone
+add_compile_definitions(uint8=uint8_t uint32=uint32_t)
+add_compile_definitions(TIME=TIME NUMBER_OF_RUNS=10000000)
+
+# Coremark
+add_compile_definitions(FLAGS_STR="")
+target_compile_options(app PRIVATE -I${CMAKE_SOURCE_DIR}/src/coremark -I${CMAKE_SOURCE_DIR}/src/coremark/barebones)
+add_compile_definitions(crc16=coremark_crc16)
+add_compile_definitions(ITERATIONS=30000 MEM_METHOD=MEM_STATIC ee_printf=printf)
+add_compile_definitions(COMPILER_FLAGS="Unknown" MAIN_HAS_NOARGC=1 CLOCKS_PER_SEC=12500000 HAS_FLOAT=0)
+
+# Debug
+target_compile_options(app PRIVATE -Os)
+# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
+# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCORE_DEBUG=1")
+
+FILE(GLOB dhry_sources src/dhrystone/*.c)
+FILE(GLOB cmark_sources src/coremark/*.c src/coremark/barebones/core_portme.c src/coremark/barebones/cvt.c)
+
+target_sources(app PRIVATE src/benchmark.c ${dhry_sources} ${cmark_sources})
diff --git a/samples/basic/benchmark/prj.conf b/samples/basic/benchmark/prj.conf
new file mode 100644
index 0000000000..5390df4303
--- /dev/null
+++ b/samples/basic/benchmark/prj.conf
@@ -0,0 +1,3 @@
+CONFIG_MAIN_STACK_SIZE=1024
+CONFIG_MINIMAL_LIBC_MALLOC=y
+CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=1024
diff --git a/samples/basic/benchmark/sample.yaml b/samples/basic/benchmark/sample.yaml
new file mode 100644
index 0000000000..46235934f5
--- /dev/null
+++ b/samples/basic/benchmark/sample.yaml
@@ -0,0 +1,10 @@
+sample:
+  name: Benchmark Sample
+tests:
+  sample.basic.benchmark:
+    tags: benchmark
+    filter: dt_enabled_alias_with_parent_compat("led0", "gpio-leds")
+    depends_on: uart
+    harness: benchmark
+    integration_platforms:
+      - R-car S4
diff --git a/samples/basic/benchmark/src/benchmark.c b/samples/basic/benchmark/src/benchmark.c
new file mode 100644
index 0000000000..0aaa66ebbc
--- /dev/null
+++ b/samples/basic/benchmark/src/benchmark.c
@@ -0,0 +1,124 @@
+#include <string.h>
+
+#define Serial_Tx   {}
+
+uint8 result_array[4][17];
+
+uint32 g_counter = 0;
+
+void __nop(void) {}
+
+/**
+ * System Call function for newlib
+ */
+#include <stddef.h>
+#include <errno.h>
+#define HEAP_SIZE 4096
+static char _HEAP[HEAP_SIZE];
+static char* const _heap_region_start = &_HEAP[0];
+static char* const _heap_region_end = &_HEAP[HEAP_SIZE-1];
+void* _sbrk(int size) {
+    static char *curr_heap_end = _heap_region_start;
+    char        *prev_heap_end;
+
+    if( (curr_heap_end+size) > _heap_region_end ) {
+        return (char*)-1;
+    }
+    prev_heap_end  = curr_heap_end;
+    curr_heap_end  += size;
+
+    return (void *)prev_heap_end;
+}
+void _write(){}
+void _read(){}
+void _open(){}
+void _close(){}
+void _lseek(){}
+
+/**
+ * external functions
+ */
+extern void dhrystone_main(void);
+extern void coremark_main(void);
+
+void print(uint8* data)
+{
+       int i = 0;
+       size_t len = strlen(data);
+       for (i=0; i< len; i++)
+       {
+               puts(*data);
+               data++;
+       }
+}
+
+void float_to_char(uint8 input_array[], float input) {
+  uint8 i, n;
+  uint8 ascii_offset = 0x20; // 0x30 = '0' / 0x20 = ' '
+  float tmp;
+
+  for (i=0; i<16; ++i){
+    tmp = input;
+    if (i < 8) {
+      for(n=i+1; n<8;++n)
+        tmp = tmp / 10;
+    }
+    else {
+      for(n=7; n<i; ++n)
+        tmp = tmp * 10;
+    }
+
+    if((int)tmp%10 != 0 || i == 7)
+      ascii_offset = 0x30; // changed at once
+
+    input_array[i+(i/8)] = (ascii_offset + (int)tmp%10); // if (i==8) i+1, else i
+  }
+  input_array[8] = '.';
+  input_array[16] = '\0';
+}
+void int_to_char(uint8 input_array[], int input) {
+  uint8 i, n;
+  uint8 ascii_offset = 0x20; // 0x30 = '0' / 0x20 = ' '
+  int tmp;
+
+  for (i=0; i<16; ++i){
+    tmp = input;
+      for(n=i+1; n<16;++n)
+        tmp = tmp / 10;
+    if((int)tmp%10 != 0)
+      ascii_offset = 0x30; // changed at once
+
+    input_array[i] = (ascii_offset + (int)tmp%10); // if (i==8) i+1, else i
+  }
+}
+
+/*
+// return msec
+uint32 ms_time() { return TICK_TO_MSEC(tpl_time_counter); }
+// return sec
+uint32 time() { return TICK_TO_SEC(tpl_time_counter); }
+*/
+/*
+void ms_sleep(int ms)
+{
+    uint32 end;
+    end = k_uptime_get_32() + ms;
+    while(end > k_uptime_get_32());
+}
+*/
+
+void main(void)
+{
+    k_msleep(3000);
+    dhrystone_main();
+    k_msleep(10000);
+
+    coremark_main();
+    k_msleep(1000);
+
+    while(1) {
+        g_counter = k_uptime_get_32()/* k_cycle_get_32()*/ /* time() */;
+        k_msleep(1000);
+    }
+    k_thread_abort(k_current_get());
+}
-- 
2.25.1

