@@ -6,17 +6,25 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <pthread.h>
#include <papi.h>
#include <papi_cnt.h>
-static int papi_event_tab[SAMPLE_COUNTER_TAB_SIZE] = {PAPI_BR_CN, PAPI_L2_DCM};
+#define PAPI_EVENTS_ENV "ODP_INSTRUM_PAPI_EVENTS"
+
+#define PAPI_EVENT_TAB_SIZE_DFLT 2
+int papi_event_tab_dflt[PAPI_EVENT_TAB_SIZE_DFLT] = {PAPI_BR_CN, PAPI_L2_DCM};
+
+static int papi_event_tab[SAMPLE_COUNTER_TAB_SIZE];
+static int papi_event_tab_size;
static __thread int event_set = PAPI_NULL;
int papi_init(void)
{
int retval, i;
+ char *papi_events_env = NULL;
retval = PAPI_library_init(PAPI_VER_CURRENT);
if (retval != PAPI_VER_CURRENT) {
@@ -35,7 +43,28 @@ int papi_init(void)
goto err_shutdown;
}
- for (i = 0; i < SAMPLE_COUNTER_TAB_SIZE; i++) {
+ papi_events_env = getenv(PAPI_EVENTS_ENV);
+ if (papi_events_env) {
+ char *tk = strtok(papi_events_env, ",");
+ int papi_event;
+
+ while (tk != NULL &&
+ papi_event_tab_size < SAMPLE_COUNTER_TAB_SIZE) {
+ if (PAPI_event_name_to_code(tk, &papi_event) == PAPI_OK)
+ papi_event_tab[papi_event_tab_size++] =
+ papi_event;
+
+ tk = strtok(NULL, ",");
+ }
+ }
+
+ if (!papi_event_tab_size) {
+ for (i = 0; i < PAPI_EVENT_TAB_SIZE_DFLT; i++)
+ papi_event_tab[i] = papi_event_tab_dflt[i];
+ papi_event_tab_size = PAPI_EVENT_TAB_SIZE_DFLT;
+ }
+
+ for (i = 0; i < papi_event_tab_size; i++) {
retval = PAPI_query_event(papi_event_tab[i]);
if (retval != PAPI_OK) {
printf("PAPI_query_event %d - error\n", i);
@@ -75,7 +104,7 @@ int papi_init_local(void)
}
retval = PAPI_add_events(event_set, papi_event_tab,
- SAMPLE_COUNTER_TAB_SIZE);
+ papi_event_tab_size);
if (retval != PAPI_OK) {
printf("PAPI_add_events error: %d\n", retval);
goto err_clean_evset;
@@ -103,7 +132,7 @@ int papi_term_local(void)
if (PAPI_stop(event_set, last_counters) == PAPI_OK) {
int i;
- for (i = 0; i < SAMPLE_COUNTER_TAB_SIZE; i++)
+ for (i = 0; i < papi_event_tab_size; i++)
printf("Counter[%d] = %lld\n", i, last_counters[i]);
}
@@ -113,6 +142,11 @@ int papi_term_local(void)
return 0;
}
+int papi_counters_cnt(void)
+{
+ return papi_event_tab_size;
+}
+
int papi_sample_start(profiling_sample_t *spl)
{
spl->timestamp_ns = PAPI_get_real_nsec();
@@ -134,7 +168,7 @@ int papi_sample_end(profiling_sample_t *spl)
return -1;
}
- for (i = 0; i < SAMPLE_COUNTER_TAB_SIZE; i++)
+ for (i = 0; i < papi_event_tab_size; i++)
spl->counters[i] = end_counters[i] - spl->counters[i];
spl->diff_cyc = end_cyc - spl->diff_cyc;
@@ -18,6 +18,8 @@ void papi_term(void);
int papi_init_local(void);
int papi_term_local(void);
+int papi_counters_cnt(void);
+
int papi_sample_start(profiling_sample_t *spl);
int papi_sample_end(profiling_sample_t *spl);
@@ -12,7 +12,7 @@ extern "C" {
#endif
#define SAMPLE_NAME_SIZE_MAX 20
-#define SAMPLE_COUNTER_TAB_SIZE 2
+#define SAMPLE_COUNTER_TAB_SIZE 10
typedef struct {
char name[SAMPLE_NAME_SIZE_MAX];
@@ -24,6 +24,7 @@ static __thread uint64_t profile_sample_ovf;
#define STORE_FILE_NAME_SIZE_MAX 250
static char store_dir[STORE_DIR_NAME_SIZE_MAX];
+static int counters_cnt;
static void store_dump(int last)
{
@@ -50,7 +51,7 @@ static void store_dump(int last)
profile_sample_tab[i].timestamp_ns,
profile_sample_tab[i].diff_cyc,
profile_sample_tab[i].name);
- for (j = 0; j < SAMPLE_COUNTER_TAB_SIZE; j++) {
+ for (j = 0; j < counters_cnt; j++) {
sprintf(smpl_tmp, ",%lld",
profile_sample_tab[i].counters[j]);
strcat(smpl, smpl_tmp);
@@ -75,6 +76,7 @@ int instr_store_init(void)
if (papi_init())
return -1;
+ counters_cnt = papi_counters_cnt();
return 0;
}