PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
multiattach2.c
Go to the documentation of this file.
1 /* This file performs the following test: start, stop and timer functionality for
2  an attached process as well as itself.
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 <inttypes.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_CONT PT_CONTINUE
28 #endif
29 
30 int
32 {
33  kill( getpid( ), SIGSTOP );
34  do_flops( NUM_FLOPS * num );
35  kill( getpid( ), SIGSTOP );
36  return 0;
37 }
38 
39 int
40 main( int argc, char **argv )
41 {
42  int status, retval, num_tests = 2, tmp;
44  int PAPI_event, PAPI_event2, mask1, mask2;
45  int num_events1, num_events2;
46  long long **values;
47  long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
48  char event_name[PAPI_MAX_STR_LEN], add_event_str[PAPI_MAX_STR_LEN];
49  const PAPI_component_info_t *cmpinfo;
50  pid_t pid;
51 
52  /* Set TESTS_QUIET variable */
53  tests_quiet( argc, argv );
54 
55  /* init the library */
57  if ( retval != PAPI_VER_CURRENT ) {
58  test_fail_exit( __FILE__, __LINE__, "PAPI_library_init", retval );
59  }
60 
61  /* get component info */
62  if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL ) {
63  test_fail_exit( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
64  }
65 
66  /* see if we support attach */
67  if ( cmpinfo->attach == 0 ) {
68  test_skip( __FILE__, __LINE__,
69  "Platform does not support attaching",0 );
70  }
71 
72  /* fork! */
73  pid = fork( );
74  if ( pid < 0 ) {
75  test_fail_exit( __FILE__, __LINE__, "fork()", PAPI_ESYS );
76  }
77 
78  /* if child, wait_for_attach_and_loop */
79  if ( pid == 0 ) {
81  }
82 
83  /* add PAPI_TOT_CYC and one of the events in
84  PAPI_FP_INS, PAPI_FP_OPS or PAPI_TOT_INS,
85  depending on the availability of the event
86  on the platform */
87  EventSet1 = add_two_events( &num_events1, &PAPI_event, &mask1 );
88  EventSet2 = add_two_events( &num_events2, &PAPI_event2, &mask2 );
89 
90  if ( cmpinfo->attach_must_ptrace ) {
91  if ( ptrace( PTRACE_ATTACH, pid, NULL, NULL ) == -1 ) {
92  perror( "ptrace(PTRACE_ATTACH)" );
93  return 1;
94  }
95  if ( waitpid( pid, &status, 0 ) == -1 ) {
96  perror( "waitpid()" );
97  exit( 1 );
98  }
99  if ( WIFSTOPPED( status ) == 0 ) {
100  test_fail( __FILE__, __LINE__,
101  "Child process didnt return true to WIFSTOPPED", 0 );
102  }
103  }
104 
105  retval = PAPI_attach( EventSet2, ( unsigned long ) pid );
106  if ( retval != PAPI_OK ) {
107  test_fail( __FILE__, __LINE__, "PAPI_attach", retval );
108  }
109 
110  retval = PAPI_event_code_to_name( PAPI_event, event_name );
111  if ( retval != PAPI_OK ) {
112  test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
113  }
114  sprintf( add_event_str, "PAPI_add_event[%s]", event_name );
115 
116  /* num_events1 is greater than num_events2 so don't worry. */
117 
118  values = allocate_test_space( num_tests, num_events1 );
119 
120  /* get before values */
121  elapsed_us = PAPI_get_real_usec( );
122  elapsed_cyc = PAPI_get_real_cyc( );
123  elapsed_virt_us = PAPI_get_virt_usec( );
124  elapsed_virt_cyc = PAPI_get_virt_cyc( );
125 
126  /* Wait for the SIGSTOP. */
127  if ( cmpinfo->attach_must_ptrace ) {
128  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
129  perror( "ptrace(PTRACE_CONT)" );
130  return 1;
131  }
132  if ( waitpid( pid, &status, 0 ) == -1 ) {
133  perror( "waitpid()" );
134  exit( 1 );
135  }
136  if ( WIFSTOPPED( status ) == 0 ) {
137  test_fail( __FILE__, __LINE__,
138  "Child process didn't return true to WIFSTOPPED", 0 );
139  }
140  if ( WSTOPSIG( status ) != SIGSTOP ) {
141  test_fail( __FILE__, __LINE__,
142  "Child process didn't stop on SIGSTOP", 0 );
143  }
144  }
145 
146  retval = PAPI_start( EventSet1 );
147  if ( retval != PAPI_OK ) {
148  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
149  }
150 
151  retval = PAPI_start( EventSet2 );
152  if ( retval != PAPI_OK ) {
153  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
154  }
155 
156  /* Wait for the SIGSTOP. */
157  if ( cmpinfo->attach_must_ptrace ) {
158  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
159  perror( "ptrace(PTRACE_ATTACH)" );
160  return 1;
161  }
162  if ( waitpid( pid, &status, 0 ) == -1 ) {
163  perror( "waitpid()" );
164  exit( 1 );
165  }
166  if ( WIFSTOPPED( status ) == 0 ) {
167  test_fail( __FILE__, __LINE__,
168  "Child process didn't return true to WIFSTOPPED", 0 );
169  }
170  if ( WSTOPSIG( status ) != SIGSTOP ) {
171  test_fail( __FILE__, __LINE__,
172  "Child process didn't stop on SIGSTOP", 0 );
173  }
174  }
175 
176  elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
177  elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
178  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
179  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
180 
181  retval = PAPI_stop( EventSet1, values[0] );
182  if ( retval != PAPI_OK ) {
183  printf( "Warning: PAPI_stop returned error %d, probably ok.\n",
184  retval );
185  }
186 
187  retval = PAPI_stop( EventSet2, values[1] );
188  if ( retval != PAPI_OK ) {
189  printf( "Warning: PAPI_stop returned error %d, probably ok.\n",
190  retval );
191  }
192 
193  remove_test_events( &EventSet1, mask1 );
194  remove_test_events( &EventSet2, mask2 );
195 
196  if ( cmpinfo->attach_must_ptrace ) {
197  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
198  perror( "ptrace(PTRACE_CONT)" );
199  return 1;
200  }
201  }
202 
203  if ( waitpid( pid, &status, 0 ) == -1 ) {
204  perror( "waitpid()" );
205  exit( 1 );
206  }
207  if ( WIFEXITED( status ) == 0 ) {
208  test_fail( __FILE__, __LINE__,
209  "Child process didn't return true to WIFEXITED", 0 );
210  }
211 
212  /* This code isn't necessary as we know the child has exited,
213  it *may* return an error if the component so chooses.
214  You should use read() instead. */
215 
216  printf( "Test case: multiple 3rd party attach start, stop.\n" );
217  printf( "-----------------------------------------------\n" );
218  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
219  printf( "Default domain is: %d (%s)\n", tmp, stringify_all_domains( tmp ) );
220  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
221  printf( "Default granularity is: %d (%s)\n", tmp,
223  printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
224  printf( "-------------------------------------------------------------------------\n" );
225 
226  sprintf( add_event_str, "(PID self) %-12s : \t",
227  event_name );
228  printf( TAB1, add_event_str, values[0][1] );
229  sprintf( add_event_str, "(PID self) PAPI_TOT_CYC : \t" );
230  printf( TAB1, add_event_str, values[0][0] );
231  sprintf( add_event_str, "(PID %jd) %-12s : \t", ( intmax_t ) pid,
232  event_name );
233  printf( TAB1, add_event_str, values[1][1] );
234  sprintf( add_event_str, "(PID %jd) PAPI_TOT_CYC : \t",
235  ( intmax_t ) pid );
236  printf( TAB1, add_event_str, values[1][0] );
237  printf( TAB1, "Real usec : \t", elapsed_us );
238  printf( TAB1, "Real cycles : \t", elapsed_cyc );
239  printf( TAB1, "Virt usec : \t", elapsed_virt_us );
240  printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
241 
242  printf( "-------------------------------------------------------------------------\n" );
243 
244  printf( "Verification: none\n" );
245 
246  test_pass( __FILE__, values, num_tests );
247 
248  return 0;
249 }
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:2225
#define NUM_FLOPS
int mask1
Definition: zero_fork.c:41
int PAPI_event[2]
Definition: data_range.c:24
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:803
long long PAPI_get_virt_usec(void)
Definition: papi.c:6233
#define PAPI_NULL
Definition: fpapi.h:13
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:6161
int num_events1
Definition: zero_fork.c:42
#define TAB1
Definition: papi_test.h:112
return PAPI_OK
Definition: linux-nvml.c:458
tests_quiet(argc, argv)
kill(master_listen_pid, SIGKILL)
#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
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:4030
long long elapsed_cyc
Definition: zero_fork.c:43
int wait_for_attach_and_loop(void)
Definition: attach2.c:31
void int num
Definition: iozone.c:22151
#define PAPI_DEFDOM
Definition: fpapi.h:49
int add_two_events(int *num_events, int *papi_event, int *mask)
Definition: test_utils.c:680
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
#define PAPI_DEFGRN
Definition: fpapi.h:51
int EventSet2
Definition: rapl_overflow.c:12
int PAPI_event_code_to_name(int EventCode, char *out)
Definition: papi.c:920
#define PAPI_ESYS
Definition: fpapi.h:108
int num_tests
Definition: zero_fork.c:46
void do_flops(int n)
Definition: multiplex.c:23
long long PAPI_get_real_usec(void)
Definition: papi.c:6125
long long status
Definition: iozone.c:1335
int PAPI_attach(int EventSet, unsigned long tid)
Definition: papi.c:3100
long long PAPI_get_real_cyc(void)
Definition: papi.c:6078
int PAPI_start(int EventSet)
Definition: papi.c:2019
void test_fail_exit(char *file, int line, char *call, int retval)
Definition: test_utils.c:567
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
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
int remove_test_events(int *EventSet, int mask)
Definition: test_utils.c:277
int EventSet1
Definition: zero_fork.c:40