-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPerfCheckUtils.java
118 lines (98 loc) · 3.13 KB
/
PerfCheckUtils.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.lang.reflect.Field;
public class PerfCheckUtils {
public native static void perfInit(int numEvents, int isSet);
public native static void singlePerfEventCheck(String eventNames);
public native static void groupPerfEventsCheck(String eventNames);
public native static void perfEnable();
public native static void perfDisable();
public native static void perfSingleRead(int id, long[] buffer);
public native static void perfMultRead(long[] buffer);
public native static long processSingleValue(long[] buffer);
public native static long[] processMultiValue(long[] buffer);
public static int eventNum = 0;
//For testing, Make the variable not be optimized as static
static int[] test = new int[100000000];
static {
System.setProperty("java.library.path", System.getProperty("user.dir"));
try {
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);
} catch (Exception e) { }
System.loadLibrary("perfCheck");
}
/**
* Initialize perf check utilities
*
* @eventNames String names of hardware counters to be checked
* @isGrouped Do you want get @eventNames counters as single read?
*/
public static void perfEventInit(String eventNames, boolean isGrouped) {
int setGroup;
String[] eventName = eventNames.split(",");
eventNum = eventName.length;
setGroup = isGrouped ? 1 : 0;
perfInit(eventNum, setGroup);
if(isGrouped) {
groupPerfEventsCheck(eventNames);
} else {
singlePerfEventCheck(eventNames);
}
perfEnable();
}
/**
* Get multiple perf counter values with single read
*/
public static long[] getMultiPerfCounter() {
long[] buffer = new long[3 + 2 * eventNum];
long[] results = new long[eventNum];
if(eventNum > 0) {
perfMultRead(buffer);
results = processMultiValue(buffer);
} else {
System.err.println("event number is 0, should call perfEventInit first!");
System.exit(-1);
}
return results;
}
/**
* Get one perf counter value a time
*/
public static long[] getSinglePerfCounter() {
long[] buffer = new long[3];
long[] results = new long[eventNum];
if(eventNum > 0) {
for(int i = 0; i < results.length; i++) {
perfSingleRead(i, buffer);
results[i] = processSingleValue(buffer);
}
} else {
System.err.println("event number is 0, should call perfEventInit first!");
System.exit(-1);
}
return results;
}
public static void main(String[] args) {
long[] preamble;
long[] epilogue;
String counters = "cache-misses,cache-references";
// perfEventInit(counters, false);
perfEventInit(counters, true);
// preamble = getSinglePerfCounter();
preamble = getMultiPerfCounter();
System.out.println("Testing...");
for(int i = 0; i < test.length; i++) {
test[i] *= test[i] + i;
}
System.out.println("Finish");
// try {
// Thread.sleep(10000);
// } catch(Exception e) {
// }
// epilogue = getSinglePerfCounter();
epilogue = getMultiPerfCounter();
System.out.println("cache misses is: " + (epilogue[0] - preamble[0]));
System.out.println("cache references is: " + (epilogue[1] - preamble[1]));
perfDisable();
}
}