1 /******************************************************************************/
2 /* */
3 /* Copyright (c) International Business Machines Corp., 2007 */
4 /* Copyright (c) Linux Test Project, 2016 */
5 /* */
6 /* This program is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* This program is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* */
19 /******************************************************************************/
20
21 /******************************************************************************/
22 /* */
23 /* File: support_numa.c */
24 /* */
25 /* Description: Allocates memory and touches it to verify numa */
26 /* */
27 /* Author: Sivakumar Chinnaiah [email protected] */
28 /* */
29 /******************************************************************************/
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include "lapi/mmap.h"
42
43 /* Global Variables */
44 #define MB (1<<20)
45 #define PAGE_SIZE getpagesize()
46 #define barrier() __asm__ __volatile__("": : :"memory")
47 #define TEST_SFILE "ltp_numa_testfile"
48 #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
49
help(void)50 static void help(void)
51 {
52 printf("Input: Describe input arguments to this program\n");
53 printf(" argv[1] == \"alloc_1MB\" then allocate 1MB of memory\n");
54 printf(" argv[1] == \"alloc_2HPSZ_THP\" then allocate 2HUGE PAGE SIZE of THP memory\n");
55 printf(" argv[1] == \"alloc_1huge_page\" then allocate 1HUGE PAGE SIZE of memory\n");
56 printf(" argv[1] == \"pause\" then pause the program to catch sigint\n");
57 printf("Exit: On failure - Exits with non-zero value\n");
58 printf(" On success - exits with 0 exit value\n");
59
60 exit(1);
61 }
62
read_hugepagesize(void)63 static int read_hugepagesize(void)
64 {
65 FILE *fp;
66 char line[BUFSIZ], buf[BUFSIZ];
67 int val;
68
69 fp = fopen("/proc/meminfo", "r");
70 if (fp == NULL) {
71 fprintf(stderr, "Failed to open /proc/meminfo");
72 return 0;
73 }
74
75 while (fgets(line, BUFSIZ, fp) != NULL) {
76 if (sscanf(line, "%64s %d", buf, &val) == 2)
77 if (strcmp(buf, "Hugepagesize:") == 0) {
78 fclose(fp);
79 return 1024 * val;
80 }
81 }
82
83 fclose(fp);
84 fprintf(stderr, "can't find \"%s\" in %s", "Hugepagesize:", "/proc/meminfo");
85
86 return 0;
87 }
88
main(int argc,char * argv[])89 int main(int argc, char *argv[])
90 {
91 int i, hpsz;
92 char *buf = NULL;
93
94 if (argc != 2) {
95 fprintf(stderr, "Here expect only one number(i.e. 2) as the parameter\n");
96 exit(1);
97 }
98
99 if (!strcmp(argv[1], "alloc_1MB")) {
100 buf = malloc(MB);
101 if (!buf) {
102 fprintf(stderr, "Memory is not available\n");
103 exit(1);
104 }
105 for (i = 0; i < MB; i += PAGE_SIZE) {
106 buf[i] = 'a';
107 barrier();
108 }
109
110 raise(SIGSTOP);
111
112 free(buf);
113 } else if (!strcmp(argv[1], "alloc_2HPSZ_THP")) {
114 ssize_t size = 2 * read_hugepagesize();
115 if (size == 0)
116 exit(1);
117
118 buf = mmap(NULL, size, PROT_READ | PROT_WRITE,
119 MAP_PRIVATE | MAP_ANONYMOUS,
120 -1, 0);
121 if (buf == MAP_FAILED) {
122 perror("mmap failed");
123 exit(1);
124 }
125
126 memset(buf, 'a', size);
127
128 raise(SIGSTOP);
129
130 munmap(buf, size);
131 } else if (!strcmp(argv[1], "alloc_1huge_page")) {
132 hpsz = read_hugepagesize();
133 if (hpsz == 0)
134 exit(1);
135
136 buf = mmap(NULL, hpsz, PROT_READ | PROT_WRITE,
137 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
138 -1, 0);
139
140 if (buf == MAP_FAILED) {
141 perror("mmap failed");
142 exit(1);
143 }
144
145 memset(buf, 'a', hpsz);
146
147 raise(SIGSTOP);
148
149 munmap(buf, hpsz);
150 } else if (!strcmp(argv[1], "pause")) {
151 raise(SIGSTOP);
152 } else {
153 help();
154 }
155
156 return 0;
157 }
158