Skip to content

Commit 59ae96f

Browse files
chansen3torvalds
authored andcommitted
tools/vm/page-types.c: add support for idle page tracking
Add a flag which causes page-types to use the kernels's idle page tracking to mark pages idle. As the tool already prints the idle flag if set, subsequent runs will show which pages have been accessed since last run. [akpm@linux-foundation.org: simplify mark_page_idle()] [chansen3@cisco.com: reorganize mark_page_idle() logic, add docs] Link: http://lkml.kernel.org/r/20180706172237.21691-1-chansen3@cisco.com Link: http://lkml.kernel.org/r/20180612153223.13174-1-chansen3@cisco.com Signed-off-by: Christian Hansen <chansen3@cisco.com> Reviewed-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7f1d23e commit 59ae96f

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎Documentation/admin-guide/mm/idle_page_tracking.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ workload one should:
6565
are not reclaimable, he or she can filter them out using
6666
``/proc/kpageflags``.
6767

68+
The page-types tool in the tools/vm directory can be used to assist in this.
69+
If the tool is run initially with the appropriate option, it will mark all the
70+
queried pages as idle. Subsequent runs of the tool can then show which pages have
71+
their idle flag cleared in the interim.
72+
6873
See :ref:`Documentation/admin-guide/mm/pagemap.rst <pagemap>` for more
6974
information about ``/proc/pid/pagemap``, ``/proc/kpageflags``, and
7075
``/proc/kpagecgroup``.

‎tools/vm/page-types.c‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
#define PROC_KPAGECOUNT "/proc/kpagecount"
7979
#define PROC_KPAGECGROUP "/proc/kpagecgroup"
8080

81+
#define SYS_KERNEL_MM_PAGE_IDLE "/sys/kernel/mm/page_idle/bitmap"
82+
8183
/* [32-] kernel hacking assistances */
8284
#define KPF_RESERVED 32
8385
#define KPF_MLOCKED 33
@@ -169,6 +171,7 @@ static const char * const debugfs_known_mountpoints[] = {
169171

170172
static int opt_raw; /* for kernel developers */
171173
static int opt_list; /* list pages (in ranges) */
174+
static int opt_mark_idle; /* set accessed bit */
172175
static int opt_no_summary; /* don't show summary */
173176
static pid_t opt_pid; /* process to walk */
174177
const char *opt_file; /* file or directory path */
@@ -198,6 +201,7 @@ static int pagemap_fd;
198201
static int kpageflags_fd;
199202
static int kpagecount_fd = -1;
200203
static int kpagecgroup_fd = -1;
204+
static int page_idle_fd = -1;
201205

202206
static int opt_hwpoison;
203207
static int opt_unpoison;
@@ -587,6 +591,30 @@ static int unpoison_page(unsigned long offset)
587591
return 0;
588592
}
589593

594+
static int mark_page_idle(unsigned long offset)
595+
{
596+
static unsigned long off;
597+
static uint64_t buf;
598+
int len;
599+
600+
if ((offset / 64 == off / 64) || buf == 0) {
601+
buf |= 1UL << (offset % 64);
602+
off = offset;
603+
return 0;
604+
}
605+
606+
len = pwrite(page_idle_fd, &buf, 8, 8 * (off / 64));
607+
if (len < 0) {
608+
perror("mark page idle");
609+
return len;
610+
}
611+
612+
buf = 1UL << (offset % 64);
613+
off = offset;
614+
615+
return 0;
616+
}
617+
590618
/*
591619
* page frame walker
592620
*/
@@ -635,6 +663,9 @@ static void add_page(unsigned long voffset, unsigned long offset,
635663
if (opt_unpoison)
636664
unpoison_page(offset);
637665

666+
if (opt_mark_idle)
667+
mark_page_idle(offset);
668+
638669
if (opt_list == 1)
639670
show_page_range(voffset, offset, 1, flags, cgroup, mapcnt);
640671
else if (opt_list == 2)
@@ -783,6 +814,9 @@ static void walk_addr_ranges(void)
783814
else
784815
walk_task(opt_offset[i], opt_size[i]);
785816

817+
if (opt_mark_idle)
818+
mark_page_idle(0);
819+
786820
close(kpageflags_fd);
787821
}
788822

@@ -813,6 +847,7 @@ static void usage(void)
813847
" -c|--cgroup path|@inode Walk pages within memory cgroup\n"
814848
" -p|--pid pid Walk process address space\n"
815849
" -f|--file filename Walk file address space\n"
850+
" -i|--mark-idle Mark pages idle\n"
816851
" -l|--list Show page details in ranges\n"
817852
" -L|--list-each Show page details one by one\n"
818853
" -C|--list-cgroup Show cgroup inode for pages\n"
@@ -1221,6 +1256,7 @@ static const struct option opts[] = {
12211256
{ "bits" , 1, NULL, 'b' },
12221257
{ "cgroup" , 1, NULL, 'c' },
12231258
{ "describe" , 1, NULL, 'd' },
1259+
{ "mark-idle" , 0, NULL, 'i' },
12241260
{ "list" , 0, NULL, 'l' },
12251261
{ "list-each" , 0, NULL, 'L' },
12261262
{ "list-cgroup", 0, NULL, 'C' },
@@ -1240,7 +1276,7 @@ int main(int argc, char *argv[])
12401276
page_size = getpagesize();
12411277

12421278
while ((c = getopt_long(argc, argv,
1243-
"rp:f:a:b:d:c:ClLMNXxF:h",
1279+
"rp:f:a:b:d:c:CilLMNXxF:h",
12441280
opts, NULL)) != -1) {
12451281
switch (c) {
12461282
case 'r':
@@ -1267,6 +1303,9 @@ int main(int argc, char *argv[])
12671303
case 'd':
12681304
describe_flags(optarg);
12691305
exit(0);
1306+
case 'i':
1307+
opt_mark_idle = 1;
1308+
break;
12701309
case 'l':
12711310
opt_list = 1;
12721311
break;
@@ -1308,6 +1347,9 @@ int main(int argc, char *argv[])
13081347
if (opt_list && opt_list_mapcnt)
13091348
kpagecount_fd = checked_open(PROC_KPAGECOUNT, O_RDONLY);
13101349

1350+
if (opt_mark_idle && opt_file)
1351+
page_idle_fd = checked_open(SYS_KERNEL_MM_PAGE_IDLE, O_RDWR);
1352+
13111353
if (opt_list && opt_pid)
13121354
printf("voffset\t");
13131355
if (opt_list && opt_file)
@@ -1341,5 +1383,8 @@ int main(int argc, char *argv[])
13411383
if (opt_list_mapcnt)
13421384
close(kpagecount_fd);
13431385

1386+
if (page_idle_fd >= 0)
1387+
close(page_idle_fd);
1388+
13441389
return 0;
13451390
}

0 commit comments

Comments
 (0)