PAPI  5.7.0.0
perf_event_uncore_attach.c
Go to the documentation of this file.
1 /*
2  * This file tests uncore events on perf_event kernels
3  *
4  * It uses the older PAPI_set_opt() way of specifying the CPU/granularity
5  * rather than the new :cpu=0 method
6  */
7 
8 #include <stdio.h>
9 
10 #include "papi.h"
11 #include "papi_test.h"
12 
13 #include "do_loops.h"
14 
15 #include "perf_event_uncore_lib.h"
16 
17 int main( int argc, char **argv ) {
18 
19  int retval,quiet;
20  int EventSet = PAPI_NULL;
21  long long values[1];
22  char *uncore_event=NULL;
23  char event_name[BUFSIZ];
24  int uncore_cidx=-1;
25  const PAPI_component_info_t *info;
26 
27  /* Set TESTS_QUIET variable */
28  quiet = tests_quiet( argc, argv );
29 
30  if (!quiet) {
31  printf("Testing creating an uncore event using PAPI_set_opt() to specify CPU\n");
32  }
33 
34  /* Init the PAPI library */
36  if ( retval != PAPI_VER_CURRENT ) {
37  test_fail( __FILE__, __LINE__, "PAPI_library_init", retval );
38  }
39 
40  /* Find the uncore PMU */
41  uncore_cidx=PAPI_get_component_index("perf_event_uncore");
42  if (uncore_cidx<0) {
43  if (!quiet) {
44  printf("perf_event_uncore component not found\n");
45  }
46  test_skip(__FILE__,__LINE__,"perf_event_uncore component not found",0);
47  }
48 
49  /* Check if component disabled */
50  info=PAPI_get_component_info(uncore_cidx);
51  if (info->disabled) {
52  if (!quiet) {
53  printf("perf_event_uncore component is disabled\n");
54  }
55  test_skip(__FILE__,__LINE__,"uncore component disabled",0);
56  }
57 
58  /* Get a relevant event name */
59  uncore_event=get_uncore_event(event_name, BUFSIZ);
60  if (uncore_event==NULL) {
61  if (!quiet) {
62  printf("uncore event name not available\n");
63  }
64  test_skip( __FILE__, __LINE__,
65  "PAPI does not support uncore on this processor",
66  PAPI_ENOSUPP );
67  }
68 
69  /* Create an eventset */
71  if (retval != PAPI_OK) {
72  test_fail(__FILE__, __LINE__, "PAPI_create_eventset",retval);
73  }
74 
75  /* Set a component for the EventSet */
77 
78  /* we need to set to a certain cpu for uncore to work */
79 
80  PAPI_cpu_option_t cpu_opt;
81 
82  cpu_opt.eventset=EventSet;
83  cpu_opt.cpu_num=0;
84 
86  if (retval != PAPI_OK) {
87  if (!quiet) {
88  printf("Could not cpu attach\n");
89  }
90  test_skip( __FILE__, __LINE__,
91  "this test; trying to PAPI_CPU_ATTACH; need to run as root",
92  retval);
93  }
94 
95  /* we need to set the granularity to system-wide for uncore to work */
96 
98 
99  gran_opt.def_cidx=0;
100  gran_opt.eventset=EventSet;
101  gran_opt.granularity=PAPI_GRN_SYS;
102 
104  if (retval != PAPI_OK) {
105  test_skip( __FILE__, __LINE__,
106  "this test; trying to set PAPI_GRN_SYS",
107  retval);
108  }
109 
110  /* we need to set domain to be as inclusive as possible */
111 
112  PAPI_domain_option_t domain_opt;
113 
114  domain_opt.def_cidx=0;
115  domain_opt.eventset=EventSet;
116  domain_opt.domain=PAPI_DOM_ALL;
117 
118  retval = PAPI_set_opt(PAPI_DOMAIN,(PAPI_option_t*)&domain_opt);
119  if (retval != PAPI_OK) {
120  if (!quiet) {
121  printf("could not set PAPI_DOM_ALL\n");
122  }
123  test_skip( __FILE__, __LINE__,
124  "this test; trying to set PAPI_DOM_ALL; need to run as root",
125  retval);
126  }
127 
128  /* Add our uncore event */
129  retval = PAPI_add_named_event(EventSet, uncore_event);
130  if (retval != PAPI_OK) {
131  if ( !quiet ) {
132  printf("Error trying to use event %s\n", uncore_event);
133  }
134  test_fail(__FILE__, __LINE__, "adding uncore event",retval);
135  }
136 
137  /* Start PAPI */
139  if ( retval != PAPI_OK ) {
140  test_fail( __FILE__, __LINE__, "PAPI_start", retval );
141  }
142 
143  /* our work code */
144  do_flops( NUM_FLOPS );
145 
146  /* Stop PAPI */
148  if ( retval != PAPI_OK ) {
149  test_fail( __FILE__, __LINE__, "PAPI_stop", retval );
150  }
151 
152  if ( !quiet ) {
153  printf("\tUsing event %s\n",uncore_event);
154  printf("\t%s: %lld\n",uncore_event,values[0]);
155  }
156 
157  test_pass( __FILE__ );
158 
159  return 0;
160 }
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
const PAPI_component_info_t * PAPI_get_component_info(int cidx)
Definition: papi.c:796
#define PAPI_CPU_ATTACH
Definition: papi.h:458
void test_pass(const char *filename)
Definition: test_utils.c:432
#define PAPI_ENOSUPP
Definition: fpapi.h:123
#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
int quiet
Definition: rapl_overflow.c:18
int PAPI_get_component_index(const char *name)
Definition: papi.c:6610
#define PAPI_NULL
Definition: fpapi.h:13
int PAPI_assign_eventset_component(int EventSet, int cidx)
Definition: papi.c:1526
int PAPI_create_eventset(int *EventSet)
Definition: papi.c:1464
void do_flops(int n)
Definition: multiplex.c:23
char * get_uncore_event(char *event, int size)
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
#define PAPI_DOMAIN
Definition: fpapi.h:50
int PAPI_start(int EventSet)
Definition: papi.c:2096
static long long values[NUM_EVENTS]
Definition: init_fini.c:10
#define PAPI_DOM_ALL
Definition: fpapi.h:25
int main(int argc, char **argv)