-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPUScaler.c
215 lines (175 loc) · 5.76 KB
/
CPUScaler.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <jni.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include "CPUScaler.h"
#include "arch_spec.h"
#include "msr.h"
rapl_msr_unit rapl_unit;
rapl_msr_parameter *parameters;
char *ener_info;
/*global variable*/
int *fd;
void copy_to_string(char *ener_info, char uncore_buffer[60], int uncore_num, char cpu_buffer[60], int cpu_num, char package_buffer[60], int package_num, int i, int *offset) {
memcpy(ener_info + *offset, &uncore_buffer, uncore_num);
//split sigh
ener_info[*offset + uncore_num] = '#';
memcpy(ener_info + *offset + uncore_num + 1, &cpu_buffer, cpu_num);
ener_info[*offset + uncore_num + cpu_num + 1] = '#';
if(i < num_pkg - 1) {
memcpy(ener_info + *offset + uncore_num + cpu_num + 2, &package_buffer, package_num);
offset += uncore_num + cpu_num + package_num + 2;
if(num_pkg > 1) {
ener_info[*offset] = '@';
offset++;
}
} else {
memcpy(ener_info + *offset + uncore_num + cpu_num + 2, &package_buffer, package_num + 1);
}
}
JNIEXPORT jint JNICALL Java_JWrapper_EnergyCheckUtils_ProfileInit(JNIEnv *env, jclass jcls) {
jintArray result;
int i;
char msr_filename[BUFSIZ];
get_cpu_model();
getSocketNum();
jint wraparound_energy;
/*only two domains are supported for parameters check*/
parameters = (rapl_msr_parameter *)malloc(2 * sizeof(rapl_msr_parameter));
fd = (int *) malloc(num_pkg * sizeof(int));
for(i = 0; i < num_pkg; i++) {
if(i > 0) {
core += num_pkg_thread / 2; //measure the first core of each package
}
sprintf(msr_filename, "/dev/cpu/%d/msr", core);
fd[i] = open(msr_filename, O_RDWR);
}
uint64_t unit_info= read_msr(fd[0], MSR_RAPL_POWER_UNIT);
//printf("open core: %d\n", core);
get_msr_unit(&rapl_unit, unit_info);
get_wraparound_energy(rapl_unit.energy);
wraparound_energy = (int)WRAPAROUND_VALUE;
return wraparound_energy;
}
JNIEXPORT jint JNICALL Java_JWrapper_EnergyCheckUtils_GetSocketNum(JNIEnv *env, jclass jcls) {
return (jint)getSocketNum();
}
JNIEXPORT jstring JNICALL Java_JWrapper_EnergyCheckUtils_EnergyStatCheck(JNIEnv *env,
jclass jcls) {
jstring ener_string;
double result = 0.0;
double package[num_pkg];
double pp0[num_pkg];
double pp1[num_pkg];
double dram[num_pkg];
//construct a string
//char *ener_info;
long dram_num = 0L;
long cpu_num = 0L;
long gpu_num = 0L;
long package_num = 0L;
int info_size;
int i;
int offset = 0;
for(i = 0; i < num_pkg; i++) {
char gpu_buffer[60];
char dram_buffer[60];
char cpu_buffer[60];
char package_buffer[60];
result = read_msr(fd[i], MSR_PKG_ENERGY_STATUS); //First 32 bits so don't need shift bits.
package[i] = (double) result * rapl_unit.energy;
result = read_msr(fd[i], MSR_PP0_ENERGY_STATUS);
pp0[i] = (double) result * rapl_unit.energy;
//printf("package energy: %f\n", package[i]);
sprintf(package_buffer, "%f", package[i]);
sprintf(cpu_buffer, "%f", pp0[i]);
//allocate space for string
//printf("%" PRIu32 "\n", cpu_model);
switch(cpu_model) {
case BROADWELL:
case KABYLAKE:
case SANDYBRIDGE_EP:
case HASWELL1:
case HASWELL2:
case HASWELL3:
case HASWELL_EP:
case SKYLAKE1:
case SKYLAKE2:
result = read_msr(fd[i],MSR_DRAM_ENERGY_STATUS);
dram[i] =(double)result*rapl_unit.energy;
sprintf(dram_buffer, "%f", dram[i]);
package_num = strlen(package_buffer);
dram_num = strlen(dram_buffer);
cpu_num = strlen(cpu_buffer);
if(i == 0) {
info_size = num_pkg * (dram_num + cpu_num + package_num + 4);
ener_info = (char *) malloc(info_size);
}
//copy_to_string(ener_info, dram_buffer, dram_num, cpu_buffer, cpu_num, package_buffer, package_num, i, &offset);
/*Insert socket number*/
memcpy(ener_info + offset, &dram_buffer, dram_num);
//split sigh
ener_info[offset + dram_num] = '#';
memcpy(ener_info + offset + dram_num + 1, &cpu_buffer, cpu_num);
ener_info[offset + dram_num + cpu_num + 1] = '#';
if(i < num_pkg - 1) {
memcpy(ener_info + offset + dram_num + cpu_num + 2, &package_buffer, package_num);
offset += dram_num + cpu_num + package_num + 2;
if(num_pkg > 1) {
ener_info[offset] = '@';
offset++;
}
} else {
memcpy(ener_info + offset + dram_num + cpu_num + 2, &package_buffer, package_num + 1);
}
break;
case SANDYBRIDGE:
case IVYBRIDGE:
result = read_msr(fd[i],MSR_PP1_ENERGY_STATUS);
pp1[i] = (double) result *rapl_unit.energy;
sprintf(gpu_buffer, "%f", pp1[i]);
package_num = strlen(package_buffer);
gpu_num = strlen(gpu_buffer);
cpu_num = strlen(cpu_buffer);
if(i == 0) {
info_size = num_pkg * (gpu_num + cpu_num + package_num + 4);
ener_info = (char *) malloc(info_size);
}
//copy_to_string(ener_info, gpu_buffer, gpu_num, cpu_buffer, cpu_num, package_buffer, package_num, i, &offset);
memcpy(ener_info + offset, &gpu_buffer, gpu_num);
//split sign
ener_info[offset + gpu_num] = '#';
memcpy(ener_info + offset + gpu_num + 1, &cpu_buffer, cpu_num);
ener_info[offset + gpu_num + cpu_num + 1] = '#';
if(i < num_pkg - 1) {
memcpy(ener_info + offset + gpu_num + cpu_num + 2, &package_buffer, package_num);
offset += gpu_num + cpu_num + package_num + 2;
if(num_pkg > 1) {
ener_info[offset] = '@';
offset++;
}
} else {
memcpy(ener_info + offset + gpu_num + cpu_num + 2, &package_buffer,
package_num + 1);
}
break;
default:
printf("non of archtectures are detected\n");
break;
}
}
ener_string = (*env)->NewStringUTF(env, ener_info);
free(ener_info);
return ener_string;
}
JNIEXPORT void JNICALL Java_JWrapper_EnergyCheckUtils_ProfileDealloc
(JNIEnv * env, jclass jcls) {
int i;
free(fd);
free(parameters);
}