PAPI  5.7.0.0
attach_cpu_sys_validate.c
Go to the documentation of this file.
1 /*
2  * This test attempts to attach to each CPU
3  * Then it runs some code on one CPU
4  * Then it reads the results, they should be different.
5  * It sets the granularity to SYS as this is known to be broken
6  * with some Linux/rdpmc combinations
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #include "papi.h"
13 #include "papi_test.h"
14 
15 #include "do_loops.h"
16 
17 #define MAX_CPUS 16
18 
19 int
20 main( int argc, char **argv )
21 {
22  int i;
23  int retval;
24  int num_cpus = 8;
25  int EventSet[MAX_CPUS];
26  const PAPI_hw_info_t *hwinfo;
27  double diff;
28 
29  long long values[MAX_CPUS];
30  char event_name[PAPI_MAX_STR_LEN] = "PAPI_TOT_INS";
31  PAPI_option_t opts;
32  int quiet;
33  long long average=0;
34  int same=0;
35 
36  /* Set TESTS_QUIET variable */
37  quiet=tests_quiet( argc, argv );
38 
40  if ( retval != PAPI_VER_CURRENT ) {
41  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
42  }
43 
44  hwinfo = PAPI_get_hardware_info( );
45  if ( hwinfo==NULL) {
46  test_fail( __FILE__, __LINE__, "PAPI_get_hardware_info", retval );
47  }
48 
49  num_cpus=hwinfo->totalcpus;
50 
51  if ( num_cpus < 2 ) {
52  if (!quiet) printf("Need at least 1 CPU\n");
53  test_skip( __FILE__, __LINE__, "num_cpus", 0 );
54  }
55 
56  if (num_cpus > MAX_CPUS) {
58  }
59 
60  for(i=0;i<num_cpus;i++) {
61 
63 
65  if ( retval != PAPI_OK ) {
66  test_fail( __FILE__, __LINE__, "PAPI_create_eventset", retval );
67  }
68 
69  /* Force event set to be associated with component 0 */
70  /* (perf_events component provides all core events) */
72  if ( retval != PAPI_OK ) {
73  test_fail( __FILE__, __LINE__, "PAPI_assign_eventset_component", retval );
74  }
75 
76  /* Force granularity to PAPI_GRN_SYS */
79  PAPI_set_opt( PAPI_GRANUL, &opts) ;
80  if (!quiet) {
81  printf( "Setting Eventset[%d] granularity to: "
82  "%d (%s)\n", i,opts.granularity.granularity,
84  }
85 
86  /* Attach this event set to cpu i */
87  opts.cpu.eventset = EventSet[i];
88  opts.cpu.cpu_num = i;
89 
91  if ( retval != PAPI_OK ) {
92  if (!quiet) printf("Can't PAPI_CPU_ATTACH: %s\n",
94  test_skip( __FILE__, __LINE__, "PAPI_set_opt", retval );
95  }
96 
98  if ( retval != PAPI_OK ) {
99  if (!quiet) printf("Trouble adding event %s\n",event_name);
100  test_skip( __FILE__, __LINE__, "PAPI_add_named_event", retval );
101  }
102  }
103 
104  for(i=0;i<num_cpus;i++) {
105  retval = PAPI_start( EventSet[i] );
106  if ( retval != PAPI_OK ) {
107  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
108  }
109  }
110 
111  // do some work
113 
114  for(i=0;i<num_cpus;i++) {
115  retval = PAPI_stop( EventSet[i], &values[i] );
116  if ( retval != PAPI_OK ) {
117  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
118  }
119  }
120 
121  for(i=0;i<num_cpus;i++) {
122  if (!quiet) {
123  printf ("Event: %s: %10lld on Cpu: %d\n",
124  event_name, values[i], i);
125  }
126  }
127 
128  for(i=0;i<num_cpus;i++) {
129  average+=values[i];
130  }
131  average/=num_cpus;
132  if (!quiet) {
133  printf("Average: %10lld\n",average);
134  }
135 
136 
137  for(i=0;i<num_cpus;i++) {
138  diff=((double)values[i]-(double)average)/(double)average;
139  if ((diff<0.01) && (diff>-0.01)) same++;
140  }
141 
142  if (same) {
143  if (!quiet) {
144  printf("Error! %d events were the same\n",same);
145  }
146  test_fail( __FILE__, __LINE__, "Too similar", 0 );
147  }
148 
149  PAPI_shutdown( );
150 
151  test_pass( __FILE__ );
152 
153  return 0;
154 
155 }
char event_name[2][PAPI_MAX_STR_LEN]
Definition: data_range.c:29
#define PAPI_OK
Definition: fpapi.h:105
int PAPI_stop(int EventSet, long long *values)
Definition: papi.c:2314
#define PAPI_GRANUL
Definition: fpapi.h:52
#define PAPI_CPU_ATTACH
Definition: papi.h:458
void test_pass(const char *filename)
Definition: test_utils.c:432
Hardware info structure.
Definition: papi.h:781
#define MAX_CPUS
char * stringify_granularity(int granularity)
Definition: test_utils.c:353
PAPI_granularity_option_t granularity
Definition: papi.h:854
#define PAPI_GRN_SYS
Definition: fpapi.h:71
#define PAPI_VER_CURRENT
Definition: fpapi.h:14
int EventSet
int retval
Definition: zero_fork.c:53
A pointer to the following is passed to PAPI_set/get_opt()
Definition: papi.h:849
#define NUM_FLOPS
Definition: sdsc-mpx.c:24
int PAPI_set_opt(int option, PAPI_option_t *ptr)
Definition: papi.c:3465
unsigned int cpu_num
Definition: papi.h:825
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
int PAPI_library_init(int version)
Definition: papi.c:500
void PAPI_shutdown(void)
Definition: papi.c:4461
int quiet
Definition: rapl_overflow.c:18
#define PAPI_NULL
Definition: fpapi.h:13
PAPI_cpu_option_t cpu
Definition: papi.h:859
int PAPI_assign_eventset_component(int EventSet, int cidx)
Definition: papi.c:1526
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
static int num_cpus
Definition: linux-rapl.c:150
void do_flops(int n)
Definition: multiplex.c:23
char * PAPI_strerror(int errorCode)
Definition: papi.c:4603
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
int main(int argc, char **argv)
int totalcpus
Definition: papi.h:787
int PAPI_start(int EventSet)
Definition: papi.c:2096
const PAPI_hw_info_t * PAPI_get_hardware_info(void)
Definition: papi.c:6185
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
int i
Definition: fileop.c:140
#define PAPI_MAX_STR_LEN
Definition: fpapi.h:43