From 4a41c885ed0d55a2b754f40995f9753ddb37c065 Mon Sep 17 00:00:00 2001 From: paboyle Date: Wed, 4 Nov 2015 03:24:19 -0800 Subject: [PATCH] Use Linux kernel interface to hardware performance counters. Dead useful. --- lib/PerfCount.h | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 lib/PerfCount.h diff --git a/lib/PerfCount.h b/lib/PerfCount.h new file mode 100644 index 00000000..264d571e --- /dev/null +++ b/lib/PerfCount.h @@ -0,0 +1,133 @@ +#ifndef GRID_PERFCOUNT_H +#define GRID_PERFCOUNT_H + +#include +#include +#include +#include +#include +#include +#include + +namespace Grid { + + +static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, + int cpu, int group_fd, unsigned long flags) +{ + int ret; + + ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, + group_fd, flags); + return ret; +} + + +class PerformanceCounter { +private: + typedef struct { + public: + uint32_t type; + uint64_t config; + const char *name; + } PerformanceCounterConfig; + + static const PerformanceCounterConfig PerformanceCounterConfigs []; + +public: + + enum PerformanceCounterType { + CPUCYCLES=0, + INSTRUCTIONS, + // STALL_CYCLES, + CACHE_REFERENCES, + CACHE_MISSES, + L1D_READ_MISS, + L1D_READ_ACCESS, + L1D_WRITE_MISS, + L1D_WRITE_ACCESS, + L1D_PREFETCH_MISS, + L1D_PREFETCH_ACCESS, + LL_READ_MISS, + // LL_READ_ACCESS, + LL_WRITE_MISS, + LL_WRITE_ACCESS, + LL_PREFETCH_MISS, + LL_PREFETCH_ACCESS, + L1I_READ_MISS, + L1I_READ_ACCESS, + PERFORMANCE_COUNTER_NUM_TYPES + }; + +public: + + int PCT; + + struct perf_event_attr pe; + long long count; + int fd; + uint64_t elapsed; + uint64_t begin; + + static int NumTypes(void){ + return PERFORMANCE_COUNTER_NUM_TYPES; + } + + PerformanceCounter(int _pct) { + assert(_pct>=0); + assert(_pct