PAPI  5.3.2.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 "papi_test.h"
18 #include <sys/ptrace.h>
19 #include <limits.h>
20 
21 #ifdef _AIX
22 #define _LINUX_SOURCE_COMPAT
23 #endif
24 
25 #if defined(__FreeBSD__)
26 # define PTRACE_ATTACH PT_ATTACH
27 # define PTRACE_TRACEME PT_TRACE_ME
28 #endif
29 
30 int
32 {
33  char *path;
34  char newpath[PATH_MAX];
35  path = getenv("PATH");
36 
37  sprintf(newpath, "PATH=./:%s", (path)?path:'\0' );
38  putenv(newpath);
39 
40  if (ptrace(PTRACE_TRACEME, 0, 0, 0) == 0) {
41  execlp("attach_target","attach_target","100000000",NULL);
42  perror("execl(attach_target) failed");
43  }
44  perror("PTRACE_TRACEME");
45  return ( 1 );
46 }
47 
48 int
49 main( int argc, char **argv )
50 {
51  int status, retval, num_tests = 1, tmp;
52  int EventSet1 = PAPI_NULL;
53  long long **values;
54  long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
56  const PAPI_hw_info_t *hw_info;
57  const PAPI_component_info_t *cmpinfo;
58  pid_t pid;
59 
60  /* Fork before doing anything with the PMU */
61 
62  setbuf(stdout,NULL);
63  pid = fork( );
64  if ( pid < 0 )
65  test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
66  if ( pid == 0 )
68 
69  tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */
70 
71 
72  /* Master only process below here */
73 
75  if ( retval != PAPI_VER_CURRENT )
76  test_fail_exit( __FILE__, __LINE__, "PAPI_library_init", retval );
77 
78  if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL )
79  test_fail_exit( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
80 
81  if ( cmpinfo->attach == 0 )
82  test_skip( __FILE__, __LINE__, "Platform does not support attaching",
83  0 );
84 
85  hw_info = PAPI_get_hardware_info( );
86  if ( hw_info == NULL )
87  test_fail_exit( __FILE__, __LINE__, "PAPI_get_hardware_info", 0 );
88 
89  /* add PAPI_TOT_CYC and one of the events in PAPI_FP_INS, PAPI_FP_OPS or
90  PAPI_TOT_INS, depending on the availability of the event on the
91  platform */
92  retval = PAPI_create_eventset(&EventSet1);
93  if ( retval != PAPI_OK )
94  test_fail_exit( __FILE__, __LINE__, "PAPI_create_eventset", retval );
95 
96  /* Here we are testing that this does not cause a fail */
97 
98  retval = PAPI_assign_eventset_component( EventSet1, 0 );
99  if ( retval != PAPI_OK )
100  test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component",
101  retval );
102 
103  retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
104  if ( retval != PAPI_OK )
105  test_fail_exit( __FILE__, __LINE__, "PAPI_attach", retval );
106 
107  retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC);
108  if ( retval != PAPI_OK )
109  test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
110 
111 
112  strcpy(event_name,"PAPI_FP_INS");
113 
114  retval = PAPI_add_named_event(EventSet1, event_name);
115  if ( retval == PAPI_ENOEVNT ) {
116  strcpy(event_name,"PAPI_TOT_INS");
117  retval = PAPI_add_named_event(EventSet1, event_name);
118  }
119 
120  if ( retval != PAPI_OK ) {
121  test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
122  }
123 
124  values = allocate_test_space( 1, 2);
125 
126  elapsed_us = PAPI_get_real_usec( );
127 
128  elapsed_cyc = PAPI_get_real_cyc( );
129 
130  elapsed_virt_us = PAPI_get_virt_usec( );
131 
132  elapsed_virt_cyc = PAPI_get_virt_cyc( );
133 
134  printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
135  pid_t child = wait( &status );
136  printf( "Debugger exited wait() with %d\n",child );
137  if (WIFSTOPPED( status ))
138  {
139  printf( "Child has stopped due to signal %d (%s)\n",
140  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
141  }
142  if (WIFSIGNALED( status ))
143  {
144  printf( "Child %ld received signal %d (%s)\n",
145  (long)child,
146  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
147  }
148  printf("After %d\n",retval);
149 
150  retval = PAPI_start( EventSet1 );
151  if ( retval != PAPI_OK )
152  test_fail_exit( __FILE__, __LINE__, "PAPI_start", retval );
153 
154  printf("Continuing\n");
155 #if defined(__FreeBSD__)
156  if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
157 #else
158  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
159 #endif
160  perror( "ptrace(PTRACE_CONT)" );
161  return 1;
162  }
163 
164 
165  do {
166  child = wait( &status );
167  printf( "Debugger exited wait() with %d\n", child);
168  if (WIFSTOPPED( status ))
169  {
170  printf( "Child has stopped due to signal %d (%s)\n",
171  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
172  }
173  if (WIFSIGNALED( status ))
174  {
175  printf( "Child %ld received signal %d (%s)\n",
176  (long)child,
177  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
178  }
179  } while (!WIFEXITED( status ));
180 
181  printf("Child exited with value %d\n",WEXITSTATUS(status));
182  if (WEXITSTATUS(status) != 0)
183  test_fail_exit( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
184 
185  retval = PAPI_stop( EventSet1, values[0] );
186  if ( retval != PAPI_OK )
187  test_fail_exit( __FILE__, __LINE__, "PAPI_stop", retval );
188 
189  elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
190 
191  elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
192 
193  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
194 
195  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
196 
197  retval = PAPI_cleanup_eventset(EventSet1);
198  if (retval != PAPI_OK)
199  test_fail_exit( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
200 
201  retval = PAPI_destroy_eventset(&EventSet1);
202  if (retval != PAPI_OK)
203  test_fail_exit( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
204 
205  printf( "Test case: 3rd party attach start, stop.\n" );
206  printf( "-----------------------------------------------\n" );
207  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
208  printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
209  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
210  printf( "Default granularity is: %d (%s)\n", tmp,
212  printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
213  printf
214  ( "-------------------------------------------------------------------------\n" );
215 
216  printf( "Test type : \t 1\n" );
217 
218  printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
219  printf( "%s : \t %12lld\n",event_name, ( values[0] )[1]);
220  printf( TAB1, "Real usec : \t", elapsed_us );
221  printf( TAB1, "Real cycles : \t", elapsed_cyc );
222  printf( TAB1, "Virt usec : \t", elapsed_virt_us );
223  printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
224 
225  printf
226  ( "-------------------------------------------------------------------------\n" );
227 
228  printf( "Verification: none\n" );
229 
230  test_pass( __FILE__, values, num_tests );
231  exit( 1 );
232 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:23
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
#define NUM_FLOPS
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:805
#define PAPI_EMISC
Definition: fpapi.h:119
char * getenv()
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1604
Hardware info structure.
Definition: papi.h:775
long long PAPI_get_virt_usec(void)
Definition: papi.c:6246
#define PAPI_NULL
Definition: fpapi.h:13
int PAPI_add_named_event(int EventSet, char *EventName)
Definition: papi.c:1816
unsigned int attach
Definition: papi.h:657
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43
char * stringify_granularity(int granularity)
Definition: test_utils.c:429
void test_skip(char *file, int line, char *call, int retval)
Definition: test_utils.c:614
long long PAPI_get_virt_cyc(void)
Definition: papi.c:6174
#define PAPI_ENOEVNT
Definition: fpapi.h:112
#define TAB1
Definition: papi_test.h:112
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
#define printf
Definition: papi_test.h:125
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
char ** argv
Definition: iozone.c:1610
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:495
#define PAPI_TOT_CYC
Definition: fpapi.h:194
char * stringify_all_domains(int domains)
Definition: test_utils.c:369
unsigned int attach_must_ptrace
Definition: papi.h:658
static int pid
int PAPI_get_opt(int option, PAPI_option_t *ptr)
Definition: papi.c:4041
long long elapsed_cyc
Definition: zero_fork.c:43
int wait_for_attach_and_loop(void)
Definition: attach2.c:31
int wait()
#define PAPI_DEFDOM
Definition: fpapi.h:49
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
#define PAPI_DEFGRN
Definition: fpapi.h:51
int PAPI_cleanup_eventset(int EventSet)
Definition: papi.c:2805
int PAPI_assign_eventset_component(int EventSet, int cidx)
Definition: papi.c:1467
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1406
#define PAPI_ESYS
Definition: fpapi.h:108
int num_tests
Definition: zero_fork.c:46
strcpy(filename, default_filename)
long long PAPI_get_real_usec(void)
Definition: papi.c:6138
long long status
Definition: iozone.c:1335
#define PATH_MAX
Definition: fileop.c:68
int PAPI_destroy_eventset(int *EventSet)
Definition: papi.c:1944
int PAPI_attach(int EventSet, unsigned long tid)
Definition: papi.c:3110
long long PAPI_get_real_cyc(void)
Definition: papi.c:6091
int PAPI_start(int EventSet)
Definition: papi.c:2026
void test_fail_exit(char *file, int line, char *call, int retval)
Definition: test_utils.c:567
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6059
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:43
long long ** allocate_test_space(int num_tests, int num_events)
Definition: test_utils.c:107
static const PAPI_hw_info_t * hw_info
Definition: byte_profile.c:23
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int EventSet1
Definition: zero_fork.c:40