Skip to contentSkip to conversation

add KSM performance tests

PATCH Superseded Zhansaya Bagdauletkyzy 2 participants 5 messages Aug 1, 2021 – Aug 4, 2021

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover add KSM performance tests

Extend KSM self tests with a performance benchmark. These tests are not part of regular regression testing, as they are mainly intended to be used by developers making changes to the memory management subsystem. Both patches were developed against linux-next.

Zhansaya Bagdauletkyzy (2):
selftests: vm: add KSM merging time test
selftests: vm: add COW time test for KSM pages

 tools/testing/selftests/vm/ksm_tests.c | 136 ++++++++++++++++++++++++-
 1 file changed, 132 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 selftests: vm: add KSM merging time test

Add ksm_merge_time() function to determine speed and time needed for merging. The total spent time is shown in seconds while speed is in MB/s. User must specify the size of duplicated memory area (in MB) before running the test.

The test is run as follows: ./ksm_tests -P -s 100
The output:
Total size: 100 MB
Total time: 0.309561 s
Average speed: 323 MB/s

---
 tools/testing/selftests/vm/ksm_tests.c | 76 ++++++++++++++++++++++++--
 1 file changed, 72 insertions(+), 4 deletions(-)
tools/testing/selftests/vm/ksm_tests.c +72 -4
index cdeb4a028538..91c6ff496655 100644
@@ -7,6 +7,7 @@
 #include <numa.h>
 
 #include "../kselftest.h"
+#include "../../../../include/vdso/time64.h"
 
 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
 #define KSM_FP(s) (KSM_SYSFS_PATH s)
@@ -15,6 +16,7 @@
 #define KSM_PROT_STR_DEFAULT "rw"
 #define KSM_USE_ZERO_PAGES_DEFAULT false
 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
+#define MB_TO_B 1000000ul
 
 struct ksm_sysfs {
 	unsigned long max_page_sharing;
@@ -30,7 +32,8 @@ enum ksm_test_name {
 	CHECK_KSM_MERGE,
 	CHECK_KSM_UNMERGE,
 	CHECK_KSM_ZERO_PAGE_MERGE,
-	CHECK_KSM_NUMA_MERGE
+	CHECK_KSM_NUMA_MERGE,
+	KSM_MERGE_TIME
 };
 
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
@@ -86,13 +89,16 @@ static int str_to_prot(char *prot_str)
 static void print_help(void)
 {
 	printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
-	       "[-z use_zero_pages] [-m merge_across_nodes]\n");
+	       "[-z use_zero_pages] [-m merge_across_nodes] [-s size]\n");
 
 	printf("Supported <test type>:\n"
 	       " -M (page merging)\n"
 	       " -Z (zero pages merging)\n"
 	       " -N (merging of pages in different NUMA nodes)\n"
-	       " -U (page unmerging)\n\n");
+	       " -U (page unmerging)\n"
+	       " -P evaluate merging time and speed.\n"
+	       "    For this test, the size of duplicated memory area (in MB)\n"
+	       "    must be provided using -s option\n\n");
 
 	printf(" -a: specify the access protections of pages.\n"
 	       "     <prot> must be of the form [rwx].\n"
@@ -105,6 +111,7 @@ static void print_help(void)
 	       "     Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
 	printf(" -m: change merge_across_nodes tunable\n"
 	       "     Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT);
+	printf(" -s: the size of duplicated memory area (in MB)\n");
 
 	exit(0);
 }
@@ -407,6 +414,49 @@ static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a
 	return KSFT_FAIL;
 }
 
