PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zero_attach.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 
20 #ifdef _AIX
21 #define _LINUX_SOURCE_COMPAT
22 #endif
23 
24 #if defined(__FreeBSD__)
25 # define PTRACE_ATTACH PT_ATTACH
26 # define PTRACE_CONT PT_CONTINUE
27 #endif
28 
29 int
31 {
32  kill( getpid( ), SIGSTOP );
34  kill( getpid( ), SIGSTOP );
35  return 0;
36 }
37 
38 int
39 main( int argc, char **argv )
40 {
41  int status, retval, num_tests = 1, tmp;
42  int EventSet1 = PAPI_NULL;
43  int PAPI_event, mask1;
44  int num_events1;
45  long long **values;
46  long long elapsed_us, elapsed_cyc, elapsed_virt_us, elapsed_virt_cyc;
47  char event_name[PAPI_MAX_STR_LEN], add_event_str[PAPI_MAX_STR_LEN];
48  const PAPI_component_info_t *cmpinfo;
49  pid_t pid;
50 
51  /* Set TESTS_QUIET variable */
52  tests_quiet( argc, argv );
53 
54  /* Initialize the PAPI library */
56  if ( retval != PAPI_VER_CURRENT ) {
57  test_fail_exit( __FILE__, __LINE__, "PAPI_library_init", retval );
58  }
59 
60  if ( ( cmpinfo = PAPI_get_component_info( 0 ) ) == NULL ) {
61  test_fail_exit( __FILE__, __LINE__, "PAPI_get_component_info", 0 );
62  }
63 
64  if ( cmpinfo->attach == 0 ) {
65  test_skip( __FILE__, __LINE__,
66  "Platform does not support attaching", 0 );
67  }
68 
69  pid = fork( );
70  if ( pid < 0 ) {
71  test_fail( __FILE__, __LINE__, "fork()", PAPI_ESYS );
72  }
73  if ( pid == 0 ) {
75  }
76 
77  /* add PAPI_TOT_CYC and one of the events in
78  PAPI_FP_INS, PAPI_FP_OPS or PAPI_TOT_INS,
79  depending on the availability of the event
80  on the platform */
81  EventSet1 = add_two_events( &num_events1, &PAPI_event, &mask1 );
82 
83  if ( cmpinfo->attach_must_ptrace ) {
84  if ( ptrace( PTRACE_ATTACH, pid, NULL, NULL ) == -1 ) {
85  perror( "ptrace(PTRACE_ATTACH)" );
86  return 1;
87  }
88  if ( waitpid( pid, &status, 0 ) == -1 ) {
89  perror( "waitpid()" );
90  exit( 1 );
91  }
92  if ( WIFSTOPPED( status ) == 0 )
93  test_fail( __FILE__, __LINE__,
94  "Child process didnt return true to WIFSTOPPED", 0 );
95  }
96 
97  retval = PAPI_attach( EventSet1, ( unsigned long ) pid );
98  if ( retval != PAPI_OK )
99  test_fail( __FILE__, __LINE__, "PAPI_attach", retval );
100 
101  retval = PAPI_event_code_to_name( PAPI_event, event_name );
102  if ( retval != PAPI_OK )
103  test_fail( __FILE__, __LINE__, "PAPI_event_code_to_name", retval );
104  sprintf( add_event_str, "PAPI_add_event[%s]", event_name );
105 
106  /* num_events1 is greater than num_events2 so don't worry. */
107 
108  values = allocate_test_space( num_tests, num_events1 );
109 
110  elapsed_us = PAPI_get_real_usec( );
111  elapsed_cyc = PAPI_get_real_cyc( );
112  elapsed_virt_us = PAPI_get_virt_usec( );
113  elapsed_virt_cyc = PAPI_get_virt_cyc( );
114 
115  /* Wait for the SIGSTOP. */
116  if ( cmpinfo->attach_must_ptrace ) {
117  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
118  perror( "ptrace(PTRACE_CONT)" );
119  return 1;
120  }
121  if ( waitpid( pid, &status, 0 ) == -1 ) {
122  perror( "waitpid()" );
123  exit( 1 );
124  }
125  if ( WIFSTOPPED( status ) == 0 ) {
126  test_fail( __FILE__, __LINE__,
127  "Child process didn't return true to WIFSTOPPED", 0 );
128  }
129  if ( WSTOPSIG( status ) != SIGSTOP ) {
130  test_fail( __FILE__, __LINE__,
131  "Child process didn't stop on SIGSTOP", 0 );
132  }
133  }
134 
135  retval = PAPI_start( EventSet1 );
136  if ( retval != PAPI_OK ) {
137  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
138  }
139 
140  /* Wait for the SIGSTOP. */
141  if ( cmpinfo->attach_must_ptrace ) {
142  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
143  perror( "ptrace(PTRACE_CONT)" );
144  return 1;
145  }
146  if ( waitpid( pid, &status, 0 ) == -1 ) {
147  perror( "waitpid()" );
148  exit( 1 );
149  }
150  if ( WIFSTOPPED( status ) == 0 ) {
151  test_fail( __FILE__, __LINE__,
152  "Child process didn't return true to WIFSTOPPED", 0 );
153  }
154  if ( WSTOPSIG( status ) != SIGSTOP ) {
155  test_fail( __FILE__, __LINE__,
156  "Child process didn't stop on SIGSTOP", 0 );
157  }
158  }
159 
160  elapsed_virt_us = PAPI_get_virt_usec( ) - elapsed_virt_us;
161  elapsed_virt_cyc = PAPI_get_virt_cyc( ) - elapsed_virt_cyc;
162  elapsed_us = PAPI_get_real_usec( ) - elapsed_us;
163  elapsed_cyc = PAPI_get_real_cyc( ) - elapsed_cyc;
164 
165  retval = PAPI_stop( EventSet1, values[0] );
166  if ( retval != PAPI_OK ) {
167  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
168  }
169 
170  remove_test_events( &EventSet1, mask1 );
171 
172  if ( cmpinfo->attach_must_ptrace ) {
173  if ( ptrace( PTRACE_CONT, pid, NULL, NULL ) == -1 ) {
174  perror( "ptrace(PTRACE_CONT)" );
175  return 1;
176  }
177  }
178 
179  if ( waitpid( pid, &status, 0 ) == -1 ) {
180  perror( "waitpid()" );
181  exit( 1 );
182  }
183  if ( WIFEXITED( status ) == 0 )
184  test_fail( __FILE__, __LINE__,
185  "Child process didn't return true to WIFEXITED", 0 );
186 
187 
188  printf( "Test case: 3rd party attach start, stop.\n" );
189  printf( "-----------------------------------------------\n" );
190  tmp = PAPI_get_opt( PAPI_DEFDOM, NULL );
191  printf( "Default domain is: %d (%s)\n", tmp,
193  tmp = PAPI_get_opt( PAPI_DEFGRN, NULL );
194  printf( "Default granularity is: %d (%s)\n", tmp,
196  printf( "Using %d iterations of c += a*b\n", NUM_FLOPS );
197  printf( "-------------------------------------------------------------------------\n" );
198 
199  printf( "Test type : \t 1\n" );
200 
201  sprintf( add_event_str, "%-12s : \t", event_name );
202  printf( TAB1, add_event_str, values[0][1] );
203  printf( TAB1, "PAPI_TOT_CYC : \t", values[0][0] );
204  printf( TAB1, "Real usec : \t", elapsed_us );
205  printf( TAB1, "Real cycles : \t", elapsed_cyc );
206  printf( TAB1, "Virt usec : \t", elapsed_virt_us );
207  printf( TAB1, "Virt cycles : \t", elapsed_virt_cyc );
208 
209  printf( "-------------------------------------------------------------------------\n" );
210 
211  printf( "Verification: none\n" );
212 
213  test_pass( __FILE__, values, num_tests );
214 
215  return 0;
216 }
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
#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 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