Implement Secure Storage Benchmarks

Access is performed chunk by chunk, default chunk size is 1KB.
If remained data size is less then a chunk (e.g. data size
is not a multiple of trunk size), chunk will be modified to
remained data size.

Tested on MediaTek MT8173 EVB, result as follows:

WRITE:
-----------------+---------------+----------------
 Data Size (B)   | Time (s)      | Speed (kB/s)
-----------------+---------------+----------------
      256        |    0.006      |   41.667
      512        |    0.008      |   62.500
     1024        |    0.011      |   90.909
     2048        |    0.034      |   58.824
     4096        |    0.144      |   27.778
    16384        |    0.577      |   27.730
   524288        |   18.519      |   27.647
  1048576        |   37.030      |   27.653
-----------------+---------------+----------------

READ:
-----------------+---------------+----------------
 Data Size (B)   | Time (s)      | Speed (kB/s)
-----------------+---------------+----------------
      256        |    0.014      |   17.857
      512        |    0.013      |   38.462
     1024        |    0.014      |   71.429
     2048        |    0.027      |   74.074
     4096        |    0.068      |   58.824
    16384        |    0.271      |   59.041
   524288        |    8.683      |   58.966
  1048576        |   17.372      |   58.945
-----------------+---------------+----------------

REWRITE: (Read-Modify-Write)
-----------------+---------------+----------------
 Data Size (B)   | Time (s)      | Speed (kB/s)
-----------------+---------------+----------------
      256        |    0.047      |    5.319
      512        |    0.047      |   10.638
     1024        |    0.047      |   21.277
     2048        |    0.094      |   21.277
     4096        |    0.230      |   17.391
    16384        |    0.921      |   17.372
   524288        |   29.469      |   17.374
  1048576        |   58.925      |   17.378
-----------------+---------------+----------------

Signed-off-by: SY Chiu <sy.chiu@linaro.org>
Signed-off-by: James Kung <james.kung@linaro.org>
Tested-by: SY Chiu <sy.chiu@linaro.org> (QEMU, MT8173 EVB)
Tested-by: Pascal Brand <pascal.brand@linaro.org> (QEMU)
Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
Reviewed-by: Pascal Brand <pascal.brand@linaro.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/host/xtest/Makefile b/host/xtest/Makefile
index 3f626b4..f695aa3 100644
--- a/host/xtest/Makefile
+++ b/host/xtest/Makefile
@@ -72,7 +72,7 @@
 CFLAGS += -I../../ta/rpc_test/include
 CFLAGS += -I../../ta/sims/include
 CFLAGS += -I../../ta/storage/include
-CFLAGS += -I../../ta/bonnie/include
+CFLAGS += -I../../ta/storage_benchmark/include
 CFLAGS += -I../../ta/concurrent/include
 ifdef CFG_GP_PACKAGE_PATH
 CFLAGS += -I../../ta/GP_TTA_Arithmetical
diff --git a/host/xtest/xtest_20000.c b/host/xtest/xtest_20000.c
index 9e6c2f0..1b76297 100644
--- a/host/xtest/xtest_20000.c
+++ b/host/xtest/xtest_20000.c
@@ -28,6 +28,7 @@
 #include <ta_storage.h>
 #include <tee_api_defines.h>
 #include <tee_api_types.h>
+#include <util.h>
 
 #define SIZE		1
 #define NUMELEM		1
@@ -49,8 +50,6 @@
 #define CORRUPT_FILE_FIRST_BYTE		1024*4096+1
 #define CORRUPT_FILE_LAST_BYTE		1024*4096
 
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
-
 #ifndef MIN
 #define MIN(a,b) ((a)<(b) ? (a) : (b))
 #endif
diff --git a/host/xtest/xtest_benchmark_1000.c b/host/xtest/xtest_benchmark_1000.c
index fa2beeb..796ace6 100644
--- a/host/xtest/xtest_benchmark_1000.c
+++ b/host/xtest/xtest_benchmark_1000.c
@@ -12,109 +12,162 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "xtest_test.h"
 #include "xtest_helpers.h"
 
-#include <ta_bonnie.h>
+#include <ta_storage_benchmark.h>
+#include <util.h>
+
+#define DO_VERIFY 0
+#define DEFAULT_DATA_SIZE (2 * 1024 * 1024) /* 2MB */
+#define DEFAULT_CHUNK_SIZE (1 * 1024) /* 1KB */
+#define DEFAULT_COUNT (10)
+
+size_t data_size_table[] = {
+	256,
+	512,
+	1024,
+	2 * 1024,
+	4 * 1024,
+	16 * 1024,
+	512 * 1024,
+	1024 * 1024,
+	0
+};
 
 static void xtest_tee_benchmark_1001(ADBG_Case_t *Case_p);
 static void xtest_tee_benchmark_1002(ADBG_Case_t *Case_p);
 static void xtest_tee_benchmark_1003(ADBG_Case_t *Case_p);
-static void xtest_tee_benchmark_1004(ADBG_Case_t *Case_p);
-static void xtest_tee_benchmark_1005(ADBG_Case_t *Case_p);
-static void xtest_tee_benchmark_1006(ADBG_Case_t *Case_p);
 
