Skip to contentSkip to conversation

sched/numa: Add statistics of numa balance task migration and swap

PATCH Superseded Chen Yu 4 participants 13 messages Apr 2, 2025 – Apr 3, 2025

Subsystems (5)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

PATCH 1/1 sched/numa: Add statistics of numa balance task migration and swap

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

Previous RFC version can be found here:
https://lore.kernel.org/lkml/1847c5ef828ad4835a35e3a54b88d2e13bce0eea.1740483690.git.yu.c.chen@intel.com/

---
RFC->v1: Rename the nr_numa_task_migrated to
numa_task_migrated, and nr_numa_task_swapped numa_task_swapped in /proc/{PID}/sched, so both cgroup's memory.stat and task's sched have the same field name.

---
 include/linux/sched.h         |  4 ++++
 include/linux/vm_event_item.h |  2 ++
 kernel/sched/core.c           | 10 ++++++++--
 kernel/sched/debug.c          |  4 ++++
 mm/memcontrol.c               |  2 ++
 mm/vmstat.c                   |  2 ++
 6 files changed, 22 insertions(+), 2 deletions(-)
include/linux/sched.h +4 -0
index 0785268c76f8..9623e5300453 100644
@@ -532,6 +532,10 @@ struct sched_statistics {
 	u64				nr_failed_migrations_running;
 	u64				nr_failed_migrations_hot;
 	u64				nr_forced_migrations;
+#ifdef CONFIG_NUMA_BALANCING
+	u64				numa_task_migrated;
+	u64				numa_task_swapped;
+#endif
 
 	u64				nr_wakeups;
 	u64				nr_wakeups_sync;
include/linux/vm_event_item.h +2 -0
index f70d0958095c..aef817474781 100644
@@ -64,6 +64,8 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,
 		NUMA_HINT_FAULTS,
 		NUMA_HINT_FAULTS_LOCAL,
 		NUMA_PAGE_MIGRATE,
+		NUMA_TASK_MIGRATE,
+		NUMA_TASK_SWAP,
 #endif
 #ifdef CONFIG_MIGRATION
 		PGMIGRATE_SUCCESS, PGMIGRATE_FAIL,
kernel/sched/core.c +8 -2
index c86c05264719..314d5cbce2b6 100644
@@ -3348,6 +3348,11 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
 #ifdef CONFIG_NUMA_BALANCING
 static void __migrate_swap_task(struct task_struct *p, int cpu)
 {
+	__schedstat_inc(p->stats.numa_task_swapped);
+
+	if (p->mm)
+		count_memcg_events_mm(p->mm, NUMA_TASK_SWAP, 1);
+
 	if (task_on_rq_queued(p)) {
 		struct rq *src_rq, *dst_rq;
 		struct rq_flags srf, drf;
@@ -7948,8 +7953,9 @@ int migrate_task_to(struct task_struct *p, int target_cpu)
 	if (!cpumask_test_cpu(target_cpu, p->cpus_ptr))
 		return -EINVAL;
 
-	/* TODO: This is not properly updating schedstats */
-
+	__schedstat_inc(p->stats.numa_task_migrated);
+	if (p->mm)
+		count_memcg_events_mm(p->mm, NUMA_TASK_MIGRATE, 1);
 	trace_sched_move_numa(p, curr_cpu, target_cpu);
 	return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
 }
kernel/sched/debug.c +4 -0
index 56ae54e0ce6a..f971c2af7912 100644
@@ -1206,6 +1206,10 @@ void proc_sched_show_task(struct task_struct *p, struct pid_namespace *ns,
 		P_SCHEDSTAT(nr_failed_migrations_running);
 		P_SCHEDSTAT(nr_failed_migrations_hot);
 		P_SCHEDSTAT(nr_forced_migrations);
+#ifdef CONFIG_NUMA_BALANCING
+		P_SCHEDSTAT(numa_task_migrated);
+		P_SCHEDSTAT(numa_task_swapped);
+#endif
 		P_SCHEDSTAT(nr_wakeups);
 		P_SCHEDSTAT(nr_wakeups_sync);
 		P_SCHEDSTAT(nr_wakeups_migrate);
mm/memcontrol.c +2 -0
index 4de6acb9b8ec..1656c90b2381 100644
@@ -460,6 +460,8 @@ static const unsigned int memcg_vm_event_stat[] = {
 	NUMA_PAGE_MIGRATE,
 	NUMA_PTE_UPDATES,
 	NUMA_HINT_FAULTS,
+	NUMA_TASK_MIGRATE,
+	NUMA_TASK_SWAP,
 #endif
 };
 
mm/vmstat.c +2 -0
index 16bfe1c694dd..7de1583a63c9 100644
@@ -1339,6 +1339,8 @@ const char * const vmstat_text[] = {
 	"numa_hint_faults",
 	"numa_hint_faults_local",
 	"numa_pages_migrated",
+	"numa_task_migrated",
+	"numa_task_swapped",
 #endif
 #ifdef CONFIG_MIGRATION
 	"pgmigrate_success",
Signature · 1 line
2.25.1
maintainer
#1 lore raw

Hello Chen.

Quoted 2 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful.

...

Quoted 7 lines

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

Why is the field /proc/$pid/sched not enough?

Also, you may want to update Documentation/admin-guide/cgroup-v2.rst too.

Thanks,
Michal

#2 lore raw

Hello Michal,

Quoted 8 lines · Michal Koutný

Hello Chen.

Quoted 2 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful.

...

Quoted 2 lines

The following two new fields:

Quoted 7 lines
Quoted 5 lines

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

Why is the field /proc/$pid/sched not enough?

The /proc/$pid/sched accounting is only done when schedstats are enabled. memcg users might want to track it separately without relying on schedstats which also enables a bunch of other scheduler related stats collection adding more overheads.

Quoted 6 lines

Also, you may want to update Documentation/admin-guide/cgroup-v2.rst too.

Thanks,
Michal

Signature · 2 lines
Thanks and Regards,
Prateek
maintainer
#3 lore raw
Quoted 4 lines · K Prateek Nayak

The /proc/$pid/sched accounting is only done when schedstats are enabled. memcg users might want to track it separately without relying on schedstats which also enables a bunch of other scheduler related stats collection adding more overheads.

.oO(memory.[numa_]stat could end up with something similar since not all users are interested in all fields but all users are affected by added fields)

Thanks,
Michal

author
#4 lore raw
In reply to Michal Koutný

Hi Michal,

thanks for taking a look at this,

Quoted 8 lines · Michal Koutný

Hello Chen.

Quoted 2 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful.

...

Quoted 2 lines

The following two new fields:

Quoted 8 lines
Quoted 5 lines

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

Why is the field /proc/$pid/sched not enough?

In the context of NUMA balancing, it would be helpful to not only monitor on the activities of individual task/thread but also the resource usage and task migrations at the group level - which helps us quickly evaluate the performance and resource usage of the container - like per memcg numa_pages_migrated, numa_pte_updates introduced in commit f77f0c751478 ("mm,memcg: provide per-cgroup counters for NUMA balancing operations"). Yes, we can iterate the /proc/$pid/sched to
find the accumulated NUMA stat, and the introduction of per - cgroup numa stat can help users more conveniently track the overall data of the workload.

Besides, I'm considering evaluating the per - cgroup NUMA balance control[1] to help users do fine - grain control per workload. This per - cgroup NUMA balance stat could be used to evaluate the efficiency of per - cgroup NUMA balance.

Quoted 2 lines

Also, you may want to update Documentation/admin-guide/cgroup-v2.rst too.

Got it, will do in next version.

[1] https://lore.kernel.org/lkml/b3f1f6c478127a38b9091a8341374ba160d25c5a.1740483690.git.yu.c.chen@intel.com/

thanks,
Chenyu

Quoted 3 lines

Thanks,
Michal

maintainer
#5 lore raw
Quoted 5 lines · Yu C

In the context of NUMA balancing, it would be helpful to not only monitor on the activities of individual task/thread but also the resource usage and task migrations at the group level - which helps us quickly evaluate the performance and resource usage of the container - like per memcg numa_pages_migrated, numa_pte_updates introduced in

Somehow I thought that these are the useful metrics (amount of misplaced memory) and the aggregated task stats aren't interesting (they'd be more or less proportion of total number of tasks in the group and you don't know which task it was).

Quoted 1 line

Got it, will do in next version.

OK.

Thanks,
Michal

#6 lore raw
In reply to Chen Yu

Hi Chen Yu,

Quoted 11 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

Quoted 7 lines

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

I applied this patch, but I still don't see the two new fields in /proc/{PID}/sched.

Am I missing any additional steps?

Thanks,
Madadi Vineeth Reddy

Quoted 19 lines

Previous RFC version can be found here:
https://lore.kernel.org/lkml/1847c5ef828ad4835a35e3a54b88d2e13bce0eea.1740483690.git.yu.c.chen@intel.com/

---
RFC->v1: Rename the nr_numa_task_migrated to
numa_task_migrated, and nr_numa_task_swapped numa_task_swapped in /proc/{PID}/sched, so both cgroup's memory.stat and task's sched have the same field name.

---
 include/linux/sched.h         |  4 ++++
 include/linux/vm_event_item.h |  2 ++
 kernel/sched/core.c           | 10 ++++++++--
 kernel/sched/debug.c          |  4 ++++
 mm/memcontrol.c               |  2 ++
 mm/vmstat.c                   |  2 ++
 6 files changed, 22 insertions(+), 2 deletions(-)
#7 lore raw
Quoted 19 lines · Madadi Vineeth Reddy

Hi Chen Yu,

Quoted 16 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

The following two new fields:

numa_task_migrated
numa_task_swapped

Quoted 7 lines
Quoted 2 lines

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

I applied this patch, but I still don't see the two new fields in /proc/{PID}/sched.

Am I missing any additional steps?

You also need to enable schedstats:

echo 1 > /proc/sys/kernel/sched_schedstats

After that it should be visible:

$ cat /proc/4030/sched
sched-messaging (4030, #threads: 641)
-------------------------------------------------------------------
se.exec_start                                :        283818.948537

...

nr_forced_migrations                         :                    0
numa_task_migrated                           :                    0
numa_task_swapped                            :                    0
nr_wakeups                                   :                    0

...

Signature · 2 lines
Thanks and Regards,
Prateek
Quoted 25 lines

Thanks,
Madadi Vineeth Reddy

Quoted 19 lines

Previous RFC version can be found here:
https://lore.kernel.org/lkml/1847c5ef828ad4835a35e3a54b88d2e13bce0eea.1740483690.git.yu.c.chen@intel.com/

---
RFC->v1: Rename the nr_numa_task_migrated to
numa_task_migrated, and nr_numa_task_swapped numa_task_swapped in /proc/{PID}/sched, so both cgroup's memory.stat and task's sched have the same field name.

---
  include/linux/sched.h         |  4 ++++
  include/linux/vm_event_item.h |  2 ++
  kernel/sched/core.c           | 10 ++++++++--
  kernel/sched/debug.c          |  4 ++++
  mm/memcontrol.c               |  2 ++
  mm/vmstat.c                   |  2 ++
  6 files changed, 22 insertions(+), 2 deletions(-)
#8 lore raw
Quoted 26 lines · K Prateek Nayak
Quoted 25 lines · Madadi Vineeth Reddy

Hi Chen Yu,

Quoted 11 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

Quoted 7 lines

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

I applied this patch, but I still don't see the two new fields in /proc/{PID}/sched.

Quoted 7 lines
Quoted 1 line

Am I missing any additional steps?

You also need to enable schedstats:

echo 1 > /proc/sys/kernel/sched_schedstats

After that it should be visible:

Thanks, Prateek! I had missed enabling schedstats. Now that it's enabled, I can see the fields.

Thanks,
Madadi Vineeth Reddy

Quoted 15 lines
$ cat /proc/4030/sched
sched-messaging (4030, #threads: 641)
-------------------------------------------------------------------
se.exec_start                                :        283818.948537

...

nr_forced_migrations                         :                    0 numa_task_migrated                           :                    0 numa_task_swapped                            :                    0 nr_wakeups                                   :                    0

...

reviewer Tested-by on v1 1/1
#9 lore raw
In reply to Chen Yu

Hello Chenyu,

Quoted 11 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

Quoted 7 lines

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

Running sched-messaging with schedstats enabled, I could see both "numa_task_migrated" and "numa_task_swapped" being populated for the sched-messaging threads:

$ for i in $(ls /proc/4030/task/); do grep "numa_task_migrated" /proc/$i/sched; done | tr -s ' ' | cut -d ' ' -f3 | sort | uniq -c
    400 0
    231 1
     10 2
$ for i in $(ls /proc/4030/task/); do grep "numa_task_swapped" /proc/$i/sched; done | tr -s ' ' | cut -d ' ' -f3 | sort | uniq -c
    389 0
    193 1
     47 2
     11 3
      1 4
Quoted 5 lines

Previous RFC version can be found here:
https://lore.kernel.org/lkml/1847c5ef828ad4835a35e3a54b88d2e13bce0eea.1740483690.git.yu.c.chen@intel.com/

Feel free to add:

Signature · 2 lines
Thanks and Regards,
Prateek
Quoted 7 lines

---
RFC->v1: Rename the nr_numa_task_migrated to
numa_task_migrated, and nr_numa_task_swapped numa_task_swapped in /proc/{PID}/sched, so both cgroup's memory.stat and task's sched have the same field name.

[..snip..]

author
#10 lore raw
Quoted 39 lines · K Prateek Nayak

Hello Chenyu,

Quoted 11 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

Quoted 7 lines

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

Running sched-messaging with schedstats enabled, I could see both "numa_task_migrated" and "numa_task_swapped" being populated for the sched-messaging threads:

$ for i in $(ls /proc/4030/task/); do grep "numa_task_migrated" /proc/ $i/sched; done | tr -s ' ' | cut -d ' ' -f3 | sort | uniq -c
     400 0
     231 1
      10 2
$ for i in $(ls /proc/4030/task/); do grep "numa_task_swapped" /proc/$i/ sched; done | tr -s ' ' | cut -d ' ' -f3 | sort | uniq -c
     389 0
     193 1
      47 2
      11 3
       1 4
Quoted 1 line
Quoted 9 lines
Quoted 4 lines

Previous RFC version can be found here:
https://lore.kernel.org/ lkml/1847c5ef828ad4835a35e3a54b88d2e13bce0eea.1740483690.git.yu.c.chen@intel.com/

Feel free to add:

Thanks Prateek!

Thanks,
Chenyu

Tested-by on v1 1/1
#11 lore raw
In reply to Chen Yu

Hi Chen Yu,

Quoted 11 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

Quoted 7 lines

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

I was able to see the fields and their corresponding values for schbench:

numa_task_swapped                            :                    2
numa_task_migrated                           :                    0
numa_task_swapped                            :                    1
numa_task_migrated                           :                    0
numa_task_swapped                            :                    0
numa_task_migrated                           :                    0
numa_task_swapped                            :                    1

Thanks,
Madadi Vineeth Reddy

Quoted 18 lines

Previous RFC version can be found here:
https://lore.kernel.org/lkml/1847c5ef828ad4835a35e3a54b88d2e13bce0eea.1740483690.git.yu.c.chen@intel.com/

---
RFC->v1: Rename the nr_numa_task_migrated to
numa_task_migrated, and nr_numa_task_swapped numa_task_swapped in /proc/{PID}/sched, so both cgroup's memory.stat and task's sched have the same field name.

---
 include/linux/sched.h         |  4 ++++
 include/linux/vm_event_item.h |  2 ++
 kernel/sched/core.c           | 10 ++++++++--
 kernel/sched/debug.c          |  4 ++++
 mm/memcontrol.c               |  2 ++
 mm/vmstat.c                   |  2 ++
 6 files changed, 22 insertions(+), 2 deletions(-)
author
#12 lore raw
Quoted 32 lines · Madadi Vineeth Reddy

Hi Chen Yu,

Quoted 11 lines · Chen Yu

On system with NUMA balancing enabled, it is found that tracking the task activities due to NUMA balancing is helpful. NUMA balancing has two mechanisms for task migration: one is to migrate the task to an idle CPU in its preferred node, the other is to swap tasks on different nodes if they are on each other's preferred node.

The kernel already has NUMA page migration statistics in /sys/fs/cgroup/mytest/memory.stat and /proc/{PID}/sched. but does not have statistics for task migration/swap. Add the task migration and swap count accordingly.

Quoted 7 lines

The following two new fields:

numa_task_migrated
numa_task_swapped

will be displayed in both /sys/fs/cgroup/{GROUP}/memory.stat and /proc/{PID}/sched

I was able to see the fields and their corresponding values for schbench:

numa_task_swapped                            :                    2
numa_task_migrated                           :                    0
numa_task_swapped                            :                    1
numa_task_migrated                           :                    0
numa_task_swapped                            :                    0
numa_task_migrated                           :                    0
numa_task_swapped                            :                    1
Quoted 2 lines

Yes, the sysfs schedstat has to be enabled. Thanks for your test, Madadi!

thanks,
Chenyu

Quoted 21 lines

Thanks,
Madadi Vineeth Reddy

Quoted 18 lines

Previous RFC version can be found here:
https://lore.kernel.org/lkml/1847c5ef828ad4835a35e3a54b88d2e13bce0eea.1740483690.git.yu.c.chen@intel.com/

---
RFC->v1: Rename the nr_numa_task_migrated to
numa_task_migrated, and nr_numa_task_swapped numa_task_swapped in /proc/{PID}/sched, so both cgroup's memory.stat and task's sched have the same field name.

---
  include/linux/sched.h         |  4 ++++
  include/linux/vm_event_item.h |  2 ++
  kernel/sched/core.c           | 10 ++++++++--
  kernel/sched/debug.c          |  4 ++++
  mm/memcontrol.c               |  2 ++
  mm/vmstat.c                   |  2 ++
  6 files changed, 22 insertions(+), 2 deletions(-)