PAPI  5.7.0.0
attach2.c
Go to the documentation of this file.
1 /* This file performs the following test: start, stop and timer functionality for
2  attached processes.
3 
4  - It attempts to use the following two counters. It may use less depending on
5  hardware counter resource limitations. These are counted in the default counting
6  domain and default granularity, depending on the platform. Usually this is
7  the user domain (PAPI_DOM_USER) and thread context (PAPI_GRN_THR).
8  + PAPI_FP_INS
9  + PAPI_TOT_CYC
10  - Get us.
11  - Start counters
12  - Do flops
13  - Stop and read counters
14  - Get us.
15 */
16 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <sys/ptrace.h>
23 #include <sys/wait.h>
24 
25 #include "papi.h"
26 #include "papi_test.h"
27 
28 #include "do_loops.h"
29 
30 
31 #ifdef _AIX
32 #define _LINUX_SOURCE_COMPAT
33 #endif
34 
35 #if defined(__FreeBSD__)
36 # define PTRACE_ATTACH PT_ATTACH
37 # define PTRACE_TRACEME PT_TRACE_ME
38 #endif
39 
40 static int
42 {
43  char *path;
44  char newpath[PATH_MAX];
45  path = getenv("PATH");
46 
47  sprintf(newpath, "PATH=./:%s", (path)?path:"\0" );
48  putenv(newpath);
49 
50  if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {
51  execlp("attach_target","attach_target","100000000",NULL);
52  perror("execl(attach_target) failed");
53  }
54  perror("PTRACE_TRACEME");
55  return ( 1 );
56 }
57 
58 int
59 main( int argc, char **argv )
60 {
61  int status, retval, tmp;
62  int EventSet1 = PAPI_NULL;
63  long long **values;
64  long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
66  const PAPI_hw_info_t *hw_info;
67  const PAPI_component_info_t *cmpinfo;
68  pid_t pid;
69  int quiet;
70 
71  /* Fork before doing anything with the PMU */
72 
73  setbuf(stdout,NULL);
74  pid = fork( );
75  if ( pid < 0 )
76  test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
77  if ( pid == 0 )
79 
80  /* Set TESTS_QUIET variable */
81  quiet=tests_quiet( argc, argv );
82 
83 
84  /* Master only process below here */
85 
87  if ( retval != PAPI_VER_CURRENT )
88  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
89 
90  if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL )
91  test_fail( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
92 
93  if ( cmpinfo->attach == 0 )
94  test_skip( __FILE__, __LINE__, "Platform does not support attaching",
95  0 );
96 
98  if ( hw_info == NULL )
99  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
100 
101  /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
102  PAPI_TOT_INS, depending on the availability of the event on the
103  platform */
105  if ( retval != PAPI_OK )
106  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
107 
108  /* Here we are testing that this does not cause a fail */
109 
111  if ( retval != PAPI_OK )
112  test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
113  retval );
114 
115  retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
116  if ( retval != PAPI_OK ) {
117  if (!quiet) printf("Cannot attach: %s\n",PAPI_strerror(retval));
118  test_skip( __FILE__, __LINE__, "PAPI_attach", retval );
119  }
120 
122  if ( retval != PAPI_OK ) {
123  if (!quiet) printf("Problem adding PAPI_TOT_CYC\n");
124  test_skip( __FILE__, __LINE__, "PAPI_add_event", retval );
125  }
126 
127  strcpy(event_name,"PAPI_FP_INS");
128 
130  if ( retval == PAPI_ENOEVNT ) {
131  strcpy(event_name,"PAPI_TOT_INS");
133  }
134 
135  if ( retval != PAPI_OK ) {
136  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
137  }
138 
139  values = allocate_test_space( 1, 2);
140 
142 
144 
145  elapsed_virt_us = PAPI_get_virt_usec( );
146 
147  elapsed_virt_cyc = PAPI_get_virt_cyc( );
148 
149  if (!quiet) printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
150  pid_t child = wait( &status );
151  if (!quiet) printf( "Debugger exited wait() with %d\n",child );
152  if (WIFSTOPPED( status ))
153  {
154  if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
155  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
156  }
157  if (WIFSIGNALED( status ))
158  {
159  if (!quiet) printf( "Child %ld received signal %d (%s)\n",
160  (long)child,
161  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
162  }
163  if (!quiet) printf("After %d\n",retval);
164 
166  if ( retval != PAPI_OK )
167  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
168 
169  if (!quiet) printf("Continuing\n");
170 #if defined(__FreeBSD__)
171  if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
172 #else
173  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
174 #endif
175  perror( "ptrace(PTRACE_CONT)" );
176  return 1;
177  }
178 
179 
180  do {
181  child = wait( &status );
182  if (!quiet) printf( "Debugger exited wait() with %d\n", child);
183  if (WIFSTOPPED( status ))
184  {
185  if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
186  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
187  }
188  if (WIFSIGNALED( status ))
189  {
190  if (!quiet) printf( "Child %ld received signal %d (%s)\n",
191  (long)child,
192  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
193  }
194  } while (!WIFEXITED( status ));
195 
196  if (!quiet) printf("Child exited with value %d\n",WEXITSTATUS(status));
197  if (WEXITSTATUS(status) != 0) {
198  test_fail( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
199  }
200  retval = PAPI_stop( EventSet1, values[0] );
201  if ( retval != PAPI_OK )
202  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
203 
204  elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
205 
206  elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
207 
209 
211 
213  if (retval != PAPI_OK)
214  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
215 
217  if (retval != PAPI_OK)
218  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
219 
220  if (!quiet) {
221  printf( "Test case: 3rd party attach start, stop.\n" );
222  printf( "-----------------------------------------------\n" );
223  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
224  printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
225  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
226  printf( "Default granularity is: %d (%s)\n", tmp,
228  printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
229  printf( "-------------------------------------------------------------------------\n" );
230 
231  printf( "Test type : \t 1\n" );
232 
233  printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
234  printf( "%s : \t %12lld\n",event_name, ( values[0] )[1]);
235  printf( TAB1, "Real usec : \t", elapsed_us );
236  printf( TAB1, "Real cycles : \t", elapsed_cyc );
237  printf( TAB1, "Virt usec : \t", elapsed_virt_us );
238  printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
239 
240  printf( "-------------------------------------------------------------------------\n" );
241 
242  printf( "Verification: none\n" );
243  }
244 
245  test_pass( __FILE__ );
246 
247  return 0;
248 
249 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#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
char * getenv()
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
long long PAPI_get_virt_usec(void)
Definition: papi.c:6372
static int wait_for_attach_and_loop(void)
Definition: attach2.c:41
unsigned int attach
Definition: papi.h:663
char * stringify_granularity(int granularity)
Definition: test_utils.c:353
long long PAPI_get_virt_cyc(void)
Definition: papi.c:6300
#define TAB1
Definition: papi_test.h:88
int main(int argc, char **argv)
Definition: attach2.c:59
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int retval
Definition: zero_fork.c:53
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
double tmp
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
#define PAPI_ESYS
Definition: fpapi.h:108
int PAPI_library_init(int version)
Definition: papi.c:500
#define PAPI_EMISC
Definition: fpapi.h:119
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
long long elapsed_cyc
Definition: zero_fork.c:50
int wait()
#define PAPI_NULL
Definition: fpapi.h:13
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
long long PAPI_get_real_usec(void)
Definition: papi.c:6264
#define PATH_MAX
Definition: fileop.c:68
#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
#define PAPI_ENOEVNT
Definition: fpapi.h:112
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
long long PAPI_get_real_cyc(void)
Definition: papi.c:6217
int PAPI_start(int EventSet)
Definition: papi.c:2096
#define PAPI_TOT_CYC
Definition: fpapi.h:195
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
long long elapsed_us
Definition: zero_fork.c:50
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:46
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:28
void exit()
int EventSet1
Definition: zero_fork.c:47
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43