Skip to content

Commit 598f0ec

Browse files
Mel Gormaningomolnar
authored andcommitted
sched/numa: Set the scan rate proportional to the memory usage of the task being scanned
The NUMA PTE scan rate is controlled with a combination of the numa_balancing_scan_period_min, numa_balancing_scan_period_max and numa_balancing_scan_size. This scan rate is independent of the size of the task and as an aside it is further complicated by the fact that numa_balancing_scan_size controls how many pages are marked pte_numa and not how much virtual memory is scanned. In combination, it is almost impossible to meaningfully tune the min and max scan periods and reasoning about performance is complex when the time to complete a full scan is is partially a function of the tasks memory size. This patch alters the semantic of the min and max tunables to be about tuning the length time it takes to complete a scan of a tasks occupied virtual address space. Conceptually this is a lot easier to understand. There is a "sanity" check to ensure the scan rate is never extremely fast based on the amount of virtual memory that should be scanned in a second. The default of 2.5G seems arbitrary but it is to have the maximum scan rate after the patch roughly match the maximum scan rate before the patch was applied. On a similar note, numa_scan_period is in milliseconds and not jiffies. Properly placed pages slow the scanning rate but adding 10 jiffies to numa_scan_period means that the rate scanning slows depends on HZ which is confusing. Get rid of the jiffies_to_msec conversion and treat it as ms. Signed-off-by: Mel Gorman <mgorman@suse.de> Reviewed-by: Rik van Riel <riel@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1381141781-10992-18-git-send-email-mgorman@suse.de Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 7e8d16b commit 598f0ec

3 files changed

Lines changed: 83 additions & 17 deletions

File tree

‎Documentation/sysctl/kernel.txt‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,16 @@ workload pattern changes and minimises performance impact due to remote
403403
memory accesses. These sysctls control the thresholds for scan delays and
404404
the number of pages scanned.
405405

406-
numa_balancing_scan_period_min_ms is the minimum delay in milliseconds
407-
between scans. It effectively controls the maximum scanning rate for
408-
each task.
406+
numa_balancing_scan_period_min_ms is the minimum time in milliseconds to
407+
scan a tasks virtual memory. It effectively controls the maximum scanning
408+
rate for each task.
409409

410410
numa_balancing_scan_delay_ms is the starting "scan delay" used for a task
411411
when it initially forks.
412412

413-
numa_balancing_scan_period_max_ms is the maximum delay between scans. It
414-
effectively controls the minimum scanning rate for each task.
413+
numa_balancing_scan_period_max_ms is the maximum time in milliseconds to
414+
scan a tasks virtual memory. It effectively controls the minimum scanning
415+
rate for each task.
415416

416417
numa_balancing_scan_size_mb is how many megabytes worth of pages are
417418
scanned for a given scan.

