PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "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_attach", retval );
95 
96  /* Force addition of component */
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  /* The following call causes this test to fail for perf_events */
104 
105  retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
106  if ( retval != PAPI_OK )
107  test_fail_exit( __FILE__, __LINE__, "PAPI_attach", retval );
108 
109 
110 
111  retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC);
112  if ( retval != PAPI_OK )
113  test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
114 
115 
116  strcpy(event_name,"PAPI_FP_INS");
117  retval = PAPI_add_named_event(EventSet1, event_name);
118  if ( retval == PAPI_ENOEVNT ) {
119  strcpy(event_name,"PAPI_TOT_INS");
120  retval = PAPI_add_named_event(EventSet1, event_name);
121  }
122 
123  if ( retval != PAPI_OK ) {
124  test_fail_exit( __FILE__, __LINE__, "PAPI_add_event", retval );
125  }
126 
127  values = allocate_test_space( 1, 2);
128 
129  elapsed_us = PAPI_get_real_usec( );
130 
131  elapsed_cyc = PAPI_get_real_cyc( );
132 
133  elapsed_virt_us = PAPI_get_virt_usec( );
134 
135  elapsed_virt_cyc = PAPI_get_virt_cyc( );
136 
137  printf("must_ptrace is %d\n",cmpinfo->attach_must_ptrace);
138  pid_t child = wait( &status );
139  printf( "Debugger exited wait() with %d\n",child );
140  if (WIFSTOPPED( status ))
141  {
142  printf( "Child has stopped due to signal %d (%s)\n",
143  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
144  }
145  if (WIFSIGNALED( status ))
146  {
147  printf( "Child %ld received signal %d (%s)\n",
148  (long)child,
149  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
150  }
151  printf("After %d\n",retval);
152 
153  retval = PAPI_start( EventSet1 );
154  if ( retval != PAPI_OK )
155  test_fail_exit( __FILE__, __LINE__, "PAPI_start", retval );
156 
157  printf("Continuing\n");
158 #if defined(__FreeBSD__)
159  if ( ptrace( PT_CONTINUE, pid, (caddr_t) 1, 0 ) == -1 ) {
160 #else
161  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
162 #endif
163  perror( "ptrace(PTRACE_CONT)" );
164  return 1;
165  }
166 
167 
168  do {
169  child = wait( &status );
170  printf( "Debugger exited wait() with %d\n", child);
171  if (WIFSTOPPED( status ))
172  {
173  printf( "Child has stopped due to signal %d (%s)\n",
174  WSTOPSIG( status ), strsignal(WSTOPSIG( status )) );
175  }
176  if (WIFSIGNALED( status ))
177  {
178  printf( "Child %ld received signal %d (%s)\n",
179  (long)child,
180  WTERMSIG(status) , strsignal(WTERMSIG( status )) );
181  }
182  } while (!WIFEXITED( status ));
183 
184  printf("Child exited with value %d\n",WEXITSTATUS(status));
185  if (WEXITSTATUS(status) != 0)
186  test_fail_exit( __FILE__, __LINE__, "Exit status of child to attach to", PAPI_EMISC);
187 
188  retval = PAPI_stop( EventSet1, values[0] );
189  if ( retval != PAPI_OK )
190  test_fail_exit( __FILE__, __LINE__, "PAPI_stop", retval );
191 
192  elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
193 
194  elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
195 
196  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
197 
198  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
199 
200  retval = PAPI_cleanup_eventset(EventSet1);
201  if (retval != PAPI_OK)
202  test_fail_exit( __FILE__, __LINE__, "PAPI_cleanup_eventset", retval );
203 
204  retval = PAPI_destroy_eventset(&EventSet1);
205  if (retval != PAPI_OK)
206  test_fail_exit( __FILE__, __LINE__, "PAPI_destroy_eventset", retval );
207 
208  printf( "Test case: 3rd party attach start, stop.\n" );
209  printf( "-----------------------------------------------\n" );
210  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
211  printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
212  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
213  printf( "Default granularity is: %d (%s)\n", tmp,
215  printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
216  printf
217  ( "-------------------------------------------------------------------------\n" );
218 
219  printf( "Test type : \t 1\n" );
220 
221  printf( TAB1, "PAPI_TOT_CYC : \t", ( values[0] )[0] );
222  printf( "%s : \t %12lld\n", event_name, ( values[0] )[1] );
223  printf( TAB1, "Real usec : \t", elapsed_us );
224  printf( TAB1, "Real cycles : \t", elapsed_cyc );
225  printf( TAB1, "Virt usec : \t", elapsed_virt_us );
226  printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
227 
228  printf
229  ( "-------------------------------------------------------------------------\n" );
230 
231  printf( "Verification: none\n" );
232 
233  test_pass( __FILE__, values, num_tests );
234  exit( 1 );
235 }
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