Skip to contentSkip to conversation

mm: add compaction success/failure tracepoints and fragmentation tracking

PATCH New Luis Chamberlain 1 participant 1 message Sep 4, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: add compaction success/failure tracepoints and fragmentation tracking

From: Swarna Prabhu <[email protected]>

Add extra compaction tracepoints to allow extended memory fragmentation introspection:

- mm_compaction_success and
- mm_compaction_failure

This allows for better introspection of what exactly is happening at either failure or success of memory compaction, as we try to evaluate memory fragmentation in extreme situations.

Changes include:

- Add two extra new tracepoints:
* mm_compaction_success
* mm_compaction_failure
- Add COMPACTSUCCESS_EXTFRAG vmstat counter to track successful compactions that still result in fragmented memory (fragmentation index > 0 and <= 1000)
- Export fill_contig_page_info() and __fragmentation_index() from vmstat.c to allow fragmentation calculation in page allocation path capture detailed compaction outcomes with zone, order, and fragmentation data
- Calculate fragmentation index after successful compaction to determine if memory remains fragmented despite successful allocation

The fragmentation index calculation helps identify cases where compaction succeeds but still leaves the zone fragmented. A positive index (0-1000) indicates some level of fragmentation exists, with higher values meaning more fragmentation. This data is crucial for understanding:

- How bad are situations, really?
- At what order did a compaction fail?
- Compaction efficiency in reducing fragmentation
- Whether successful allocations are masking underlying fragmentation issues
- The relationship between allocation success and zone health

The new tracepoints provide runtime visibility into:

- Which zones and orders are experiencing compaction events
- The resulting fragmentation state after successful compaction
- Failure patterns for specific allocation orders

This instrumentation will help us build visualizations of compaction over time.

---

Just posting this for posterity so its public, and also here's a branch which carries both this patch and my folio migration debugfs stats patch, they are based on v6.17-rc4:

https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git/log/?h=20250903-compaction-tracepoints

This should help us test memory managmement intensive workflows. I've also put this tree up to help visualize things:

https://github.com/mcgrof/plot-fragmentation

I'll move on to integrate to kdevops now.

Luis

 include/linux/vm_event_item.h     |  2 +-
 include/linux/vmstat.h            | 11 +++++
 include/trace/events/page_alloc.h | 71 +++++++++++++++++++++++++++++++
 mm/page_alloc.c                   | 20 ++++++++-
 mm/vmstat.c                       | 11 ++---
 5 files changed, 105 insertions(+), 10 deletions(-)
 create mode 100644 include/trace/events/page_alloc.h
include/linux/vm_event_item.h +1 -1
index 9e15a088ba38..a26e790cd485 100644
@@ -76,7 +76,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
 #ifdef CONFIG_COMPACTION
 		COMPACTMIGRATE_SCANNED, COMPACTFREE_SCANNED,
 		COMPACTISOLATED,
-		COMPACTSTALL, COMPACTFAIL, COMPACTSUCCESS,
+		COMPACTSTALL, COMPACTFAIL, COMPACTSUCCESS, COMPACTSUCCESS_EXTFRAG,
 		KCOMPACTD_WAKE,
 		KCOMPACTD_MIGRATE_SCANNED, KCOMPACTD_FREE_SCANNED,
 #endif
include/linux/vmstat.h +11 -0
index c287998908bf..e6560ed87953 100644
@@ -269,6 +269,17 @@ static inline void fold_vm_numa_events(void)
 }
 #endif /* CONFIG_NUMA */
 
+#ifdef CONFIG_COMPACTION
+struct contig_page_info {
+	unsigned long free_pages;
+	unsigned long free_blocks_total;
+	unsigned long free_blocks_suitable;
+};
+void fill_contig_page_info(struct zone *zone, unsigned int suitable_order,struct contig_page_info *info);
+int __fragmentation_index(unsigned int order, struct contig_page_info *info);
+
+#endif /*CONFIG_COMPACTION*/
+
 #ifdef CONFIG_SMP
 void __mod_zone_page_state(struct zone *, enum zone_stat_item item, long);
 void __inc_zone_page_state(struct page *, enum zone_stat_item);
