app/dma-perf: parse the cmdline with getopt_long

Message ID 20230718131117.684986-1-shiyangx.he@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series app/dma-perf: parse the cmdline with getopt_long |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/intel-Functional success Functional PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-aarch-unit-testing success Testing PASS
ci/iol-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-unit-testing fail Testing issues
ci/iol-aarch64-compile-testing success Testing PASS

Commit Message

Shiyang He July 18, 2023, 1:11 p.m. UTC
  Use getopt_long to parse the command line for ease of extension and
improved code readability.

Fixes: 623dc9364dc6 ("app/dma-perf: introduce DMA performance test")
Cc: stable@dpdk.org

Signed-off-by: Shiyang He <shiyangx.he@intel.com>
---
 app/test-dma-perf/main.c     | 60 ++++++++++++++++++++++++++----------
 app/test-dma-perf/main.h     |  4 +++
 doc/guides/tools/dmaperf.rst |  2 +-
 3 files changed, 48 insertions(+), 18 deletions(-)
  

Patch

diff --git a/app/test-dma-perf/main.c b/app/test-dma-perf/main.c
index e5bccc27da..a7e00998e6 100644
--- a/app/test-dma-perf/main.c
+++ b/app/test-dma-perf/main.c
@@ -472,6 +472,36 @@  append_eal_args(int argc, char **argv, const char *eal_args, char **new_argv)
 	return new_argc;
 }
 
+/*
+ * Parse command line options.
+ */
+static void
+parse_opts(struct test_options *options, int argc, char **argv)
+{
+	static const struct option long_options[] = {
+		{ "config", required_argument, NULL, 0},
+		{ "result", required_argument, NULL, 1},
+		{ NULL },
+	};
+
+	int opt_idx;
+	int opt;
+
+	while ((opt = getopt_long(argc, argv, "", long_options, &opt_idx))
+			!= EOF) {
+		switch (opt) {
+		case 0:
+			options->cfg_path_ptr = optarg;
+			break;
+		case 1:
+			options->rst_path_ptr = optarg;
+			break;
+		default:
+			break;
+		}
+	}
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -482,39 +512,35 @@  main(int argc, char *argv[])
 	int wstatus;
 	char args[MAX_EAL_PARAM_NB][MAX_EAL_PARAM_LEN];
 	char *pargs[MAX_EAL_PARAM_NB];
-	char *cfg_path_ptr = NULL;
-	char *rst_path_ptr = NULL;
 	char rst_path[PATH_MAX];
 	int new_argc;
+	struct test_options opt;
 
 	memset(args, 0, sizeof(args));
+	memset(&opt, 0, sizeof(opt));
 
 	for (i = 0; i < RTE_DIM(pargs); i++)
 		pargs[i] = args[i];
 
-	for (i = 0; i < (uint32_t)argc; i++) {
-		if (strncmp(argv[i], CMDLINE_CONFIG_ARG, MAX_LONG_OPT_SZ) == 0)
-			cfg_path_ptr = argv[i + 1];
-		if (strncmp(argv[i], CMDLINE_RESULT_ARG, MAX_LONG_OPT_SZ) == 0)
-			rst_path_ptr = argv[i + 1];
-	}
-	if (cfg_path_ptr == NULL) {
+	parse_opts(&opt, argc, argv);
+
+	if (opt.cfg_path_ptr == NULL) {
 		printf("Config file not assigned.\n");
 		return -1;
 	}
-	if (rst_path_ptr == NULL) {
-		strlcpy(rst_path, cfg_path_ptr, PATH_MAX);
+	if (opt.rst_path_ptr == NULL) {
+		strlcpy(rst_path, opt.cfg_path_ptr, PATH_MAX);
 		char *token = strtok(basename(rst_path), ".");
 		if (token == NULL) {
 			printf("Config file error.\n");
 			return -1;
 		}
 		strcat(token, "_result.csv");
-		rst_path_ptr = rst_path;
+		opt.rst_path_ptr = rst_path;
 	}
 
-	case_nb = load_configs(cfg_path_ptr);
-	fd = fopen(rst_path_ptr, "w");
+	case_nb = load_configs(opt.cfg_path_ptr);
+	fd = fopen(opt.rst_path_ptr, "w");
 	if (fd == NULL) {
 		printf("Open output CSV file error.\n");
 		return -1;
@@ -527,7 +553,7 @@  main(int argc, char *argv[])
 			printf("Invalid test case %d.\n\n", i + 1);
 			snprintf(output_str[0], MAX_OUTPUT_STR_LEN, "Invalid case %d\n", i + 1);
 
-			fd = fopen(rst_path_ptr, "a");
+			fd = fopen(opt.rst_path_ptr, "a");
 			if (!fd) {
 				printf("Open output CSV file error.\n");
 				return 0;
@@ -541,7 +567,7 @@  main(int argc, char *argv[])
 			printf("No valid test type in test case %d.\n\n", i + 1);
 			snprintf(output_str[0], MAX_OUTPUT_STR_LEN, "Invalid case %d\n", i + 1);
 
-			fd = fopen(rst_path_ptr, "a");
+			fd = fopen(opt.rst_path_ptr, "a");
 			if (!fd) {
 				printf("Open output CSV file error.\n");
 				return 0;
@@ -569,7 +595,7 @@  main(int argc, char *argv[])
 				rte_exit(EXIT_FAILURE,
 					"There should be at least 2 worker lcores.\n");
 
-			fd = fopen(rst_path_ptr, "a");
+			fd = fopen(opt.rst_path_ptr, "a");
 			if (!fd) {
 				printf("Open output CSV file error.\n");
 				return 0;
diff --git a/app/test-dma-perf/main.h b/app/test-dma-perf/main.h
index f65e264378..b2f870a4e0 100644
--- a/app/test-dma-perf/main.h
+++ b/app/test-dma-perf/main.h
@@ -18,6 +18,10 @@ 
 
 extern char output_str[MAX_WORKER_NB + 1][MAX_OUTPUT_STR_LEN];
 
+struct test_options {
+	char *cfg_path_ptr;
+	char *rst_path_ptr;
+};
 typedef enum {
 	OP_NONE = 0,
 	OP_ADD,
diff --git a/doc/guides/tools/dmaperf.rst b/doc/guides/tools/dmaperf.rst
index 9e3e78a6b7..9fc77ca943 100644
--- a/doc/guides/tools/dmaperf.rst
+++ b/doc/guides/tools/dmaperf.rst
@@ -119,7 +119,7 @@  Typical command-line invocation to execute the application:
 
 .. code-block:: console
 
-   dpdk-test-dma-perf --config=./config_dma.ini --result=./res_dma.csv
+   dpdk-test-dma-perf --config ./config_dma.ini --result ./res_dma.csv
 
 Where ``config_dma.ini`` is the configuration file,
 and ``res_dma.csv`` will be the generated result file.