From patchwork Thu Mar 14 00:57:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13592019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E163AC54E66 for ; Thu, 14 Mar 2024 00:57:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BDCE8D006D; Wed, 13 Mar 2024 20:57:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46CD76B007B; Wed, 13 Mar 2024 20:57:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 30DED8D006D; Wed, 13 Mar 2024 20:57:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1EBB76B0071 for ; Wed, 13 Mar 2024 20:57:18 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DE48316087F for ; Thu, 14 Mar 2024 00:57:17 +0000 (UTC) X-FDA: 81893830914.27.1462B48 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf10.hostedemail.com (Postfix) with ESMTP id 60DEBC000C for ; Thu, 14 Mar 2024 00:57:15 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=rxtsGAnj; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf10.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710377835; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=LBPQUH1A+ayT+t1NyYBhKLCUNFBTXFzwBcbdjjDLePI=; b=iEdXOmsGoQPthmfY11TpXMEey89wNAqoI6ami5PKIG54X67P+E05gQk2NMY/rr7Auyr7P0 g9XiGDnzWbYWjEdFgToOzEwXnjgWwaVDJbTpsF7f2HtAd2aXxb/ST8veN8B5liSVo7IDfw vf7pOLfjtIHsskzbReG5+du8H4PvPSM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=rxtsGAnj; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf10.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710377835; a=rsa-sha256; cv=none; b=aifxddDCKxpQDuQ2sXIVfkB+yTak0YVTMbLUjVivKuCXcfvqn13Jmq1Z1/Lq5oX6wE4lCZ zFrQps1ASTLpT9wbc0uerPRGDPJY327stWCyschL+3zdvrjR6ZPvpd5f9a71fig+ZAGWYF 9yqBXt3NkVzskV3RWwbKjL5zlho51dY= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:In-Reply-To:References; bh=LBPQUH1A+ayT+t1NyYBhKLCUNFBTXFzwBcbdjjDLePI=; b=rxtsGAnjmbV2rJ2NHM+9o+cqYj uh4EKUrvITTt43dBigW6dAkJRGBJB3lqkNTH0UhEUXMwW1AHJLLNW+lDP8GYLDIioc26TkIoqHXon uFb8fTIKqcFP37sxqANdPu5kNVzVUOZB7paMdYFliPXVoVLGYQ8qFK8cBkKjLHpHfm5z7QWCEsrWp I3M+bX8R+04bVcdVO7Yfa9jSQHU1N3HNdIPyz9PgzeDRT8gt28JIUcAtLOYbRwRmriKjniLoJfrie 0bQTGhgaEkDXaHJ90ki30/HSzVkBU8kO8+PZLc2bI8CeqgDDzIPWMUiBh+D9MFmvTN0wwOWouh3NB iJdUOqLQ==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1rkZPE-0000000CRHg-1cy4; Thu, 14 Mar 2024 00:57:12 +0000 From: Luis Chamberlain To: akpm@linux-foundation.org, jhubbard@nvidia.com, vbabka@suse.cz, mgorman@suse.de, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, dave@stgolabs.net, p.raghav@samsung.com, da.gomez@samsung.com, mcgrof@kernel.org Subject: [RFC] mm/vmstat: add a single value debugfs fragmentation metric Date: Wed, 13 Mar 2024 17:57:10 -0700 Message-ID: <20240314005710.2964798-1-mcgrof@kernel.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 60DEBC000C X-Stat-Signature: w9efj9u4ki9he3tu7knuk5zn6haobsre X-Rspam-User: X-HE-Tag: 1710377835-869130 X-HE-Meta: U2FsdGVkX1+XgRAIuFijE82ydzTlQ0Ue0rcr+wGHJqhiy70LD2FRG6w8rwgCpCdgJs4NmGqsQwK8qn5bY5GI5kPKZhVassH83SR4tbH639XJLcCh5iFRiLH+8WQrrOVkJUFi+bclkoHa3EvAT8JWauzpTyFywauYYVXfQmYISQhLg6Iq1iVnTH2bpFJzluqZnbxOOIrGh+LXJPw9hnkvdAuB04yEr9vUE1fzrRxAnA8gzpVcfbkAh5bdaL3RlACru2WP2BR348MvdBYCIIy8hhgEliWBcEB/lA5rrjeHXDnS+q1Vs/XFVRSJFFl3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sJ9+/2qGQJBCrcB3EPyNhGjmfsOGidCh6XeH4Ksyk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In considering the general impact to memory fragmentation over new features, or enhancehments I had asked what metric we could use which is a single digit value. John Hubbard provided one [0] however we'd need to tally up used folios per order as well, and tallying this up would be expensive today. The value would also only tell us how memory fragmented a system is. We can instead just use the existing fragmentation index but generalize a single value from it. This tells us more, when generalized to one value it can tell us both how likely memory allocations might fail due to external fragmention and how likely we are to fail allocations due to low memory. Today we expose an external fragmentation index per node and per zone, per each supported order. This value is useful to tell us how externally fragmented a system might be with the full scope of the CPU topology. Obviously, the CPU topology can vary per system and per architecture, for instance two separate x86 systems may have: cat /sys/kernel/debug/extfrag/extfrag_index Node 0, zone DMA -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 Node 0, zone DMA32 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 Node 0, zone Normal -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 Node 1, zone Normal -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 cat /sys/kernel/debug/extfrag/extfrag_index Node 0, zone DMA -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 Node 0, zone DMA32 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 Node 0, zone Normal -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 -1.000 The number of zones we have may also change over time. This puts a bit of onus on userspace if all it wants is a general sense of how externally fragmented a system is, overall. Provide a simple one unit average for the fragmentation index to allow to simplify measurements in userspace. To make it easier for humans to grok, adjust it to be a value between -100 (allocations failing due to lack of memory) to 100 (super fragmented). [0] https://lore.kernel.org/all/5ac6a387-0ca7-45ca-bebc-c3bdd48452cb@nvidia.com/T/#u Signed-off-by: Luis Chamberlain --- mm/vmstat.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/mm/vmstat.c b/mm/vmstat.c index 582f89b37ccf..e80983772c83 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -2262,6 +2262,54 @@ static const struct seq_operations extfrag_sops = { DEFINE_SEQ_ATTRIBUTE(extfrag); +static ssize_t read_extfrag_pct(struct file *file, char __user *user_buf, + size_t count, loff_t *ppos) +{ + char buf[32]; + unsigned int len; + pg_data_t *pgdat; + struct zone *zone; + unsigned long flags; + unsigned int order; + unsigned int num_pgdats = 0; + int index_total = 0; + + for_each_online_pgdat(pgdat) { + int index_pgt = 0; + int num_zones = 0; + num_pgdats++; + for_each_populated_zone_pgdat(zone, pgdat) { + num_zones++; + int index = 0; + + spin_lock_irqsave(&zone->lock, flags); + for (order = 0; order < NR_PAGE_ORDERS; ++order) { + index += fragmentation_index(zone, order); + } + spin_unlock_irqrestore(&zone->lock, flags); + + index_pgt += index / NR_PAGE_ORDERS; + } + + BUG_ON(!num_zones); + + index_total += index_pgt / num_zones; + } + + index_total = index_total / num_pgdats; + + len = sprintf(buf, "%d.%02d\n", index_total / 10, index_total % 10); + + return simple_read_from_buffer(user_buf, count, ppos, buf, len); +} + +static const struct file_operations extfrag_pct_fops = { + .read = read_extfrag_pct, + .open = simple_open, + .owner = THIS_MODULE, + .llseek = default_llseek, +}; + static int __init extfrag_debug_init(void) { struct dentry *extfrag_debug_root; @@ -2274,6 +2322,9 @@ static int __init extfrag_debug_init(void) debugfs_create_file("extfrag_index", 0444, extfrag_debug_root, NULL, &extfrag_fops); + debugfs_create_file("extfrag_pct", 0444, extfrag_debug_root, NULL, + &extfrag_pct_fops); + return 0; }