From patchwork Tue Apr 8 10:14:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Yu X-Patchwork-Id: 14042641 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 CF3BEC3600C for ; Tue, 8 Apr 2025 10:21:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 802DB6B0007; Tue, 8 Apr 2025 06:21:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B15F6B0008; Tue, 8 Apr 2025 06:21:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 62B456B000A; Tue, 8 Apr 2025 06:21:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 3FB206B0007 for ; Tue, 8 Apr 2025 06:21:37 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4A287AD2B9 for ; Tue, 8 Apr 2025 10:21:38 +0000 (UTC) X-FDA: 83310485076.05.5965908 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf21.hostedemail.com (Postfix) with ESMTP id 4CBE11C0014 for ; Tue, 8 Apr 2025 10:21:35 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Od1W2qo3; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of yu.c.chen@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=yu.c.chen@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744107696; h=from:from: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=e56P81jqITTMn/jcF3EWCHVhE/xrHJXiVrDjDPTnU0g=; b=agTnvG8Ck/4r3a7qfAQIfWZJXqZqanhwbdVOp7lvxkcGsq6RcGR2GesjXVbdF20F1QdLEa X/m3xmqpyjKijwkdgH6/SW+lGithutDEj9983nPEJTZc5ExdwS0dwWnTP54QrqNqSH8EB+ AqJX/ogpNsJnJCEA0vABq3yDGAh7Ozw= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Od1W2qo3; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of yu.c.chen@intel.com designates 192.198.163.11 as permitted sender) smtp.mailfrom=yu.c.chen@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744107696; a=rsa-sha256; cv=none; b=7RFkvG4bKG4CcRxR3yJWmGuvK9vwPm2cdCPO1LYkUQxOpUktPU+DZdhgiI8XIBnX2+g+Cr T0w6BE3BYsiBIfg1j2zblCAiCT/Vu2piF/IKpo5FLdTCb8UtsFZD8/GNMrBNdrtddD5lU0 SBwcm7JZkk3ocq+qVPrMmwgLQCLOzyg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1744107696; x=1775643696; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=2TWbx80DBfebYdtB+8UqZOQggmllLAdKvcVPGbHUZJk=; b=Od1W2qo34OL0DoK/GNBY/hN7EtGlGZtIxpMZ0HGKu29UZXl/0RmjsinD W7AKNIfWAt/4kExRv+oSkb4zxkABrOK220qxstL/JCjtGE3AnFyC1zPYl TE9d+eckvu3w5t8qku+ltZUGrxREMBhuo+lnCEc1V1GKipF8k7Vm+iAPr Dk2NBLxNWQfWP0BOJTw+UGr8y1TEbFWTBlqikS0wBTw+F+SRsTY6wXi1z NPLuECfio3veFjiME/+c9s9q5hQ6/mbHSgZVcQgCtAjlMUVa/hXNknger BwSgfsFtUJ5Dao3mCtDmakRbhp8/XVBUc+Sz5pdnB7pa7LEtNEKPDdU8g g==; X-CSE-ConnectionGUID: YJtFwHinRuWZ+JVhrEnkxg== X-CSE-MsgGUID: HXWdyX1OT5m2TB34dj0QgQ== X-IronPort-AV: E=McAfee;i="6700,10204,11397"; a="56196524" X-IronPort-AV: E=Sophos;i="6.15,197,1739865600"; d="scan'208";a="56196524" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Apr 2025 03:21:19 -0700 X-CSE-ConnectionGUID: AkbhsusUSYGQ+uYCTBeKCA== X-CSE-MsgGUID: ECRVkKJ1RkOqWDOy3NxCKQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.15,197,1739865600"; d="scan'208";a="132360362" Received: from chenyu-dev.sh.intel.com ([10.239.62.107]) by fmviesa003.fm.intel.com with ESMTP; 08 Apr 2025 03:21:10 -0700 From: Chen Yu To: Peter Zijlstra , Ingo Molnar , Tejun Heo , "Michal Koutny" , Johannes Weiner , Jonathan Corbet , Mel Gorman , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton Cc: cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Tim Chen , Aubrey Li , Chen Yu , Chen Yu , K Prateek Nayak , Madadi Vineeth Reddy Subject: [PATCH v2] sched/numa: Add statistics of numa balance task migration and swap Date: Tue, 8 Apr 2025 18:14:44 +0800 Message-Id: <20250408101444.192519-1-yu.c.chen@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 4CBE11C0014 X-Stat-Signature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VAJ2HU1geZUkR+mlFWtI4phZ/+jGKyMihjD9km0AY70xN/fD9GnAle7Kittet4YfNkUHJdQdJ0myHOFdBRZEJP+cmv4cg/WQzEPWY+xRzgn+fTsNd87Y0+Vra+ti9A3K8hlSGzFWEW5Gb8oyCS9DmdX9WY/2MB4JyD1g8yQfXoiFdTbfqrjabK935D0/TL3PlWLQUpw4+eXixU 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: On systems 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 Introducing both pertask and permemcg NUMA balancing statistics helps to quickly evaluate the performance and resource usage of the target workload. For example, the user can first identify the container which has high NUMA balance activity and then narrow down to a specific task within that group, and tune the memory policy of that task. In summary, it is plausible to iterate the /proc/$pid/sched to find the offending task, but the introduction of per memcg tasks' Numa balancing aggregated activity can further help users identify the task in a divide-and-conquer way. Tested-by: K Prateek Nayak Tested-by: Madadi Vineeth Reddy Signed-off-by: Chen Yu --- v1->v2: Update the Documentation/admin-guide/cgroup-v2.rst. (Michal) --- Documentation/admin-guide/cgroup-v2.rst | 6 ++++++ 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 ++ 7 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index f293a13b42ed..b698be14942c 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1652,6 +1652,12 @@ The following nested keys are defined. numa_hint_faults (npn) Number of NUMA hinting faults. + numa_task_migrated (npn) + Number of task migration by NUMA balancing. + + numa_task_swapped (npn) + Number of task swap by NUMA balancing. + pgdemote_kswapd Number of pages demoted by kswapd. diff --git a/include/linux/sched.h b/include/linux/sched.h index 56ddeb37b5cd..2e91326c16ec 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -549,6 +549,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; diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index 5a37cb2b6f93..df8a1b30930f 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -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, diff --git a/kernel/sched/core.c b/kernel/sched/core.c index b434c2f7e3c1..54e7d63f7785 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -3352,6 +3352,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; @@ -7955,8 +7960,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); } diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index 56ae54e0ce6a..f971c2af7912 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -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); diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 83c2df73e4b6..1ba1fa9ed8cb 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -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 }; diff --git a/mm/vmstat.c b/mm/vmstat.c index 651318765ebf..4abd2ca05d2a 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1342,6 +1342,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",