-
-static TEEC_Result run_test(enum bonnie_cmd cmd)
+static TEEC_Result run_test_with_args(enum storage_benchmark_cmd cmd,
+		uint32_t arg0, uint32_t arg1, uint32_t arg2,
+		uint32_t arg3, uint32_t *out0, uint32_t *out1)
 {
 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 	TEEC_Result res;
 	TEEC_Session sess;
 	uint32_t orig;
 
-	res = xtest_teec_open_session(&sess, &bonnie_ta_uuid, NULL, &orig);
+	res = xtest_teec_open_session(&sess, &storage_benchmark_ta_uuid, NULL, &orig);
 	if (res != TEEC_SUCCESS)
 		return res;
 
+	op.params[0].value.a = arg0;
+	op.params[0].value.b = arg1;
+	op.params[1].value.a = arg2;
+	op.params[1].value.b = arg3;
+
+	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
+			TEEC_VALUE_INPUT, TEEC_VALUE_OUTPUT, TEEC_NONE);
+
 	res = TEEC_InvokeCommand(&sess, cmd, &op, &orig);
 
+	if (out0)
+		*out0 = op.params[2].value.a;
+	if (out1)
+		*out1 = op.params[2].value.b;
+
 	TEEC_CloseSession(&sess);
 
 	return res;
 }
 
+struct test_record {
+	size_t data_size;
+	float spent_time;
+	float speed_in_kb;
+};
+
+static TEEC_Result run_chunk_access_test(enum storage_benchmark_cmd cmd,
+		uint32_t data_size, uint32_t chunk_size, struct test_record *rec)
+{
+	TEE_Result res;
+	uint32_t spent_time;
+
+	res = run_test_with_args(cmd, data_size, chunk_size, DO_VERIFY, 0,
+				&spent_time, NULL);
+
+	rec->data_size = data_size;
+	rec->spent_time = (float)spent_time / 1000.0;
+	rec->speed_in_kb = ((float)data_size / 1024.0) / rec->spent_time;
+
+	return res;
+}
+
+static void show_test_result(struct test_record records[], size_t size)
+{
+	uint i;
+
+	printf("-----------------+---------------+----------------\n");
+	printf(" Data Size (B) \t | Time (s)\t | Speed (kB/s)\t \n");
+	printf("-----------------+---------------+----------------\n");
+
+	for (i = 0; i < size; i++) {
+		printf(" %8zd \t | %8.3f \t | %8.3f\n",
+			records[i].data_size, records[i].spent_time,
+			records[i].speed_in_kb);
+	}
+
+	printf("-----------------+---------------+----------------\n");
+
+}
+
+static void chunk_test(ADBG_Case_t *c, enum storage_benchmark_cmd cmd)
+{
+	uint32_t chunk_size = DEFAULT_CHUNK_SIZE;
+	struct test_record records[ARRAY_SIZE(data_size_table) - 1];
+	uint i;
+
+	for (i = 0; data_size_table[i]; i++) {
+		ADBG_EXPECT_TEEC_SUCCESS(c,
+			run_chunk_access_test(cmd, data_size_table[i],
+				chunk_size, &records[i]));
+	}
+
+	show_test_result(records, ARRAY_SIZE(records));
+}
+
 static void xtest_tee_benchmark_1001(ADBG_Case_t *c)
 {
-	ADBG_EXPECT_TEEC_SUCCESS(c, run_test(TA_BONNIE_CMD_TEST_PUTC));
+	chunk_test(c, TA_STORAGE_BENCHMARK_CMD_TEST_WRITE);
 }
 
 static void xtest_tee_benchmark_1002(ADBG_Case_t *c)
 {
-	ADBG_EXPECT_TEEC_SUCCESS(c, run_test(TA_BONNIE_CMD_TEST_REWRITE));
+	chunk_test(c, TA_STORAGE_BENCHMARK_CMD_TEST_READ);
 }
 
 static void xtest_tee_benchmark_1003(ADBG_Case_t *c)
 {
-	ADBG_EXPECT_TEEC_SUCCESS(c, run_test(TA_BONNIE_CMD_TEST_FASTWRITE));
+	chunk_test(c, TA_STORAGE_BENCHMARK_CMD_TEST_REWRITE);
 }
 
-static void xtest_tee_benchmark_1004(ADBG_Case_t *c)
-{
-	ADBG_EXPECT_TEEC_SUCCESS(c, run_test(TA_BONNIE_CMD_TEST_GETC));
-}
-
-static void xtest_tee_benchmark_1005(ADBG_Case_t *c)
-{
-	ADBG_EXPECT_TEEC_SUCCESS(c, run_test(TA_BONNIE_CMD_TEST_FASTREAD));
-}
-
-static void xtest_tee_benchmark_1006(ADBG_Case_t *c)
-{
-	ADBG_EXPECT_TEEC_SUCCESS(c, run_test(TA_BONNIE_CMD_TEST_LSEEK));
-}
-
-
 ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_1001, xtest_tee_benchmark_1001,
