From patchwork Sat Jan 14 18:24:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bill Fischofer X-Patchwork-Id: 91510 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp732449qgi; Sat, 14 Jan 2017 10:24:50 -0800 (PST) X-Received: by 10.55.151.199 with SMTP id z190mr25687525qkd.166.1484418290225; Sat, 14 Jan 2017 10:24:50 -0800 (PST) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id b193si10967264qkc.58.2017.01.14.10.24.33; Sat, 14 Jan 2017 10:24:50 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 4E5FD60AE3; Sat, 14 Jan 2017 18:24:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 2432D608A5; Sat, 14 Jan 2017 18:24:27 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 95E5E60A2D; Sat, 14 Jan 2017 18:24:24 +0000 (UTC) Received: from mail-ot0-f177.google.com (mail-ot0-f177.google.com [74.125.82.177]) by lists.linaro.org (Postfix) with ESMTPS id 62CBD60899 for ; Sat, 14 Jan 2017 18:24:23 +0000 (UTC) Received: by mail-ot0-f177.google.com with SMTP id 73so21515419otj.0 for ; Sat, 14 Jan 2017 10:24:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=LJYUl0V9CxUCPi68so8UXJ6huB66Wu60rEtrRGaXigE=; b=rPJOrMOIE7ODCLMdLjO0QLmPUZ/kv+FpxWHLiZeqnazEcPq+GW6kSZsaWQTmeVnYEG wnswbmmF97GVW62SEt7r+VDZmH3NyMeS4JBb3K4ATMUQ7EEmVW+inSa3PYMCUUCSzxdb vyghqBce9+PdHRzqTV5EJ/yevxXr1/2+np168IWOxs1PwUeEPYZWfMajCFOdgrS+ALuG XYw2kBYcQ2+WDRDvcdOEnrMCzhbuYmV8w3Qe/ggDh1Mf1ndSFWzemV5yphXJAEN+7f9F LBtO4gwOWzzlJ29V12KFJ3SfTjYE3O0taVPyotipwjl47jWDUL2s9gY0cin9Sbvrhfrx ygiA== X-Gm-Message-State: AIkVDXJl6D6rkZ8jExeMcjybCnyU8Gj1SUalFuvON+9Plb5nmkvXs5R43ZbiW3cY91d78q0ybh0= X-Received: by 10.157.2.39 with SMTP id 36mr13533091otb.150.1484418262677; Sat, 14 Jan 2017 10:24:22 -0800 (PST) Received: from localhost.localdomain (cpe-70-121-83-241.austin.res.rr.com. [70.121.83.241]) by smtp.gmail.com with ESMTPSA id y11sm7991973oia.2.2017.01.14.10.24.21 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 14 Jan 2017 10:24:21 -0800 (PST) From: Bill Fischofer To: lng-odp@lists.linaro.org Date: Sat, 14 Jan 2017 12:24:18 -0600 Message-Id: <20170114182418.21770-1-bill.fischofer@linaro.org> X-Mailer: git-send-email 2.9.3 Subject: [lng-odp] [API-NEXT PATCHv3] linux-generic: packet: tune alloc/free performance for non-reference paths X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" As reported in Bug https://bugs.linaro.org/show_bug.cgi?id=2816 packet alloc/free performance for non-references appears significantly degraded after the introduction of packet reference support. Add fastpaths for non-reference packet frees to optimize this. Signed-off-by: Bill Fischofer --- Changes for v3: - Fix memory leak detected by odp_bench_packet. Changes for v2: - Reworked odp_packet_free_multi() to streamline processing for reference and non-reference paths. platform/linux-generic/include/odp_pool_internal.h | 22 ++++- platform/linux-generic/odp_packet.c | 97 ++++++++++++++++++++-- platform/linux-generic/odp_pool.c | 62 ++++---------- 3 files changed, 126 insertions(+), 55 deletions(-) -- 2.9.3 diff --git a/platform/linux-generic/include/odp_pool_internal.h b/platform/linux-generic/include/odp_pool_internal.h index b0805ac..8fdc688 100644 --- a/platform/linux-generic/include/odp_pool_internal.h +++ b/platform/linux-generic/include/odp_pool_internal.h @@ -121,9 +121,29 @@ static inline odp_buffer_hdr_t *buf_hdl_to_hdr(odp_buffer_t buf) return pool_buf_hdl_to_hdr(pool, buf); } +static inline odp_pool_t pool_index_to_handle(uint32_t pool_idx) +{ + return _odp_cast_scalar(odp_pool_t, pool_idx); +} + +static inline uint32_t pool_id_from_buf(odp_buffer_t buf) +{ + odp_buffer_bits_t handle; + + handle.handle = buf; + return handle.pool_id; +} + int buffer_alloc_multi(pool_t *pool, odp_buffer_t buf[], odp_buffer_hdr_t *buf_hdr[], int num); -void buffer_free_multi(const odp_buffer_t buf[], int num_free); +void buffer_free_to_pool(uint32_t pool_id, const odp_buffer_t buf[], int num); + +static inline void buffer_free_multi(const odp_buffer_t buf[], int num_total) +{ + uint32_t pool_id = pool_id_from_buf(buf[0]); + + buffer_free_to_pool(pool_id, buf, num_total); +} #ifdef __cplusplus } diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c index 170965a..a0e65cc 100644 --- a/platform/linux-generic/odp_packet.c +++ b/platform/linux-generic/odp_packet.c @@ -30,7 +30,7 @@ static inline odp_packet_t packet_handle(odp_packet_hdr_t *pkt_hdr) static inline odp_buffer_t buffer_handle(odp_packet_hdr_t *pkt_hdr) { - return pkt_hdr->buf_hdr.handle.handle; + return odp_hdr_to_buf(&pkt_hdr->buf_hdr); } static inline uint32_t packet_ref_inc(odp_packet_hdr_t *pkt_hdr) @@ -599,18 +599,69 @@ int odp_packet_alloc_multi(odp_pool_t pool_hdl, uint32_t len, return num; } -static inline void packet_free(odp_packet_hdr_t *pkt_hdr) +static inline odp_packet_hdr_t *packet_free_to_list(odp_packet_hdr_t *pkt_hdr, + odp_buffer_t buf[], + int nbufs, + int *nfree) { odp_packet_hdr_t *ref_hdr; uint32_t ref_count; + int num_seg, i; do { + ref_count = packet_ref_count(pkt_hdr) - 1; + num_seg = pkt_hdr->buf_hdr.segcount; ref_hdr = pkt_hdr->ref_hdr; + + if (odp_likely((CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1) && + ref_count == 0)) { + if (*nfree >= nbufs) + break; + + buf[(*nfree)++] = buffer_handle(pkt_hdr); + } else { + if (*nfree + num_seg >= nbufs) + break; + + for (i = 0; i < num_seg; i++) { + odp_packet_hdr_t *hdr = + pkt_hdr->buf_hdr.seg[i].hdr; + + if (packet_ref_dec(hdr) == 1) + buf[(*nfree)++] = buffer_handle(hdr); + } + + if (ref_count == 1) + pkt_hdr->unshared_len = pkt_hdr->frame_len; + } + + pkt_hdr = ref_hdr; + } while (pkt_hdr); + + return pkt_hdr; +} + +static inline void packet_free(odp_packet_hdr_t *pkt_hdr) +{ + odp_packet_hdr_t *ref_hdr; + uint32_t ref_count; + int num_seg; + + do { ref_count = packet_ref_count(pkt_hdr) - 1; - free_bufs(pkt_hdr, 0, pkt_hdr->buf_hdr.segcount); + num_seg = pkt_hdr->buf_hdr.segcount; + ref_hdr = pkt_hdr->ref_hdr; - if (ref_count == 1) - pkt_hdr->unshared_len = pkt_hdr->frame_len; + if (odp_likely((CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1) && + ref_count == 0)) { + buffer_free_multi((odp_buffer_t *) + &pkt_hdr->buf_hdr.handle.handle, 1); + } else { + free_bufs(pkt_hdr, 0, num_seg); + + if (ref_count == 1) + pkt_hdr->unshared_len = pkt_hdr->frame_len; + } pkt_hdr = ref_hdr; } while (pkt_hdr); @@ -623,10 +674,40 @@ void odp_packet_free(odp_packet_t pkt) void odp_packet_free_multi(const odp_packet_t pkt[], int num) { - int i; + int nbufs = num * CONFIG_PACKET_MAX_SEGS * 2; + odp_buffer_t buf[nbufs]; + int nfree = 0; + int i = 0; + odp_packet_hdr_t *pkt_hdr = odp_packet_hdr(pkt[0]); + uint32_t pool_id = pool_id_from_buf(pkt_hdr->buf_hdr.handle.handle); + uint32_t next_pool; + + while (1) { + pkt_hdr = packet_free_to_list(pkt_hdr, buf, nbufs, &nfree); - for (i = 0; i < num; i++) - packet_free(odp_packet_hdr(pkt[i])); + if (pkt_hdr) { + buffer_free_to_pool(pool_id, buf, nfree); + nfree = 0; + continue; + } + + if (++i == num) + break; + + pkt_hdr = odp_packet_hdr(pkt[i]); + next_pool = pool_id_from_buf(pkt_hdr->buf_hdr.handle.handle); + + if (odp_unlikely(next_pool != pool_id)) { + if (nfree > 0) { + buffer_free_to_pool(pool_id, buf, nfree); + nfree = 0; + } + pool_id = next_pool; + } + } + + if (nfree > 0) + buffer_free_to_pool(pool_id, buf, nfree); } int odp_packet_reset(odp_packet_t pkt, uint32_t len) diff --git a/platform/linux-generic/odp_pool.c b/platform/linux-generic/odp_pool.c index 090a55f..2648068 100644 --- a/platform/linux-generic/odp_pool.c +++ b/platform/linux-generic/odp_pool.c @@ -47,19 +47,6 @@ typedef struct pool_local_t { pool_table_t *pool_tbl; static __thread pool_local_t local; -static inline odp_pool_t pool_index_to_handle(uint32_t pool_idx) -{ - return _odp_cast_scalar(odp_pool_t, pool_idx); -} - -static inline uint32_t pool_id_from_buf(odp_buffer_t buf) -{ - odp_buffer_bits_t handle; - - handle.handle = buf; - return handle.pool_id; -} - int odp_pool_init_global(void) { uint32_t i; @@ -671,8 +658,8 @@ int buffer_alloc_multi(pool_t *pool, odp_buffer_t buf[], return num_ch + num_deq; } -static inline void buffer_free_to_pool(uint32_t pool_id, - const odp_buffer_t buf[], int num) +void buffer_free_to_pool(uint32_t pool_id, + const odp_buffer_t buf[], int num) { pool_t *pool; int i; @@ -732,36 +719,6 @@ static inline void buffer_free_to_pool(uint32_t pool_id, cache->num = cache_num + num; } -void buffer_free_multi(const odp_buffer_t buf[], int num_total) -{ - uint32_t pool_id; - int num; - int i; - int first = 0; - - while (1) { - num = 1; - i = 1; - pool_id = pool_id_from_buf(buf[first]); - - /* 'num' buffers are from the same pool */ - if (num_total > 1) { - for (i = first; i < num_total; i++) - if (pool_id != pool_id_from_buf(buf[i])) - break; - - num = i - first; - } - - buffer_free_to_pool(pool_id, &buf[first], num); - - if (i == num_total) - return; - - first = i; - } -} - odp_buffer_t odp_buffer_alloc(odp_pool_t pool_hdl) { odp_buffer_t buf; @@ -793,7 +750,20 @@ void odp_buffer_free(odp_buffer_t buf) void odp_buffer_free_multi(const odp_buffer_t buf[], int num) { - buffer_free_multi(buf, num); + uint32_t next_pool, pool_id = pool_id_from_buf(buf[0]); + int i, first = 0; + + for (i = 1; i < num; i++) { + next_pool = pool_id_from_buf(buf[i]); + + if (odp_unlikely(next_pool != pool_id)) { + buffer_free_to_pool(pool_id, &buf[first], i - first); + first = i; + pool_id = next_pool; + } + } + + buffer_free_to_pool(pool_id, &buf[first], num - first); } int odp_pool_capability(odp_pool_capability_t *capa)