blob: 690039ccfc8e64a0b4397d6156fb69561e703330 [file] [log] [blame]
Zhouping Liud7f19f52013-04-16 22:30:09 +08001/*
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 under mempolicy (NUMA)
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 "numa_helper.h"
50#include "test.h"
Zhouping Liud7f19f52013-04-16 22:30:09 +080051#include "mem.h"
52
53char *TCID = "thp05";
54int TST_TOTAL = 1;
55
56#if HAVE_NUMA_H && HAVE_LINUX_MEMPOLICY_H && HAVE_NUMAIF_H \
57 && HAVE_MPOL_CONSTANTS
58
59option_t thp_options[] = {
60 {"n:", &opt_nr_children, &opt_nr_children_str},
61 {"N:", &opt_nr_thps, &opt_nr_thps_str},
62 {NULL, NULL, NULL}
63};
64
65static int pre_thp_scan_sleep_millisecs;
66static int pre_thp_alloc_sleep_millisecs;
67static char pre_thp_enabled[BUFSIZ];
68
69int main(int argc, char *argv[])
70{
71 int lc;
72 char *msg;
73 int nr_children = 2, nr_thps = 64;
74
75 msg = parse_opts(argc, argv, thp_options, thp_usage);
76 if (msg != NULL)
77 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
78 check_thp_options(&nr_children, &nr_thps);
79
80 setup();
81
82 tst_resm(TINFO, "Start to test transparent hugepage...");
83 tst_resm(TINFO, "There are %d children allocating %d "
84 "transparent hugepages", nr_children, nr_thps);
85 for (lc = 0; TEST_LOOPING(lc); lc++) {
86 tst_count = 0;
87
88 tst_resm(TINFO, "THP on MPOL_BIND mempolicy...");
89 test_transparent_hugepage(nr_children, nr_thps, 1, MPOL_BIND);
90
91 tst_resm(TINFO, "THP on MPOL_INTERLEAVE mempolicy...");
92 test_transparent_hugepage(nr_children, nr_thps, 1,
93 MPOL_INTERLEAVE);
94
95 tst_resm(TINFO, "THP on MPOL_PREFERRED mempolicy...");
96 test_transparent_hugepage(nr_children, nr_thps, 1,
97 MPOL_PREFERRED);
98 }
99
100 cleanup();
101 tst_exit();
102}
103
104void setup(void)
105{
106 tst_require_root(NULL);
107
108 if (access(PATH_THP, F_OK) == -1)
109 tst_brkm(TCONF, NULL, "THP is not enabled");
110
111 if (!is_numa(NULL))
112 tst_brkm(TCONF, NULL, "The case need a NUMA system.");
113
114 SAFE_FILE_SCANF(NULL, PATH_KHPD "scan_sleep_millisecs",
115 "%d", &pre_thp_scan_sleep_millisecs);
116 SAFE_FILE_PRINTF(NULL, PATH_KHPD "scan_sleep_millisecs", "0");
117
118 SAFE_FILE_SCANF(NULL, PATH_KHPD "alloc_sleep_millisecs",
119 "%d", &pre_thp_alloc_sleep_millisecs);
120 SAFE_FILE_PRINTF(NULL, PATH_KHPD "alloc_sleep_millisecs", "0");
121
122 SAFE_FILE_SCANF(NULL, PATH_THP "enabled", "%[^\n]", pre_thp_enabled);
123 SAFE_FILE_PRINTF(NULL, PATH_THP "enabled", "always");
124
125 tst_sig(FORK, DEF_HANDLER, NULL);
126 TEST_PAUSE;
127}
128
129void cleanup(void)
130{
Li Wang8bfd7c12014-12-03 08:11:36 -0500131 FILE_PRINTF(PATH_KHPD "scan_sleep_millisecs",
Zhouping Liud7f19f52013-04-16 22:30:09 +0800132 "%d", pre_thp_scan_sleep_millisecs);
133
Li Wang8bfd7c12014-12-03 08:11:36 -0500134 FILE_PRINTF(PATH_KHPD "alloc_sleep_millisecs",
Zhouping Liud7f19f52013-04-16 22:30:09 +0800135 "%d", pre_thp_alloc_sleep_millisecs);
136
137 if (strcmp(pre_thp_enabled, "[always] madvise never") == 0)
Li Wang8bfd7c12014-12-03 08:11:36 -0500138 FILE_PRINTF(PATH_THP "enabled", "always");
Zhouping Liud7f19f52013-04-16 22:30:09 +0800139 else if (strcmp(pre_thp_enabled, "always [madvise] never") == 0)
Li Wang8bfd7c12014-12-03 08:11:36 -0500140 FILE_PRINTF(PATH_THP "enabled", "madvise");
Zhouping Liud7f19f52013-04-16 22:30:09 +0800141 else
Li Wang8bfd7c12014-12-03 08:11:36 -0500142 FILE_PRINTF(PATH_THP "enabled", "never");
Zhouping Liud7f19f52013-04-16 22:30:09 +0800143}
144
145#else /* no NUMA */
146int main(void)
147{
148 tst_brkm(TCONF, NULL, "no NUMA development packages installed.");
149}
150#endif