PAPI  5.7.0.0
attach_validate.c
Go to the documentation of this file.
1 /* This test attempts to attach to a child and makes sure we are */
2 /* getting the expected results for the child only, not local. */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <string.h>
8 #include <limits.h>
9 #include <sys/ptrace.h>
10 #include <sys/wait.h>
11 
12 #include "papi.h"
13 #include "papi_test.h"
14 
15 #include "testcode.h"
16 
17 #ifdef _AIX
18 #define _LINUX_SOURCE_COMPAT
19 #endif
20 
21 #if defined(__FreeBSD__)
22 # define PTRACE_ATTACH PT_ATTACH
23 # define PTRACE_TRACEME PT_TRACE_ME
24 #endif
25 
26 static int wait_for_attach_and_loop( int quiet ) {
27 
28  int i,result;
29 
30  if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {
31  raise(SIGSTOP);
32 
33  if (!quiet) printf("Child running 50 million instructions\n");
34 
35  /* Run 50 million instructions */
36  for(i=0;i<50;i++) {
37  result=instructions_million();
38  }
39  }
40  perror("PTRACE_TRACEME");
41  (void)result;
42 
43  return 0;
44 }
45 
46 int main( int argc, char **argv ) {
47 
48  int status, retval, tmp;
49  int EventSet1 = PAPI_NULL;
50  long long values[1];
51  const PAPI_hw_info_t *hw_info;
52  const PAPI_component_info_t *cmpinfo;
53  pid_t pid;
54  int quiet;
55  int i,result;
56 
57  /* Set TESTS_QUIET variable */
58  quiet=tests_quiet( argc, argv );
59 
60  /* Fork before doing anything with the PMU */
61  setbuf(stdout,NULL);
62  pid = fork( );
63  if ( pid < 0 ) {
64  test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
65  }
66 
67  /* If child */
68  if ( pid == 0 ) {
70  }
71 
72 
73  /* Parent process below here */
74 
76  if ( retval != PAPI_VER_CURRENT ) {
77  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
78  }
79 
80  if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL ) {
81  test_fail( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
82  }
83 
84  if ( cmpinfo->attach == 0 ) {
85  test_skip( __FILE__, __LINE__, "Platform does not support attaching",
86  0 );
87  }
88 
90  if ( hw_info == NULL ) {
91  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
92  }
93 
94  /* Create Eventset */
96  if ( retval != PAPI_OK ) {
97  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
98  }
99 
101  if ( retval != PAPI_OK ) {
102  test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
103  retval );
104  }
105 
106  /* Attach to our child */
107  retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
108  if ( retval != PAPI_OK ) {
109  if (!quiet) printf("Cannot attach: %s\n",PAPI_strerror(retval));
110  test_skip( __FILE__, __LINE__, "PAPI_attach", retval );
111  }
112 
113  /* Add instructions event */
115  if ( retval != PAPI_OK ) {
116  if (!quiet) printf("Problem adding PAPI_TOT_INS\n");
117  test_skip( __FILE__, __LINE__, "PAPI_add_event", retval );
118  }
119 
120 
121  if (!quiet) {
122  printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
123  }
124 
125  /* Wait for child to stop for debugging */
126  pid_t child = wait( &status );
127 
128  if (!quiet) printf( "Debugger exited wait() with %d\n",child );
129  if (WIFSTOPPED( status )) {
130  if (!quiet) {
131  printf( "Child has stopped due to signal %d (%s)\n",
132  WSTOPSIG( status ),
133  strsignal(WSTOPSIG( status )) );
134  }
135  }
136 
137  if (WIFSIGNALED( status )) {
138  if (!quiet) {
139  printf( "Child %ld received signal %d (%s)\n",
140  (long)child,
141  WTERMSIG(status),
142  strsignal(WTERMSIG( status )) );
143  }
144  }
145  if (!quiet) {
146  printf("After %d\n",retval);
147  }
148 
149  /* Start eventset */
151  if ( retval != PAPI_OK ) {
152  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
153  }
154 
155  if (!quiet) {
156  printf("Continuing\n");
157  }
158 
159 #if defined(__FreeBSD__)
160  if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
161  perror( "ptrace(PTRACE_CONT)" );
162  test_fail( __FILE__, __LINE__,
163  "Continuing", PAPI_EMISC);
164  return 1;
165  }
166 #else
167  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
168  perror( "ptrace(PTRACE_CONT)" );
169  test_fail( __FILE__, __LINE__,
170  "Continuing", PAPI_EMISC);
171  }
172 #endif
173 
174 
175  /* Run a billion instructions, should not appear in count */
176 
177  for(i=0;i<1000;i++) {
178  result=instructions_million();
179  }
180 
181  /* Wait for child to finish */
182  do {
183  child = wait( &status );
184  if (!quiet) {
185  printf( "Debugger exited wait() with %d\n", child);
186  }
187  if (WIFSTOPPED( status )) {
188  if (!quiet) {
189  printf( "Child has stopped due to signal "
190  "%d (%s)\n",
191  WSTOPSIG( status ),
192  strsignal(WSTOPSIG( status )) );
193  }
194  }
195  if (WIFSIGNALED( status )) {
196  if (!quiet) {
197  printf( "Child %ld received signal "
198  "%d (%s)\n",
199  (long)child,
200  WTERMSIG(status),
201  strsignal(WTERMSIG( status )) );
202  }
203  }
204  } while (!WIFEXITED( status ));
205 
206  if (!quiet) {
207  printf("Child exited with value %d\n",WEXITSTATUS(status));
208  }
209 
210  if (WEXITSTATUS(status) != 0) {
211  test_fail( __FILE__, __LINE__,
212  "Exit status of child to attach to", PAPI_EMISC);
213  }
214 
215  /* Stop counts */
216  retval = PAPI_stop( EventSet1, &values[0] );
217  if ( retval != PAPI_OK ) {
218  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
219  }
220 
222  if (retval != PAPI_OK) {
223  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
224  }
225 
227  if (retval != PAPI_OK) {
228  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
229  }
230 
231  if (!quiet) {
232  printf( "Test case: attach validation.\n" );
233  printf( "-----------------------------------------------\n" );
234  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
235  printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
236  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
237  printf( "Default granularity is: %d (%s)\n", tmp,
239  printf( "Using 50 million instructions\n");
240  printf( "-------------------------------------------------------------------------\n" );
241 
242  printf( "Test type : \t 1\n" );
243 
244  printf( TAB1, "PAPI_TOT_INS : \t", ( values[0] ) );
245 
246  printf( "-------------------------------------------------------------------------\n" );
247 
248  }
249 
250  if (values[0]<100) {
251  test_fail( __FILE__, __LINE__, "wrong result", PAPI_EMISC );
252  }
253 
254  if (values[0]>60000000) {
255  test_fail( __FILE__, __LINE__, "wrong result", PAPI_EMISC );
256  }
257 
258  (void)result;
259 
260  test_pass( __FILE__ );
261 
262  return 0;
263 
264 }
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:796
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
Hardware info structure.
Definition: papi.h:781
unsigned int attach
Definition: papi.h:663
char * stringify_granularity(int granularity)
Definition: test_utils.c:353
#define TAB1
Definition: papi_test.h:88
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int retval
Definition: zero_fork.c:53
double tmp
Return codes and api definitions.
int main(int argc, char **argv)
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
#define PAPI_ESYS
Definition: fpapi.h:108
int PAPI_library_init(int version)
Definition: papi.c:500
#define PAPI_EMISC
Definition: fpapi.h:119
#define PAPI_TOT_INS
Definition: fpapi.h:186
char * stringify_all_domains(int domains)
Definition: test_utils.c:293
int quiet
Definition: rapl_overflow.c:18
unsigned int attach_must_ptrace
Definition: papi.h:664
static int pid
int PAPI_get_opt(int option, PAPI_option_t *ptr)
Definition: papi.c:4143
int wait()
#define PAPI_NULL
Definition: fpapi.h:13
int instructions_million(void)
static int wait_for_attach_and_loop(int quiet)
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2890
int PAPI_assign_eventset_component(int EventSet, int cidx)
Definition: papi.c:1526
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
#define PAPI_DEFGRN
Definition: fpapi.h:51
#define PAPI_DEFDOM
Definition: fpapi.h:49
char * PAPI_strerror(int errorCode)
Definition: papi.c:4603
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
long long status
Definition: iozone.c:1335
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
char * caddr_t
int PAPI_attach(int EventSet, unsigned long tid)
Definition: papi.c:3201
int PAPI_start(int EventSet)
Definition: papi.c:2096
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6185
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:28
void exit()
int EventSet1
Definition: zero_fork.c:47
int i
Definition: fileop.c:140