From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: "Bruce Richardson" <bruce.richardson@intel.com>,
"Ciara Power" <ciara.power@intel.com>,
"Morten Brørup" <mb@smartsharesystems.com>
Subject: [PATCH v3 12/13] telemetry: eliminate duplicate code for json output
Date: Fri, 9 Sep 2022 10:35:22 +0100 [thread overview]
Message-ID: <20220909093523.471727-13-bruce.richardson@intel.com> (raw)
In-Reply-To: <20220909093523.471727-1-bruce.richardson@intel.com>
When preparing the json response to a telemetry socket query, the code
for prefixing the command name, and appending the file "}" on the end of
the response was duplicated for multiple reply types. Taking this code
out of the switch statement reduces the duplication and makes the code
more maintainable.
For completeness of testing, add in a test case to validate the "null"
response type - the only leg of the switch statment not already covered
by an existing test case in the telemetry_data tests.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Ciara Power <ciara.power@intel.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
app/test/test_telemetry_data.c | 7 +++++++
lib/telemetry/telemetry.c | 35 ++++++++++++----------------------
2 files changed, 19 insertions(+), 23 deletions(-)
diff --git a/app/test/test_telemetry_data.c b/app/test/test_telemetry_data.c
index 69ca8b6c6f..d92667a527 100644
--- a/app/test/test_telemetry_data.c
+++ b/app/test/test_telemetry_data.c
@@ -93,6 +93,12 @@ check_output(const char *func_name, const char *expected)
return strncmp(expected, buf, sizeof(buf));
}
+static int
+test_null_return(void)
+{
+ return CHECK_OUTPUT("null");
+}
+
static int
test_simple_string(void)
{
@@ -419,6 +425,7 @@ telemetry_data_autotest(void)
return -1;
test_case test_cases[] = {
+ test_null_return,
test_simple_string,
test_case_array_string,
test_case_array_int, test_case_array_u64,
diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c
index 03651e947d..cf60d27bd4 100644
--- a/lib/telemetry/telemetry.c
+++ b/lib/telemetry/telemetry.c
@@ -233,27 +233,22 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s)
RTE_BUILD_BUG_ON(sizeof(out_buf) < MAX_CMD_LEN +
RTE_TEL_MAX_SINGLE_STRING_LEN + 10);
+
+ prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
+ MAX_CMD_LEN, cmd);
+ cb_data_buf = &out_buf[prefix_used];
+ buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
+
switch (d->type) {
case RTE_TEL_NULL:
- used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":null}",
- MAX_CMD_LEN, cmd ? cmd : "none");
+ used = strlcpy(cb_data_buf, "null", buf_len);
break;
- case RTE_TEL_STRING:
- prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
- MAX_CMD_LEN, cmd);
- cb_data_buf = &out_buf[prefix_used];
- buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
+ case RTE_TEL_STRING:
used = rte_tel_json_str(cb_data_buf, buf_len, 0, d->data.str);
- used += prefix_used;
- used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
break;
- case RTE_TEL_DICT:
- prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
- MAX_CMD_LEN, cmd);
- cb_data_buf = &out_buf[prefix_used];
- buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
+ case RTE_TEL_DICT:
used = rte_tel_json_empty_obj(cb_data_buf, buf_len, 0);
for (i = 0; i < d->data_len; i++) {
const struct tel_dict_entry *v = &d->data.dict[i];
@@ -289,18 +284,12 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s)
}
}
}
- used += prefix_used;
- used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
break;
+
case RTE_TEL_ARRAY_STRING:
case RTE_TEL_ARRAY_INT:
case RTE_TEL_ARRAY_U64:
case RTE_TEL_ARRAY_CONTAINER:
- prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":",
- MAX_CMD_LEN, cmd);
- cb_data_buf = &out_buf[prefix_used];
- buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */
-
used = rte_tel_json_empty_array(cb_data_buf, buf_len, 0);
for (i = 0; i < d->data_len; i++)
if (d->type == RTE_TEL_ARRAY_STRING)
@@ -328,10 +317,10 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s)
if (!rec_data->keep)
rte_tel_data_free(rec_data->data);
}
- used += prefix_used;
- used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
break;
}
+ used += prefix_used;
+ used += strlcat(out_buf + used, "}", sizeof(out_buf) - used);
if (write(s, out_buf, used) < 0)
perror("Error writing to socket");
}
--
2.34.1
next prev parent reply other threads:[~2022-09-09 9:36 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-23 16:42 [RFC PATCH 0/6] add json string escaping to telemetry Bruce Richardson
2022-06-23 16:42 ` [RFC PATCH 1/6] test/telemetry_json: print success or failure per subtest Bruce Richardson
2022-06-23 16:42 ` [RFC PATCH 2/6] telemetry: fix escaping of invalid json characters Bruce Richardson
2022-06-23 18:34 ` Morten Brørup
2022-06-23 18:39 ` Stephen Hemminger
2022-06-23 18:48 ` Morten Brørup
2022-06-24 8:00 ` Bruce Richardson
2022-06-24 11:16 ` Bruce Richardson
2022-06-24 11:29 ` Morten Brørup
2022-06-24 15:06 ` Stephen Hemminger
2022-06-24 8:03 ` Bruce Richardson
2022-06-23 16:42 ` [RFC PATCH 3/6] telemetry: use json string function for string outputs Bruce Richardson
2022-06-23 16:42 ` [RFC PATCH 4/6] test/telemetry_json: add test for string character escaping Bruce Richardson
2022-06-23 16:42 ` [RFC PATCH 5/6] telemetry: add escaping of strings in arrays Bruce Richardson
2022-06-23 16:42 ` [RFC PATCH 6/6] test/telemetry-json: add test case for escaping " Bruce Richardson
2022-06-23 19:04 ` [RFC PATCH 0/6] add json string escaping to telemetry Morten Brørup
2022-06-24 8:13 ` Bruce Richardson
2022-06-24 9:12 ` Morten Brørup
2022-06-24 9:17 ` Bruce Richardson
2022-06-24 10:22 ` Morten Brørup
2022-07-14 15:42 ` Morten Brørup
2022-07-25 16:38 ` Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 00/13] telemetry JSON escaping and other enhancements Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 01/13] test/telemetry_json: print success or failure per subtest Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 02/13] telemetry: fix escaping of invalid json characters Bruce Richardson
2022-07-26 18:25 ` Morten Brørup
2022-07-27 8:21 ` Bruce Richardson
2022-07-27 1:13 ` fengchengwen
2022-07-27 8:27 ` Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 03/13] test/telemetry_json: add test for string character escaping Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 04/13] telemetry: add escaping of strings in arrays Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 05/13] test/telemetry-json: add test for escaping " Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 06/13] telemetry: limit characters allowed in dictionary names Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 07/13] telemetry: add escaping of strings in dicts Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 08/13] test/telemetry_json: add test for string escaping in objects Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 09/13] telemetry: limit command characters Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 10/13] test/telemetry_data: refactor for maintainability Bruce Richardson
2022-08-23 12:33 ` Power, Ciara
2022-07-25 16:35 ` [PATCH v2 11/13] test/telemetry_data: add test cases for character escaping Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 12/13] telemetry: eliminate duplicate code for json output Bruce Richardson
2022-07-25 16:35 ` [PATCH v2 13/13] telemetry: make help command more helpful Bruce Richardson
2022-07-26 14:36 ` [PATCH v2 00/13] telemetry JSON escaping and other enhancements Morten Brørup
2022-07-27 1:51 ` fengchengwen
2022-07-27 9:12 ` Bruce Richardson
2022-07-27 9:49 ` Morten Brørup
2022-08-23 12:35 ` Power, Ciara
2022-09-09 9:35 ` [PATCH v3 " Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 01/13] telemetry: limit characters allowed in dictionary names Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 02/13] test/telemetry_json: print success or failure per subtest Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 03/13] telemetry: fix escaping of invalid json characters Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 04/13] test/telemetry_json: add test for string character escaping Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 05/13] telemetry: add escaping of strings in arrays Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 06/13] test/telemetry-json: add test for escaping " Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 07/13] telemetry: add escaping of strings in dicts Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 08/13] test/telemetry_json: add test for string escaping in objects Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 09/13] telemetry: limit command characters Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 10/13] test/telemetry_data: refactor for maintainability Bruce Richardson
2022-09-09 9:35 ` [PATCH v3 11/13] test/telemetry_data: add test cases for character escaping Bruce Richardson
2022-09-09 9:35 ` Bruce Richardson [this message]
2022-09-09 9:35 ` [PATCH v3 13/13] telemetry: make help command more helpful Bruce Richardson
2022-09-13 0:35 ` [PATCH v3 00/13] telemetry JSON escaping and other enhancements fengchengwen
2022-09-26 11:52 ` David Marchand
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=20220909093523.471727-13-bruce.richardson@intel.com \
--to=bruce.richardson@intel.com \
--cc=ciara.power@intel.com \
--cc=dev@dpdk.org \
--cc=mb@smartsharesystems.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).