Grid 0.7.0
Stat.cc
Go to the documentation of this file.
1#include <Grid/GridCore.h>
3#include <Grid/perfmon/Stat.h>
4
6
8
9
10void PmuStat::init(const char *regname)
11{
12#ifdef __x86_64__
13 name = regname;
14 if (!pmu_initialized)
15 {
16 std::cout<<"initialising pmu"<<std::endl;
17 pmu_initialized = true;
18 pmu_init();
19 }
20 clear();
21#endif
22}
24{
25#ifdef __x86_64__
26 count = 0;
27 tregion = 0;
28 pmc0 = 0;
29 pmc1 = 0;
30 inst = 0;
31 cyc = 0;
32 ref = 0;
33 tcycles = 0;
34 reads = 0;
35 writes = 0;
36#endif
37}
39{
40#ifdef __x86_64__
41 std::cout <<"Reg "<<std::string(name)<<":\n";
42 std::cout <<" region "<<tregion<<std::endl;
43 std::cout <<" cycles "<<tcycles<<std::endl;
44 std::cout <<" inst "<<inst <<std::endl;
45 std::cout <<" cyc "<<cyc <<std::endl;
46 std::cout <<" ref "<<ref <<std::endl;
47 std::cout <<" pmc0 "<<pmc0 <<std::endl;
48 std::cout <<" pmc1 "<<pmc1 <<std::endl;
49 std::cout <<" count "<<count <<std::endl;
50 std::cout <<" reads "<<reads <<std::endl;
51 std::cout <<" writes "<<writes <<std::endl;
52#endif
53}
55{
56#ifdef __x86_64__
57 pmu_start();
58 ++count;
60 tstart = __rdtsc();
61#endif
62}
63void PmuStat::enter(int t)
64{
65#ifdef __x86_64__
66 counters[0][t] = __rdpmc(0);
67 counters[1][t] = __rdpmc(1);
68 counters[2][t] = __rdpmc((1<<30)|0);
69 counters[3][t] = __rdpmc((1<<30)|1);
70 counters[4][t] = __rdpmc((1<<30)|2);
71 counters[5][t] = __rdtsc();
72#endif
73}
74void PmuStat::exit(int t)
75{
76#ifdef __x86_64__
77 counters[0][t] = __rdpmc(0) - counters[0][t];
78 counters[1][t] = __rdpmc(1) - counters[1][t];
79 counters[2][t] = __rdpmc((1<<30)|0) - counters[2][t];
80 counters[3][t] = __rdpmc((1<<30)|1) - counters[3][t];
81 counters[4][t] = __rdpmc((1<<30)|2) - counters[4][t];
82 counters[5][t] = __rdtsc() - counters[5][t];
83#endif
84}
85void PmuStat::accum(int nthreads)
86{
87#ifdef __x86_64__
88 tend = __rdtsc();
90 pmu_stop();
91 for (int t = 0; t < nthreads; ++t) {
92 pmc0 += counters[0][t];
93 pmc1 += counters[1][t];
94 inst += counters[2][t];
95 cyc += counters[3][t];
96 ref += counters[4][t];
97 tcycles += counters[5][t];
98 }
99 uint64_t region = tend - tstart;
100 tregion += region;
101 uint64_t mreads = mrend - mrstart;
102 reads += mreads;
103 uint64_t mwrites = mwend - mwstart;
104 writes += mwrites;
105#endif
106}
107
108
109void PmuStat::pmu_fini(void) {}
110void PmuStat::pmu_start(void) {};
111void PmuStat::pmu_stop(void) {};
113{
114#ifdef _KNIGHTS_LANDING_
115 KNLsetup();
116#endif
117}
118void PmuStat::xmemctrs(uint64_t *mr, uint64_t *mw)
119{
120#ifdef _KNIGHTS_LANDING_
121 ctrs c;
122 KNLreadctrs(c);
123 uint64_t emr = 0, emw = 0;
124 for (int i = 0; i < NEDC; ++i)
125 {
126 emr += c.edcrd[i];
127 emw += c.edcwr[i];
128 }
129 *mr = emr;
130 *mw = emw;
131#else
132 *mr = *mw = 0;
133#endif
134}
135
136#ifdef _KNIGHTS_LANDING_
137
138struct knl_gbl_ PmuStat::gbl;
139
140#define PMU_MEM
141
142void PmuStat::KNLevsetup(const char *ename, int &fd, int event, int umask)
143{
144 char fname[1024];
145 snprintf(fname, sizeof(fname), "%s/type", ename);
146 FILE *fp = fopen(fname, "r");
147 if (fp == 0) {
148 ::printf("open %s", fname);
149 ::exit(0);
150 }
151 int type;
152 int ret = fscanf(fp, "%d", &type);
153 assert(ret == 1);
154 fclose(fp);
155 // std::cout << "Using PMU type "<<type<<" from " << std::string(ename) <<std::endl;
156
157 struct perf_event_attr hw = {};
158 hw.size = sizeof(hw);
159 hw.type = type;
160 // see /sys/devices/uncore_*/format/*
161 // All of the events we are interested in are configured the same way, but
162 // that isn't always true. Proper code would parse the format files
163 hw.config = event | (umask << 8);
164 //hw.read_format = PERF_FORMAT_GROUP;
165 // unfortunately the above only works within a single PMU; might
166 // as well just read them one at a time
167 int cpu = 0;
168 fd = perf_event_open(&hw, -1, cpu, -1, 0);
169 if (fd == -1) {
170 ::printf("CPU %d, box %s, event 0x%lx", cpu, ename, hw.config);
171 ::exit(0);
172 } else {
173 // std::cout << "event "<<std::string(ename)<<" set up for fd "<<fd<<" hw.config "<<hw.config <<std::endl;
174 }
175}
176
177
178void PmuStat::KNLsetup(void){
179
180 int ret;
181 char fname[1024];
182
183 // MC RPQ inserts and WPQ inserts (reads & writes)
184 for (int mc = 0; mc < NMC; ++mc)
185 {
186 ::snprintf(fname, sizeof(fname), "/sys/devices/uncore_imc_%d",mc);
187 // RPQ Inserts
188 KNLevsetup(fname, gbl.mc_rd[mc], 0x1, 0x1);
189 // WPQ Inserts
190 KNLevsetup(fname, gbl.mc_wr[mc], 0x2, 0x1);
191 }
192 // EDC RPQ inserts and WPQ inserts
193 for (int edc=0; edc < NEDC; ++edc)
194 {
195 ::snprintf(fname, sizeof(fname), "/sys/devices/uncore_edc_eclk_%d",edc);
196 // RPQ inserts
197 KNLevsetup(fname, gbl.edc_rd[edc], 0x1, 0x1);
198 // WPQ inserts
199 KNLevsetup(fname, gbl.edc_wr[edc], 0x2, 0x1);
200 }
201 // EDC HitE, HitM, MissE, MissM
202 for (int edc=0; edc < NEDC; ++edc)
203 {
204 ::snprintf(fname, sizeof(fname), "/sys/devices/uncore_edc_uclk_%d", edc);
205 KNLevsetup(fname, gbl.edc_hite[edc], 0x2, 0x1);
206 KNLevsetup(fname, gbl.edc_hitm[edc], 0x2, 0x2);
207 KNLevsetup(fname, gbl.edc_misse[edc], 0x2, 0x4);
208 KNLevsetup(fname, gbl.edc_missm[edc], 0x2, 0x8);
209 }
210}
211
212uint64_t PmuStat::KNLreadctr(int fd)
213{
214 uint64_t data;
215 size_t s = ::read(fd, &data, sizeof(data));
216 if (s != sizeof(uint64_t)){
217 ::printf("read counter %lu", s);
218 ::exit(0);
219 }
220 return data;
221}
222
223void PmuStat::KNLreadctrs(ctrs &c)
224{
225 for (int i = 0; i < NMC; ++i)
226 {
227 c.mcrd[i] = KNLreadctr(gbl.mc_rd[i]);
228 c.mcwr[i] = KNLreadctr(gbl.mc_wr[i]);
229 }
230 for (int i = 0; i < NEDC; ++i)
231 {
232 c.edcrd[i] = KNLreadctr(gbl.edc_rd[i]);
233 c.edcwr[i] = KNLreadctr(gbl.edc_wr[i]);
234 }
235 for (int i = 0; i < NEDC; ++i)
236 {
237 c.edchite[i] = KNLreadctr(gbl.edc_hite[i]);
238 c.edchitm[i] = KNLreadctr(gbl.edc_hitm[i]);
239 c.edcmisse[i] = KNLreadctr(gbl.edc_misse[i]);
240 c.edcmissm[i] = KNLreadctr(gbl.edc_missm[i]);
241 }
242}
243
244#endif
246
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
Definition Stat.h:46
void clear(void)
Definition Stat.cc:23
void enter(int t)
Definition Stat.cc:63
uint64_t tregion
Definition Stat.h:62
uint64_t pmc0
Definition Stat.h:65
uint64_t mrstart
Definition Stat.h:55
uint64_t writes
Definition Stat.h:54
uint64_t tcycles
Definition Stat.h:63
uint64_t mwstart
Definition Stat.h:57
uint64_t tstart
Definition Stat.h:68
uint64_t tend
Definition Stat.h:69
uint64_t cyc
Definition Stat.h:64
const char * name
Definition Stat.h:51
static void pmu_fini(void)
Definition Stat.cc:109
uint64_t counters[8][256]
Definition Stat.h:47
uint64_t count
Definition Stat.h:61
uint64_t inst
Definition Stat.h:64
static void xmemctrs(uint64_t *mr, uint64_t *mw)
Definition Stat.cc:118
void accum(int nthreads)
Definition Stat.cc:85
uint64_t reads
Definition Stat.h:53
static bool pmu_initialized
Definition Stat.h:77
uint64_t pmc1
Definition Stat.h:65
static void pmu_stop(void)
Definition Stat.cc:111
void start(void)
Definition Stat.cc:54
void print(void)
Definition Stat.cc:38
void init(const char *regname)
Definition Stat.cc:10
uint64_t mrend
Definition Stat.h:56
static void pmu_init(void)
Definition Stat.cc:112
uint64_t ref
Definition Stat.h:64
void exit(int t)
Definition Stat.cc:74
uint64_t mwend
Definition Stat.h:58
static void pmu_start(void)
Definition Stat.cc:110