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 2FFC0A0543; Thu, 15 Dec 2022 02:32:17 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C6EF940684; Thu, 15 Dec 2022 02:32:16 +0100 (CET) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by mails.dpdk.org (Postfix) with ESMTP id 37C14400D6 for ; Thu, 15 Dec 2022 02:32:15 +0100 (CET) Received: from kwepemm600004.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NXZPZ0tYMzJpMh; Thu, 15 Dec 2022 09:28:34 +0800 (CST) Received: from [10.67.103.231] (10.67.103.231) 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.34; Thu, 15 Dec 2022 09:32:12 +0800 Message-ID: <2a6492f6-5d3d-d107-e801-25dd90b15d3c@huawei.com> Date: Thu, 15 Dec 2022 09:32:12 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: [PATCH V5 6/8] telemetry: support adding integer value as hexadecimal To: Bruce Richardson CC: , , , , , References: <20221208080540.62913-1-lihuisong@huawei.com> <20221214123253.29549-1-lihuisong@huawei.com> <20221214123253.29549-7-lihuisong@huawei.com> From: "lihuisong (C)" In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.67.103.231] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) 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 在 2022/12/14 23:38, Bruce Richardson 写道: > On Wed, Dec 14, 2022 at 08:32:51PM +0800, Huisong Li wrote: >> Sometimes displaying a unsigned integer value as hexadecimal encoded style >> is more expected for human consumption, such as, offload capability and >> device flag. This patch introduces two APIs to add unsigned integer value >> as hexadecimal encoded string to array or dictionary. And user can choose >> whether the stored value is padding to the specified width. >> >> Signed-off-by: Huisong Li >> Acked-by: Morten Brørup >> Acked-by: Chengwen Feng >> --- >> lib/telemetry/rte_telemetry.h | 47 +++++++++++++++++++++ >> lib/telemetry/telemetry_data.c | 74 ++++++++++++++++++++++++++++++++++ >> lib/telemetry/version.map | 9 +++++ >> 3 files changed, 130 insertions(+) >> >> diff --git a/lib/telemetry/rte_telemetry.h b/lib/telemetry/rte_telemetry.h >> index 40e9a3bf9d..b24f0310ea 100644 >> --- a/lib/telemetry/rte_telemetry.h >> +++ b/lib/telemetry/rte_telemetry.h >> @@ -10,6 +10,7 @@ extern "C" { >> #endif >> >> #include >> +#include >> >> /** Maximum length for string used in object. */ >> #define RTE_TEL_MAX_STRING_LEN 128 >> @@ -153,6 +154,28 @@ int >> rte_tel_data_add_array_container(struct rte_tel_data *d, >> struct rte_tel_data *val, int keep); >> >> +/** >> + * Convert a unsigned integer 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 val >> + * The number to be returned in the array as a hexadecimal encoded strings. >> + * @param display_bitwidth >> + * The display bit width of the 'val'. If 'display_bitwidth' is zero, the >> + * value is stored in the array as no-padding zero hexadecimal encoded string, >> + * or the value is stored as padding zero to specified hexadecimal width. >> + * @return >> + * 0 on success, negative errno on error >> + */ >> +__rte_experimental >> +int >> +rte_tel_data_add_array_uint_hex(struct rte_tel_data *d, uint64_t val, >> + uint8_t display_bitwidth); >> + >> /** >> * Add a string value to a dictionary. >> * The dict must have been started by rte_tel_data_start_dict(). >> @@ -231,6 +254,30 @@ int >> rte_tel_data_add_dict_container(struct rte_tel_data *d, const char *name, >> struct rte_tel_data *val, int keep); >> >> +/** >> + * Convert a unsigned integer to hexadecimal encoded strings and add this string >> + * to an 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 of the value is to be stored 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. >> + * @param display_bitwidth >> + * The display bit width of the 'val'. If 'display_bitwidth' is zero, the >> + * value is stored in the array as no-padding zero hexadecimal encoded string, >> + * or the value is stored as padding zero to specified hexadecimal width. >> + * @return >> + * 0 on success, negative errno on error >> + */ >> +__rte_experimental >> +int >> +rte_tel_data_add_dict_uint_hex(struct rte_tel_data *d, const char *name, >> + uint64_t val, uint8_t display_bitwidth); >> + >> /** >> * 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 34366ecee3..14cb56d680 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,8 @@ >> >> #include "telemetry_data.h" >> >> +#define RTE_TEL_UINT_HEX_STR_BUF_LEN 64 >> + >> int >> rte_tel_data_start_array(struct rte_tel_data *d, enum rte_tel_value_type type) >> { >> @@ -97,6 +100,60 @@ rte_tel_data_add_array_container(struct rte_tel_data *d, >> return 0; >> } >> >> +/* To suppress compiler warning about format string. */ >> +#if defined(RTE_TOOLCHAIN_GCC) >> +#pragma GCC diagnostic push >> +#pragma GCC diagnostic ignored "-Wformat-nonliteral" >> +#elif defined(RTE_TOOLCHAIN_CLANG) >> +#pragma clang diagnostic push >> +#pragma clang diagnostic ignored "-Wformat-nonliteral" >> +#endif >> + >> +static int >> +rte_tel_uint_to_hex_encoded_str(char *buf, uint16_t len, uint64_t val, >> + uint8_t display_bitwidth) >> +{ >> +#define RTE_TEL_UINT_HEX_FORMAT_LEN 16 >> + >> + uint8_t spec_hex_width = (display_bitwidth + 3) / 4; >> + char format[RTE_TEL_UINT_HEX_FORMAT_LEN]; >> + >> + /* Buffer needs to have room to contain the prefix '0x' and '\0'. */ >> + if (len < spec_hex_width + 3) >> + return -EINVAL; >> + >> + if (display_bitwidth != 0) { >> + sprintf(format, "0x%%0%u" PRIx64, spec_hex_width); >> + sprintf(buf, format, val); >> + } else { >> + sprintf(buf, "0x%" PRIx64, val); >> + } >> + >> + return 0; >> +} >> + >> +#if defined(RTE_TOOLCHAIN_GCC) >> +#pragma GCC diagnostic pop >> +#elif defined(RTE_TOOLCHAIN_CLANG) >> +#pragma clang diagnostic pop >> +#endif >> + >> +int >> +rte_tel_data_add_array_uint_hex(struct rte_tel_data *d, uint64_t val, >> + uint8_t display_bitwidth) >> +{ >> + char hex_str[RTE_TEL_UINT_HEX_STR_BUF_LEN]; >> + int ret; >> + >> + ret = rte_tel_uint_to_hex_encoded_str(hex_str, >> + RTE_TEL_UINT_HEX_STR_BUF_LEN, val, >> + display_bitwidth); > Small nit - the indentation here - and in a few other places does not match > that in the rest of the file. The existing file only uses tabs for indent, > and does not align continuations on brackets, it just double-indents. All right. Let's keep it in line with the file. > > /Bruce > .