DPDK patches and discussions
 help / color / mirror / Atom feed
From: Euan Bourke <euan.bourke@intel.com>
To: dev@dpdk.org
Cc: Euan Bourke <euan.bourke@intel.com>,
	Bruce Richardson <bruce.richardson@intel.com>
Subject: [PATCH v4 6/8] arg_parser: added common core string and heuristic parsers
Date: Fri, 15 Dec 2023 17:26:30 +0000	[thread overview]
Message-ID: <20231215172632.3102502-7-euan.bourke@intel.com> (raw)
In-Reply-To: <20231215172632.3102502-1-euan.bourke@intel.com>

Two new functions, the first is a 'heuristic parser' which examines a
string describing a set of cores and determines based off heuristics
whether its a coremask or a corelist.

Second is a 'combined parser' which calls the first function and then
based off the returned value will call the relevant core string parser.
This function also takes a 'default_type' int which corresponds to
which parser should be used in the case of an ambiguous string.

Signed-off-by: Euan Bourke <euan.bourke@intel.com>
---
 lib/arg_parser/arg_parser.c     | 62 ++++++++++++++++++++++++++++++++
 lib/arg_parser/rte_arg_parser.h | 64 +++++++++++++++++++++++++++++++++
 lib/arg_parser/version.map      |  2 ++
 3 files changed, 128 insertions(+)

diff --git a/lib/arg_parser/arg_parser.c b/lib/arg_parser/arg_parser.c
index 8d86a7b618..1755ecb3b2 100644
--- a/lib/arg_parser/arg_parser.c
+++ b/lib/arg_parser/arg_parser.c
@@ -157,3 +157,65 @@ rte_arg_parse_coremask(const char *coremask, uint16_t *cores, uint32_t cores_len
 
 	return corebits_to_array(&mask, cores, cores_len);
 }
+
+int
+rte_arg_parse_arg_type(const char *core_string)
+{
+	/* Remove leading whitespace */
+	while (isblank(*core_string))
+		core_string++;
+
+	/* Check for 0x prefix */
+	if (core_string[0] == '0' && tolower(core_string[1]) == 'x') {
+		if (core_string[2] != '\0')
+			return RTE_ARG_PARSE_TYPE_COREMASK;
+		return -EINVAL;
+	}
+
+	int i = 0, idx = 0;
+	/* Check for ',' and '-' and check for A-F */
+	do {
+		while (isblank(core_string[idx]))
+			idx++;
+
+		if (core_string[idx] == ',' || core_string[idx] == '-')
+			return RTE_ARG_PARSE_TYPE_CORELIST;
+
+		if (isalpha(core_string[idx])) {
+			if (isxdigit(core_string[idx]))
+				return RTE_ARG_PARSE_TYPE_COREMASK;
+			return -EINVAL;
+		}
+		idx++;
+		i++;
+	} while (core_string[idx] != '\0');
+
+	/* Check length of core_string if ambiguous as max length of a uint16_t is 5 digits
+	 * implying its a coremask.
+	 */
+	if (i > 5)
+		return RTE_ARG_PARSE_TYPE_COREMASK;
+
+	return -EINVAL;
+}
+
+int
+rte_arg_parse_core_string(const char *core_string, uint16_t *cores, uint32_t cores_len,
+		int default_type)
+{
+	if (default_type != RTE_ARG_PARSE_TYPE_COREMASK &&
+			default_type != RTE_ARG_PARSE_TYPE_CORELIST) {
+		return -EINVAL;
+	}
+	switch (rte_arg_parse_arg_type(core_string)) {
+	case RTE_ARG_PARSE_TYPE_COREMASK:
+		return rte_arg_parse_coremask(core_string, cores, cores_len);
+	case RTE_ARG_PARSE_TYPE_CORELIST:
+		return rte_arg_parse_corelist(core_string, cores, cores_len);
+	default:
+		return default_type == RTE_ARG_PARSE_TYPE_COREMASK ?
+			rte_arg_parse_coremask(core_string, cores, cores_len) :
+			rte_arg_parse_corelist(core_string, cores, cores_len);
+		return -EINVAL;
+	}
+}
diff --git a/lib/arg_parser/rte_arg_parser.h b/lib/arg_parser/rte_arg_parser.h
index 49d7daa204..3b2df74d10 100644
--- a/lib/arg_parser/rte_arg_parser.h
+++ b/lib/arg_parser/rte_arg_parser.h
@@ -23,6 +23,9 @@ extern "C" {
 
 #include <rte_compat.h>
 
+#define RTE_ARG_PARSE_TYPE_COREMASK 0
+#define RTE_ARG_PARSE_TYPE_CORELIST 1
+#define RTE_ARG_PARSE_TYPE_UNKNOWN 2
 
 /**
  * Convert a string describing a list of core ids into an array of core ids.
@@ -92,6 +95,67 @@ __rte_experimental
 int
 rte_arg_parse_coremask(const char *coremask, uint16_t *cores, uint32_t cores_len);
 
+/**
+ * Use heuristics to determine if a string contains a coremask or a corelist.
+ *
+ * This function will check a series of conditions and return an int representing which
+ * core type (mask or list) the string represents or report the type as unknown if the
+ * string is ambiguous.
+ *
+ * @param core_string
+ *   A string describing the intended cores to be parsed
+ * @return
+ *   int representing the core type
+ *   RTE_ARG_PARSE_TYPE_COREMASK: coremask.
+ *   RTE_ARG_PARSE_TYPE_CORELIST: corelist.
+ *   RTE_ARG_PARSE_TYPE_UNKNOWN: unknown (ambiguous).
+ *   -EINVAL if the string was invalid.
+ */
+__rte_experimental
+int
+rte_arg_parse_arg_type(const char *core_string);
+
+/**
+ * Convert a string describing either a corelist or coremask into an array of core ids.
+ *
+ * This function will fill the "cores" array up to "cores_len" with the core ids described
+ * in the "core_string". The string can either describe a corelist or a coremask, and
+ * will be parsed accordingly. The number of unique core ids in the string is then returned.
+ * For example:
+ * "1-4" is treated as a corelist and results in an array of [1,2,3,4] with 4 being returned
+ * "0xA1" is treated as a coremask and results in an array of [0,5,7] with 3 being returned
+ *
+ * In the case of an ambiguous string, the function will use the default_type parameter to
+ * decide.
+ *
+ * NOTE: if the length of the input array is insufficient to hold the number of core ids
+ * in "core_string" the input array is filled to capacity but the return value is the
+ * number of elements which would have been written to the array, had enough space been
+ * available. [This is similar to the behaviour of the snprintf function]. Because of
+ * this, the number of core values in the "core_string" may be determined by calling the
+ * function with a NULL array pointer and array length given as 0.
+ *
+ * @param core_string
+ *   A string describing the intended cores to be parsed.
+ * @param cores
+ *   An array where to store the core ids.
+ *   Array can be NULL if "cores_len" is 0.
+ * @param cores_len
+ *   The length of the "cores" array.
+ *   If the size is smaller than that needed to hold all cores from "core_string"
+ * @param default_type
+ *   How to treat ambiguous cases (e.g. '4' could be mask or list).
+ *   RTE_ARG_PARSE_TYPE_COREMASK: coremask.
+ *   RTE_ARG_PARSE_TYPE_CORELIST: corelist.
+ * @return
+ *   n: the number of unique cores present in "core_string".
+ *   -EINVAL if the string was invalid.
+ *   NOTE: if n > "cores_len", then only "cores_len" elements in the "cores" array are valid.
+ */
+__rte_experimental
+int
+rte_arg_parse_core_string(const char *core_string, uint16_t *cores, uint32_t cores_len,
+		int default_type);
 
 #ifdef __cplusplus
 }