+static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size)
+{
+	void *map_ptr;
+	struct timespec start_time, end_time;
+	long scan_time_s, scan_time_ns;
+	int avg_speed;
+
+	map_size *= MB_TO_B;
+
+	map_ptr = allocate_memory(NULL, prot, mapping, '*', map_size);
+	if (!map_ptr)
+		return KSFT_FAIL;
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+		perror("clock_gettime");
+		goto err_out;
+	}
+	if (ksm_merge_pages(map_ptr, map_size, start_time, timeout))
+		goto err_out;
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
+		perror("clock_gettime");
+		goto err_out;
+	}
+
+	scan_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
+		       (end_time.tv_nsec - start_time.tv_nsec);
+	scan_time_s = scan_time_ns / NSEC_PER_SEC;
+	scan_time_ns %= NSEC_PER_SEC;
+	avg_speed = map_size / (scan_time_s * USEC_PER_SEC + scan_time_ns / NSEC_PER_USEC);
+
+	printf("Total size:    %lu MB\n", map_size / MB_TO_B);
+	printf("Total time:    %ld.%06ld s\n", scan_time_s, scan_time_ns / NSEC_PER_USEC);
+	printf("Average speed: %d MB/s\n", avg_speed);
+
+	munmap(map_ptr, map_size);
+	return KSFT_PASS;
+
+err_out:
+	printf("Not OK\n");
+	munmap(map_ptr, map_size);
+	return KSFT_FAIL;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret, opt;
@@ -418,8 +468,9 @@ int main(int argc, char *argv[])
 	int test_name = CHECK_KSM_MERGE;
 	bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
 	bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
+	long size_MB = 0;
 
-	while ((opt = getopt(argc, argv, "ha:p:l:z:m:MUZN")) != -1) {
+	while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNP")) != -1) {
 		switch (opt) {
 		case 'a':
 			prot = str_to_prot(optarg);
@@ -453,6 +504,12 @@ int main(int argc, char *argv[])
 			else
 				merge_across_nodes = 1;
 			break;
+		case 's':
+			size_MB = atoi(optarg);
+			if (size_MB <= 0) {
+				printf("Size must be greater than 0\n");
+				return KSFT_FAIL;
+			}
 		case 'M':
 			break;
 		case 'U':
@@ -464,6 +521,9 @@ int main(int argc, char *argv[])
 		case 'N':
 			test_name = CHECK_KSM_NUMA_MERGE;
 			break;
+		case 'P':
+			test_name = KSM_MERGE_TIME;
+			break;
 		default:
 			return KSFT_FAIL;
 		}
@@ -505,6 +565,14 @@ int main(int argc, char *argv[])
 		ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
 					   merge_across_nodes, page_size);
 		break;
+	case KSM_MERGE_TIME:
+		if (size_MB == 0) {
+			printf("Option '-s' is required.\n");
+			return KSFT_FAIL;
+		}
+		ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
+				     size_MB);
+		break;
 	}
 
 	if (ksm_restore(&ksm_sysfs_old)) {
Signature · 1 line
2.25.1
#3 lore raw

PATCH 2/2 selftests: vm: add COW time test for KSM pages

In reply to Zhansaya Bagdauletkyzy

Since merged pages are copied every time they need to be modified, the write access time is different between shared and non-shared pages. Add ksm_cow_time() function which evaluates latency of these COW breaks. First, duplicated pages are merged and then the time required to write to each of the pages is detected.

The test is run as follows: ./ksm_tests -C -p 5000 The output:
Total COW time: 0.012612 s
The number of pages: 5000
Average speed: 1623 MB/s

---
 tools/testing/selftests/vm/ksm_tests.c | 66 ++++++++++++++++++++++++--
 1 file changed, 63 insertions(+), 3 deletions(-)
tools/testing/selftests/vm/ksm_tests.c +63 -3
index 91c6ff496655..3ffd899b2bd9 100644
@@ -33,7 +33,8 @@ enum ksm_test_name {
 	CHECK_KSM_UNMERGE,
 	CHECK_KSM_ZERO_PAGE_MERGE,
 	CHECK_KSM_NUMA_MERGE,
-	KSM_MERGE_TIME
+	KSM_MERGE_TIME,
+	KSM_COW_TIME
 };
 
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
@@ -98,7 +99,9 @@ static void print_help(void)
 	       " -U (page unmerging)\n"
 	       " -P evaluate merging time and speed.\n"
 	       "    For this test, the size of duplicated memory area (in MB)\n"
-	       "    must be provided using -s option\n\n");
+	       "    must be provided using -s option\n"
+	       " -C evaluate the time required to break COW of merged pages.\n"
+	       "    The number of pages can be defined using -p option.\n\n");
 
 	printf(" -a: specify the access protections of pages.\n"
 	       "     <prot> must be of the form [rwx].\n"
@@ -457,6 +460,56 @@ static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size)
 	return KSFT_FAIL;
 }
 
