1 // SPDX-License-Identifier: GPL-2.0
9 #include "../kselftest.h"
10 #include "../../../../include/vdso/time64.h"
12 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
13 #define KSM_FP(s) (KSM_SYSFS_PATH s)
14 #define KSM_SCAN_LIMIT_SEC_DEFAULT 120
15 #define KSM_PAGE_COUNT_DEFAULT 10l
16 #define KSM_PROT_STR_DEFAULT "rw"
17 #define KSM_USE_ZERO_PAGES_DEFAULT false
18 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
19 #define MB (1ul << 20)
22 unsigned long max_page_sharing;
23 unsigned long merge_across_nodes;
24 unsigned long pages_to_scan;
26 unsigned long sleep_millisecs;
27 unsigned long stable_node_chains_prune_millisecs;
28 unsigned long use_zero_pages;
34 CHECK_KSM_ZERO_PAGE_MERGE,
40 static int ksm_write_sysfs(const char *file_path, unsigned long val)
42 FILE *f = fopen(file_path, "w");
45 fprintf(stderr, "f %s\n", file_path);
49 if (fprintf(f, "%lu", val) < 0) {
58 static int ksm_read_sysfs(const char *file_path, unsigned long *val)
60 FILE *f = fopen(file_path, "r");
63 fprintf(stderr, "f %s\n", file_path);
67 if (fscanf(f, "%lu", val) != 1) {
76 static int str_to_prot(char *prot_str)
80 if ((strchr(prot_str, 'r')) != NULL)
82 if ((strchr(prot_str, 'w')) != NULL)
84 if ((strchr(prot_str, 'x')) != NULL)
90 static void print_help(void)
92 printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
93 "[-z use_zero_pages] [-m merge_across_nodes] [-s size]\n");
95 printf("Supported <test type>:\n"
96 " -M (page merging)\n"
97 " -Z (zero pages merging)\n"
98 " -N (merging of pages in different NUMA nodes)\n"
99 " -U (page unmerging)\n"
100 " -P evaluate merging time and speed.\n"
101 " For this test, the size of duplicated memory area (in MiB)\n"
102 " must be provided using -s option\n"
103 " -C evaluate the time required to break COW of merged pages.\n\n");
105 printf(" -a: specify the access protections of pages.\n"
106 " <prot> must be of the form [rwx].\n"
107 " Default: %s\n", KSM_PROT_STR_DEFAULT);
108 printf(" -p: specify the number of pages to test.\n"
109 " Default: %ld\n", KSM_PAGE_COUNT_DEFAULT);
110 printf(" -l: limit the maximum running time (in seconds) for a test.\n"
111 " Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT);
112 printf(" -z: change use_zero_pages tunable\n"
113 " Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
114 printf(" -m: change merge_across_nodes tunable\n"
115 " Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT);
116 printf(" -s: the size of duplicated memory area (in MiB)\n");
121 static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_t map_size)
123 void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0);
129 memset(map_ptr, data, map_size);
130 if (mprotect(map_ptr, map_size, prot)) {
132 munmap(map_ptr, map_size);
139 static int ksm_do_scan(int scan_count, struct timespec start_time, int timeout)
141 struct timespec cur_time;
142 unsigned long cur_scan, init_scan;
144 if (ksm_read_sysfs(KSM_FP("full_scans"), &init_scan))
146 cur_scan = init_scan;
148 while (cur_scan < init_scan + scan_count) {
149 if (ksm_read_sysfs(KSM_FP("full_scans"), &cur_scan))
151 if (clock_gettime(CLOCK_MONOTONIC_RAW, &cur_time)) {
152 perror("clock_gettime");
155 if ((cur_time.tv_sec - start_time.tv_sec) > timeout) {
156 printf("Scan time limit exceeded\n");
164 static int ksm_merge_pages(void *addr, size_t size, struct timespec start_time, int timeout)
166 if (madvise(addr, size, MADV_MERGEABLE)) {
170 if (ksm_write_sysfs(KSM_FP("run"), 1))
173 /* Since merging occurs only after 2 scans, make sure to get at least 2 full scans */
174 if (ksm_do_scan(2, start_time, timeout))
180 static bool assert_ksm_pages_count(long dupl_page_count)
182 unsigned long max_page_sharing, pages_sharing, pages_shared;
184 if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) ||
185 ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) ||
186 ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing))
190 * Since there must be at least 2 pages for merging and 1 page can be
191 * shared with the limited number of pages (max_page_sharing), sometimes
192 * there are 'leftover' pages that cannot be merged. For example, if there
193 * are 11 pages and max_page_sharing = 10, then only 10 pages will be
194 * merged and the 11th page won't be affected. As a result, when the number
195 * of duplicate pages is divided by max_page_sharing and the remainder is 1,
196 * pages_shared and pages_sharing values will be equal between dupl_page_count
197 * and dupl_page_count - 1.
199 if (dupl_page_count % max_page_sharing == 1 || dupl_page_count % max_page_sharing == 0) {
200 if (pages_shared == dupl_page_count / max_page_sharing &&
201 pages_sharing == pages_shared * (max_page_sharing - 1))
204 if (pages_shared == (dupl_page_count / max_page_sharing + 1) &&
205 pages_sharing == dupl_page_count - pages_shared)
212 static int ksm_save_def(struct ksm_sysfs *ksm_sysfs)
214 if (ksm_read_sysfs(KSM_FP("max_page_sharing"), &ksm_sysfs->max_page_sharing) ||
215 ksm_read_sysfs(KSM_FP("merge_across_nodes"), &ksm_sysfs->merge_across_nodes) ||
216 ksm_read_sysfs(KSM_FP("sleep_millisecs"), &ksm_sysfs->sleep_millisecs) ||
217 ksm_read_sysfs(KSM_FP("pages_to_scan"), &ksm_sysfs->pages_to_scan) ||
218 ksm_read_sysfs(KSM_FP("run"), &ksm_sysfs->run) ||
219 ksm_read_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
220 &ksm_sysfs->stable_node_chains_prune_millisecs) ||
221 ksm_read_sysfs(KSM_FP("use_zero_pages"), &ksm_sysfs->use_zero_pages))
227 static int ksm_restore(struct ksm_sysfs *ksm_sysfs)
229 if (ksm_write_sysfs(KSM_FP("max_page_sharing"), ksm_sysfs->max_page_sharing) ||
230 ksm_write_sysfs(KSM_FP("merge_across_nodes"), ksm_sysfs->merge_across_nodes) ||
231 ksm_write_sysfs(KSM_FP("pages_to_scan"), ksm_sysfs->pages_to_scan) ||
232 ksm_write_sysfs(KSM_FP("run"), ksm_sysfs->run) ||
233 ksm_write_sysfs(KSM_FP("sleep_millisecs"), ksm_sysfs->sleep_millisecs) ||
234 ksm_write_sysfs(KSM_FP("stable_node_chains_prune_millisecs"),
235 ksm_sysfs->stable_node_chains_prune_millisecs) ||
236 ksm_write_sysfs(KSM_FP("use_zero_pages"), ksm_sysfs->use_zero_pages))
242 static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, size_t page_size)
245 struct timespec start_time;
247 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
248 perror("clock_gettime");
252 /* fill pages with the same data and merge them */
253 map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
257 if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
260 /* verify that the right number of pages are merged */
261 if (assert_ksm_pages_count(page_count)) {
263 munmap(map_ptr, page_size * page_count);
269 munmap(map_ptr, page_size * page_count);
273 static int check_ksm_unmerge(int mapping, int prot, int timeout, size_t page_size)
276 struct timespec start_time;
279 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
280 perror("clock_gettime");
284 /* fill pages with the same data and merge them */
285 map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
289 if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
292 /* change 1 byte in each of the 2 pages -- KSM must automatically unmerge them */
293 memset(map_ptr, '-', 1);
294 memset(map_ptr + page_size, '+', 1);
296 /* get at least 1 scan, so KSM can detect that the pages were modified */
297 if (ksm_do_scan(1, start_time, timeout))
300 /* check that unmerging was successful and 0 pages are currently merged */
301 if (assert_ksm_pages_count(0)) {
303 munmap(map_ptr, page_size * page_count);
309 munmap(map_ptr, page_size * page_count);
313 static int check_ksm_zero_page_merge(int mapping, int prot, long page_count, int timeout,
314 bool use_zero_pages, size_t page_size)
317 struct timespec start_time;
319 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
320 perror("clock_gettime");
324 if (ksm_write_sysfs(KSM_FP("use_zero_pages"), use_zero_pages))
327 /* fill pages with zero and try to merge them */
328 map_ptr = allocate_memory(NULL, prot, mapping, 0, page_size * page_count);
332 if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
336 * verify that the right number of pages are merged:
337 * 1) if use_zero_pages is set to 1, empty pages are merged
338 * with the kernel zero page instead of with each other;
339 * 2) if use_zero_pages is set to 0, empty pages are not treated specially
340 * and merged as usual.
342 if (use_zero_pages && !assert_ksm_pages_count(0))
344 else if (!use_zero_pages && !assert_ksm_pages_count(page_count))
348 munmap(map_ptr, page_size * page_count);
353 munmap(map_ptr, page_size * page_count);
357 static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_across_nodes,
360 void *numa1_map_ptr, *numa2_map_ptr;
361 struct timespec start_time;
364 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
365 perror("clock_gettime");
369 if (numa_available() < 0) {
370 perror("NUMA support not enabled");
373 if (numa_max_node() < 1) {
374 printf("At least 2 NUMA nodes must be available\n");
377 if (ksm_write_sysfs(KSM_FP("merge_across_nodes"), merge_across_nodes))
380 /* allocate 2 pages in 2 different NUMA nodes and fill them with the same data */
381 numa1_map_ptr = numa_alloc_onnode(page_size, 0);
382 numa2_map_ptr = numa_alloc_onnode(page_size, 1);
383 if (!numa1_map_ptr || !numa2_map_ptr) {
384 perror("numa_alloc_onnode");
388 memset(numa1_map_ptr, '*', page_size);
389 memset(numa2_map_ptr, '*', page_size);
391 /* try to merge the pages */
392 if (ksm_merge_pages(numa1_map_ptr, page_size, start_time, timeout) ||
393 ksm_merge_pages(numa2_map_ptr, page_size, start_time, timeout))
397 * verify that the right number of pages are merged:
398 * 1) if merge_across_nodes was enabled, 2 duplicate pages will be merged;
399 * 2) if merge_across_nodes = 0, there must be 0 merged pages, since there is
400 * only 1 unique page in each node and they can't be shared.
402 if (merge_across_nodes && !assert_ksm_pages_count(page_count))
404 else if (!merge_across_nodes && !assert_ksm_pages_count(0))
407 numa_free(numa1_map_ptr, page_size);
408 numa_free(numa2_map_ptr, page_size);
413 numa_free(numa1_map_ptr, page_size);
414 numa_free(numa2_map_ptr, page_size);
419 static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size)
422 struct timespec start_time, end_time;
423 unsigned long scan_time_ns;
427 map_ptr = allocate_memory(NULL, prot, mapping, '*', map_size);
431 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
432 perror("clock_gettime");
435 if (ksm_merge_pages(map_ptr, map_size, start_time, timeout))
437 if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
438 perror("clock_gettime");
442 scan_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
443 (end_time.tv_nsec - start_time.tv_nsec);
445 printf("Total size: %lu MiB\n", map_size / MB);
446 printf("Total time: %ld.%09ld s\n", scan_time_ns / NSEC_PER_SEC,
447 scan_time_ns % NSEC_PER_SEC);
448 printf("Average speed: %.3f MiB/s\n", (map_size / MB) /
449 ((double)scan_time_ns / NSEC_PER_SEC));
451 munmap(map_ptr, map_size);
456 munmap(map_ptr, map_size);
460 static int ksm_cow_time(int mapping, int prot, int timeout, size_t page_size)
463 struct timespec start_time, end_time;
464 unsigned long cow_time_ns;
466 /* page_count must be less than 2*page_size */
467 size_t page_count = 4000;
469 map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
473 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
474 perror("clock_gettime");
477 for (size_t i = 0; i < page_count - 1; i = i + 2)
478 memset(map_ptr + page_size * i, '-', 1);
479 if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
480 perror("clock_gettime");
484 cow_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
485 (end_time.tv_nsec - start_time.tv_nsec);
487 printf("Total size: %lu MiB\n\n", (page_size * page_count) / MB);
488 printf("Not merged pages:\n");
489 printf("Total time: %ld.%09ld s\n", cow_time_ns / NSEC_PER_SEC,
490 cow_time_ns % NSEC_PER_SEC);
491 printf("Average speed: %.3f MiB/s\n\n", ((page_size * (page_count / 2)) / MB) /
492 ((double)cow_time_ns / NSEC_PER_SEC));
494 /* Create 2000 pairs of duplicate pages */
495 for (size_t i = 0; i < page_count - 1; i = i + 2) {
496 memset(map_ptr + page_size * i, '+', i / 2 + 1);
497 memset(map_ptr + page_size * (i + 1), '+', i / 2 + 1);
499 if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
502 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
503 perror("clock_gettime");
506 for (size_t i = 0; i < page_count - 1; i = i + 2)
507 memset(map_ptr + page_size * i, '-', 1);
508 if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
509 perror("clock_gettime");
513 cow_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
514 (end_time.tv_nsec - start_time.tv_nsec);
516 printf("Merged pages:\n");
517 printf("Total time: %ld.%09ld s\n", cow_time_ns / NSEC_PER_SEC,
518 cow_time_ns % NSEC_PER_SEC);
519 printf("Average speed: %.3f MiB/s\n", ((page_size * (page_count / 2)) / MB) /
520 ((double)cow_time_ns / NSEC_PER_SEC));
522 munmap(map_ptr, page_size * page_count);
527 munmap(map_ptr, page_size * page_count);
531 int main(int argc, char *argv[])
535 int ksm_scan_limit_sec = KSM_SCAN_LIMIT_SEC_DEFAULT;
536 long page_count = KSM_PAGE_COUNT_DEFAULT;
537 size_t page_size = sysconf(_SC_PAGESIZE);
538 struct ksm_sysfs ksm_sysfs_old;
539 int test_name = CHECK_KSM_MERGE;
540 bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
541 bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
544 while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNPC")) != -1) {
547 prot = str_to_prot(optarg);
550 page_count = atol(optarg);
551 if (page_count <= 0) {
552 printf("The number of pages must be greater than 0\n");
557 ksm_scan_limit_sec = atoi(optarg);
558 if (ksm_scan_limit_sec <= 0) {
559 printf("Timeout value must be greater than 0\n");
567 if (strcmp(optarg, "0") == 0)
573 if (strcmp(optarg, "0") == 0)
574 merge_across_nodes = 0;
576 merge_across_nodes = 1;
579 size_MB = atoi(optarg);
581 printf("Size must be greater than 0\n");
587 test_name = CHECK_KSM_UNMERGE;
590 test_name = CHECK_KSM_ZERO_PAGE_MERGE;
593 test_name = CHECK_KSM_NUMA_MERGE;
596 test_name = KSM_MERGE_TIME;
599 test_name = KSM_COW_TIME;
607 prot = str_to_prot(KSM_PROT_STR_DEFAULT);
609 if (access(KSM_SYSFS_PATH, F_OK)) {
610 printf("Config KSM not enabled\n");
614 if (ksm_save_def(&ksm_sysfs_old)) {
615 printf("Cannot save default tunables\n");
619 if (ksm_write_sysfs(KSM_FP("run"), 2) ||
620 ksm_write_sysfs(KSM_FP("sleep_millisecs"), 0) ||
621 ksm_write_sysfs(KSM_FP("merge_across_nodes"), 1) ||
622 ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count))
626 case CHECK_KSM_MERGE:
627 ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
628 ksm_scan_limit_sec, page_size);
630 case CHECK_KSM_UNMERGE:
631 ret = check_ksm_unmerge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
634 case CHECK_KSM_ZERO_PAGE_MERGE:
635 ret = check_ksm_zero_page_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count,
636 ksm_scan_limit_sec, use_zero_pages, page_size);
638 case CHECK_KSM_NUMA_MERGE:
639 ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
640 merge_across_nodes, page_size);
644 printf("Option '-s' is required.\n");
647 ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
651 ret = ksm_cow_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
656 if (ksm_restore(&ksm_sysfs_old)) {
657 printf("Cannot restore default tunables\n");