From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 906B9A0540; Fri, 9 Sep 2022 11:36:57 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9B89E42B9B; Fri, 9 Sep 2022 11:35:58 +0200 (CEST) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mails.dpdk.org (Postfix) with ESMTP id D21DD42B83 for ; Fri, 9 Sep 2022 11:35:54 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662716155; x=1694252155; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VQAGXAJSVEJmt//LQD1buGWpxsCyKtStxK8yQanehtU=; b=d16XN6GRgiffSUEvgHRnFLFNVsbQ0vz8SE3XlLY5cXpEmoqq2czrE2Js F9x9yHhANPIxD27qVG+y3rw1zdtzTbSKGb1rmu6il5jXPmNMvvaJW+V5k Rlj357NdxITs5a+e93342vX9/aqpRYdMSAO0GIRsmbxZg2TYBx3JX9PBK rgJCeApkDv6IvahM4Xxsnqq+/6NIRqQPmK0AlrfXxj44RCXxxtWFz+5/7 /TJEGddecVIYkdSPrGgJlvolfN9Deyi30/RX0mHa7+MhpmzLyrq3B6tfo iOSsp9G5LbQBTkYE7nyWZgQpRf5nnu8ifw70f91933hknns0alBzkzPdA g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="297437156" X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="297437156" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 02:35:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="740996453" Received: from silpixa00401385.ir.intel.com ([10.237.214.161]) by orsmga004.jf.intel.com with ESMTP; 09 Sep 2022 02:35:53 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: Bruce Richardson , Ciara Power , =?UTF-8?q?Morten=20Br=C3=B8rup?= Subject: [PATCH v3 12/13] telemetry: eliminate duplicate code for json output Date: Fri, 9 Sep 2022 10:35:22 +0100 Message-Id: <20220909093523.471727-13-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220909093523.471727-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> <20220909093523.471727-1-bruce.richardson@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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 Acked-by: Ciara Power Acked-by: Morten Brørup --- 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