DPDK patches and discussions
 help / color / mirror / Atom feed
From: Shiyang He <shiyangx.he@intel.com>
To: dev@dpdk.org
Cc: yidingx.zhou@intel.com, Shiyang He <shiyangx.he@intel.com>,
	stable@dpdk.org, Cheng Jiang <cheng1.jiang@intel.com>,
	Chengwen Feng <fengchengwen@huawei.com>,
	Yuan Wang <yuanx.wang@intel.com>, Jiayu Hu <jiayu.hu@intel.com>,
	Anoob Joseph <anoobj@marvell.com>
Subject: [PATCH] app/dma-perf: parse the cmdline with getopt_long
Date: Tue, 18 Jul 2023 13:11:17 +0000	[thread overview]
Message-ID: <20230718131117.684986-1-shiyangx.he@intel.com> (raw)

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(-)

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.
-- 
2.37.2


                 reply	other threads:[~2023-07-18  5:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230718131117.684986-1-shiyangx.he@intel.com \
    --to=shiyangx.he@intel.com \
    --cc=anoobj@marvell.com \
    --cc=cheng1.jiang@intel.com \
    --cc=dev@dpdk.org \
    --cc=fengchengwen@huawei.com \
    --cc=jiayu.hu@intel.com \
    --cc=stable@dpdk.org \
    --cc=yidingx.zhou@intel.com \
    --cc=yuanx.wang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).