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