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
|
#include <glib.h>
#include <cpuid.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include "stdlib.h"
#include "string.h"
#include "zenmonitor.h"
#include "msr.h"
#define MSR_PWR_PRINTF_FORMAT " %8.3f W"
#define MESUREMENT_TIME 0.1
// AMD PPR = https://www.amd.com/system/files/TechDocs/54945_PPR_Family_17h_Models_00h-0Fh.pdf
// AMD OSRR = https://developer.amd.com/wp-content/resources/56255_3_03.PDF
guint cores = 0;
guint threads_per_code = 0;
gdouble energy_unit = 0;
gint *msr_files = NULL;
gulong package_eng_b = 0;
gulong package_eng_a = 0;
gulong *core_eng_b = NULL;
gulong *core_eng_a = NULL;
gfloat package_power;
gfloat package_power_min;
gfloat package_power_max;
gfloat *core_power;
gfloat *core_power_min;
gfloat *core_power_max;
static guint get_core_count() {
guint eax = 0, ebx = 0, ecx = 0, edx = 0;
guint logical_cpus;
// AMD PPR: page 57 - CPUID_Fn00000001_EBX
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
logical_cpus = (ebx >> 16) & 0xFF;
// AMD PPR: page 82 - CPUID_Fn8000001E_EBX
__get_cpuid(0x8000001E, &eax, &ebx, &ecx, &edx);
threads_per_code = ((ebx >> 8) & 0xF) + 1;
if (threads_per_code == 0)
return logical_cpus;
return logical_cpus / threads_per_code;
}
static gint open_msr(gshort core) {
gchar msr_path[20];
sprintf(msr_path, "/dev/cpu/%d/msr", core * threads_per_code);
return open(msr_path, O_RDONLY);
}
static gboolean read_msr(gint file, guint index, gulong *data) {
if (file < 0)
return FALSE;
return pread(file, data, sizeof *data, index) == sizeof *data;
}
gdouble get_energy_unit() {
gulong data;
// AMD OSRR: page 139 - MSRC001_0299
if (!read_msr(msr_files[0], 0xC0010299, &data))
return 0.0;
return pow(1.0/2.0, (double)((data >> 8) & 0x1F));
}
gulong get_package_energy() {
gulong data;
// AMD OSRR: page 139 - MSRC001_029B
if (!read_msr(msr_files[0], 0xC001029B, &data))
return 0;
return data;
}
gulong get_core_energy(gint core) {
gulong data;
// AMD OSRR: page 139 - MSRC001_029A
if (!read_msr(msr_files[core], 0xC001029A, &data))
return 0;
return data;
}
gboolean msr_init() {
int i;
size_t sz;
if (!check_zen())
return FALSE;
cores = get_core_count();
if (cores == 0)
return FALSE;
msr_files = malloc(cores * sizeof (gint));
for (i = 0; i < cores; i++) {
msr_files[i] = open_msr(i);
}
energy_unit = get_energy_unit();
if (energy_unit == 0)
return FALSE;
core_eng_b = malloc(cores * sizeof (gulong));
core_eng_a = malloc(cores * sizeof (gulong));
core_power = malloc(cores * sizeof (gfloat));
core_power_min = malloc(cores * sizeof (gfloat));
core_power_max = malloc(cores * sizeof (gfloat));
msr_update();
memcpy(core_power_min, core_power, cores * sizeof (gfloat));
memcpy(core_power_max, core_power, cores * sizeof (gfloat));
package_power_min = package_power;
package_power_max = package_power;
return TRUE;
}
void msr_update() {
GSList *list = NULL;
gint i;
package_eng_b = get_package_energy();
for (i = 0; i < cores; i++) {
core_eng_b[i] = get_core_energy(i);
}
usleep(MESUREMENT_TIME*1000000);
package_eng_a = get_package_energy();
for (i = 0; i < cores; i++) {
core_eng_a[i] = get_core_energy(i);
}
package_power = (package_eng_a - package_eng_b) * energy_unit / MESUREMENT_TIME;
if (package_power < package_power_min)
package_power_min = package_power;
if (package_power > package_power_max)
package_power_max = package_power;
for (i = 0; i < cores; i++) {
core_power[i] = (core_eng_a[i] - core_eng_b[i]) * energy_unit / MESUREMENT_TIME;
if (core_power[i] < core_power_min[i])
core_power_min[i] = core_power[i];
if (core_power[i] > core_power_max[i])
core_power_max[i] = core_power[i];
}
}
void msr_clear_minmax() {
gint i;
package_power_min = package_power;
package_power_max = package_power;
for (i = 0; i < cores; i++) {
core_power_min[i] = core_power[i];
}
}
GSList* msr_get_sensors() {
GSList *list = NULL;
SensorInit *data;
gint i;
data = sensor_init_new();
data->label = g_strdup("Package Power");
data->value = &package_power;
data->min = &package_power_min;
data->max = &package_power_max;
data->printf_format = MSR_PWR_PRINTF_FORMAT;
list = g_slist_append(list, data);
for (i = 0; i < cores; i++) {
data = sensor_init_new();
data->label = g_strdup_printf("Core %d Power", i);
data->value = &(core_power[i]);
data->min = &(core_power_min[i]);
data->max = &(core_power_max[i]);
data->printf_format = MSR_PWR_PRINTF_FORMAT;
list = g_slist_append(list, data);
}
return list;
}
|