-		/* Title */ "TEE Trusted Storage Performance Test (PUTC)",
-		/* Short description */ "",
+		/* Title */
+		"TEE Trusted Storage Performance Test (WRITE)",
+		/* Short description */
+		"Write a chunk of data",
 		/* Requirement IDs */ "",
 		/* How to implement */ ""
 		);
 
 ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_1002, xtest_tee_benchmark_1002,
-		/* Title */ "TEE Trusted Storage Performance Test (REWRITE)",
-		/* Short description */ "",
+		/* Title */
+		"TEE Trusted Storage Performance Test (READ)",
+		/* Short description */
+		"Read a chunk of data",
 		/* Requirement IDs */ "",
 		/* How to implement */ ""
 		);
 
 ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_1003, xtest_tee_benchmark_1003,
-		/* Title */ "TEE Trusted Storage Performance Test (FASTWRITE)",
-		/* Short description */ "",
+		/* Title */
+		"TEE Trusted Storage Performance Test (REWRITE)",
+		/* Short description */
+		"Read a chunk of data then write it",
 		/* Requirement IDs */ "",
 		/* How to implement */ ""
 		);
-
-ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_1004, xtest_tee_benchmark_1004,
-		/* Title */ "TEE Trusted Storage Performance Test (GETC)",
-		/* Short description */ "",
-		/* Requirement IDs */ "",
-		/* How to implement */ ""
-		);
-
-ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_1005, xtest_tee_benchmark_1005,
-		/* Title */ "TEE Trusted Storage Performance Test (FASTREAD)",
-		/* Short description */ "",
-		/* Requirement IDs */ "",
-		/* How to implement */ ""
-		);
-
-ADBG_CASE_DEFINE(XTEST_TEE_BENCHMARK_1006, xtest_tee_benchmark_1006,
-		/* Title */ "TEE Trusted Storage Performance Test (LSEEK)",
-		/* Short description */ "",
-		/* Requirement IDs */ "",
-		/* How to implement */ ""
-		);
-
diff --git a/host/xtest/xtest_main.c b/host/xtest/xtest_main.c
index ee0f6df..04f2342 100644
--- a/host/xtest/xtest_main.c
+++ b/host/xtest/xtest_main.c
@@ -111,9 +111,6 @@
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1001, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1002, NULL)
 ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1003, NULL)
-ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1004, NULL)
-ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1005, NULL)
-ADBG_SUITE_ENTRY(XTEST_TEE_BENCHMARK_1006, NULL)
 ADBG_SUITE_DEFINE_END()
 
 static const char gdevname_tz[] = "opteearmtz00";
diff --git a/host/xtest/xtest_test.c b/host/xtest/xtest_test.c
index 28603e9..88c944f 100644
--- a/host/xtest/xtest_test.c
+++ b/host/xtest/xtest_test.c
@@ -21,7 +21,7 @@
 #include <ta_storage.h>
 #include <ta_concurrent.h>
 #include <enc_fs_key_manager_test.h>
-#include <ta_bonnie.h>
+#include <ta_storage_benchmark.h>
 #include <tee_api_defines.h>
 #ifdef WITH_GP_TESTS
 #include <tee_api_types.h>
@@ -81,7 +81,7 @@
 const TEEC_UUID storage_ta_uuid = TA_STORAGE_UUID;
 const TEEC_UUID enc_fs_key_manager_test_ta_uuid = ENC_FS_KEY_MANAGER_TEST_UUID;
 const TEEC_UUID concurrent_ta_uuid = TA_CONCURRENT_UUID;
-const TEEC_UUID bonnie_ta_uuid = TA_BONNIE_UUID;
+const TEEC_UUID storage_benchmark_ta_uuid = TA_STORAGE_BENCHMARK_UUID;
 #ifdef WITH_GP_TESTS
 const TEEC_UUID gp_tta_ds_uuid = TA_TTA_DS_UUID;
 #endif
diff --git a/host/xtest/xtest_test.h b/host/xtest/xtest_test.h
index d07f451..39f43b3 100644
--- a/host/xtest/xtest_test.h
+++ b/host/xtest/xtest_test.h
@@ -94,6 +94,10 @@
 ADBG_CASE_DECLARE(XTEST_TEE_20523);
 #endif /* CFG_ENC_FS */
 
+ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_1001);
+ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_1002);
+ADBG_CASE_DECLARE(XTEST_TEE_BENCHMARK_1003);
+
 #ifdef WITH_GP_TESTS
 #include "adbg_case_declare.h"
 ADBG_CASE_DECLARE_AUTO_GENERATED_TESTS()
@@ -201,7 +205,7 @@
 extern const TEEC_UUID ecc_test_ta_uuid;
 extern const TEEC_UUID gp_tta_time_uuid;
 extern const TEEC_UUID concurrent_ta_uuid;
-extern const TEEC_UUID bonnie_ta_uuid;
+extern const TEEC_UUID storage_benchmark_ta_uuid;
 extern char *_device;
 
 #endif /*XTEST_TEST_H*/