From patchwork Wed Feb 4 21:07:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ola Liljedahl X-Patchwork-Id: 44385 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-we0-f198.google.com (mail-we0-f198.google.com [74.125.82.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id DA3D621173 for ; Wed, 4 Feb 2015 21:07:59 +0000 (UTC) Received: by mail-we0-f198.google.com with SMTP id q59sf2883942wes.1 for ; Wed, 04 Feb 2015 13:07:59 -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=lO53IrEAujv5BaRJIz7r4/KEgsK6XR6L3e4fADP4/xE=; b=cMlPRs9Edv1bLspHGWiaA4YTzuMPmH2zj0otyJOqvrbuBUyZX2salePmP4VHZn5I9q WH8mbDYmbljrQCTIzML1K12NEz2ZCo9KYjDOkiP03AMdyWcf1eWuP0GkamnqiWCBFlMB AYdAFXxo4rVq6lch4I0eB7JoOs0zh4CuwbsEfun9xqrqDBqNwrdcHvVjZ3RmY36anAxt WO2p6SuS1c/n/3MuNrLv6E+elL1O4wuiD3g4vFkiSoO/NhYk/+y/2k6pCr0NG0wUl0Ji OewA5am2U+goK3l8R6ke9gsCx7DRkLUU7A5LfUQKFCFcIuyiNlNe9cQnBbIQ9KFMcc/r 6S+A== X-Gm-Message-State: ALoCoQmLUYX5ByIUfbcGXVYu8pR7OaTDjpIwcdemEjPhc6ND1zjLQfCJXCzjTKg9xgW55WYU66JC X-Received: by 10.112.30.131 with SMTP id s3mr33671lbh.13.1423084079071; Wed, 04 Feb 2015 13:07:59 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.116.106 with SMTP id jv10ls86635lab.93.gmail; Wed, 04 Feb 2015 13:07:58 -0800 (PST) X-Received: by 10.152.6.195 with SMTP id d3mr379079laa.36.1423084078834; Wed, 04 Feb 2015 13:07:58 -0800 (PST) Received: from mail-lb0-f170.google.com (mail-lb0-f170.google.com. [209.85.217.170]) by mx.google.com with ESMTPS id yh1si2387932lab.112.2015.02.04.13.07.58 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Feb 2015 13:07:58 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.170 as permitted sender) client-ip=209.85.217.170; Received: by mail-lb0-f170.google.com with SMTP id w7so3796004lbi.1 for ; Wed, 04 Feb 2015 13:07:58 -0800 (PST) X-Received: by 10.112.188.227 with SMTP id gd3mr383056lbc.22.1423084078711; Wed, 04 Feb 2015 13:07:58 -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 h5csp743734lbj; Wed, 4 Feb 2015 13:07:57 -0800 (PST) X-Received: by 10.224.61.203 with SMTP id u11mr498004qah.33.1423084076977; Wed, 04 Feb 2015 13:07:56 -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 bt3si3408271qcb.47.2015.02.04.13.07.55 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 04 Feb 2015 13:07:56 -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 1YJ7Av-0006pX-Qs; Wed, 04 Feb 2015 21:07:53 +0000 Received: from mail-lb0-f175.google.com ([209.85.217.175]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YJ7Am-0006nZ-2g for lng-odp@lists.linaro.org; Wed, 04 Feb 2015 21:07:44 +0000 Received: by mail-lb0-f175.google.com with SMTP id 10so2844990lbg.6 for ; Wed, 04 Feb 2015 13:07:37 -0800 (PST) X-Received: by 10.112.92.129 with SMTP id cm1mr199467lbb.85.1423084057852; Wed, 04 Feb 2015 13:07:37 -0800 (PST) Received: from macmini.lan (78-82-118-111.tn.glocalnet.net. [78.82.118.111]) by mx.google.com with ESMTPSA id r7sm567954lbs.37.2015.02.04.13.07.36 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 04 Feb 2015 13:07:36 -0800 (PST) From: Ola Liljedahl To: lng-odp@lists.linaro.org Date: Wed, 4 Feb 2015 22:07:23 +0100 Message-Id: <1423084051-12962-2-git-send-email-ola.liljedahl@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1423084051-12962-1-git-send-email-ola.liljedahl@linaro.org> References: <1423084051-12962-1-git-send-email-ola.liljedahl@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCHv6 1/9] api: odp_cpumask.h: odp_cpumask_to_str() return chars written or error 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.170 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 Add define ODP_CPUMASK_STR_SIZE for minimum output buffer size for odp_cpumask_to_str(). odp_cpumask_to_str() takes output buffer size as input and returns number of chars written (on success), <0 on failure. Updated the implementation. Updated all usages in example and test programs. 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 | 4 ++-- example/ipsec/odp_ipsec.c | 4 ++-- example/l2fwd/odp_l2fwd.c | 4 ++-- example/packet/odp_pktio.c | 4 ++-- example/timer/odp_timer_test.c | 4 ++-- include/odp/api/cpumask.h | 23 ++++++++++++++----- .../linux-generic/include/odp/plat/cpumask_types.h | 5 +++++ platform/linux-generic/odp_cpumask.c | 26 +++++++++++++--------- test/api_test/odp_common.c | 4 ++-- test/performance/odp_scheduling.c | 4 ++-- 10 files changed, 51 insertions(+), 31 deletions(-) diff --git a/example/generator/odp_generator.c b/example/generator/odp_generator.c index b073987..124966a 100644 --- a/example/generator/odp_generator.c +++ b/example/generator/odp_generator.c @@ -549,7 +549,7 @@ int main(int argc, char *argv[]) int i; odp_shm_t shm; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_STR_SIZE]; odp_pool_param_t params; /* Init ODP before calling anything else */ @@ -600,7 +600,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, sizeof(cpumaskstr)); 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 1defb8f..c5edc22 100644 --- a/example/ipsec/odp_ipsec.c +++ b/example/ipsec/odp_ipsec.c @@ -1197,7 +1197,7 @@ main(int argc, char *argv[]) int stream_count; odp_shm_t shm; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_STR_SIZE]; odp_pool_param_t params; /* Init ODP before calling anything else */ @@ -1246,7 +1246,7 @@ 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, sizeof(cpumaskstr)); 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 aeb55ff..4873270 100644 --- a/example/l2fwd/odp_l2fwd.c +++ b/example/l2fwd/odp_l2fwd.c @@ -297,7 +297,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_STR_SIZE]; odp_pool_param_t params; /* Init ODP before calling anything else */ @@ -339,7 +339,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, sizeof(cpumaskstr)); 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 4c02ece..7c908b4 100644 --- a/example/packet/odp_pktio.c +++ b/example/packet/odp_pktio.c @@ -293,7 +293,7 @@ int main(int argc, char *argv[]) int i; int cpu; odp_cpumask_t cpumask; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_STR_SIZE]; odp_pool_param_t params; args = calloc(1, sizeof(args_t)); @@ -330,7 +330,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, sizeof(cpumaskstr)); 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 0df041f..9d16b8e 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_STR_SIZE]; printf("\nODP timer example starts\n"); @@ -358,7 +358,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, sizeof(cpumaskstr)); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); diff --git a/include/odp/api/cpumask.h b/include/odp/api/cpumask.h index a78d83c..7d782c7 100644 --- a/include/odp/api/cpumask.h +++ b/include/odp/api/cpumask.h @@ -18,12 +18,20 @@ extern "C" { #endif +#include +#include + /** @addtogroup odp_scheduler * CPU mask operations. * @{ */ - /** +/** + * @def ODP_CPUMASK_STR_SIZE + * Minimum size of output buffer for odp_cpumask_to_str() + */ + +/** * Add CPU mask bits from a string * * @param mask CPU mask to modify @@ -33,13 +41,16 @@ extern "C" { 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] str Output buffer (use ODP_CPUMASK_STR_SIZE) + * @param size Size of output buffer * - * @param mask CPU mask - * @param str String for output - * @param len Size of string length (incl. ending zero) + * @return number of characters written (including terminating null char) + * @retval <0 on failure (buffer too small) */ -void odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int len); +ssize_t odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, ssize_t size); /** * Clear entire CPU mask diff --git a/platform/linux-generic/include/odp/plat/cpumask_types.h b/platform/linux-generic/include/odp/plat/cpumask_types.h index 6d9e129..df3c797 100644 --- a/platform/linux-generic/include/odp/plat/cpumask_types.h +++ b/platform/linux-generic/include/odp/plat/cpumask_types.h @@ -25,6 +25,11 @@ extern "C" { #include /** + * Minimum size of output buffer for odp_cpumask_to_str() + */ +#define ODP_CPUMASK_STR_SIZE ((ODP_CONFIG_MAX_THREADS + 3) / 4 + 3) + +/** * CPU mask * * Don't access directly, use access functions. diff --git a/platform/linux-generic/odp_cpumask.c b/platform/linux-generic/odp_cpumask.c index 1749083..24b9d75 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,31 @@ 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) +ssize_t odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, ssize_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) + return -1; /* Failure */ + + /* Handle no CPU found */ if (cpu < 0) { - if (len >= 4) - strcpy(str, "0x0"); - return; + strcpy(str, "0x0"); + return strlen(str) + 1; /* 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; + return -1; /* Failure */ /* Prefix */ *p++ = '0'; @@ -110,6 +113,7 @@ void odp_cpumask_to_str(const odp_cpumask_t *mask, char *str, int len) /* Terminate the string */ *p++ = 0; + return p - str; /* 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 5f9504c..906c0ad 100644 --- a/test/api_test/odp_common.c +++ b/test/api_test/odp_common.c @@ -30,14 +30,14 @@ __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_STR_SIZE]; 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, sizeof(str)); printf("\n"); printf("ODP system info\n"); diff --git a/test/performance/odp_scheduling.c b/test/performance/odp_scheduling.c index 98c060e..46af479 100644 --- a/test/performance/odp_scheduling.c +++ b/test/performance/odp_scheduling.c @@ -832,7 +832,7 @@ int main(int argc, char *argv[]) int prios; odp_shm_t shm; test_globals_t *globals; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_STR_SIZE]; odp_pool_param_t params; printf("\nODP example starts\n\n"); @@ -883,7 +883,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, sizeof(cpumaskstr)); printf("num worker threads: %i\n", num_workers); printf("first CPU: %i\n", odp_cpumask_first(&cpumask));