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 1MB of 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
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] == 1 then allocate 1MB of memory\n");
54 printf(" argv[1] == 2 then allocate 1MB of share memory\n");
55 printf(" argv[1] == 3 then pause the program to catch sigint\n");
56 printf("Exit: On failure - Exits with non-zero value\n");
57 printf(" On success - exits with 0 exit value\n");
58
59 exit(1);
60 }
61
main(int argc,char * argv[])62 int main(int argc, char *argv[])
63 {
64 int i, fd, rc;
65 char *buf = NULL;
66 struct stat sb;
67
68 if (argc != 2) {
69 fprintf(stderr, "Here expect only one number(i.e. 2) as the parameter\n");
70 exit(1);
71 }
72
73 switch (atoi(argv[1])) {
74 case 1:
75 buf = malloc(MB);
76 if (!buf) {
77 fprintf(stderr, "Memory is not available\n");
78 exit(1);
79 }
80 for (i = 0; i < MB; i += PAGE_SIZE) {
81 buf[i] = 'a';
82 barrier();
83 }
84
85 raise(SIGSTOP);
86
87 free(buf);
88 break;
89 case 2:
90 fd = open(TEST_SFILE, O_RDWR | O_CREAT, 0666);
91 /* Writing 1MB of random data into this file [32 * 32768 = 1024 * 1024] */
92 for (i = 0; i < 32768; i++){
93 rc = write(fd, STR, strlen(STR));
94 if (rc == -1 || ((size_t)rc != strlen(STR)))
95 fprintf(stderr, "write failed\n");
96 }
97
98 if ((fstat(fd, &sb)) == -1)
99 fprintf(stderr, "fstat failed\n");
100
101 buf = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
102 if (buf == MAP_FAILED){
103 fprintf(stderr, "mmap failed\n");
104 close(fd);
105 exit(1);
106 }
107
108 memset(buf, 'a', sb.st_size);
109
110 raise(SIGSTOP);
111
112 munmap(buf, sb.st_size);
113 close(fd);
114 remove(TEST_SFILE);
115 break;
116 case 3:
117 raise(SIGSTOP);
118 break;
119 default:
120 help();
121 }
122
123 return 0;
124 }
125