1616#include <string.h>
1717#include <sys/mman.h>
1818#include "vm_util.h"
19+ #include "../kselftest.h"
1920
2021int backing_fd = -1 ;
2122int mmap_flags = MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE ;
@@ -34,6 +35,8 @@ int main(int argc, char **argv)
3435 int pagemap_fd ;
3536 int duration = 0 ;
3637
38+ ksft_print_header ();
39+
3740 ram = sysconf (_SC_PHYS_PAGES );
3841 if (ram > SIZE_MAX / psize () / 4 )
3942 ram = SIZE_MAX / 4 ;
@@ -43,7 +46,8 @@ int main(int argc, char **argv)
4346
4447 while (++ i < argc ) {
4548 if (!strcmp (argv [i ], "-h" ))
46- errx (1 , "usage: %s [-f <filename>] [-d <duration>] [size in MiB]" , argv [0 ]);
49+ ksft_exit_fail_msg ("usage: %s [-f <filename>] [-d <duration>] [size in MiB]\n" ,
50+ argv [0 ]);
4751 else if (!strcmp (argv [i ], "-f" ))
4852 name = argv [++ i ];
4953 else if (!strcmp (argv [i ], "-d" ))
@@ -52,10 +56,12 @@ int main(int argc, char **argv)
5256 len = atoll (argv [i ]) << 20 ;
5357 }
5458
59+ ksft_set_plan (1 );
60+
5561 if (name ) {
5662 backing_fd = open (name , O_RDWR );
5763 if (backing_fd == -1 )
58- errx ( 2 , "open %s" , name );
64+ ksft_exit_fail_msg ( "open %s\n " , name );
5965 mmap_flags = MAP_SHARED ;
6066 }
6167
@@ -65,21 +71,21 @@ int main(int argc, char **argv)
6571
6672 pagemap_fd = open ("/proc/self/pagemap" , O_RDONLY );
6773 if (pagemap_fd < 0 )
68- err ( 2 , "open pagemap" );
74+ ksft_exit_fail_msg ( "open pagemap\n " );
6975
7076 len -= len % HPAGE_SIZE ;
7177 ptr = mmap (NULL , len + HPAGE_SIZE , PROT_RW , mmap_flags , backing_fd , 0 );
7278 if (ptr == MAP_FAILED )
73- err ( 2 , "initial mmap" );
79+ ksft_exit_fail_msg ( "initial mmap" );
7480 ptr += HPAGE_SIZE - (uintptr_t )ptr % HPAGE_SIZE ;
7581
7682 if (madvise (ptr , len , MADV_HUGEPAGE ))
77- err ( 2 , "MADV_HUGEPAGE" );
83+ ksft_exit_fail_msg ( "MADV_HUGEPAGE" );
7884
7985 map_len = ram >> (HPAGE_SHIFT - 1 );
8086 map = malloc (map_len );
8187 if (!map )
82- errx ( 2 , "map malloc" );
88+ ksft_exit_fail_msg ( "map malloc\n " );
8389
8490 clock_gettime (CLOCK_MONOTONIC , & start );
8591
@@ -103,7 +109,7 @@ int main(int argc, char **argv)
103109 if (idx >= map_len ) {
104110 map = realloc (map , idx + 1 );
105111 if (!map )
106- errx ( 2 , "map realloc" );
112+ ksft_exit_fail_msg ( "map realloc\n " );
107113 memset (map + map_len , 0 , idx + 1 - map_len );
108114 map_len = idx + 1 ;
109115 }
@@ -114,17 +120,19 @@ int main(int argc, char **argv)
114120
115121 /* split transhuge page, keep last page */
116122 if (madvise (p , HPAGE_SIZE - psize (), MADV_DONTNEED ))
117- err ( 2 , "MADV_DONTNEED" );
123+ ksft_exit_fail_msg ( "MADV_DONTNEED" );
118124 }
119125 clock_gettime (CLOCK_MONOTONIC , & b );
120126 s = b .tv_sec - a .tv_sec + (b .tv_nsec - a .tv_nsec ) / 1000000000. ;
121127
122- warnx ("%.3f s/loop, %.3f ms/page, %10.3f MiB/s\t"
123- "%4d succeed, %4d failed, %4d different pages" ,
124- s , s * 1000 / (len >> HPAGE_SHIFT ), len / s / (1 << 20 ),
125- nr_succeed , nr_failed , nr_pages );
128+ ksft_print_msg ("%.3f s/loop, %.3f ms/page, %10.3f MiB/s\t"
129+ "%4d succeed, %4d failed, %4d different pages\n " ,
130+ s , s * 1000 / (len >> HPAGE_SHIFT ), len / s / (1 << 20 ),
131+ nr_succeed , nr_failed , nr_pages );
126132
127- if (duration > 0 && b .tv_sec - start .tv_sec >= duration )
128- return 0 ;
133+ if (duration > 0 && b .tv_sec - start .tv_sec >= duration ) {
134+ ksft_test_result_pass ("Completed\n" );
135+ ksft_finished ();
136+ }
129137 }
130138}
0 commit comments