+static int ksm_cow_time(int mapping, int prot, int timeout, size_t page_size, long page_count)
+{
+	void *map_ptr;
+	struct timespec start_time, end_time;
+	long cow_time_s, cow_time_ns;
+	int avg_speed;
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+		perror("clock_gettime");
+		return KSFT_FAIL;
+	}
+
+	map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
+	if (!map_ptr)
+		return KSFT_FAIL;
+
+	if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
+		goto err_out;
+
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+		perror("clock_gettime");
+		goto err_out;
+	}
+	for (size_t i = 0; i < page_count; i++)
+		memset(map_ptr + page_size * i, '-', 1);
+	if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
+		perror("clock_gettime");
+		goto err_out;
+	}
+
+	cow_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
+		       (end_time.tv_nsec - start_time.tv_nsec);
+	cow_time_s = cow_time_ns / NSEC_PER_SEC;
+	cow_time_ns %= NSEC_PER_SEC;
+	avg_speed = (page_size * page_count) / (cow_time_s * USEC_PER_SEC +
+						cow_time_ns / NSEC_PER_USEC);
+
+	printf("Total COW time:      %ld.%06ld s\n", cow_time_s, cow_time_ns / NSEC_PER_USEC);
+	printf("The number of pages: %ld\n", page_count);
+	printf("Average speed:	     %d MB/s\n", avg_speed);
+
+	munmap(map_ptr, page_size * page_count);
+	return KSFT_PASS;
+
+err_out:
+	printf("Not OK\n");
+	munmap(map_ptr, page_size * page_count);
+	return KSFT_FAIL;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret, opt;
@@ -470,7 +523,7 @@ int main(int argc, char *argv[])
 	bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
 	long size_MB = 0;
 
-	while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNP")) != -1) {
+	while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNPC")) != -1) {
 		switch (opt) {
 		case 'a':
 			prot = str_to_prot(optarg);
@@ -524,6 +577,9 @@ int main(int argc, char *argv[])
 		case 'P':
 			test_name = KSM_MERGE_TIME;
 			break;
+		case 'C':
+			test_name = KSM_COW_TIME;
+			break;
 		default:
 			return KSFT_FAIL;
 		}
@@ -573,6 +629,10 @@ int main(int argc, char *argv[])
 		ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
 				     size_MB);
 		break;
+	case KSM_COW_TIME:
+		ret = ksm_cow_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
+				   page_size, page_count);
+		break;
 	}
 
 	if (ksm_restore(&ksm_sysfs_old)) {
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/2] selftests: vm: add KSM merging time test

In reply to Zhansaya Bagdauletkyzy
Quoted 30 lines · Zhansaya Bagdauletkyzy

Add ksm_merge_time() function to determine speed and time needed for merging. The total spent time is shown in seconds while speed is in MB/s. User must specify the size of duplicated memory area (in MB) before running the test.

The test is run as follows: ./ksm_tests -P -s 100
The output:
Total size: 100 MB
Total time: 0.309561 s
Average speed: 323 MB/s

---
 tools/testing/selftests/vm/ksm_tests.c | 76 ++++++++++++++++++++++++--
 1 file changed, 72 insertions(+), 4 deletions(-)
tools/testing/selftests/vm/ksm_tests.c +1 -0
index cdeb4a028538..91c6ff496655 100644
@@ -7,6 +7,7 @@
 #include <numa.h>
 
 #include "../kselftest.h"
+#include "../../../../include/vdso/time64.h"
 
 #define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/"
 #define KSM_FP(s) (KSM_SYSFS_PATH s)

tools/testing/selftests/vm/ksm_tests.c line 19

Quoted 4 lines
tools/testing/selftests/vm/ksm_tests.c +1 -0
@@ -15,6 +16,7 @@
 #define KSM_PROT_STR_DEFAULT "rw"
 #define KSM_USE_ZERO_PAGES_DEFAULT false
 #define KSM_MERGE_ACROSS_NODES_DEFAULT true
+#define MB_TO_B 1000000ul

We should calculate everything in true megabytes.

