PAPI  5.7.0.0
papi_tot_cyc.c
Go to the documentation of this file.
1 /* This file attempts to test the PAPI_TOT_CYC */
2 /* performance counter. */
3 
4 /* by Vince Weaver, <vincent.weaver@maine.edu> */
5 
6 
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/time.h>
12 
13 #include <time.h>
14 
15 #include "papi.h"
16 #include "papi_test.h"
17 
18 #include "display_error.h"
19 
20 #include "matrix_multiply.h"
21 
22 #define SLEEP_RUNS 3
23 
24 
25 static long long convert_to_ns(struct timespec *before,
26  struct timespec *after) {
27 
28  long long seconds;
29  long long ns;
30 
31  seconds=after->tv_sec - before->tv_sec;
32  ns = after->tv_nsec - before->tv_nsec;
33 
34  ns = (seconds*1000000000ULL)+ns;
35 
36  return ns;
37 }
38 
39 
40 int main(int argc, char **argv) {
41 
42  int quiet;
43  double mmm_ghz;
44 
45  double error;
46 
47  int i;
48  long long count,high=0,low=0,total=0,average=0;
49  long long nsecs;
50  long long mmm_count;
51  long long expected;
52  int retval;
53  int eventset=PAPI_NULL;
54 
55  struct timespec before,after;
56 
57  quiet=tests_quiet(argc,argv);
58 
59  /* Init the PAPI library */
61  if ( retval != PAPI_VER_CURRENT ) {
62  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
63  }
64 
65  if (!quiet) {
66  printf("\nTesting PAPI_TOT_CYC\n\n");
67  }
68 
69  if (!quiet) {
70  printf("Testing a sleep of 1 second (%d times):\n",SLEEP_RUNS);
71  }
72 
73  retval=PAPI_create_eventset(&eventset);
74  if (retval!=PAPI_OK) {
75  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
76  }
77 
78  retval=PAPI_add_named_event(eventset,"PAPI_TOT_CYC");
79  if (retval!=PAPI_OK) {
80  if (!quiet) printf("Could not add PAPI_TOT_CYC\n");
81  test_skip( __FILE__, __LINE__, "adding PAPI_TOT_CYC", retval );
82  }
83 
84 
85  for(i=0;i<SLEEP_RUNS;i++) {
86 
87  PAPI_reset(eventset);
88  PAPI_start(eventset);
89 
90  sleep(1);
91 
92  retval=PAPI_stop(eventset,&count);
93  if (retval!=PAPI_OK) {
94  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
95  }
96 
97  if (count>high) high=count;
98  if ((low==0) || (count<low)) low=count;
99  total+=count;
100  }
101 
102  average=total/SLEEP_RUNS;
103 
104  if (!quiet) {
105  printf("\tAverage should be low, as no user cycles when sleeping\n");
106  printf("\tMeasured average: %lld\n",average);
107  }
108 
109  if (average>100000) {
110  if (!quiet) printf("Average cycle count too high!\n");
111  test_fail( __FILE__, __LINE__, "idle average", retval );
112  }
113 
114  /*****************************/
115  /* testing Matrix Matrix GHz */
116  /*****************************/
117 
118  if (!quiet) {
119  printf("\nEstimating GHz with matrix matrix multiply\n");
120  }
121 
122  // We have a problem with subsequent runs requiring fewer cycles than
123  // the first run. This may be system dependent; so we do a throw-away
124  // run here, so we end up comparing the SECOND run to the 3rd, 4th, etc.
125 
126  naive_matrix_multiply(quiet); // A first run, to init system.
127 
128  clock_gettime(CLOCK_REALTIME,&before);
129 
130  PAPI_reset(eventset);
131  PAPI_start(eventset);
132 
134 
135  retval=PAPI_stop(eventset,&count);
136 
137  if (retval!=PAPI_OK) {
138  test_fail( __FILE__, __LINE__, "Problem stopping!", retval );
139  }
140 
141  clock_gettime(CLOCK_REALTIME,&after);
142 
143  nsecs=convert_to_ns(&before,&after);
144 
145  mmm_ghz=(double)count/(double)nsecs;
146 
147  if (!quiet) {
148  printf("\tActual measured cycles = %lld\n",count);
149  printf("\tEstimated actual GHz = %.2lfGHz\n",mmm_ghz);
150  }
151 
152  mmm_count=count;
153 
154  /************************************/
155  /* Check for Linear Speedup */
156  /************************************/
157 
158  if (!quiet) printf("\nTesting for a linear cycle increase\n");
159 
160 #define REPITITIONS 2
161 
162  clock_gettime(CLOCK_REALTIME,&before);
163 
164  PAPI_reset(eventset);
165  PAPI_start(eventset);
166 
167  for(i=0;i<REPITITIONS;i++) {
169  }
170 
171  retval=PAPI_stop(eventset,&count);
172 
173  if (retval!=PAPI_OK) {
174  test_fail( __FILE__, __LINE__, "Problem stopping!", retval );
175  }
176 
177  clock_gettime(CLOCK_REALTIME,&after);
178 
179  nsecs=convert_to_ns(&before,&after);
180 
181  expected=mmm_count*REPITITIONS;
182 
183  error= 100.0 * (double)(count-expected) / (double)expected;
184 
185  if (!quiet) {
186  printf("\tExpected %lld, got %lld\n",expected,count);
187  printf("\tError=%.2f%%\n",error);
188  }
189 
190  if ((error>10.0) || (error<-10.0)) {
191 
192  if (!quiet) printf("Error too high!\n");
193  test_fail( __FILE__, __LINE__, "Error too high", retval );
194  }
195 
196  if (!quiet) printf("\n");
197 
198  test_pass( __FILE__ );
199 
200  return 0;
201 }
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define REPITITIONS
static long long convert_to_ns(struct timespec *before, struct timespec *after)
Definition: papi_tot_cyc.c:25
void test_pass(const char *filename)
Definition: test_utils.c:432
static int expected[NUM_THREADS]
int PAPI_reset(int EventSet)
Definition: papi.c:2459
__time_t tv_sec
double naive_matrix_multiply(int quiet)
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int retval
Definition: zero_fork.c:53
Return codes and api definitions.
void test_skip(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:561
int PAPI_add_named_event(int EventSet, const char *EventName)
Definition: papi.c:1876
__syscall_slong_t tv_nsec
int PAPI_library_init(int version)
Definition: papi.c:500
int quiet
Definition: rapl_overflow.c:18
int main(int argc, char **argv)
Definition: papi_tot_cyc.c:40
#define PAPI_NULL
Definition: fpapi.h:13
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
static int total
Definition: rapl_overflow.c:9
int tests_quiet(int argc, char **argv)
Definition: test_utils.c:376
#define SLEEP_RUNS
Definition: papi_tot_cyc.c:22
void test_fail(const char *file, int line, const char *call, int retval)
Definition: test_utils.c:468
int PAPI_start(int EventSet)
Definition: papi.c:2096
static long count
int i
Definition: fileop.c:140