diff --git a/lib/arg_parser/version.map b/lib/arg_parser/version.map
index b44d4b02b7..1e54b91dae 100644
--- a/lib/arg_parser/version.map
+++ b/lib/arg_parser/version.map
@@ -6,6 +6,8 @@ EXPERIMENTAL {
 	global:
 
 	# added in 24.03
+	rte_arg_parse_arg_type;
 	rte_arg_parse_corelist;
 	rte_arg_parse_coremask;
+	rte_arg_parse_core_string;
 };
-- 
2.34.1


  parent reply	other threads:[~2023-12-15 17:27 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <https://inbox.dpdk.org/dev/20231207161818.2590661-1-euan.bourke@intel.com/>
2023-12-15 17:26 ` [PATCH v4 0/8] add new command line argument parsing library Euan Bourke
2023-12-15 17:26   ` [PATCH v4 1/8] arg_parser: new library for command line parsing Euan Bourke
2024-01-24 13:16     ` Morten Brørup
2024-01-24 13:31       ` Bruce Richardson
2023-12-15 17:26   ` [PATCH v4 2/8] arg_parser: add new coremask parsing API Euan Bourke
2023-12-15 17:26   ` [PATCH v4 3/8] eal: add support for new arg parsing library Euan Bourke
2023-12-15 17:26   ` [PATCH v4 4/8] eal: update to service core related parsers Euan Bourke
2023-12-15 17:26   ` [PATCH v4 5/8] event/dlb2: add new arg parsing library API support Euan Bourke
2023-12-15 17:26   ` Euan Bourke [this message]
2023-12-15 17:26   ` [PATCH v4 7/8] examples/eventdev_pipeline: update to call arg parser API Euan Bourke
2023-12-15 17:26   ` [PATCH v4 8/8] examples/l3fwd-power: " Euan Bourke
2023-12-18  3:14     ` Tummala, Sivaprasad
2024-10-04 16:05     ` Stephen Hemminger
2023-12-15 21:53   ` [PATCH v4 0/8] add new command line argument parsing library Stephen Hemminger
2023-12-18  9:18     ` Bruce Richardson
2024-01-24 13:33       ` Thomas Monjalon
2024-02-14 17:01         ` Thomas Monjalon

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=20231215172632.3102502-7-euan.bourke@intel.com \
    --to=euan.bourke@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    /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).