• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  Sivakumar.C@in.ibm.com                   */
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 <sys/mman.h>
41 #include <fcntl.h>
42 #include "lapi/mmap.h"
43 
44 /* Global Variables */
45 #define MB (1<<20)
46 #define PAGE_SIZE getpagesize()
47 #define barrier() __asm__ __volatile__("": : :"memory")
48 #define TEST_SFILE "ltp_numa_testfile"
49 #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
50 
help(void)51 static void help(void)
52 {
53 	printf("Input:	Describe input arguments to this program\n");
54 	printf("	argv[1] == \"alloc_1MB\" then allocate 1MB of memory\n");
55 	printf("	argv[1] == \"alloc_2HPSZ_THP\" then allocate 2HUGE PAGE SIZE of THP memory\n");
56 	printf("        argv[1] == \"alloc_1huge_page\" then allocate 1HUGE PAGE SIZE of memory\n");
57 	printf("        argv[1] == \"pause\" then pause the program to catch sigint\n");
58 	printf("Exit:	On failure - Exits with non-zero value\n");
59 	printf("	On success - exits with 0 exit value\n");
60 
61 	exit(1);
62 }
63 
read_hugepagesize(void)64 static int read_hugepagesize(void)
65 {
66 	FILE *fp;
67 	char line[BUFSIZ], buf[BUFSIZ];
68 	int val;
69 
70 	fp = fopen("/proc/meminfo", "r");
71 	if (fp == NULL) {
72 		fprintf(stderr, "Failed to open /proc/meminfo");
73 		return 0;
74 	}
75 
76 	while (fgets(line, BUFSIZ, fp) != NULL) {
77 		if (sscanf(line, "%64s %d", buf, &val) == 2)
78 			if (strcmp(buf, "Hugepagesize:") == 0) {
79 				fclose(fp);
80 				return 1024 * val;
81 			}
82 	}
83 
84 	fclose(fp);
85 	fprintf(stderr, "can't find \"%s\" in %s", "Hugepagesize:", "/proc/meminfo");
86 
87 	return 0;
88 }
89 
main(int argc,char * argv[])90 int main(int argc, char *argv[])
91 {
92 	int i, hpsz;
93 	char *buf = NULL;
94 
95 	if (argc != 2) {
96 		fprintf(stderr, "Here expect only one number(i.e. 2) as the parameter\n");
97 		exit(1);
98 	}
99 
100 	if (!strcmp(argv[1], "alloc_1MB")) {
101 		buf = malloc(MB);
102 		if (!buf) {
103 			fprintf(stderr, "Memory is not available\n");
104 			exit(1);
105 		}
106 		for (i = 0; i < MB; i += PAGE_SIZE) {
107 			buf[i] = 'a';
108 			barrier();
109 		}
110 
111 		raise(SIGSTOP);
112 
113 		free(buf);
114 	} else if (!strcmp(argv[1], "alloc_2HPSZ_THP")) {
115 		ssize_t size = 2 * read_hugepagesize();
116 		if (size == 0)
117 			exit(1);
118 
119 		buf = mmap(NULL, size, PROT_READ | PROT_WRITE,
120 				MAP_PRIVATE | MAP_ANONYMOUS,
121 				-1, 0);
122 		if (buf == MAP_FAILED) {
123 			perror("mmap failed");
124 			exit(1);
125 		}
126 
127 		memset(buf, 'a', size);
128 
129 		raise(SIGSTOP);
130 
131 		munmap(buf, size);
132 	} else if (!strcmp(argv[1], "alloc_1huge_page")) {
133 		hpsz = read_hugepagesize();
134 		if (hpsz == 0)
135 			exit(1);
136 
137 		buf = mmap(NULL, hpsz, PROT_READ | PROT_WRITE,
138 				MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
139 				-1, 0);
140 
141 		if (buf == MAP_FAILED) {
142 			perror("mmap failed");
143 			exit(1);
144 		}
145 
146 		memset(buf, 'a', hpsz);
147 
148 		raise(SIGSTOP);
149 
150 		munmap(buf, hpsz);
151 	} else if (!strcmp(argv[1], "pause")) {
152 		raise(SIGSTOP);
153 	} else {
154 		help();
155 	}
156 
157 	return 0;
158 }
159