/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/PerfCount.h Copyright (C) 2015 Author: Azusa Yamaguchi Author: Peter Boyle Author: paboyle This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. See the full license in the file "LICENSE" in the top level distribution directory *************************************************************************************/ /* END LEGAL */ #ifndef GRID_PERFCOUNT_H #define GRID_PERFCOUNT_H #include #include #include #include #include #include #ifdef __linux__ #include #include #else #include #endif namespace Grid { #ifdef __linux__ static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) { int ret=0; ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); return ret; } #endif #ifdef TIMERS_OFF inline uint64_t cyclecount(void){ return 0; } #define __SSC_MARK(mark) __asm__ __volatile__ ("movl %0, %%ebx; .byte 0x64, 0x67, 0x90 " ::"i"(mark):"%ebx") #define __SSC_STOP __SSC_MARK(0x110) #define __SSC_START __SSC_MARK(0x111) #else #define __SSC_MARK(mark) #define __SSC_STOP #define __SSC_START /* * cycle counters arch dependent */ #ifdef __bgq__ inline uint64_t cyclecount(void){ uint64_t tmp; asm volatile ("mfspr %0,0x10C" : "=&r" (tmp) ); return tmp; } #elif defined __x86_64__ #include inline uint64_t cyclecount(void){ return __rdtsc(); // unsigned int dummy; // return __rdtscp(&dummy); } #else inline uint64_t cyclecount(void){ return 0; } #endif #endif class PerformanceCounter { private: typedef struct { public: uint32_t type; uint64_t config; const char *name; int normalisation; } PerformanceCounterConfig; static const PerformanceCounterConfig PerformanceCounterConfigs []; public: enum PerformanceCounterType { CACHE_REFERENCES=0, CACHE_MISSES=1, CPUCYCLES=2, INSTRUCTIONS=3, L1D_READ_ACCESS=4, PERFORMANCE_COUNTER_NUM_TYPES=19 }; public: int PCT; long long count; long long cycles; int fd; int cyclefd; unsigned long long elapsed; uint64_t begin; static int NumTypes(void){ return PERFORMANCE_COUNTER_NUM_TYPES; } PerformanceCounter(int _pct) { #ifdef __linux__ assert(_pct>=0); assert(_pct