PAPI  5.6.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
97  hw_info = PAPI_get_hardware_info( );
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 */
104  retval = PAPI_create_eventset(&EventSet1);
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 
110  retval = PAPI_assign_eventset_component( EventSet1, 0 );
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  test_fail( __FILE__, __LINE__, "PAPI_attach", retval );
118 
119  retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC);
120  if ( retval != PAPI_OK ) {
121  if (!quiet) printf("Problem adding PAPI_TOT_CYC\n");
122  test_skip( __FILE__, __LINE__, "PAPI_add_event", retval );
123  }
124 
125  strcpy(event_name,"PAPI_FP_INS");
126 
127  retval = PAPI_add_named_event(EventSet1, event_name);
128  if ( retval == PAPI_ENOEVNT ) {
129  strcpy(event_name,"PAPI_TOT_INS");
130  retval = PAPI_add_named_event(EventSet1, event_name);
131  }
132 
133  if ( retval != PAPI_OK ) {
134  test_fail( __FILE__, __LINE__, "PAPI_add_event", retval );
135  }
136 
137  values = allocate_test_space( 1, 2);
138 
139  elapsed_us = PAPI_get_real_usec( );
140 
141  elapsed_cyc = PAPI_get_real_cyc( );
142 
143  elapsed_virt_us = PAPI_get_virt_usec( );
144 
145  elapsed_virt_cyc = PAPI_get_virt_cyc( );
146 
147  if (!quiet) printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
148  pid_t child = wait( &status );
149  if (!quiet) printf( "Debugger exited wait() with %d\n",child );
150  if (WIFSTOPPED( status ))
151  {
152  if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
153  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
154  }
155  if (WIFSIGNALED( status ))
156  {
157  if (!quiet) printf( "Child %ld received signal %d (%s)\n",
158  (long)child,
159  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
160  }
161  if (!quiet) printf("After %d\n",retval);
162 
163  retval = PAPI_start( EventSet1 );
164  if ( retval != PAPI_OK )
165  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
166 
167  if (!quiet) printf("Continuing\n");
168 #if defined(__FreeBSD__)
169  if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
170 #else
171  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
172 #endif
173  perror( "ptrace(PTRACE_CONT)" );
174  return 1;
175  }
176 
177 
178  do {
179  child = wait( &status );
180  if (!quiet) printf( "Debugger exited wait() with %d\n", child);
181  if (WIFSTOPPED( status ))
182  {
183  if (!quiet) printf( "Child has stopped due to signal %d (%s)\n",
184  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
185  }
186  if (WIFSIGNALED( status ))
187  {
188  if (!quiet) printf( "Child %ld received signal %d (%s)\n",
189  (long)child,
190  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
191  }
192  } while (!WIFEXITED( status ));
193 
194  if (!quiet) printf("Child exited with value %d\n",WEXITSTATUS(status));
195  if (WEXITSTATUS(status) != 0) {
196  test_fail( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
197  }
198  retval = PAPI_stop( EventSet1, values[0] );
199  if ( retval != PAPI_OK )
200  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
201 
202  elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
203 
204  elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
205 
206  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
207 
208  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
209 
210  retval = PAPI_cleanup_eventset(EventSet1);
211  if (retval != PAPI_OK)
212  test_fail( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
213 
214  retval = PAPI_destroy_eventset(&EventSet1);
215  if (retval != PAPI_OK)
216  test_fail( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
217 
218  if (!quiet) {
219  printf( "Test case: 3rd party attach start, stop.\n" );
220  printf( "-----------------------------------------------\n" );
221  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
222  printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
223  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
224  printf( "Default granularity is: %d (%s)\n", tmp,
225  stringify_granularity( tmp ) );
226  printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
227  printf( "-------------------------------------------------------------------------\n" );
228 
229  printf( "Test type : \t 1\n" );
230 
231  printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
232  printf( "%s : \t %12lld\n",event_name, ( values[0] )[1]);
233  printf( TAB1, "Real usec : \t", elapsed_us );
234  printf( TAB1, "Real cycles : \t", elapsed_cyc );
235  printf( TAB1, "Virt usec : \t", elapsed_virt_us );
236  printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
237 
238  printf( "-------------------------------------------------------------------------\n" );
239 
240  printf( "Verification: none\n" );
241  }
242 
243  test_pass( __FILE__ );
244 
245  return 0;
246 
247 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#define PAPI_ENOEVNT
Definition: papi.h:260
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
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:780
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:662
char * stringify_granularity(int granularity)
Definition: test_utils.c:353
long long PAPI_get_virt_cyc(void)
Definition: papi.c:6300
#define PAPI_NULL
Definition: papi.h:292
#define TAB1
Definition: papi_test.h:88
return PAPI_OK
Definition: linux-nvml.c:497
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:559
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
char ** argv
Definition: iozone.c:1610
#define PAPI_EMISC
Definition: papi.h:267
int PAPI_library_init(int version)
Definition: papi.c:500
char * stringify_all_domains(int domains)
Definition: test_utils.c:293
int quiet
Definition: rapl_overflow.c:18
#define PAPI_TOT_CYC
unsigned int attach_must_ptrace
Definition: papi.h:663
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_ESYS
Definition: papi.h:255
#define PAPI_DEFGRN
Definition: papi.h:434
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
strcpy(filename, default_filename)
long long PAPI_get_real_usec(void)
Definition: papi.c:6264
printf("\tTry: -i 0 -i 1 \n\n")
long long status
Definition: iozone.c:1335
#define PATH_MAX
Definition: fileop.c:68
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
#define PAPI_DEFDOM
Definition: papi.h:432
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:2014
#define PAPI_MAX_STR_LEN
Definition: papi.h:465
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
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
ssize_t retval
Definition: libasync.c:338
long long tmp
Definition: iozone.c:12031
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
#define PAPI_VER_CURRENT
Definition: papi.h:225
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:28
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int EventSet1
Definition: zero_fork.c:47