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 A8B71440F0; Tue, 28 May 2024 08:14:01 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2DAE7402D2; Tue, 28 May 2024 08:14:01 +0200 (CEST) Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) by mails.dpdk.org (Postfix) with ESMTP id E1AF640150 for ; Tue, 28 May 2024 08:13:58 +0200 (CEST) Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2bf196037a7so101341a91.1 for ; Mon, 27 May 2024 23:13:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1716876838; x=1717481638; darn=dpdk.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=8D6BpvMSa3mQDLNfPg50Cg0sk5O+7UvCLab1ByHcFYM=; b=ZmscFlNwSiNS53qAWTSjA4Gi0Prx1o4QoanpAXo+jix3z4jiF/Gbo9fjiA4C+/+YyD XfUEZglkuxu8A3SVbYCcp9pI4fgevDB0PuhnoRf5oeMlVohussljQyRsQBdZSuJBgd8c 5Ja7h/m3wKNl8MFQqmdLnEzu/PvD3382+eyYG1rsYpTH5fqM2sDBzex98FIG9kXdLP/R d+ps9MTyutGCrGi7IzqcrA5XCwTOuHrI9bT0QoV8Q5tajXx+cO5FP+p0JWm4jVAHD5cY iGoQkSEqCmTRs8ARD8vkIKVZHKz5PviYmniUs7VAY7EmWtrS+pAk8FHBWqnQS8B4vYUu am2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716876838; x=1717481638; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=8D6BpvMSa3mQDLNfPg50Cg0sk5O+7UvCLab1ByHcFYM=; b=ct5hICO8MLwzR18Y00ykPsVPwCb2EKLKsWXCbts2m2YhyaGvP3gj7UwBPCRlhGIIxD cxKbQhgPVJmZ86Y6LQ80ZC4iQZx0ykScb8lHxwy8gKxVzx7kYqRUWg8OXCpyyrDH08Ak GgJILvWPHY7zLhnge10uEG5i9LmFaDxzvP/fapJ6Jt6icBRwL0kGFtuhSQD1NTXmeD/P e/EfCjAEUCGrJtyhURoii3U0N5LY4/30a/7Stv7EXCnEYDo4SLmwLkkRYBscXlEerrZD fkYCjfLJJFykbhB+5KlyVVLI9TNZINfHbX3WCkA3WMsWT97DkhNO4lXazjjKlrIadSeW 0lOg== X-Forwarded-Encrypted: i=1; AJvYcCU5rHqXISe163F9BLApNkVeuf166fCBsQL1HzT0pkIF8y8KQHLnr9xOM9pzEg9xCfo/zp0I384WO68Ek1o= X-Gm-Message-State: AOJu0YzLv4MdP9CzW4EvFiZ4cxDbniVRNf20aOh+APxdQO2ROCkOQCs3 3Y2dt5xmBOM/kxpnkUmeeuMWAm/B5E7ayufCwx4Mp3NI1xWwqmb/mNJjCUOdRcY= X-Google-Smtp-Source: AGHT+IHUb1XLHzBobiJl6mIsQ8brFxbACCQE8MjhJLTXevEqVDDTRJaHG652cFwi0qkJBytWjFbiow== X-Received: by 2002:a17:902:da90:b0:1f2:efdf:a400 with SMTP id d9443c01a7336-1f4498e98e6mr126591255ad.5.1716876837824; Mon, 27 May 2024 23:13:57 -0700 (PDT) Received: from HTW5T2C6VL.bytedance.net ([139.177.225.253]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f4b8458009sm18292405ad.88.2024.05.27.23.13.55 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 27 May 2024 23:13:56 -0700 (PDT) From: Fengnan Chang To: anatoly.burakov@intel.com, dev@dpdk.org, xuemingl@mellanox.com Cc: Fengnan Chang Subject: [PATCH] eal: speed up dpdk init time Date: Tue, 28 May 2024 14:12:59 +0800 Message-Id: <20240528061259.29528-1-changfengnan@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 If we have a lot of huge pages in system, the memory init will cost long time in legacy-mem mode. For example, we have 120G memory in unit of 2MB hugepage, the env init will cost 43s. Almost half of time spent on find_numasocket, since the address in /proc/self/numa_maps is orderd, we can sort hugepg_tbl by orig_va first and then just read numa_maps line by line is enough to find socket. In my test, spent time reduced to 19s. Signed-off-by: Fengnan Chang --- lib/eal/linux/eal_memory.c | 115 +++++++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 43 deletions(-) diff --git a/lib/eal/linux/eal_memory.c b/lib/eal/linux/eal_memory.c index 45879ca743..28cc136ac0 100644 --- a/lib/eal/linux/eal_memory.c +++ b/lib/eal/linux/eal_memory.c @@ -414,7 +414,7 @@ map_all_hugepages(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi, static int find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi) { - int socket_id; + int socket_id = -1; char *end, *nodestr; unsigned i, hp_count = 0; uint64_t virt_addr; @@ -432,54 +432,61 @@ find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi) snprintf(hugedir_str, sizeof(hugedir_str), "%s/%s", hpi->hugedir, eal_get_hugefile_prefix()); - /* parse numa map */ - while (fgets(buf, sizeof(buf), f) != NULL) { - - /* ignore non huge page */ - if (strstr(buf, " huge ") == NULL && + /* if we find this page in our mappings, set socket_id */ + for (i = 0; i < hpi->num_pages[0]; i++) { + void *va = NULL; + /* parse numa map */ + while (fgets(buf, sizeof(buf), f) != NULL) { + if (strstr(buf, " huge ") == NULL && strstr(buf, hugedir_str) == NULL) - continue; - - /* get zone addr */ - virt_addr = strtoull(buf, &end, 16); - if (virt_addr == 0 || end == buf) { - EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__); - goto error; - } + continue; + /* get zone addr */ + virt_addr = strtoull(buf, &end, 16); + if (virt_addr == 0 || end == buf) { + EAL_LOG(ERR, "error in numa_maps parsing"); + goto error; + } - /* get node id (socket id) */ - nodestr = strstr(buf, " N"); - if (nodestr == NULL) { - EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__); - goto error; - } - nodestr += 2; - end = strstr(nodestr, "="); - if (end == NULL) { - EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__); - goto error; - } - end[0] = '\0'; - end = NULL; + /* get node id (socket id) */ + nodestr = strstr(buf, " N"); + if (nodestr == NULL) { + EAL_LOG(ERR, "error in numa_maps parsing"); + goto error; + } + nodestr += 2; + end = strstr(nodestr, "="); + if (end == NULL) { + EAL_LOG(ERR, "error in numa_maps parsing"); + goto error; + } + end[0] = '\0'; + end = NULL; - socket_id = strtoul(nodestr, &end, 0); - if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) { - EAL_LOG(ERR, "%s(): error in numa_maps parsing", __func__); - goto error; + socket_id = strtoul(nodestr, &end, 0); + if ((nodestr[0] == '\0') || (end == NULL) || (*end != '\0')) { + EAL_LOG(ERR, "error in numa_maps parsing"); + goto error; + } + va = (void *)(unsigned long)virt_addr; + if (hugepg_tbl[i].orig_va != va) { + EAL_LOG(DEBUG, "search %p not seq, let's start from begin", + hugepg_tbl[i].orig_va); + fseek(f, 0, SEEK_SET); + } else { + break; + } } - - /* if we find this page in our mappings, set socket_id */ - for (i = 0; i < hpi->num_pages[0]; i++) { - void *va = (void *)(unsigned long)virt_addr; - if (hugepg_tbl[i].orig_va == va) { - hugepg_tbl[i].socket_id = socket_id; - hp_count++; + if (hugepg_tbl[i].orig_va == va) { + hugepg_tbl[i].socket_id = socket_id; + hp_count++; #ifdef RTE_EAL_NUMA_AWARE_HUGEPAGES - EAL_LOG(DEBUG, - "Hugepage %s is on socket %d", - hugepg_tbl[i].filepath, socket_id); + EAL_LOG(DEBUG, + "Hugepage %s is on socket %d", + hugepg_tbl[i].filepath, socket_id); #endif - } + } else { + EAL_LOG(ERR, + "shoudn't happen %p", hugepg_tbl[i].orig_va); } } @@ -494,6 +501,25 @@ find_numasocket(struct hugepage_file *hugepg_tbl, struct hugepage_info *hpi) return -1; } +static int +cmp_orig_va(const void *a, const void *b) +{ +#ifndef RTE_ARCH_PPC_64 + const struct hugepage_file *p1 = a; + const struct hugepage_file *p2 = b; +#else + /* PowerPC needs memory sorted in reverse order from x86 */ + const struct hugepage_file *p1 = b; + const struct hugepage_file *p2 = a; +#endif + if (p1->orig_va < p2->orig_va) + return -1; + else if (p1->orig_va > p2->orig_va) + return 1; + else + return 0; +} + static int cmp_physaddr(const void *a, const void *b) { @@ -1324,6 +1350,9 @@ eal_legacy_hugepage_init(void) } } + qsort(&tmp_hp[hp_offset], hpi->num_pages[0], + sizeof(struct hugepage_file), cmp_orig_va); + if (find_numasocket(&tmp_hp[hp_offset], hpi) < 0){ EAL_LOG(DEBUG, "Failed to find NUMA socket for %u MB pages", (unsigned)(hpi->hugepage_sz / 0x100000)); -- 2.37.1 (Apple Git-137.1)