Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Linux Test Project |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * Further, this software is distributed without any warranty that it |
| 13 | * is free of the rightful claim of any third person regarding |
| 14 | * infringement or the like. Any license provided herein, whether |
| 15 | * implied or otherwise, applies only to this software file. Patent |
| 16 | * licenses, if any, provided herein do not apply to combinations of |
| 17 | * this program with other software, or any other product whatsoever. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 22 | * 02110-1301, USA. |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * The case is designed to test the functionality of transparent |
| 27 | * hugepage - THP |
| 28 | * |
| 29 | * when one process allocate hugepage aligned anonymous pages, |
| 30 | * kernel thread 'khugepaged' controlled by sysfs knobs |
| 31 | * /sys/kernel/mm/transparent_hugepage/ will scan them, and make |
| 32 | * them as transparent hugepage if they are suited, you can find out |
| 33 | * how many transparent hugepages are there in one process from |
| 34 | * /proc/<pid>/smaps, among the file contents, 'AnonHugePages' entry |
| 35 | * stand for transparent hugepage. |
| 36 | */ |
| 37 | |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <sys/wait.h> |
| 42 | #include <errno.h> |
| 43 | #include <fcntl.h> |
| 44 | #include <signal.h> |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <string.h> |
| 48 | #include <unistd.h> |
| 49 | #include "test.h" |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 50 | #include "mem.h" |
| 51 | |
| 52 | char *TCID = "thp04"; |
| 53 | int TST_TOTAL = 1; |
| 54 | |
| 55 | option_t thp_options[] = { |
| 56 | {"n:", &opt_nr_children, &opt_nr_children_str}, |
| 57 | {"N:", &opt_nr_thps, &opt_nr_thps_str}, |
| 58 | {NULL, NULL, NULL} |
| 59 | }; |
| 60 | |
| 61 | static int pre_thp_scan_sleep_millisecs; |
| 62 | static int pre_thp_alloc_sleep_millisecs; |
| 63 | static char pre_thp_enabled[BUFSIZ]; |
| 64 | |
| 65 | int main(int argc, char *argv[]) |
| 66 | { |
| 67 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 68 | const char *msg; |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 69 | int nr_children = 2, nr_thps = 64; |
| 70 | |
| 71 | msg = parse_opts(argc, argv, thp_options, thp_usage); |
| 72 | if (msg != NULL) |
| 73 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 74 | check_thp_options(&nr_children, &nr_thps); |
| 75 | |
| 76 | setup(); |
| 77 | |
| 78 | tst_resm(TINFO, "Start to test transparent hugepage..."); |
| 79 | tst_resm(TINFO, "There are %d children allocating %d " |
| 80 | "transparent hugepages", nr_children, nr_thps); |
| 81 | |
| 82 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 83 | tst_count = 0; |
| 84 | |
Zhouping Liu | d6b67f8 | 2013-04-16 22:30:08 +0800 | [diff] [blame] | 85 | test_transparent_hugepage(nr_children, nr_thps, 1, 0); |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | cleanup(); |
| 89 | tst_exit(); |
| 90 | } |
| 91 | |
| 92 | void setup(void) |
| 93 | { |
| 94 | tst_require_root(NULL); |
| 95 | |
| 96 | if (access(PATH_THP, F_OK) == -1) |
| 97 | tst_brkm(TCONF, NULL, "THP is not enabled"); |
| 98 | |
| 99 | SAFE_FILE_SCANF(NULL, PATH_KHPD "scan_sleep_millisecs", |
| 100 | "%d", &pre_thp_scan_sleep_millisecs); |
| 101 | /* set 0 to khugepaged/scan_sleep_millisecs to run khugepaged 100% */ |
| 102 | SAFE_FILE_PRINTF(NULL, PATH_KHPD "scan_sleep_millisecs", "0"); |
| 103 | |
| 104 | SAFE_FILE_SCANF(NULL, PATH_KHPD "alloc_sleep_millisecs", |
| 105 | "%d", &pre_thp_alloc_sleep_millisecs); |
| 106 | /* |
| 107 | * set 0 to khugepaged/alloc_sleep_millisecs to make sure khugepaged |
| 108 | * don't stop if there's a hugepage allcation failure. |
| 109 | */ |
| 110 | SAFE_FILE_PRINTF(NULL, PATH_KHPD "alloc_sleep_millisecs", "0"); |
| 111 | |
| 112 | SAFE_FILE_SCANF(NULL, PATH_THP "enabled", "%[^\n]", pre_thp_enabled); |
| 113 | /* open khugepaged as 'always' mode */ |
| 114 | SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "always"); |
| 115 | |
| 116 | tst_sig(FORK, DEF_HANDLER, NULL); |
| 117 | TEST_PAUSE; |
| 118 | } |
| 119 | |
| 120 | void cleanup(void) |
| 121 | { |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 122 | FILE_PRINTF(PATH_KHPD "scan_sleep_millisecs", |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 123 | "%d", pre_thp_scan_sleep_millisecs); |
| 124 | |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 125 | FILE_PRINTF(PATH_KHPD "alloc_sleep_millisecs", |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 126 | "%d", pre_thp_alloc_sleep_millisecs); |
| 127 | |
| 128 | /* |
| 129 | * The value of transparent_hugepage/enabled is speical, |
| 130 | * we need to recover the previous value one by one for |
| 131 | * the three mode: always, madvise, never. |
| 132 | */ |
| 133 | if (strcmp(pre_thp_enabled, "[always] madvise never") == 0) |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 134 | FILE_PRINTF(PATH_THP "enabled", "always"); |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 135 | else if (strcmp(pre_thp_enabled, "always [madvise] never") == 0) |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 136 | FILE_PRINTF(PATH_THP "enabled", "madvise"); |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 137 | else |
Li Wang | 8bfd7c1 | 2014-12-03 08:11:36 -0500 | [diff] [blame] | 138 | FILE_PRINTF(PATH_THP "enabled", "never"); |
Zhouping Liu | 0b34193 | 2013-04-16 22:30:07 +0800 | [diff] [blame] | 139 | } |