‎include/linux/sched.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ struct task_struct {
13391339
int numa_scan_seq;
13401340
int numa_migrate_seq;
13411341
unsigned int numa_scan_period;
1342+
unsigned int numa_scan_period_max;
13421343
u64 node_stamp; /* migration stamp */
13431344
struct callback_head numa_work;
13441345
#endif /* CONFIG_NUMA_BALANCING */

‎kernel/sched/fair.c‎

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -818,18 +818,65 @@ update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
818818

819819
#ifdef CONFIG_NUMA_BALANCING
820820
/*
821-
* numa task sample period in ms
821+
* Approximate time to scan a full NUMA task in ms. The task scan period is
822+
* calculated based on the tasks virtual memory size and
823+
* numa_balancing_scan_size.
822824
*/
823-
unsigned int sysctl_numa_balancing_scan_period_min = 100;
824-
unsigned int sysctl_numa_balancing_scan_period_max = 100*50;
825-
unsigned int sysctl_numa_balancing_scan_period_reset = 100*600;
825+
unsigned int sysctl_numa_balancing_scan_period_min = 1000;
826+
unsigned int sysctl_numa_balancing_scan_period_max = 60000;
827+
unsigned int sysctl_numa_balancing_scan_period_reset = 60000;
826828

827829
/* Portion of address space to scan in MB */
828830
unsigned int sysctl_numa_balancing_scan_size = 256;
829831

830832
/* Scan @scan_size MB every @scan_period after an initial @scan_delay in ms */
831833
unsigned int sysctl_numa_balancing_scan_delay = 1000;
832834

835+
static unsigned int task_nr_scan_windows(struct task_struct *p)
836+
{
837+
unsigned long rss = 0;
838+
unsigned long nr_scan_pages;
839+
840+
/*
841+
* Calculations based on RSS as non-present and empty pages are skipped
842+
* by the PTE scanner and NUMA hinting faults should be trapped based
843+
* on resident pages
844+
*/
845+
nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
846+
rss = get_mm_rss(p->mm);
847+
if (!rss)
848+
rss = nr_scan_pages;
849+
850+
rss = round_up(rss, nr_scan_pages);
851+
return rss / nr_scan_pages;
852+
}
853+
854+
/* For sanitys sake, never scan more PTEs than MAX_SCAN_WINDOW MB/sec. */
855+
#define MAX_SCAN_WINDOW 2560
856+
857+
static unsigned int task_scan_min(struct task_struct *p)
858+
{
859+
unsigned int scan, floor;
860+
unsigned int windows = 1;
861+
862+
if (sysctl_numa_balancing_scan_size < MAX_SCAN_WINDOW)
863+
windows = MAX_SCAN_WINDOW / sysctl_numa_balancing_scan_size;
864+
floor = 1000 / windows;
865+
866+
scan = sysctl_numa_balancing_scan_period_min / task_nr_scan_windows(p);
867+
return max_t(unsigned int, floor, scan);
868+
}
869+
870+
static unsigned int task_scan_max(struct task_struct *p)
871+
{
872+
unsigned int smin = task_scan_min(p);
873+
unsigned int smax;
874+
875+
/* Watch for min being lower than max due to floor calculations */
876+
smax = sysctl_numa_balancing_scan_period_max / task_nr_scan_windows(p);
877+
return max(smin, smax);
878+
}
879+
833880
static void task_numa_placement(struct task_struct *p)
834881
{
835882
int seq;
@@ -840,6 +887,7 @@ static void task_numa_placement(struct task_struct *p)
840887
if (p->numa_scan_seq == seq)
841888
return;
842889
p->numa_scan_seq = seq;
890+
p->numa_scan_period_max = task_scan_max(p);
843891

844892
/* FIXME: Scheduling placement policy hints go here */
845893
}
@@ -860,9 +908,14 @@ void task_numa_fault(int node, int pages, bool migrated)
860908
* If pages are properly placed (did not migrate) then scan slower.
861909
* This is reset periodically in case of phase changes
862910
*/
863-
if (!migrated)
864-
p->numa_scan_period = min(sysctl_numa_balancing_scan_period_max,
865-
p->numa_scan_period + jiffies_to_msecs(10));
911+
if (!migrated) {
912+
/* Initialise if necessary */
913+
if (!p->numa_scan_period_max)
914+
p->numa_scan_period_max = task_scan_max(p);
915+
916+
p->numa_scan_period = min(p->numa_scan_period_max,
917+
p->numa_scan_period + 10);
918+
}
866919

867920
task_numa_placement(p);
868921
}
@@ -884,6 +937,7 @@ void task_numa_work(struct callback_head *work)
884937
struct mm_struct *mm = p->mm;
885938
struct vm_area_struct *vma;
886939
unsigned long start, end;
940+
unsigned long nr_pte_updates = 0;
887941
long pages;
888942

889943
WARN_ON_ONCE(p != container_of(work, struct task_struct, numa_work));
@@ -915,7 +969,7 @@ void task_numa_work(struct callback_head *work)
915969
*/
916970
migrate = mm->numa_next_reset;
917971
if (time_after(now, migrate)) {
918-
p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
972+
p->numa_scan_period = task_scan_min(p);
919973
next_scan = now + msecs_to_jiffies(sysctl_numa_balancing_scan_period_reset);
920974
xchg(&mm->numa_next_reset, next_scan);
921975
}
@@ -927,8 +981,10 @@ void task_numa_work(struct callback_head *work)
927981
if (time_before(now, migrate))
928982
return;
929983

930-
if (p->numa_scan_period == 0)
931-
p->numa_scan_period = sysctl_numa_balancing_scan_period_min;
984+
if (p->numa_scan_period == 0) {
985+
p->numa_scan_period_max = task_scan_max(p);
986+
p->numa_scan_period = task_scan_min(p);
987+
}
932988

933989
next_scan = now + msecs_to_jiffies(p->numa_scan_period);
934990
if (cmpxchg(&mm->numa_next_scan, migrate, next_scan) != migrate)
@@ -965,7 +1021,15 @@ void task_numa_work(struct callback_head *work)
9651021
start = max(start, vma->vm_start);
9661022
end = ALIGN(start + (pages << PAGE_SHIFT), HPAGE_SIZE);
9671023
end = min(end, vma->vm_end);
968-
pages -= change_prot_numa(vma, start, end);
1024+
nr_pte_updates += change_prot_numa(vma, start, end);
1025+
1026+
/*
1027+
* Scan sysctl_numa_balancing_scan_size but ensure that
1028+
* at least one PTE is updated so that unused virtual
1029+
* address space is quickly skipped.
1030+
*/
1031+
if (nr_pte_updates)
1032+
pages -= (end - start) >> PAGE_SHIFT;
9691033

9701034
start = end;
9711035
if (pages <= 0)
@@ -1012,7 +1076,7 @@ void task_tick_numa(struct rq *rq, struct task_struct *curr)
10121076

10131077
if (now - curr->node_stamp > period) {
10141078
if (!curr->node_stamp)
1015-
curr->numa_scan_period = sysctl_numa_balancing_scan_period_min;
1079+
curr->numa_scan_period = task_scan_min(curr);
10161080
curr->node_stamp += period;
10171081

10181082
if (!time_before(jiffies, curr->mm->numa_next_scan)) {

0 commit comments

Comments
 (0)