PAPI  5.7.0.0
attach3.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 
22 #include <limits.h>
23 #include <sys/ptrace.h>
24 #include <sys/wait.h>
25 
26 #include "papi.h"
27 #include "papi_test.h"
28 
29 #include "do_loops.h"
30 
31 
32 #ifdef _AIX
33 #define _LINUX_SOURCE_COMPAT
34 #endif
35 
36 #if defined(__FreeBSD__)
37 # define PTRACE_ATTACH PT_ATTACH
38 # define PTRACE_TRACEME PT_TRACE_ME
39 #endif
40 
41 static int
43 {
44  char *path;
45  char newpath[PATH_MAX];
46  path = getenv("PATH");
47 
48  sprintf(newpath, "PATH=./:%s", (path)?path:"\0" );
49  putenv(newpath);
50 
51  if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {
52  execlp("attach_target","attach_target","100000000",NULL);
53  perror("execl(attach_target) failed");
54  }
55  perror("PTRACE_TRACEME");
56  return ( 1 );
57 }
58 
59 int
60 main( int argc, char **argv )
61 {
62  int status, retval, tmp;
63  int EventSet1 = PAPI_NULL;
64  long long **values;
65  long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
67  const PAPI_hw_info_t *hw_info;
68  const PAPI_component_info_t *cmpinfo;
69  pid_t pid;
70  int quiet;
71 
72  /* Fork before doing anything with the PMU */
73 
74  setbuf(stdout,NULL);
75  pid = fork( );
76  if ( pid < 0 )
77  test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
78  if ( pid == 0 )
80 
81  /* Set TESTS_QUIET variable */
82  quiet=tests_quiet( argc, argv );
83 
84 
85  /* Master only process below here */
86 
88  if ( retval != PAPI_VER_CURRENT )
89  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
90 
91  if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL )
92  test_fail( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
93 
94  if ( cmpinfo->attach == 0 )
95  test_skip( __FILE__, __LINE__, "Platform does not support attaching",
96  0 );
97 
99  if ( hw_info == NULL )
100  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
101 
102  /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
103  PAPI_TOT_INS, depending on the availability of the event on the
104  platform */
106  if ( retval != PAPI_OK ) {
107  test_fail( __FILE__, __LINE__, "PAPI_attach", retval );
108  }
109 
110  /* Force addition of component */
111 
113  if ( retval != PAPI_OK )
114  test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
115  retval );
116 
117  /* The following call causes this test to fail for perf_events */
118 
119  retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
120  if ( retval != PAPI_OK ) {
121  if (!quiet) printf("Cannot attach: %s\n",PAPI_strerror(retval));
122  test_skip( __FILE__, __LINE__, "PAPI_attach", retval );
123  }
124 
125 
127  if ( retval != PAPI_OK ) {
128  if (!quiet) printf("Could not add PAPI_TOT_CYC\n");
129  test_skip( __FILE__, __LINE__, "PAPI_add_event", retval );
130  }
131 
132  strcpy(event_name,"PAPI_FP_INS");
134  if ( retval == PAPI_ENOEVNT ) {
135  strcpy(event_name,"PAPI_TOT_INS");
137  }
138 
139  if ( retval != PAPI_OK ) {
140  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
141  }
142 
143  values = allocate_test_space( 1, 2);
144 
146 
148 
149  elapsed_virt_us = PAPI_get_virt_usec( );
150 
151  elapsed_virt_cyc = PAPI_get_virt_cyc( );
152 
153  if (!quiet) printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
154  pid_t child = wait( &status );
155  if (!quiet) printf( "Debugger exited wait() with %d\n",child );
156  if (WIFSTOPPED( status ))
157  {
158  if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
159  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
160  }
161  if (WIFSIGNALED( status ))
162  {
163  if (!quiet) printf( "Child %ld received signal %d (%s)\n",
164  (long)child,
165  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
166  }
167  if (!quiet) printf("After %d\n",retval);
168 
170  if ( retval != PAPI_OK )
171  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
172 
173  if (!quiet) printf("Continuing\n");
174 #if defined(__FreeBSD__)
175  if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
176 #else
177  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
178 #endif
179  perror( "ptrace(PTRACE_CONT)" );
180  return 1;
181  }
182 
183 
184  do {
185  child = wait( &status );
186  if (!quiet) printf( "Debugger exited wait() with %d\n", child);
187  if (WIFSTOPPED( status ))
188  {
189  if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
190  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
191  }
192  if (WIFSIGNALED( status ))
193  {
194  if (!quiet) printf( "Child %ld received signal %d (%s)\n",
195  (long)child,
196  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
197  }
198  } while (!WIFEXITED( status ));
199 
200  if (!quiet) printf("Child exited with value %d\n",WEXITSTATUS(status));
201  if (WEXITSTATUS(status) != 0) {
202  test_fail( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
203  }
204  retval = PAPI_stop( EventSet1, values[0] );
205  if ( retval != PAPI_OK )
206  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
207 
208  elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
209 
210  elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
211 
213 
215 
217  if (retval != PAPI_OK)
218  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
219 
221  if (retval != PAPI_OK)
222  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
223 
224  if (!quiet) {
225  printf( "Test case: 3rd party attach start, stop.\n" );
226  printf( "-----------------------------------------------\n" );
227  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
228  printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
229  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
230  printf( "Default granularity is: %d (%s)\n", tmp,
232  printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
233  printf( "-------------------------------------------------------------------------\n" );
234 
235  printf( "Test type : \t 1\n" );
236 
237  printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
238  printf( "%s : \t %12lld\n", event_name, ( values[0] )[1] );
239  printf( TAB1, "Real usec : \t", elapsed_us );
240  printf( TAB1, "Real cycles : \t", elapsed_cyc );
241  printf( TAB1, "Virt usec : \t", elapsed_virt_us );
242  printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
243 
244  printf( "-------------------------------------------------------------------------\n" );
245 
246  printf( "Verification: none\n" );
247  }
248  test_pass( __FILE__ );
249 
250  return 0;
251 
252 }
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
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
#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
static int wait_for_attach_and_loop(void)
Definition: attach3.c:42
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 main(int argc, char **argv)
Definition: attach3.c:60
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