From patchwork Tue Feb 3 14:53:04 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ola Liljedahl X-Patchwork-Id: 44230 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-we0-f200.google.com (mail-we0-f200.google.com [74.125.82.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 359A32034D for ; Tue, 3 Feb 2015 14:54:01 +0000 (UTC) Received: by mail-we0-f200.google.com with SMTP id m14sf21272716wev.3 for ; Tue, 03 Feb 2015 06:54:00 -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=V//NmnLCgLoZVXEdxsoKTi/pMQXG7NgngDlu0vtQBV4=; b=he8J1Pe7x4x1Jr08Rj5Kt7JtLKAbezFfJ7aaHpDg9L6X9psbJj30UyaOSJd5DKh1Mz UTVuLsAmzb+W/MRgEf1Yx2/FX7fvzyO51YoGTwH3+HYnehZsY4Xd3MAYoYx2hoYZAVq+ tg7dd7YaCb0P/4DkKhi2ppmr/uUDmVkuQOXiuGKtlOWcLbRSGYMT1+ieUCBiyYMKrFlL VvfO4G4Raahrzfh2mdtuy0jp6x6aw+r0I+qwF+ai4aScUbXq5J/mUFQ6RCOPFNDrdpRH Oa3eESx3oq8O+C9yxH0+7bjJjv6y+4VkdLY4CLxsW6JcwLF8vShA9xczdC4li6mEVKk/ /fpg== X-Gm-Message-State: ALoCoQn8C5rZsAA50JnyhS7WgE81h02cG1knA51vO4RLa8l5Sao0g+PJh1BhMuoDDVKIfdqGNW7z X-Received: by 10.194.204.201 with SMTP id la9mr3177459wjc.6.1422975240407; Tue, 03 Feb 2015 06:54:00 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.8.13 with SMTP id n13ls766321laa.80.gmail; Tue, 03 Feb 2015 06:54:00 -0800 (PST) X-Received: by 10.152.29.6 with SMTP id f6mr13550711lah.82.1422975240244; Tue, 03 Feb 2015 06:54:00 -0800 (PST) Received: from mail-la0-f43.google.com (mail-la0-f43.google.com. [209.85.215.43]) by mx.google.com with ESMTPS id q7si19389681lah.13.2015.02.03.06.54.00 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 03 Feb 2015 06:54:00 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 as permitted sender) client-ip=209.85.215.43; Received: by mail-la0-f43.google.com with SMTP id pn19so6448557lab.2 for ; Tue, 03 Feb 2015 06:54:00 -0800 (PST) X-Received: by 10.152.8.1 with SMTP id n1mr23996699laa.47.1422975240125; Tue, 03 Feb 2015 06:54:00 -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 h5csp1987742lbj; Tue, 3 Feb 2015 06:53:59 -0800 (PST) X-Received: by 10.224.14.84 with SMTP id f20mr53308598qaa.43.1422975235246; Tue, 03 Feb 2015 06:53:55 -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 q10si848051qat.3.2015.02.03.06.53.46 (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 03 Feb 2015 06:53:55 -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 1YIerI-0001ZC-Lu; Tue, 03 Feb 2015 14:53:44 +0000 Received: from mail-la0-f47.google.com ([209.85.215.47]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1YIer8-0001Xm-4z for lng-odp@lists.linaro.org; Tue, 03 Feb 2015 14:53:34 +0000 Received: by mail-la0-f47.google.com with SMTP id hz20so52409839lab.6 for ; Tue, 03 Feb 2015 06:53:28 -0800 (PST) X-Received: by 10.152.116.9 with SMTP id js9mr24848101lab.20.1422975207930; Tue, 03 Feb 2015 06:53:27 -0800 (PST) Received: from localhost.localdomain (78-82-118-111.tn.glocalnet.net. [78.82.118.111]) by mx.google.com with ESMTPSA id jf3sm5082572lbc.44.2015.02.03.06.53.26 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 03 Feb 2015 06:53:27 -0800 (PST) From: Ola Liljedahl To: lng-odp@lists.linaro.org Date: Tue, 3 Feb 2015 15:53:04 +0100 Message-Id: <1422975201-3364-2-git-send-email-ola.liljedahl@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1422975201-3364-1-git-send-email-ola.liljedahl@linaro.org> References: <1422975201-3364-1-git-send-email-ola.liljedahl@linaro.org> X-Topics: patch Subject: [lng-odp] [PATCHv4 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.43 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..65969c3 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] buf Output buffer (use ODP_CPUMASK_BUFSIZE) + * @param bufsz 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 *buf, ssize_t bufsz); /** * 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));