-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.cpp
78 lines (71 loc) · 2.17 KB
/
metrics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "metrics.h"
unsigned long getTimeDiff(struct timespec start_time, struct timespec end_time) {
return (unsigned long)((end_time.tv_sec - start_time.tv_sec)*1000000 +
double(end_time.tv_nsec - start_time.tv_nsec)/1000);
return 0;
}
unsigned long rdpmc_retired_inst_all() {
#if _COLLECT_METRICS_
unsigned long a, d, c;
c = (1<<30);
__asm__ __volatile__("rdpmc" : "=a" (a), "=d" (d) : "c" (c));
return ((unsigned long)a) | (((unsigned long)d) << 32);
#else
return 0;
#endif
}
// This gives L3 cache misses
unsigned long rdpmc_llc_cache_misses() {
#if _COLLECT_METRICS_
// LONGEST_LAT_CACHE.MISS
// mask:event = 0x43412e in 0x186
unsigned long a, d, c;
c = 0;
__asm__ __volatile__("rdpmc" : "=a" (a), "=d" (d) : "c" (c));
return ((unsigned long)a) | (((unsigned long)d) << 32);
#else
return 0;
#endif
}
// This gives L2 cache references
// These are basically L1 misses
unsigned long rdpmc_l2_cache_refs() {
#if _COLLECT_METRICS_
// L2_RQSTS.REFERENCES
// mask:event = 0x43ef24 in 0x187
unsigned long a, d, c;
c = 1;
__asm__ __volatile__("rdpmc" : "=a" (a), "=d" (d) : "c" (c));
return ((unsigned long)a) | (((unsigned long)d) << 32);
#else
return 0;
#endif
}
// This gives L2 cache misses
unsigned long rdpmc_l2_cache_misses() {
#if _COLLECT_METRICS_
// L2_RQSTS.MISS
// mask:event = 0x433f24 in 0x188
unsigned long a, d, c;
c = 2;
__asm__ __volatile__("rdpmc" : "=a" (a), "=d" (d) : "c" (c));
return ((unsigned long)a) | (((unsigned long)d) << 32);
#else
return 0;
#endif
}
void getMetricsStart(Metrics &m) {
clock_gettime(CLOCK_MONOTONIC, &(m.startTime));
m.l3Miss = rdpmc_llc_cache_misses();
m.l2Miss = rdpmc_l2_cache_misses();
m.l1Miss = rdpmc_l2_cache_refs();
m.retiredInst = rdpmc_retired_inst_all();
}
void getMetricsEnd(Metrics &m) {
m.retiredInst = rdpmc_retired_inst_all() - m.retiredInst;
m.l1Miss = rdpmc_l2_cache_refs() - m.l1Miss;
m.l2Miss = rdpmc_l2_cache_misses() - m.l2Miss;
m.l3Miss = rdpmc_llc_cache_misses() - m.l3Miss;
clock_gettime(CLOCK_MONOTONIC, &(m.endTime));
m.timeElapsedus = getTimeDiff(m.startTime, m.endTime);
}