From patchwork Tue Feb 3 16:48:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ola Liljedahl X-Patchwork-Id: 44257 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f200.google.com (mail-wi0-f200.google.com [209.85.212.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1F2322034D for ; Tue, 3 Feb 2015 16:49:00 +0000 (UTC) Received: by mail-wi0-f200.google.com with SMTP id fb4sf26490620wid.3 for ; Tue, 03 Feb 2015 08:48: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=Nk2/VA0G3OjH3AK6DUsxIO55C+D1JAzJlFCDwWsA+d0=; b=MnwTKbwEMtmfloMvhuR4J5lxl4hjfyoZWJjQrbNoe6y8FnCtlQw7+JMGKLLW0cr7Kg oxha4d/0OfXp14nUO13IRyXSSH10kX9xLYCEM+uxgFQmjYT8DwW8fFMgZPJ37PvpwcIF GO02AC8pji/7II+ibHx59scWW52LTKGF6GubpkVoiaPELIRGen+2u3G4K2GMy4r7vaLg WdCM/wk3fam+58VU6FSfI1NxQLUDkLwt2O4EITErLoGWCrtfN1PvZXqX/CJKW+DKtjEL PqqNpAa/0j2q0/rR3DpAKB5PolA6T6+v6BnmAWDDiPkgR9Ism3PZnekmw8Ezkg8zTP8Q ej+w== X-Gm-Message-State: ALoCoQlBZj6mAteSopqVS2s5aNdzJh+qHg0WhHFGFIPENgYStBeEs5iIve2YDI+PWgCBLOi/adJ4 X-Received: by 10.180.92.133 with SMTP id cm5mr2092207wib.4.1422982139353; Tue, 03 Feb 2015 08:48:59 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.3.170 with SMTP id d10ls690118lad.37.gmail; Tue, 03 Feb 2015 08:48:59 -0800 (PST) X-Received: by 10.152.6.8 with SMTP id w8mr10735858law.45.1422982139182; Tue, 03 Feb 2015 08:48:59 -0800 (PST) Received: from mail-la0-f46.google.com (mail-la0-f46.google.com. [209.85.215.46]) by mx.google.com with ESMTPS id xv1si19681280lab.61.2015.02.03.08.48.59 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 03 Feb 2015 08:48:59 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.46 as permitted sender) client-ip=209.85.215.46; Received: by mail-la0-f46.google.com with SMTP id s18so53525529lam.5 for ; Tue, 03 Feb 2015 08:48:59 -0800 (PST) X-Received: by 10.112.173.202 with SMTP id bm10mr25608843lbc.24.1422982139047; Tue, 03 Feb 2015 08:48:59 -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 h5csp2058183lbj; Tue, 3 Feb 2015 08:48:55 -0800 (PST) X-Received: by 10.224.126.3 with SMTP id a3mr55196241qas.100.1422982134786; Tue, 03 Feb 2015 08:48:54 -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 s95si29420010qgs.30.2015.02.03.08.48.53 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 03 Feb 2015 08:48:54 -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 1YIgeh-00087l-91; Tue, 03 Feb 2015 16:48:51 +0000 Received: from mail-la0-f45.google.com ([209.85.215.45]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YIgeW-00086b-J6 for lng-odp@lists.linaro.org; Tue, 03 Feb 2015 16:48:40 +0000 Received: by mail-la0-f45.google.com with SMTP id gd6so53264277lab.4 for ; Tue, 03 Feb 2015 08:48:35 -0800 (PST) X-Received: by 10.112.118.144 with SMTP id km16mr25834656lbb.75.1422982114972; Tue, 03 Feb 2015 08:48:34 -0800 (PST) Received: from macmini.lan (78-82-118-111.tn.glocalnet.net. [78.82.118.111]) by mx.google.com with ESMTPSA id y2sm434179lay.23.2015.02.03.08.48.34 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 03 Feb 2015 08:48:34 -0800 (PST) From: Ola Liljedahl To: lng-odp@lists.linaro.org Date: Tue, 3 Feb 2015 17:48:11 +0100 Message-Id: <1422982108-13813-2-git-send-email-ola.liljedahl@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1422982108-13813-1-git-send-email-ola.liljedahl@linaro.org> References: <1422982108-13813-1-git-send-email-ola.liljedahl@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCHv5 01/18] 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.215.46 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_BUFSIZE 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 03dab50..2433cb6 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,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 03f2419..869fd3a 100644 --- a/example/ipsec/odp_ipsec.c +++ b/example/ipsec/odp_ipsec.c @@ -1193,7 +1193,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 */ @@ -1242,7 +1242,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 7a520fb..e22ec0c 100644 --- a/example/l2fwd/odp_l2fwd.c +++ b/example/l2fwd/odp_l2fwd.c @@ -294,7 +294,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 */ @@ -336,7 +336,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 28ed98c..e38029a 100644 --- a/example/packet/odp_pktio.c +++ b/example/packet/odp_pktio.c @@ -287,7 +287,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)); @@ -324,7 +324,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..299ed89 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,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 7482899..409f3b4 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_BUFSIZE + * 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_BUFSIZE) + * @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 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..20626f0 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_BUFSIZE ((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 6088ca2..0bae1ce 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..b5f2d4d 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_BUFSIZE]; 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 3d7167f..ba13d8e 100644 --- a/test/performance/odp_scheduling.c +++ b/test/performance/odp_scheduling.c @@ -828,7 +828,7 @@ int main(int argc, char *argv[]) int prios; odp_shm_t shm; test_globals_t *globals; - char cpumaskstr[64]; + char cpumaskstr[ODP_CPUMASK_BUFSIZE]; odp_pool_param_t params; printf("\nODP example starts\n\n"); @@ -879,7 +879,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));