PAPI  5.3.2.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
examples/overflow_pthreads.c
Go to the documentation of this file.
1 /* This file performs the following test: overflow dispatch with pthreads
2 
3  - This example tests the dispatch of overflow calls from PAPI. The event
4  set is counted in the default counting domain and default granularity,
5  depending on the platform. Usually this is the user domain
6  (PAPI_DOM_USER) and thread context (PAPI_GRN_THR).
7 
8  The Eventset contains:
9  + PAPI_TOT_INS (overflow monitor)
10  + PAPI_TOT_CYC
11 
12  Each thread will do the followings :
13  - enable overflow
14  - Start eventset 1
15  - Do flops
16  - Stop eventset 1
17  - disable overflow
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <pthread.h>
22 #include "papi.h"
23 
24 #define THRESHOLD 200000
25 #define OVER_FMT "handler(%d ) Overflow at %p! bit=%#llx \n"
26 #define ERROR_RETURN(retval) { fprintf(stderr, "Error %d %s:line %d: \n", retval,__FILE__,__LINE__); exit(retval); }
27 
28 
29 int total = 0;
30 
31 void do_flops(int n)
32 {
33  int i;
34  double c = 0.11;
35  double a = 0.5;
36  double b = 6.2;
37 
38  for (i=0; i < n; i++)
39  c += a * b;
40 }
41 
42 /* overflow handler */
43 void
44 handler(int EventSet, void *address, long long overflow_vector, void *context)
45 {
46  fprintf(stderr, OVER_FMT, EventSet, address, overflow_vector);
47  total++;
48 }
49 
50 void *Thread(void *arg)
51 {
52  int retval;
53  int EventSet1=PAPI_NULL;
54  long long values[2];
55  long long elapsed_us, elapsed_cyc;
56 
57  fprintf(stderr,"Thread %lx running PAPI\n",PAPI_thread_id());
58 
59  /* create the event set */
60  if ( (retval = PAPI_create_eventset(&EventSet1))!=PAPI_OK)
61  ERROR_RETURN(retval);
62 
63  /* query whether the event exists */
64  if ((retval=PAPI_query_event(PAPI_TOT_INS)) != PAPI_OK)
65  ERROR_RETURN(retval);
66  if ((retval=PAPI_query_event(PAPI_TOT_CYC)) != PAPI_OK)
67  ERROR_RETURN(retval);
68 
69  /* add events to the event set */
70  if ( (retval = PAPI_add_event(EventSet1, PAPI_TOT_INS))!= PAPI_OK)
71  ERROR_RETURN(retval);
72 
73  if ( (retval = PAPI_add_event(EventSet1, PAPI_TOT_CYC)) != PAPI_OK)
74  ERROR_RETURN(retval);
75 
76  elapsed_us = PAPI_get_real_usec();
77 
78  elapsed_cyc = PAPI_get_real_cyc();
79 
80  retval = PAPI_overflow(EventSet1, PAPI_TOT_CYC, THRESHOLD, 0, handler);
81  if(retval !=PAPI_OK)
82  ERROR_RETURN(retval);
83 
84  /* start counting */
85  if((retval = PAPI_start(EventSet1))!=PAPI_OK)
86  ERROR_RETURN(retval);
87 
88  do_flops(*(int *)arg);
89 
90  if ((retval = PAPI_stop(EventSet1, values))!=PAPI_OK)
91  ERROR_RETURN(retval);
92 
93  elapsed_us = PAPI_get_real_usec() - elapsed_us;
94 
95  elapsed_cyc = PAPI_get_real_cyc() - elapsed_cyc;
96 
97  /* disable overflowing */
98  retval = PAPI_overflow(EventSet1, PAPI_TOT_CYC, 0, 0, handler);
99  if(retval !=PAPI_OK)
100  ERROR_RETURN(retval);
101 
102  /* remove the event from the eventset */
103  retval = PAPI_remove_event(EventSet1, PAPI_TOT_INS);
104  if (retval != PAPI_OK)
105  ERROR_RETURN(retval);
106 
107  retval = PAPI_remove_event(EventSet1, PAPI_TOT_CYC);
108  if (retval != PAPI_OK)
109  ERROR_RETURN(retval);
110 
111  printf("Thread %#x PAPI_TOT_INS : \t%lld\n",(int)PAPI_thread_id(),
112  values[0]);
113  printf(" PAPI_TOT_CYC: \t%lld\n", values[1]);
114  printf(" Real usec : \t%lld\n", elapsed_us);
115  printf(" Real cycles : \t%lld\n", elapsed_cyc);
116 
117  pthread_exit(NULL);
118 }
119 
120 int main(int argc, char **argv)
121 {
122  pthread_t thread_one;
123  pthread_t thread_two;
124  int flops1, flops2;
125  int rc,retval;
126  pthread_attr_t attr;
127  long long elapsed_us, elapsed_cyc;
128 
129 
130  /* papi library initialization */
132  {
133  printf("Library initialization error! \n");
134  exit(1);
135  }
136 
137  /* thread initialization */
138  retval=PAPI_thread_init((unsigned long(*)(void))(pthread_self));
139  if (retval != PAPI_OK)
140  ERROR_RETURN(retval);
141 
142  /* return the number of microseconds since some arbitrary starting point */
143  elapsed_us = PAPI_get_real_usec();
144 
145  /* return the number of cycles since some arbitrary starting point */
146  elapsed_cyc = PAPI_get_real_cyc();
147 
148  /* pthread attribution init */
149  pthread_attr_init(&attr);
150  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
151 
152  /* create the first thread */
153  flops1 = 1000000;
154  rc = pthread_create(&thread_one, &attr, Thread, (void *)&flops1);
155  if (rc)
156  ERROR_RETURN(rc);
157 
158  /* create the second thread */
159  flops2 = 4000000;
160  rc = pthread_create(&thread_two, &attr, Thread, (void *)&flops2);
161  if (rc)
162  ERROR_RETURN(rc);
163 
164  /* wait for the threads to finish */
165  pthread_attr_destroy(&attr);
166  pthread_join(thread_one, NULL);
167  pthread_join(thread_two, NULL);
168 
169  /* compute the elapsed cycles and microseconds */
170  elapsed_cyc = PAPI_get_real_cyc() - elapsed_cyc;
171 
172  elapsed_us = PAPI_get_real_usec() - elapsed_us;
173 
174  printf("Master real usec : \t%lld\n", elapsed_us);
175  printf("Master real cycles : \t%lld\n", elapsed_cyc);
176 
177  /* clean up */
178  PAPI_shutdown();
179  exit(0);
180 }
181 
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2232
int PAPI_add_event(int EventSet, int EventCode)
Definition: papi.c:1604
#define PAPI_NULL
Definition: fpapi.h:13
unsigned long PAPI_thread_id(void)
Definition: papi.c:157
int PAPI_remove_event(int EventSet, int EventCode)
Definition: papi.c:1699
#define PAPI_TOT_INS
Definition: fpapi.h:185
return PAPI_OK
Definition: linux-nvml.c:458
int PAPI_overflow(int EventSet, int EventCode, int threshold, int flags, PAPI_overflow_handler_t handler)
Definition: papi.c:4668
#define printf
Definition: papi_test.h:125
double c
Definition: multiplex.c:22
int int argc
Definition: iozone.c:1609
static double a[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:37
Return codes and api definitions.
char ** argv
Definition: iozone.c:1610
int PAPI_thread_init(unsigned long int(*id_fn)(void))
Definition: papi.c:118
int PAPI_library_init(int version)
Definition: papi.c:495
int i
Definition: fileop.c:140
#define PAPI_TOT_CYC
Definition: fpapi.h:194
void PAPI_shutdown(void)
Definition: papi.c:4357
long long elapsed_cyc
Definition: zero_fork.c:43
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
void * Thread(void *arg)
#define ERROR_RETURN(retval)
void handler(int EventSet, void *address, long long overflow_vector, void *context)
Definition: rapl_overflow.c:16
pthread_t
Definition: iozone.c:18557
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1406
void do_flops(int n)
Definition: multiplex.c:23
int PAPI_query_event(int EventCode)
Definition: papi.c:696
int EventSet
static double b[MATRIX_SIZE][MATRIX_SIZE]
Definition: rapl_basic.c:38
long long PAPI_get_real_usec(void)
Definition: papi.c:6138
static int total
Definition: rapl_overflow.c:5
#define THRESHOLD
int rc
Definition: iozone.c:20281
#define OVER_FMT
long long PAPI_get_real_cyc(void)
Definition: papi.c:6091
int PAPI_start(int EventSet)
Definition: papi.c:2026
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
ssize_t retval
Definition: libasync.c:338
long long elapsed_us
Definition: zero_fork.c:43
int main(int argc, char **argv)
List all appio events codes and names.
void exit()
pthread_attr_t attr
Definition: iozone.c:18466
int EventSet1
Definition: zero_fork.c:40
int n
Definition: mendes-alt.c:164