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 96AF6A00C2; Fri, 9 Dec 2022 12:05:51 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8C58242D62; Fri, 9 Dec 2022 12:05:01 +0100 (CET) Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by mails.dpdk.org (Postfix) with ESMTP id D1C1442D40 for ; Fri, 9 Dec 2022 12:04:54 +0100 (CET) Received: from kwepemm600004.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NT7SF6CNvzJqRc; Fri, 9 Dec 2022 19:03:57 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by kwepemm600004.china.huawei.com (7.193.23.242) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 9 Dec 2022 19:04:46 +0800 From: Huisong Li To: CC: , , , , , , Subject: [PATCH V2 09/11] telemetry: support adding integer value as hexadecimal Date: Fri, 9 Dec 2022 19:04:48 +0800 Message-ID: <20221209110450.62456-10-lihuisong@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20221209110450.62456-1-lihuisong@huawei.com> References: <20221208080540.62913-1-lihuisong@huawei.com> <20221209110450.62456-1-lihuisong@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To kwepemm600004.china.huawei.com (7.193.23.242) X-CFilter-Loop: Reflected 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 Sometimes displaying a integer value as hexadecimal encoded style is more expected for human consumption, such as, offload capability and device falg. This patch introduces some APIs to add 'u32' and 'u64' value as hexadecimal encoded string to array or dictionary. Signed-off-by: Huisong Li --- lib/telemetry/rte_telemetry.h | 72 ++++++++++++++++++++++++++++++++++ lib/telemetry/telemetry_data.c | 46 ++++++++++++++++++++++ lib/telemetry/version.map | 5 +++ 3 files changed, 123 insertions(+) diff --git a/lib/telemetry/rte_telemetry.h b/lib/telemetry/rte_telemetry.h index 5efdafc156..dac87050da 100644 --- a/lib/telemetry/rte_telemetry.h +++ b/lib/telemetry/rte_telemetry.h @@ -170,6 +170,38 @@ int rte_tel_data_add_array_container(struct rte_tel_data *d, struct rte_tel_data *val, int keep); +/** + * Convert a uint32_t to hexadecimal encoded strings and add this string + * to an array. + * The array must have been started by rte_tel_data_start_array() with + * RTE_TEL_STRING_VAL as the type parameter. + * + * @param d + * The data structure passed to the callback + * @param x + * The number to be returned in the array as a hexadecimal encoded strings + * @return + * 0 on success, negative errno on error + */ +__rte_experimental +int rte_tel_data_add_array_hex_u32_str(struct rte_tel_data *d, uint32_t x); + +/** + * Convert a uint64_t to hexadecimal encoded strings and add this string + * to an array. + * The array must have been started by rte_tel_data_start_array() with + * RTE_TEL_STRING_VAL as the type parameter. + * + * @param d + * The data structure passed to the callback + * @param x + * The number to be returned in the array as a hexadecimal encoded strings + * @return + * 0 on success, negative errno on error + */ +__rte_experimental +int rte_tel_data_add_array_hex_u64_str(struct rte_tel_data *d, uint64_t x); + /** * Add a string value to a dictionary. * The dict must have been started by rte_tel_data_start_dict(). @@ -266,6 +298,46 @@ int rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name, struct rte_tel_data *val, int keep); +/** + * Convert a uint32_t to hexadecimal encoded strings and add this string + * to a dictionary. + * The dict must have been started by rte_tel_data_start_dict(). + * + * @param d + * The data structure passed to the callback + * @param name + * The name the value is to be stored under in the dict + * Must contain only alphanumeric characters or the symbols: '_' or '/' + * @param val + * The number to be stored in the dict as a hexadecimal encoded strings + * @return + * 0 on success, negative errno on error, E2BIG on string truncation of + * either name or value. + */ +__rte_experimental +int rte_tel_data_add_dict_hex_u32_str(struct rte_tel_data *d, + const char *name, uint32_t val); + +/** + * Convert a uint64_t to hexadecimal encoded strings and add this string + * to a dictionary. + * The dict must have been started by rte_tel_data_start_dict(). + * + * @param d + * The data structure passed to the callback + * @param name + * The name the value is to be stored under in the dict + * Must contain only alphanumeric characters or the symbols: '_' or '/' + * @param val + * The number to be stored in the dict as a hexadecimal encoded strings + * @return + * 0 on success, negative errno on error, E2BIG on string truncation of + * either name or value. + */ +__rte_experimental +int rte_tel_data_add_dict_hex_u64_str(struct rte_tel_data *d, + const char *name, uint64_t val); + /** * This telemetry callback is used when registering a telemetry command. * It handles getting and formatting information to be returned to telemetry diff --git a/lib/telemetry/telemetry_data.c b/lib/telemetry/telemetry_data.c index f8efc37763..b195b79fa4 100644 --- a/lib/telemetry/telemetry_data.c +++ b/lib/telemetry/telemetry_data.c @@ -4,6 +4,7 @@ #include #include +#include #undef RTE_USE_LIBBSD #include @@ -12,6 +13,9 @@ #include "telemetry_data.h" +/* The string length is equal to (sizeof(uint64_t) * 2 + 3) */ +#define RTE_TEL_HEX_UINT_MAX_STRING_LEN 19 + int rte_tel_data_start_array(struct rte_tel_data *d, enum rte_tel_value_type type) { @@ -126,6 +130,26 @@ rte_tel_data_add_array_container(struct rte_tel_data *d, return 0; } +int +rte_tel_data_add_array_hex_u32_str(struct rte_tel_data *d, uint32_t x) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%x", x); + + return rte_tel_data_add_array_string(d, hex_str); +} + +int +rte_tel_data_add_array_hex_u64_str(struct rte_tel_data *d, uint64_t x) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%"PRIx64"", x); + + return rte_tel_data_add_array_string(d, hex_str); +} + static bool valid_name(const char *name) { @@ -254,6 +278,28 @@ rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name, return bytes < RTE_TEL_MAX_STRING_LEN ? 0 : E2BIG; } +int +rte_tel_data_add_dict_hex_u32_str(struct rte_tel_data *d, const char *name, + uint32_t val) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%x", val); + + return rte_tel_data_add_dict_string(d, name, hex_str); +} + +int +rte_tel_data_add_dict_hex_u64_str(struct rte_tel_data *d, const char *name, + uint64_t val) +{ + char hex_str[RTE_TEL_HEX_UINT_MAX_STRING_LEN]; + + snprintf(hex_str, RTE_TEL_HEX_UINT_MAX_STRING_LEN, "0x%"PRIx64"", val); + + return rte_tel_data_add_dict_string(d, name, hex_str); +} + struct rte_tel_data * rte_tel_data_alloc(void) { diff --git a/lib/telemetry/version.map b/lib/telemetry/version.map index 3d1fb15637..994414c47f 100644 --- a/lib/telemetry/version.map +++ b/lib/telemetry/version.map @@ -3,6 +3,11 @@ EXPERIMENTAL { rte_tel_data_add_array_u32; rte_tel_data_add_dict_u32; + rte_tel_data_add_array_hex_u32_str; + rte_tel_data_add_array_hex_u64_str; + rte_tel_data_add_dict_hex_u32_str; + rte_tel_data_add_dict_hex_u64_str; + local: *; }; -- 2.33.0