1 /******************************************************************************/
2 /*                                                                            */
3 /* Copyright (c) International Business Machines  Corp., 2007                 */
4 /*                                                                            */
5 /* This program is free software;  you can redistribute it and/or modify      */
6 /* it under the terms of the GNU General Public License as published by       */
7 /* the Free Software Foundation; either version 2 of the License, or          */
8 /* (at your option) any later version.                                        */
9 /*                                                                            */
10 /* This program is distributed in the hope that it will be useful,            */
11 /* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
13 /* the GNU General Public License for more details.                           */
14 /*                                                                            */
15 /* You should have received a copy of the GNU General Public License          */
16 /* along with this program;  if not, write to the Free Software               */
17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
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 /* History:     Created - Jul 18 2007 - Sivakumar Chinnaiah                   */
30 /*                                                 Sivakumar.C@in.ibm.com     */
31 /*                                                                            */
32 /******************************************************************************/
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <signal.h>
39 #include <limits.h>
40 #include <string.h>
41 #include "test.h"
42 
43 /* Global Variables */
44 #define MB (1<<20)
45 #define PAGE_SIZE getpagesize()
46 #define barrier() __asm__ __volatile__("": : :"memory")
47 
48 /* Extern Global Variables */
49 extern int tst_count;		/* to avoid compilation errors. */
50 extern char *TESTDIR;		/* to avoid compilation errors. */
51 
52 /* Global Variables */
53 char *TCID = "support_numa";	/* to avoid compilation errors. */
54 int TST_TOTAL = 1;		/* to avoid compilation errors. */
55 
sigfunc(int sig)56 void sigfunc(int sig)
57 {
58 	tst_resm(TINFO, "#Caught signal signum=%d", sig);
59 }
60 
61 /******************************************************************************/
62 /*                                                                            */
63 /* Function:    main                                                          */
64 /*                                                                            */
65 /* Description: Alloctes 1MB of memory and touches it to verify numa behaviour*/
66 /*                                                                            */
67 /* Input:       Describe input arguments to this program                      */
68 /*               argv[1] ==1 then print pagesize                              */
69 /*               argv[1] ==2 then allocate 1MB of memory                      */
70 /*		 argv[1] ==3 then pause the program to catch sigint	      */
71 /*                                                                            */
72 /* Exit:       On failure - Exits with non-zero value.                        */
73 /*             On success - exits with 0 exit value.                          */
74 /*                                                                            */
75 /******************************************************************************/
76 
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 	int i;
80 	char *buf = NULL;
81 	int count = 0;
82 	struct sigaction sa;
83 
84 	switch (atoi(argv[1])) {
85 	case 1:
86 		printf("%d", PAGE_SIZE);
87 		tst_exit();
88 	case 2:
89 		buf = malloc(MB);
90 		if (!buf) {
91 			tst_resm(TINFO, "#Memory is not available\n");
92 			tst_exit();
93 			exit(2);
94 		}
95 		for (i = 0; i < MB; i += PAGE_SIZE) {
96 			count++;
97 			buf[i] = 'a';
98 			barrier();
99 		}
100 		free(buf);
101 		tst_exit();
102 	case 3:
103 		/* Trap SIGINT */
104 		sa.sa_handler = sigfunc;
105 		sa.sa_flags = SA_RESTART;
106 		sigemptyset(&sa.sa_mask);
107 		if (sigaction(SIGINT, &sa, 0) < 0) {
108 			tst_brkm(TBROK, NULL, "#Sigaction SIGINT failed\n");
109 			tst_exit();
110 			exit(1);
111 		}
112 		/* wait for signat Int */
113 		pause();
114 		tst_exit();
115 	default:
116 		exit(1);
117 	}
118 }
119