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