From patchwork Thu Jan 29 10:39:53 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ola Liljedahl X-Patchwork-Id: 43903 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5EE8C24119 for ; Thu, 29 Jan 2015 10:40:38 +0000 (UTC) Received: by mail-wi0-f197.google.com with SMTP id n3sf11419943wiv.0 for ; Thu, 29 Jan 2015 02:40:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:subject:precedence:list-id:list-unsubscribe:list-archive :list-post:list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=a11k8cYq6CkYF7guSspoIueExxZq7BUx2ONf8L2mH20=; b=P2IJk07NzOR01JF6PcFjm4pH7iQAykAISH7LemGs5V9cbOhBsMIC5BbjPB2MEiZtYe zzmcxP/Sm8J08Gh8nrUnPM/RHaar+lCbEAeEVwWriiu2WEwrCsOF5Dl7s6d0lMxErlX/ TfmEVrMQoJ/y5VTHwJKKwyX3lFnB3OJk9tGgWUtNk1/C3ryjj9SrwM1rmIpc7I1RZspT fS1uaFgGJKwAYISP6Wj99wWkvlyl8VZ+EOm2MSa7oMBHwSa2E10sjZkRiAXRUTJ72zoX kqPo/X/z7SyaMLbjG72DneIczP8qajqivPpabzE5wU9kNSNhZM8+C0sSbwLgMqoLNByl ZlHQ== X-Gm-Message-State: ALoCoQlC/etSXXdZ1dDnKZ87WE3YpHwhT0dZLH+X/dpEtLpoONl6UWxIkLV8TD9l5rFxW8NIYEqm X-Received: by 10.180.98.199 with SMTP id ek7mr310070wib.1.1422528037711; Thu, 29 Jan 2015 02:40:37 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.36.42 with SMTP id n10ls271250laj.95.gmail; Thu, 29 Jan 2015 02:40:37 -0800 (PST) X-Received: by 10.112.181.41 with SMTP id dt9mr55111lbc.56.1422528037552; Thu, 29 Jan 2015 02:40:37 -0800 (PST) Received: from mail-lb0-f169.google.com (mail-lb0-f169.google.com. [209.85.217.169]) by mx.google.com with ESMTPS id jq17si6984360lab.78.2015.01.29.02.40.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 29 Jan 2015 02:40:37 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.169 as permitted sender) client-ip=209.85.217.169; Received: by mail-lb0-f169.google.com with SMTP id f15so26587579lbj.0 for ; Thu, 29 Jan 2015 02:40:37 -0800 (PST) X-Received: by 10.112.188.227 with SMTP id gd3mr13373497lbc.22.1422528037451; Thu, 29 Jan 2015 02:40:37 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.35.133 with SMTP id h5csp1719407lbj; Thu, 29 Jan 2015 02:40:36 -0800 (PST) X-Received: by 10.229.182.9 with SMTP id ca9mr5990qcb.31.1422528035587; Thu, 29 Jan 2015 02:40:35 -0800 (PST) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id h39si4304027qgd.108.2015.01.29.02.40.34 (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 29 Jan 2015 02:40:35 -0800 (PST) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-35-177-41.ec2.internal) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YGmWW-0007KH-BL; Thu, 29 Jan 2015 10:40:32 +0000 Received: from mail-lb0-f177.google.com ([209.85.217.177]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YGmWH-0007Ic-N1 for lng-odp@lists.linaro.org; Thu, 29 Jan 2015 10:40:17 +0000 Received: by mail-lb0-f177.google.com with SMTP id p9so26551215lbv.8 for ; Thu, 29 Jan 2015 02:40:11 -0800 (PST) X-Received: by 10.112.125.41 with SMTP id mn9mr13374947lbb.80.1422528011826; Thu, 29 Jan 2015 02:40:11 -0800 (PST) Received: from macmini.lan (78-82-118-111.tn.glocalnet.net. [78.82.118.111]) by mx.google.com with ESMTPSA id y11sm2037670lba.5.2015.01.29.02.40.10 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 29 Jan 2015 02:40:10 -0800 (PST) From: Ola Liljedahl To: lng-odp@lists.linaro.org Date: Thu, 29 Jan 2015 11:39:53 +0100 Message-Id: <1422528008-29845-2-git-send-email-ola.liljedahl@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1422528008-29845-1-git-send-email-ola.liljedahl@linaro.org> References: <1422528008-29845-1-git-send-email-ola.liljedahl@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCH 01/16] api: odp_cpumask.h: odp_cpumask_to_str() return bufsz X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: lng-odp-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ola.liljedahl@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.169 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 odp_cpumask_to_str() definition modified to pass output buffer size by reference and the variable is updated with the number of bytes written on success or the minimum required buffer size should it be too small. odp_cpumask_to_str() returns 0 on success and <0 on failure. Updated the implementation and usages of this call to match. Signed-off-by: Ola Liljedahl --- (This document/code contribution attached is provided under the terms of agreement LES-LTM-21309) example/generator/odp_generator.c | 5 +- example/ipsec/odp_ipsec.c | 5 +- example/l2fwd/odp_l2fwd.c | 5 +- example/packet/odp_pktio.c | 5 +- example/timer/odp_timer_test.c | 5 +- platform/linux-generic/include/api/odp_cpumask.h | 103 ++++++++++++++--------- platform/linux-generic/odp_cpumask.c | 34 +++++--- test/api_test/odp_common.c | 5 +- test/performance/odp_scheduling.c | 5 +- 9 files changed, 106 insertions(+), 66 deletions(-) diff --git a/example/generator/odp_generator.c b/example/generator/odp_generator.c index 492664e..635d15d 100644 --- a/example/generator/odp_generator.c +++ b/example/generator/odp_generator.c @@ -545,7 +545,7 @@ int main(int argc, char *argv[]) int i; odp_shm_t shm; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_BUFSIZE]; odp_pool_param_t params; /* Init ODP before calling anything else */ @@ -596,7 +596,8 @@ int main(int argc, char *argv[]) * Start mapping thread from CPU #1 */ num_workers = odph_linux_cpumask_default(&cpumask, num_workers); - odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); + size_t bufsz = sizeof(cpumaskstr); + (void)odp_cpumask_to_str(&cpumask, cpumaskstr, &bufsz); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); diff --git a/example/ipsec/odp_ipsec.c b/example/ipsec/odp_ipsec.c index f2d2fc7..730491d 100644 --- a/example/ipsec/odp_ipsec.c +++ b/example/ipsec/odp_ipsec.c @@ -1175,7 +1175,7 @@ main(int argc, char *argv[]) int stream_count; odp_shm_t shm; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_BUFSIZE]; odp_pool_param_t params; /* Init ODP before calling anything else */ @@ -1224,7 +1224,8 @@ main(int argc, char *argv[]) * Start mapping thread from CPU #1 */ num_workers = odph_linux_cpumask_default(&cpumask, num_workers); - odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); + size_t bufsz = sizeof(cpumaskstr); + (void)odp_cpumask_to_str(&cpumask, cpumaskstr, &bufsz); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); diff --git a/example/l2fwd/odp_l2fwd.c b/example/l2fwd/odp_l2fwd.c index 18403da..9a550fc 100644 --- a/example/l2fwd/odp_l2fwd.c +++ b/example/l2fwd/odp_l2fwd.c @@ -292,7 +292,7 @@ int main(int argc, char *argv[]) int num_workers; odp_shm_t shm; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_BUFSIZE]; odp_pool_param_t params; /* Init ODP before calling anything else */ @@ -334,7 +334,8 @@ int main(int argc, char *argv[]) * Start mapping thread from CPU #1 */ num_workers = odph_linux_cpumask_default(&cpumask, num_workers); - odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); + size_t bufsz = sizeof(cpumaskstr); + (void)odp_cpumask_to_str(&cpumask, cpumaskstr, &bufsz); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); diff --git a/example/packet/odp_pktio.c b/example/packet/odp_pktio.c index c4c720b..924a6c7 100644 --- a/example/packet/odp_pktio.c +++ b/example/packet/odp_pktio.c @@ -285,7 +285,7 @@ int main(int argc, char *argv[]) int i; int cpu; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_BUFSIZE]; odp_pool_param_t params; args = calloc(1, sizeof(args_t)); @@ -322,7 +322,8 @@ int main(int argc, char *argv[]) * Start mapping thread from CPU #1 */ num_workers = odph_linux_cpumask_default(&cpumask, num_workers); - odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); + size_t bufsz = sizeof(cpumaskstr); + (void)odp_cpumask_to_str(&cpumask, cpumaskstr, &bufsz); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); diff --git a/example/timer/odp_timer_test.c b/example/timer/odp_timer_test.c index fafa0a4..e33468b 100644 --- a/example/timer/odp_timer_test.c +++ b/example/timer/odp_timer_test.c @@ -317,7 +317,7 @@ int main(int argc, char *argv[]) odp_timer_pool_param_t tparams; odp_timer_pool_info_t tpinfo; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_BUFSIZE]; printf("\nODP timer example starts\n"); @@ -358,7 +358,8 @@ int main(int argc, char *argv[]) * Start mapping thread from CPU #1 */ num_workers = odph_linux_cpumask_default(&cpumask, num_workers); - odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); + size_t bufsz = sizeof(cpumaskstr); + (void)odp_cpumask_to_str(&cpumask, cpumaskstr, &bufsz); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); diff --git a/platform/linux-generic/include/api/odp_cpumask.h b/platform/linux-generic/include/api/odp_cpumask.h index b746a4d..1784d93 100644 --- a/platform/linux-generic/include/api/odp_cpumask.h +++ b/platform/linux-generic/include/api/odp_cpumask.h @@ -22,6 +22,7 @@ extern "C" { #include #include +#include /** @addtogroup odp_scheduler * CPU mask operations. @@ -29,6 +30,11 @@ extern "C" { */ /** + * Minimum size of output buffer for odp_cpumask_to_str() + */ +#define ODP_CPUMASK_BUFSIZE ((ODP_CONFIG_MAX_THREADS + 3) / 4 + 3) + +/** * CPU mask * * Don't access directly, use access functions. @@ -47,111 +53,127 @@ typedef struct odp_cpumask_t { void odp_cpumask_from_str(odp_cpumask_t *mask, const char *str); /** - * Write CPU mask as a string of hexadecimal digits + * Format CPU mask as a string of hexadecimal digits + * + * @param mask CPU mask to format + * @param[out] buf Output buffer + * @param[in,out] bufsz Size of output buffer, updated with size of data + * actually written or minimum required buffer size * - * @param mask CPU mask - * @param str String for output - * @param len Size of string length (incl. ending zero) + * @return 0 on success and '*bufsz' updated with size of data written + * @retval <0 on failure (buffer too small) and '*bufsz' updated with minumum + * required buffer size */ -void odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int len); +int odp_cpumask_to_str(const odp_cpumask_t *mask, char *buf, size_t *bufsz); /** - * Clear entire mask - * @param mask CPU mask to flush with zero value + * Clear entire CPU mask + * @param mask CPU mask to update */ void odp_cpumask_zero(odp_cpumask_t *mask); /** - * Add cpu to mask - * @param mask add cpu number in CPU mask + * Add CPU to mask + * @param mask CPU mask to update * @param cpu CPU number */ void odp_cpumask_set(odp_cpumask_t *mask, int cpu); /** - * Remove cpu from mask - * @param mask clear cpu number from CPU mask + * Remove CPU from mask + * @param mask CPU mask to update * @param cpu CPU number */ void odp_cpumask_clr(odp_cpumask_t *mask, int cpu); /** - * Test if cpu is a member of mask - * @param mask CPU mask to check if cpu num set or not + * Test if CPU is a member of mask + * @param mask CPU mask to test * @param cpu CPU number - * @return non-zero if set otherwise 0 + * @return non-zero if set + * @retval 0 if not set */ int odp_cpumask_isset(const odp_cpumask_t *mask, int cpu); /** - * Count number of cpus in mask + * Count number of CPU's in mask * @param mask CPU mask - * @return cpumask count + * @return population count */ int odp_cpumask_count(const odp_cpumask_t *mask); /** - * Logical AND over two source masks. + * Member-wise AND over two CPU masks * - * @param dest Destination mask, can be one of the source masks - * @param src1 Source mask 1 - * @param src2 Source mask 2 + * @param dest Destination CPU mask (may be one of the source masks) + * @param src1 Source CPU mask 1 + * @param src2 Source CPU mask 2 */ void odp_cpumask_and(odp_cpumask_t *dest, odp_cpumask_t *src1, odp_cpumask_t *src2); /** - * Logical OR over two source masks. + * Member-wise OR over two CPU masks * - * @param dest Destination mask, can be one of the source masks - * @param src1 Source mask 1 - * @param src2 Source mask 2 + * @param dest Destination CPU mask (may be one of the source masks) + * @param src1 Source CPU mask 1 + * @param src2 Source CPU mask 2 */ void odp_cpumask_or(odp_cpumask_t *dest, odp_cpumask_t *src1, odp_cpumask_t *src2); /** - * Logical XOR over two source masks. + * Member-wise XOR over two CPU masks * - * @param dest Destination mask, can be one of the source masks - * @param src1 Source mask 1 - * @param src2 Source mask 2 + * @param dest Destination CPU mask (may be one of the source masks) + * @param src1 Source CPU mask 1 + * @param src2 Source CPU mask 2 */ void odp_cpumask_xor(odp_cpumask_t *dest, odp_cpumask_t *src1, odp_cpumask_t *src2); /** - * Test if two masks contain the same cpus + * Test if two CPU masks contain the same CPU's + * + * @param mask1 CPU mask 1 + * @param mask2 CPU mask 2 */ int odp_cpumask_equal(const odp_cpumask_t *mask1, const odp_cpumask_t *mask2); /** * Copy a CPU mask + * + * @param dest Destination CPU mask + * @param src Source CPU mask */ void odp_cpumask_copy(odp_cpumask_t *dest, const odp_cpumask_t *src); /** - * Find first CPU that is set in the mask + * Find first set CPU in mask * - * @param mask is the mask to be searched - * @return cpu else -1 if no bits set in cpumask + * @param mask CPU mask + * + * @return cpu number + * @retval <0 if no CPU found */ int odp_cpumask_first(const odp_cpumask_t *mask); /** - * Find last CPU that is set in the mask + * Find last set CPU in mask + * + * @param mask CPU mask * - * @param mask is the mask to be searched - * @return cpu else -1 if no bits set in cpumask + * @return cpu number + * @retval <0 if no CPU found */ int odp_cpumask_last(const odp_cpumask_t *mask); /** - * Find next cpu in mask + * Find next set CPU in mask * - * Finds the next cpu in the CPU mask, starting at the cpu passed. - * Use with odp_cpumask_first to traverse a CPU mask, i.e. + * Finds the next CPU in the CPU mask, starting at the CPU passed. + * Use with odp_cpumask_first to traverse a CPU mask, e.g. * * int cpu = odp_cpumask_first(&mask); * while (0 <= cpu) { @@ -160,9 +182,10 @@ int odp_cpumask_last(const odp_cpumask_t *mask); * cpu = odp_cpumask_next(&mask, cpu); * } * - * @param mask CPU mask to find next cpu in + * @param mask CPU mask * @param cpu CPU to start from - * @return cpu found else -1 + * @return cpu number + * @retval <0 if no CPU found * * @see odp_cpumask_first() */ diff --git a/platform/linux-generic/odp_cpumask.c b/platform/linux-generic/odp_cpumask.c index d9931b4..5c571d9 100644 --- a/platform/linux-generic/odp_cpumask.c +++ b/platform/linux-generic/odp_cpumask.c @@ -8,6 +8,7 @@ #define _GNU_SOURCE #endif #include +#include #include #include @@ -60,29 +61,36 @@ void odp_cpumask_from_str(odp_cpumask_t *mask, const char *str_in) memcpy(&mask->set, &cpuset, sizeof(cpuset)); } -void odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int len) +int odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, size_t *len) { char *p = str; int cpu = odp_cpumask_last(mask); - int nibbles; + unsigned int nibbles; int value; - /* Quickly handle bad string length or empty mask */ - if (len <= 0) - return; - *str = 0; + /* Handle bad string length, need at least 4 chars for "0x0" and + * terminating null char */ + if (*len < 4) { + *len = ODP_CPUMASK_BUFSIZE; + return -1; /* Failure */ + } + + /* Handle no CPU found */ if (cpu < 0) { - if (len >= 4) - strcpy(str, "0x0"); - return; + strcpy(str, "0x0"); + *len = strlen(str) + 1; /* Include terminating null char */ + return 0; /* Success */ } + /* CPU was found and cpu >= 0 */ - /* Compute number nibbles in cpumask that have bits set */ + /* Compute number of nibbles in cpumask that have bits set */ nibbles = (cpu / 4) + 1; /* Verify minimum space (account for "0x" and termination) */ - if (len < (3 + nibbles)) - return; + if (*len < (3 + nibbles)) { + *len = ODP_CPUMASK_BUFSIZE; + return -1; /* Failure */ + } /* Prefix */ *p++ = '0'; @@ -110,6 +118,8 @@ void odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int len) /* Terminate the string */ *p++ = 0; + *len = p - str; + return 0; /* Success */ } void odp_cpumask_zero(odp_cpumask_t *mask) diff --git a/test/api_test/odp_common.c b/test/api_test/odp_common.c index bce6f09..c8e9f78 100644 --- a/test/api_test/odp_common.c +++ b/test/api_test/odp_common.c @@ -30,14 +30,15 @@ __thread test_shared_data_t *test_shared_data; /**< pointer to shared data * void odp_print_system_info(void) { odp_cpumask_t cpumask; - char str[32]; + char str[ODP_CPUMASK_BUFSIZE]; + size_t strsz = sizeof(str); memset(str, 1, sizeof(str)); odp_cpumask_zero(&cpumask); odp_cpumask_from_str(&cpumask, "0x1"); - odp_cpumask_to_str(&cpumask, str, sizeof(str)); + (void)odp_cpumask_to_str(&cpumask, str, &strsz); printf("\n"); printf("ODP system info\n"); diff --git a/test/performance/odp_scheduling.c b/test/performance/odp_scheduling.c index 32155bd..fbe22b8 100644 --- a/test/performance/odp_scheduling.c +++ b/test/performance/odp_scheduling.c @@ -828,7 +828,8 @@ int main(int argc, char *argv[]) int prios; odp_shm_t shm; test_globals_t *globals; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_BUFSIZE]; + size_t bufsz = sizeof(cpumaskstr); odp_pool_param_t params; printf("\nODP example starts\n\n"); @@ -879,7 +880,7 @@ int main(int argc, char *argv[]) * Start mapping thread from CPU #1 */ num_workers = odph_linux_cpumask_default(&cpumask, num_workers); - odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); + (void)odp_cpumask_to_str(&cpumask, cpumaskstr, &bufsz); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask));