From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 0034542649; Tue, 26 Sep 2023 22:59:45 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C5CC340269; Tue, 26 Sep 2023 22:59:45 +0200 (CEST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mails.dpdk.org (Postfix) with ESMTP id 3454F400D5 for ; Tue, 26 Sep 2023 22:59:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695761983; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=3oBfIH7lf2gkJKp9uVcpCIFOBO7krwYQ1UtRLslWVwQ=; b=FxTgJjqpZVJLiMOhsgBtE3fJOy9ZTcfItuw1kW7D6T9PbI4IFVhaMeJGLfbb/gPbyM2ZX5 UCPDd9HFB81gATmza+PxX2udQpFLO5lRjo9Z7MBBvMyWGc6yWAdWo/b1AFW/VbB8P+PwGF t9puvPa3wIjj9UpajbEOiMPxFE5h7qI= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-423-gGZyMP2MORis4P261GeMUg-1; Tue, 26 Sep 2023 16:59:39 -0400 X-MC-Unique: gGZyMP2MORis4P261GeMUg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6B2843C11C66; Tue, 26 Sep 2023 20:59:39 +0000 (UTC) Received: from RHTPC1VM0NT (unknown [10.22.8.239]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B847040C6EA8; Tue, 26 Sep 2023 20:59:38 +0000 (UTC) From: Aaron Conole To: dev@dpdk.org Cc: John Romein , Dmitry Kozlyuk , Matan Azrad , Viacheslav Ovsiienko , Ori Kam , Suanming Mou Subject: [PATCH] common/mlx5: Optimize mlx5 mempool get extmem Date: Tue, 26 Sep 2023 16:59:38 -0400 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: John Romein This patch reduces the time to allocate and register tens of gigabytes of GPU memory from hours to seconds, by sorting the heap only once instead of for each object in the mempool. Fixes: 690b2a8 ("common/mlx5: add mempool registration facilities") Signed-off-by: John Romein --- NOTE: this is a post of https://github.com/DPDK/dpdk/pull/70 to the mailing list. drivers/common/mlx5/mlx5_common_mr.c | 69 ++++++++-------------------- 1 file changed, 20 insertions(+), 49 deletions(-) diff --git a/drivers/common/mlx5/mlx5_common_mr.c b/drivers/common/mlx5/mlx5_common_mr.c index 40ff9153bd8..3f95438045f 100644 --- a/drivers/common/mlx5/mlx5_common_mr.c +++ b/drivers/common/mlx5/mlx5_common_mr.c @@ -1389,63 +1389,23 @@ mlx5_mempool_get_chunks(struct rte_mempool *mp, struct mlx5_range **out, return 0; } -struct mlx5_mempool_get_extmem_data { - struct mlx5_range *heap; - unsigned int heap_size; - int ret; -}; - static void mlx5_mempool_get_extmem_cb(struct rte_mempool *mp, void *opaque, void *obj, unsigned int obj_idx) { - struct mlx5_mempool_get_extmem_data *data = opaque; + struct mlx5_range *heap = opaque; struct rte_mbuf *mbuf = obj; uintptr_t addr = (uintptr_t)mbuf->buf_addr; - struct mlx5_range *seg, *heap; struct rte_memseg_list *msl; size_t page_size; uintptr_t page_start; - unsigned int pos = 0, len = data->heap_size, delta; RTE_SET_USED(mp); - RTE_SET_USED(obj_idx); - if (data->ret < 0) - return; - /* Binary search for an already visited page. */ - while (len > 1) { - delta = len / 2; - if (addr < data->heap[pos + delta].start) { - len = delta; - } else { - pos += delta; - len -= delta; - } - } - if (data->heap != NULL) { - seg = &data->heap[pos]; - if (seg->start <= addr && addr < seg->end) - return; - } - /* Determine the page boundaries and remember them. */ - heap = realloc(data->heap, sizeof(heap[0]) * (data->heap_size + 1)); - if (heap == NULL) { - free(data->heap); - data->heap = NULL; - data->ret = -1; - return; - } - data->heap = heap; - data->heap_size++; - seg = &heap[data->heap_size - 1]; msl = rte_mem_virt2memseg_list((void *)addr); page_size = msl != NULL ? msl->page_sz : rte_mem_page_size(); page_start = RTE_PTR_ALIGN_FLOOR(addr, page_size); - seg->start = page_start; - seg->end = page_start + page_size; - /* Maintain the heap order. */ - qsort(data->heap, data->heap_size, sizeof(heap[0]), - mlx5_range_compare_start); + heap[obj_idx].start = page_start; + heap[obj_idx].end = page_start + page_size; } /** @@ -1457,15 +1417,26 @@ static int mlx5_mempool_get_extmem(struct rte_mempool *mp, struct mlx5_range **out, unsigned int *out_n) { - struct mlx5_mempool_get_extmem_data data; + unsigned out_size = 1; + struct mlx5_range *heap; DRV_LOG(DEBUG, "Recovering external pinned pages of mempool %s", mp->name); - memset(&data, 0, sizeof(data)); - rte_mempool_obj_iter(mp, mlx5_mempool_get_extmem_cb, &data); - *out = data.heap; - *out_n = data.heap_size; - return data.ret; + heap = malloc(mp->size * sizeof(struct mlx5_range)); + if (heap == NULL) + return -1; + rte_mempool_obj_iter(mp, mlx5_mempool_get_extmem_cb, heap); + qsort(heap, mp->size, sizeof heap[0], mlx5_range_compare_start); + /* remove duplicates */ + for (unsigned i = 1; i < mp->size; i ++) + if (heap[out_size - 1].start != heap[i].start) + heap[out_size ++] = heap[i]; + heap = realloc(heap, out_size * sizeof(struct mlx5_range)); + if (heap == NULL) + return -1; + *out = heap; + *out_n = out_size; + return 0; } /** --- 2.40.1