PAPI  5.7.0.0
system_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 static int Event[MAX_EVENTS] = {
21 };
22 
23 static int Threshold[MAX_EVENTS] = {
24  8000000,
25  4000000,
26  4000000,
27 };
28 
29 static int num_events = 1;
30 static int EventSet = PAPI_NULL;
31 static const char *name = "unknown";
32 static struct timeval start, last;
33 static long count, total;
34 
35 static 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 static void
48 zero_count( void )
49 {
50  gettimeofday( &start, NULL );
51  last = start;
52  count = 0;
53  total = 0;
54 }
55 
56 static void
57 print_here( const char *str) {
58 
59  if (!TESTS_QUIET) printf("[%d] %s, %s\n", getpid(), name, str);
60 }
61 
62 static void
63 print_rate( const char *str )
64 {
65  static int last_count = -1;
66  struct timeval now;
67  double st_secs, last_secs;
68 
69  gettimeofday( &now, NULL );
70  st_secs = ( double ) ( now.tv_sec - start.tv_sec )
71  + ( ( double ) ( now.tv_usec - start.tv_usec ) ) / 1000000.0;
72  last_secs = ( double ) ( now.tv_sec - last.tv_sec )
73  + ( ( double ) ( now.tv_usec - last.tv_usec ) ) / 1000000.0;
74  if ( last_secs <= 0.001 )
75  last_secs = 0.001;
76 
77  if (!TESTS_QUIET) {
78  printf( "[%d] %s, time = %.3f, total = %ld, last = %ld, rate = %.1f/sec\n",
79  getpid( ), str, st_secs, total, count,
80  ( ( double ) count ) / last_secs );
81  }
82 
83  if ( last_count != -1 ) {
84  if ( count < .1 * last_count ) {
85  test_fail( name, __LINE__, "Interrupt rate changed!", 1 );
86  exit( 1 );
87  }
88  }
89  last_count = ( int ) count;
90  count = 0;
91  last = now;
92 }
93 
94 static void
96 {
97  struct timeval start, now;
98  double x, sum;
99 
100  gettimeofday( &start, NULL );
101 
102  for ( ;; ) {
103  sum = 1.0;
104  for ( x = 1.0; x < 250000.0; x += 1.0 )
105  sum += x;
106  if ( sum < 0.0 )
107  printf( "==>> SUM IS NEGATIVE !! <<==\n" );
108 
109  gettimeofday( &now, NULL );
110  if ( now.tv_sec >= start.tv_sec + program_time )
111  break;
112  }
113 }
114 
115 static void
117 {
118  int ev;
119 
121 
123  test_fail( name, __LINE__, "PAPI_create_eventset failed", 1 );
124 
125  for ( ev = 0; ev < num_events; ev++ ) {
126  if ( PAPI_add_event( EventSet, Event[ev] ) != PAPI_OK ) {
127  if (!TESTS_QUIET) printf("Trouble adding event\n");
128  test_skip( name, __LINE__, "PAPI_add_event failed", 1 );
129  }
130  }
131 
132  for ( ev = 0; ev < num_events; ev++ ) {
133  if ( PAPI_overflow( EventSet, Event[ev], Threshold[ev], 0, my_handler )
134  != PAPI_OK ) {
135  test_fail( name, __LINE__, "PAPI_overflow failed", 1 );
136  }
137  }
138 
139  if ( PAPI_start( EventSet ) != PAPI_OK )
140  test_fail( name, __LINE__, "PAPI_start failed", 1 );
141 }
142 
143 static void
144 run( const char *str, int len )
145 {
146  int n;
147 
148  for ( n = 1; n <= len; n++ ) {
149  do_cycles( 1 );
150  print_rate( str );
151  }
152 }
153 
154 int
155 main( int argc, char **argv )
156 {
157  char buf[100];
158 
159  int quiet,retval;
160 
161  /* Used to be able to set this via command line */
162  num_events=1;
163 
164  /* Set TESTS_QUIET variable */
165  quiet=tests_quiet( argc, argv );
166 
167  do_cycles( 1 );
168 
169  zero_count( );
170 
171 
173  if (retval!=PAPI_VER_CURRENT) {
174  test_fail( name, __LINE__, "PAPI_library_init failed", 1 );
175  }
176 
177  name = argv[0];
178  if (!quiet) printf( "[%d] %s, num_events = %d\n", getpid( ), name, num_events );
179  sprintf( buf, "%d", num_events );
180  my_papi_start( );
181  run( name, 3 );
182 
183  print_here( "system(./burn)" );
184  if ( access( "./burn", X_OK ) == 0 )
185  ( quiet ? system( "./burn TESTS_QUIET" ) : system( "./burn" ) );
186  else if ( access( "./ctests/burn", X_OK ) == 0 )
187  ( quiet ? system( "./ctests/burn TESTS_QUIET" ) :
188  system( "./ctests/burn" ) );
189 
190  test_pass(__FILE__);
191 
192  return 0;
193 }
#define PAPI_OK
Definition: fpapi.h:105
static void do_cycles(int program_time)
void test_pass(const char *filename)
Definition: test_utils.c:432
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1663
static void run(const char *str, int len)
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int PAPI_overflow(int EventSet, int EventCode, int threshold, int flags, PAPI_overflow_handler_t handler)
Definition: papi.c:4788
int retval
Definition: zero_fork.c:53
static struct timeval start
static const char * name
Return codes and api definitions.
static void my_papi_start(void)
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
int PAPI_library_init(int version)
Definition: papi.c:500
static int program_time
static int Event[MAX_EVENTS]
int quiet
Definition: rapl_overflow.c:18
static long count
static long total
static int num_events
#define PAPI_NULL
Definition: fpapi.h:13
#define PAPI_FAD_INS
Definition: fpapi.h:234
static void zero_count(void)
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
__time_t tv_sec
int main(int argc, char **argv)
static void my_handler(int EventSet, void *pc, long long ovec, void *context)
static void print_here(const char *str)
__suseconds_t tv_usec
int gettimeofday(void *ptr1, void *ptr2)
static int EventSet
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
static void print_rate(const char *str)
int x
Definition: fileop.c:78
int PAPI_start(int EventSet)
Definition: papi.c:2096
#define PAPI_TOT_CYC
Definition: fpapi.h:195
int system()
volatile int buf[CACHE_FLUSH_BUFFER_SIZE_INTS]
Definition: do_loops.c:12
void exit()
static int Threshold[MAX_EVENTS]
static struct timeval start last
#define PAPI_FP_INS
Definition: fpapi.h:188
#define MAX_EVENTS