#define MB (1ul << 20)

Also, print in MiB/s.

Quoted 41 lines
tools/testing/selftests/vm/ksm_tests.c +8 -3
 struct ksm_sysfs {
        unsigned long max_page_sharing;
@@ -30,7 +32,8 @@ enum ksm_test_name {
        CHECK_KSM_MERGE,
        CHECK_KSM_UNMERGE,
        CHECK_KSM_ZERO_PAGE_MERGE,
-       CHECK_KSM_NUMA_MERGE
+       CHECK_KSM_NUMA_MERGE,
+       KSM_MERGE_TIME
 };
 
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
@@ -86,13 +89,16 @@ static int str_to_prot(char *prot_str)
 static void print_help(void)
 {
        printf("usage: ksm_tests [-h] <test type> [-a prot] [-p page_count] [-l timeout]\n"
-              "[-z use_zero_pages] [-m merge_across_nodes]\n");
+              "[-z use_zero_pages] [-m merge_across_nodes] [-s size]\n");
 
        printf("Supported <test type>:\n"
               " -M (page merging)\n"
               " -Z (zero pages merging)\n"
               " -N (merging of pages in different NUMA nodes)\n"
-              " -U (page unmerging)\n\n");
+              " -U (page unmerging)\n"
+              " -P evaluate merging time and speed.\n"
+              "    For this test, the size of duplicated memory area (in MB)\n"
+              "    must be provided using -s option\n\n");
 
        printf(" -a: specify the access protections of pages.\n"
               "     <prot> must be of the form [rwx].\n"
@@ -105,6 +111,7 @@ static void print_help(void)
               "     Default: %d\n", KSM_USE_ZERO_PAGES_DEFAULT);
        printf(" -m: change merge_across_nodes tunable\n"
               "     Default: %d\n", KSM_MERGE_ACROSS_NODES_DEFAULT);
+       printf(" -s: the size of duplicated memory area (in MB)\n");
 
        exit(0);
 }

tools/testing/selftests/vm/ksm_tests.c static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a · line 421

Quoted 8 lines
tools/testing/selftests/vm/ksm_tests.c +5 -0
@@ -407,6 +414,49 @@ static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a
        return KSFT_FAIL;
 }
 
+static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size)
+{
+       void *map_ptr;
+       struct timespec start_time, end_time;
+       long scan_time_s, scan_time_ns;

Should be "unsigned long".

tools/testing/selftests/vm/ksm_tests.c static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a · line 422

Quoted 1 line
tools/testing/selftests/vm/ksm_tests.c +1 -0
+       int avg_speed;

No needed, see below.

Quoted 14 lines
tools/testing/selftests/vm/ksm_tests.c +14 -0
+
+       map_size *= MB_TO_B;
+
+       map_ptr = allocate_memory(NULL, prot, mapping, '*', map_size);
+       if (!map_ptr)
+               return KSFT_FAIL;
+
+       if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+               perror("clock_gettime");
+               goto err_out;
+       }
+       if (ksm_merge_pages(map_ptr, map_size, start_time, timeout))
+               goto err_out;
+       if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {

tools/testing/selftests/vm/ksm_tests.c static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a · line 445

Quoted 10 lines
tools/testing/selftests/vm/ksm_tests.c +9 -0
+               perror("clock_gettime");
+               goto err_out;
+       }
+
+       scan_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
+                      (end_time.tv_nsec - start_time.tv_nsec);
 
+       scan_time_s = scan_time_ns / NSEC_PER_SEC;
+       scan_time_ns %= NSEC_PER_SEC;
+       avg_speed = map_size / (scan_time_s * USEC_PER_SEC + scan_time_ns / NSEC_PER_USEC);

Please remove the above three lines, and replace with bellow suggestions:

tools/testing/selftests/vm/ksm_tests.c static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a · line 448

Quoted 2 lines
tools/testing/selftests/vm/ksm_tests.c +2 -0
+
+       printf("Total size:    %lu MB\n", map_size / MB_TO_B);

printf("Total size: %lu MiB\n", map_size / MB);

tools/testing/selftests/vm/ksm_tests.c static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a · line 448

