From: Bogdan Pricope <bogdan.pric...@linaro.org>

Configure the set of papi counters to be acquired via an
environment variable.

Signed-off-by: Bogdan Pricope <bogdan.pric...@linaro.org>
---
/** Email created from pull request 406 (bogdanPricope:cat_benchmark_pr)
 ** https://github.com/Linaro/odp/pull/406
 ** Patch: https://github.com/Linaro/odp/pull/406.patch
 ** Base sha: 079b524277414f57411d3223563353bbdbc1a536
 ** Merge commit sha: c25517810ea3389a28c0adb2fdde601eae684851
 **/
 example/instrum/papi_cnt.c | 44 +++++++++++++++++++++++++++++++++++++++-----
 example/instrum/papi_cnt.h |  2 ++
 example/instrum/sample.h   |  2 +-
 example/instrum/store.c    |  4 +++-
 4 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/example/instrum/papi_cnt.c b/example/instrum/papi_cnt.c
index c38c856a0..da8267834 100644
--- a/example/instrum/papi_cnt.c
+++ b/example/instrum/papi_cnt.c
@@ -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;
diff --git a/example/instrum/papi_cnt.h b/example/instrum/papi_cnt.h
index a4546c954..21ffbb7ca 100644
--- a/example/instrum/papi_cnt.h
+++ b/example/instrum/papi_cnt.h
@@ -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);
 
diff --git a/example/instrum/sample.h b/example/instrum/sample.h
index 090308348..7ed03fb9b 100644
--- a/example/instrum/sample.h
+++ b/example/instrum/sample.h
@@ -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];
diff --git a/example/instrum/store.c b/example/instrum/store.c
index 77137aad8..6f7294f24 100644
--- a/example/instrum/store.c
+++ b/example/instrum/store.c
@@ -23,6 +23,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)
 {
@@ -49,7 +50,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);
@@ -74,6 +75,7 @@ int instr_store_init(void)
        if (papi_init())
                return -1;
 
+       counters_cnt = papi_counters_cnt();
        return 0;
 }
 

Reply via email to