include/trace/events/page_alloc.h new file +71 -0

71 lines not on this page · raw message

mm/page_alloc.c +19 -1
index d1d037f97c5f..17ea91febacf 100644
@@ -56,10 +56,17 @@
 #include <linux/cacheinfo.h>
 #include <linux/pgalloc_tag.h>
 #include <asm/div64.h>
+#include <linux/vmstat.h>
+#include <linux/debugfs.h>
+#include <linux/cpumask.h>
 #include "internal.h"
 #include "shuffle.h"
 #include "page_reporting.h"
 
+#if defined CONFIG_COMPACTION
+#define CREATE_TRACE_POINTS
+#include <trace/events/page_alloc.h>
+#endif
 /* Free Page Internal flags: for internal, non-pcp variants of free_pages(). */
 typedef int __bitwise fpi_t;
 
@@ -4088,10 +4095,20 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
 
 	if (page) {
 		struct zone *zone = page_zone(page);
-
 		zone->compact_blockskip_flush = false;
 		compaction_defer_reset(zone, order, true);
+
+		struct contig_page_info info;
+		int res_index;
+
+		fill_contig_page_info(zone, order, &info);
+		res_index = __fragmentation_index(order, &info);
+
 		count_vm_event(COMPACTSUCCESS);
+		trace_mm_compaction_success(zone, order, res_index); /* success trace point captured */
+		if (res_index > 0 && res_index <= 1000) {
+			count_vm_event(COMPACTSUCCESS_EXTFRAG);
+		}
 		return page;
 	}
 
@@ -4100,6 +4117,7 @@ __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
 	 * is that pages exist, but not enough to satisfy watermarks.
 	 */
 	count_vm_event(COMPACTFAIL);
+        trace_mm_compaction_failure(order); /*failure trace point captured */
 
 	cond_resched();
 
mm/vmstat.c +3 -8
index 71cd1ceba191..ce82ddb5afcc 100644
@@ -1056,12 +1056,6 @@ void memmap_pages_add(long delta)
 
 #ifdef CONFIG_COMPACTION
 
-struct contig_page_info {
-	unsigned long free_pages;
-	unsigned long free_blocks_total;
-	unsigned long free_blocks_suitable;
-};
-
 /*
  * Calculate the number of free pages in a zone, how many contiguous
  * pages are free and how many are large enough to satisfy an allocation of
@@ -1070,7 +1064,7 @@ struct contig_page_info {
  * migrated. Calculating that is possible, but expensive and can be
  * figured out from userspace
  */
-static void fill_contig_page_info(struct zone *zone,
+void fill_contig_page_info(struct zone *zone,
 				unsigned int suitable_order,
 				struct contig_page_info *info)
 {
@@ -1109,7 +1103,7 @@ static void fill_contig_page_info(struct zone *zone,
  * The value can be used to determine if page reclaim or compaction
  * should be used
  */
-static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
+int __fragmentation_index(unsigned int order, struct contig_page_info *info)
 {
 	unsigned long requested = 1UL << order;
 
@@ -1380,6 +1374,7 @@ const char * const vmstat_text[] = {
 	[I(COMPACTSTALL)]			= "compact_stall",
 	[I(COMPACTFAIL)]			= "compact_fail",
 	[I(COMPACTSUCCESS)]			= "compact_success",
+	[I(COMPACTSUCCESS_EXTFRAG)]		= "compact_success_extfrag",
 	[I(KCOMPACTD_WAKE)]			= "compact_daemon_wake",
 	[I(KCOMPACTD_MIGRATE_SCANNED)]		= "compact_daemon_migrate_scanned",
 	[I(KCOMPACTD_FREE_SCANNED)]		= "compact_daemon_free_scanned",
Signature · 1 line
2.45.2