Quoted 1 line
tools/testing/selftests/vm/ksm_tests.c +1 -0
+       printf("Total time:    %ld.%06ld s\n", scan_time_s, scan_time_ns / NSEC_PER_USEC);

printf("Total time: %ld.%09ld s\n", scan_time_ns / NSEC_PER_SEC,
scan_time_ns % NSEC_PER_SEC));

tools/testing/selftests/vm/ksm_tests.c static int check_ksm_numa_merge(int mapping, int prot, int timeout, bool merge_a · line 449

Quoted 1 line
tools/testing/selftests/vm/ksm_tests.c +1 -0
+       printf("Average speed: %d MB/s\n", avg_speed);

The following would give a much better precision:
printf("Average speed: %.3f MiB/s\n", (map_size / MB) / ((double)scan_time_ns / NSEC_PER_SEC));

Quoted 65 lines
tools/testing/selftests/vm/ksm_tests.c +10 -0
+
+       munmap(map_ptr, map_size);
+       return KSFT_PASS;
+
+err_out:
+       printf("Not OK\n");
+       munmap(map_ptr, map_size);
+       return KSFT_FAIL;
+}
+
 int main(int argc, char *argv[])
 {
        int ret, opt;
tools/testing/selftests/vm/ksm_tests.c +19 -2
@@ -418,8 +468,9 @@ int main(int argc, char *argv[])
        int test_name = CHECK_KSM_MERGE;
        bool use_zero_pages = KSM_USE_ZERO_PAGES_DEFAULT;
        bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
+       long size_MB = 0;
 
-       while ((opt = getopt(argc, argv, "ha:p:l:z:m:MUZN")) != -1) {
+       while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNP")) != -1) {
                switch (opt) {
                case 'a':
                        prot = str_to_prot(optarg);
@@ -453,6 +504,12 @@ int main(int argc, char *argv[])
                        else
                                merge_across_nodes = 1;
                        break;
+               case 's':
+                       size_MB = atoi(optarg);
+                       if (size_MB <= 0) {
+                               printf("Size must be greater than 0\n");
+                               return KSFT_FAIL;
+                       }
                case 'M':
                        break;
                case 'U':
@@ -464,6 +521,9 @@ int main(int argc, char *argv[])
                case 'N':
                        test_name = CHECK_KSM_NUMA_MERGE;
                        break;
+               case 'P':
+                       test_name = KSM_MERGE_TIME;
+                       break;
                default:
                        return KSFT_FAIL;
                }
@@ -505,6 +565,14 @@ int main(int argc, char *argv[])
                ret = check_ksm_numa_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
                                           merge_across_nodes, page_size);
                break;
+       case KSM_MERGE_TIME:
+               if (size_MB == 0) {
+                       printf("Option '-s' is required.\n");
+                       return KSFT_FAIL;
+               }
+               ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
+                                    size_MB);
+               break;
        }
 
        if (ksm_restore(&ksm_sysfs_old)) {
--

2.25.1

#4 lore raw

Re: [PATCH 2/2] selftests: vm: add COW time test for KSM pages

In reply to Zhansaya Bagdauletkyzy
Quoted 6 lines · Zhansaya Bagdauletkyzy

Since merged pages are copied every time they need to be modified, the write access time is different between shared and non-shared pages. Add ksm_cow_time() function which evaluates latency of these COW breaks. First, duplicated pages are merged and then the time required to write to each of the pages is detected.

Hi Zhansaya,

This test would make more sense if we also had a baseline on how long it takes to modify unmerged pages. This way it would show the true cost of having a write on a previously merged page vs. if it was never merged.

Also, instead of having all pages merged together, I'd rather see a collection of merged pairs of pages, which are decoupled as one of the pages gets modified: i.e.
Page1 - Page2: merged
Page3 - Page4: merged
...

then, go through pages: Page2, Page4, ... and modify the first byte in each of them and measure the time it takes. Do the same when these pages are not merged, and print both numbers so the cost of KSM and COW can be calculated.

Thanks,
Pasha

Quoted 127 lines

The test is run as follows: ./ksm_tests -C -p 5000 The output:
Total COW time: 0.012612 s
The number of pages: 5000
Average speed: 1623 MB/s

---
 tools/testing/selftests/vm/ksm_tests.c | 66 ++++++++++++++++++++++++--
 1 file changed, 63 insertions(+), 3 deletions(-)
tools/testing/selftests/vm/ksm_tests.c +63 -4
index 91c6ff496655..3ffd899b2bd9 100644
@@ -33,7 +33,8 @@ enum ksm_test_name {
        CHECK_KSM_UNMERGE,
        CHECK_KSM_ZERO_PAGE_MERGE,
        CHECK_KSM_NUMA_MERGE,
-       KSM_MERGE_TIME
+       KSM_MERGE_TIME,
+       KSM_COW_TIME
 };
 
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
@@ -98,7 +99,9 @@ static void print_help(void)
               " -U (page unmerging)\n"
               " -P evaluate merging time and speed.\n"
               "    For this test, the size of duplicated memory area (in MB)\n"
-              "    must be provided using -s option\n\n");
+              "    must be provided using -s option\n"
+              " -C evaluate the time required to break COW of merged pages.\n"
+              "    The number of pages can be defined using -p option.\n\n");
 
        printf(" -a: specify the access protections of pages.\n"
               "     <prot> must be of the form [rwx].\n"
@@ -457,6 +460,56 @@ static int ksm_merge_time(int mapping, int prot, int timeout, size_t map_size)
        return KSFT_FAIL;
 }
 
