PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fork_exec_overflow.c
Go to the documentation of this file.
1 /*
2  * Test PAPI with fork() and exec().
3  */
4 
5 #include <sys/time.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include "papi.h"
13 #include "papi_test.h"
14 
15 #define MAX_EVENTS 3
16 
17 int Event[MAX_EVENTS] = {
21 };
22 
24  8000000,
25  4000000,
26  4000000,
27 };
28 
29 int num_events = 1;
31 char *name = "unknown";
32 struct timeval start, last;
33 long count, total;
34 
35 void
36 my_handler( int EventSet, void *pc, long long ovec, void *context )
37 {
38  ( void ) EventSet;
39  ( void ) pc;
40  ( void ) ovec;
41  ( void ) context;
42 
43  count++;
44  total++;
45 }
46 
47 void
48 zero_count( void )
49 {
50  gettimeofday( &start, NULL );
51  last = start;
52  count = 0;
53  total = 0;
54 }
55 
56 #define HERE(str) printf("[%d] %s, %s\n", getpid(), name, str);
57 
58 void
59 print_rate( char *str )
60 {
61  static int last_count = -1;
62  struct timeval now;
63  double st_secs, last_secs;
64 
65  gettimeofday( &now, NULL );
66  st_secs = ( double ) ( now.tv_sec - start.tv_sec )
67  + ( ( double ) ( now.tv_usec - start.tv_usec ) ) / 1000000.0;
68  last_secs = ( double ) ( now.tv_sec - last.tv_sec )
69  + ( ( double ) ( now.tv_usec - last.tv_usec ) ) / 1000000.0;
70  if ( last_secs <= 0.001 )
71  last_secs = 0.001;
72 
73  printf( "[%d] %s, time = %.3f, total = %ld, last = %ld, rate = %.1f/sec\n",
74  getpid( ), str, st_secs, total, count,
75  ( ( double ) count ) / last_secs );
76 
77  if ( last_count != -1 ) {
78  if ( count < .1 * last_count ) {
79  test_fail( name, __LINE__, "Interrupt rate changed!", 1 );
80  exit( 1 );
81  }
82  }
83  last_count = ( int ) count;
84  count = 0;
85  last = now;
86 }
87 
88 void
90 {
91  struct timeval start, now;
92  double x, sum;
93 
94  gettimeofday( &start, NULL );
95 
96  for ( ;; ) {
97  sum = 1.0;
98  for ( x = 1.0; x < 250000.0; x += 1.0 )
99  sum += x;
100  if ( sum < 0.0 )
101  printf( "==>> SUM IS NEGATIVE !! <<==\n" );
102 
103  gettimeofday( &now, NULL );
104  if ( now.tv_sec >= start.tv_sec + program_time )
105  break;
106  }
107 }
108 
109 void
111 {
113  test_fail( name, __LINE__, "PAPI_library_init failed", 1 );
114 }
115 
116 void
118 {
119  int ev;
120 
122 
124  test_fail( name, __LINE__, "PAPI_create_eventset failed", 1 );
125 
126  for ( ev = 0; ev < num_events; ev++ ) {
127  if ( PAPI_add_event( EventSet, Event[ev] ) != PAPI_OK )
128  test_fail( name, __LINE__, "PAPI_add_event failed", 1 );
129  }
130 
131  for ( ev = 0; ev < num_events; ev++ ) {
132  if ( PAPI_overflow( EventSet, Event[ev], Threshold[ev], 0, my_handler )
133  != PAPI_OK ) {
134  test_fail( name, __LINE__, "PAPI_overflow failed", 1 );
135  }
136  }
137 
138  if ( PAPI_start( EventSet ) != PAPI_OK )
139  test_fail( name, __LINE__, "PAPI_start failed", 1 );
140 }
141 
142 void
144 {
145  if ( PAPI_stop( EventSet, NULL ) != PAPI_OK )
146  test_fail( name, __LINE__, "PAPI_stop failed", 1 );
147 }
148 
149 void
150 run( char *str, int len )
151 {
152  int n;
153 
154  for ( n = 1; n <= len; n++ ) {
155  do_cycles( 1 );
156  print_rate( str );
157  }
158 }
159 
160 int
161 main( int argc, char **argv )
162 {
163  char buf[100];
164 
165  if ( argc < 2 || sscanf( argv[1], "%d", &num_events ) < 1 )
166  num_events = 1;
167  if ( num_events < 0 || num_events > MAX_EVENTS )
168  num_events = 1;
169 
170  tests_quiet( argc, argv ); /* Set TESTS_QUIET variable */
171  do_cycles( 1 );
172  zero_count( );
173  my_papi_init( );
174  name = argv[0];
175  printf( "[%d] %s, num_events = %d\n", getpid( ), name, num_events );
176  sprintf( buf, "%d", num_events );
177  my_papi_start( );
178  run( name, 3 );
179 #if defined(PCHILD)
180  HERE( "stop" );
181  my_papi_stop( );
182  HERE( "end" );
183  test_pass( name, NULL, 0 );
184 #elif defined(PEXEC)
185  HERE( "stop" );
186  my_papi_stop( );
187  HERE( "exec(./child_overflow)" );
188  if ( access( "./child_overflow", X_OK ) == 0 )
189  execl( "./child_overflow", "./child_overflow",
190  ( TESTS_QUIET ? "TESTS_QUIET" : NULL ), NULL );
191  else if ( access( "./ctests/child_overflow", X_OK ) == 0 )
192  execl( "./ctests/child_overflow", "./ctests/child_overflow",
193  ( TESTS_QUIET ? "TESTS_QUIET" : NULL ), NULL );
194  test_fail( name, __LINE__, "exec failed", 1 );
195 #elif defined(SYSTEM)
196  HERE( "system(./child_overflow)" );
197  if ( access( "./child_overflow", X_OK ) == 0 )
198  ( TESTS_QUIET ? system( "./child_overflow TESTS_QUIET" ) :
199  system( "./child_overflow" ) );
200  else if ( access( "./ctests/child_overflow", X_OK ) == 0 )
201  ( TESTS_QUIET ? system( "./ctests/child_overflow TESTS_QUIET" ) :
202  system( "./ctests/child_overflow" ) );
203  test_pass( name, NULL, 0 );
204 #elif defined(SYSTEM2)
205  HERE( "system(./burn)" );
206  if ( access( "./burn", X_OK ) == 0 )
207  ( TESTS_QUIET ? system( "./burn TESTS_QUIET" ) : system( "./burn" ) );
208  else if ( access( "./ctests/burn", X_OK ) == 0 )
209  ( TESTS_QUIET ? system( "./ctests/burn TESTS_QUIET" ) :
210  system( "./ctests/burn" ) );
211  test_pass( name, NULL, 0 );
212 #else
213  HERE( "fork" );
214  {
215  int ret = fork( );
216  if ( ret < 0 )
217  test_fail( name, __LINE__, "fork failed", 1 );
218  if ( ret == 0 ) {
219  /*
220  * Child process.
221  */
222  zero_count( );
223  my_papi_init( );
224  my_papi_start( );
225  run( "child", 5 );
226  HERE( "stop" );
227  my_papi_stop( );
228  sleep( 3 );
229  HERE( "end" );
230  exit( 0 );
231  }
232  run( "main", 14 );
233  my_papi_stop( );
234  {
235  int status;
236  wait( &status );
237  HERE( "end" );
238  if ( WEXITSTATUS( status ) != 0 )
239  test_fail( name, __LINE__, "child failed", 1 );
240  else
241  test_pass( name, NULL, 0 );
242  }
243  }
244 #endif
245  exit( 0 );
246 }
sprintf(splash[splash_line++],"\tIozone: Performance Test of File I/O\n")
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
void do_cycles(int program_time)
void my_papi_start(void)
sleep(1)
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1604
#define PAPI_NULL
Definition: fpapi.h:13
start
Definition: iozone.c:22736
int num_events
void my_handler(int EventSet, void *pc, long long ovec, void *context)
return PAPI_OK
Definition: linux-nvml.c:458
int count
Definition: iozone.c:22422
tests_quiet(argc, argv)
int PAPI_overflow(int EventSet, int EventCode, int threshold, int flags, PAPI_overflow_handler_t handler)
Definition: papi.c:4668
void
Definition: iozone.c:18627
#define printf
Definition: papi_test.h:125
static double
Definition: fileop.c:1281
test_pass(__FILE__, NULL, 0)
int int argc
Definition: iozone.c:1609
Return codes and api definitions.
long long ret
Definition: iozone.c:1346
int TESTS_QUIET
Definition: test_utils.c:11
char ** argv
Definition: iozone.c:1610
#define MAX_EVENTS
test_fail(__FILE__, __LINE__,"PAPI_library_init", retval)
int PAPI_library_init(int version)
Definition: papi.c:495
int program_time
char buf[200]
Definition: iozone.c:19609
#define PAPI_TOT_CYC
Definition: fpapi.h:194
void run(char *str, int len)
int wait()
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
static struct temp_event * last
#define PAPI_FP_INS
Definition: fpapi.h:187
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1406
#define PAPI_FAD_INS
Definition: fpapi.h:233
int EventSet
sscanf(mnc->m_child_port,"%d",&mc.m_child_port)
long long status
Definition: iozone.c:1335
#define HERE(str)
char * name
Definition: iozone.c:23648
static int total
Definition: rapl_overflow.c:5
int
Definition: iozone.c:18528
int x
Definition: fileop.c:78
int Event[MAX_EVENTS]
int Threshold[MAX_EVENTS]
void zero_count(void)
void my_papi_init(void)
int PAPI_start(int EventSet)
Definition: papi.c:2026
int system()
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
void print_rate(char *str)
void my_papi_stop(void)
int n
Definition: mendes-alt.c:164