+static int ksm_cow_time(int mapping, int prot, int timeout, size_t page_size, long page_count)
+{
+       void *map_ptr;
+       struct timespec start_time, end_time;
+       long cow_time_s, cow_time_ns;
+       int avg_speed;
+
+       if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+               perror("clock_gettime");
+               return KSFT_FAIL;
+       }
+
+       map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count);
+       if (!map_ptr)
+               return KSFT_FAIL;
+
+       if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout))
+               goto err_out;
+
+       if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) {
+               perror("clock_gettime");
+               goto err_out;
+       }
+       for (size_t i = 0; i < page_count; i++)
+               memset(map_ptr + page_size * i, '-', 1);
+       if (clock_gettime(CLOCK_MONOTONIC_RAW, &end_time)) {
+               perror("clock_gettime");
+               goto err_out;
+       }
+
+       cow_time_ns = (end_time.tv_sec - start_time.tv_sec) * NSEC_PER_SEC +
+                      (end_time.tv_nsec - start_time.tv_nsec);
+       cow_time_s = cow_time_ns / NSEC_PER_SEC;
+       cow_time_ns %= NSEC_PER_SEC;
+       avg_speed = (page_size * page_count) / (cow_time_s * USEC_PER_SEC +
+                                               cow_time_ns / NSEC_PER_USEC);
+
+       printf("Total COW time:      %ld.%06ld s\n", cow_time_s, cow_time_ns / NSEC_PER_USEC);
+       printf("The number of pages: %ld\n", page_count);
+       printf("Average speed:       %d MB/s\n", avg_speed);
+
+       munmap(map_ptr, page_size * page_count);
+       return KSFT_PASS;
+
+err_out:
+       printf("Not OK\n");
+       munmap(map_ptr, page_size * page_count);
+       return KSFT_FAIL;
+}
+
 int main(int argc, char *argv[])
 {
        int ret, opt;
@@ -470,7 +523,7 @@ int main(int argc, char *argv[])
        bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT;
        long size_MB = 0;
 
-       while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNP")) != -1) {
+       while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:MUZNPC")) != -1) {
                switch (opt) {
                case 'a':
                        prot = str_to_prot(optarg);
@@ -524,6 +577,9 @@ int main(int argc, char *argv[])
                case 'P':
                        test_name = KSM_MERGE_TIME;
                        break;
+               case 'C':
+                       test_name = KSM_COW_TIME;
+                       break;
                default:
                        return KSFT_FAIL;
                }
@@ -573,6 +629,10 @@ int main(int argc, char *argv[])
                ret = ksm_merge_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
                                     size_MB);
                break;
+       case KSM_COW_TIME:
+               ret = ksm_cow_time(MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec,
+                                  page_size, page_count);
+               break;
        }
 
        if (ksm_restore(&ksm_sysfs_old)) {
--

2.25.1