Debian Codegraph @

example/amdsmi_esmi_intg_example.cc

raw
1 /*
2 * Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 #include <cassert>
24 #include <cstdint>
25 #include <unistd.h>
26 #include <vector>
27 #include <iostream>
28 #include <iomanip>
29 #include "amd_smi/amdsmi.h"
30 #include <cstring>
31 #include <cmath>
32
33 #define SHOWLINESZ 256
34
35 #define CHK_AMDSMI_RET(RET) \
36 { \
37 if (RET != AMDSMI_STATUS_SUCCESS) { \
38 const char *err_str; \
39 amdsmi_status_t status; \
40 status = amdsmi_get_esmi_err_msg(RET, &err_str); \
41 std::cout << "AMDSMI call returned " << status << " at line " \
42 << __LINE__ << std::endl; \
43 std::cout << err_str << std::endl; \
44 return RET; \
45 } \
46 }
47
48 using std::cin;
49 using std::cout;
50 using std::endl;
51 using std::fixed;
52 using std::setprecision;
53 using std::vector;
54
55 int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) {
56 amdsmi_status_t ret;
57 uint32_t proto_ver;
58 amdsmi_smu_fw_version_t smu_fw = {};
59
60 // Initialize esmi for AMD CPUs
61 ret = amdsmi_init(AMDSMI_INIT_AMD_CPUS);
62 CHK_AMDSMI_RET(ret)
63
64 // Get all sockets
65 uint32_t socket_count = 0;
66
67 ret = amdsmi_get_socket_handles(&socket_count, nullptr);
68 CHK_AMDSMI_RET(ret)
69
70 // Allocate the memory for the sockets
71 vector<amdsmi_socket_handle> sockets(socket_count);
72
73 // Get the sockets of the system
74 ret = amdsmi_get_socket_handles(&socket_count, &sockets[0]);
75 CHK_AMDSMI_RET(ret)
76
77 cout << "Total Socket: " << socket_count << endl;
78
79 // For each socket, get cpus and cores
80 for (uint32_t i = 0; i < socket_count; i++) {
81 cout << endl << "Socket " << i << endl;
82 uint32_t cpu_count = 0;
83 uint32_t core_count = 0;
84
85 // Set processor type as AMDSMI_PROCESSOR_TYPE_AMD_CPU
86 processor_type_t processor_type = AMDSMI_PROCESSOR_TYPE_AMD_CPU;
87 ret = amdsmi_get_processor_handles_by_type(sockets[i], processor_type, nullptr, &cpu_count);
88 CHK_AMDSMI_RET(ret)
89
90 // Allocate the memory for the cpus
91 vector<amdsmi_processor_handle> plist(cpu_count);
92
93 // Get the cpus for each socket
94 ret = amdsmi_get_processor_handles_by_type(sockets[i], processor_type, &plist[0], &cpu_count);
95 CHK_AMDSMI_RET(ret)
96
97 // Set processor type as AMDSMI_PROCESSOR_TYPE_AMD_CPU_CORE
98 processor_type = AMDSMI_PROCESSOR_TYPE_AMD_CPU_CORE;
99 ret = amdsmi_get_processor_handles_by_type(sockets[i], processor_type, nullptr, &core_count);
100 CHK_AMDSMI_RET(ret)
101
102 // Allocate the memory for the cpu cores
103 vector<amdsmi_processor_handle> core_list(core_count);
104
105 // Get the cpu cores for each socket
106 ret = amdsmi_get_processor_handles_by_type(sockets[i], processor_type, &core_list[0], &core_count);
107 CHK_AMDSMI_RET(ret)
108
109 for (uint32_t index = 0; index < plist.size(); index++) {
110 ret = amdsmi_get_cpu_hsmp_proto_ver(plist[index], &proto_ver);
111 if(ret != AMDSMI_STATUS_SUCCESS)
112 cout<<"Failed to get hsmp proto version"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
113
114 cout<<"\n------------------------------------------";
115 cout<<"\n| HSMP Proto Version | "<< proto_ver <<"\t\t |"<< endl;
116 cout<<"------------------------------------------\n";
117
118 ret = amdsmi_get_cpu_smu_fw_version(plist[index], &smu_fw);
119 if(ret != AMDSMI_STATUS_SUCCESS)
120 cout<<"Failed to get smu fw version"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
121
122 cout<<"\n------------------------------------------";
123 cout<<"\n| SMU FW Version | "
124 <<(unsigned)smu_fw.major<<"."
125 <<(unsigned)smu_fw.minor<<"."
126 <<(unsigned)smu_fw.debug
127 <<"\t\t |"<<endl;
128 cout<<"------------------------------------------\n";
129
130 uint32_t err_bits = 0;
131
132 uint32_t prochot;
133 cout<<setprecision(3)<<" CPU "<<index<<"\t|";
134 cout<<"\n-------------------------------------------------";
135 cout<<"\n| ProchotStatus:\t\t |";
136
137 ret = amdsmi_get_cpu_prochot_status(plist[index], &prochot);
138 if(ret != AMDSMI_STATUS_SUCCESS)
139 cout<<"Failed to get prochot status"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
140
141 if (!ret) {
142 cout<<setprecision(7)<< (prochot ? "active" : "inactive")<<"\t|";
143 } else {
144 err_bits |= 1 << ret;
145 cout<<" NA (Err:" <<ret<<" |";
146 }
147 cout<<"\n-------------------------------------------------\n";
148
149 size_t len;
150 char str[SHOWLINESZ] = {};
151 int retVal = 0;
152 cout<<setprecision(3)<<" CPU "<<index<<"\t|";
153 cout<<"\n-------------------------------------------------";
154 cout<<"\n| fclk (Mhz)\t\t\t |";
155 retVal = snprintf(str, SHOWLINESZ, "\n| mclk (Mhz)\t\t\t |");
156
157 len = strlen(str);
158 uint32_t fclk, mclk;
159 err_bits = 0;
160
161 ret = amdsmi_get_cpu_fclk_mclk(plist[index], &fclk, &mclk);
162 if(ret != AMDSMI_STATUS_SUCCESS)
163 cout<<"Failed to get cpu fclk mclk"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
164
165 if (!ret) {
166 cout<<setprecision(7)<<" "<<fclk<<"\t\t|";
167 retVal = snprintf(str + len, SHOWLINESZ - len, " %d\t\t|", mclk);
168 } else {
169 err_bits |= 1 << ret;
170 cout<<" NA (Err: "<<setprecision(2)<<ret<<" |";
171 retVal = snprintf(str + len, SHOWLINESZ - len, " NA (Err: %-2d) |", ret);
172 }
173 if (retVal > 0 && retVal < SHOWLINESZ)
174 cout << str;
175 else
176 cout <<"error writing to buffer" << endl;
177
178 cout<<"\n-------------------------------------------------\n";
179
180 uint32_t socket_power;
181 cout<<setprecision(3)<<" CPU "<<index<<"\t|";
182 cout<<"\n-------------------------------------------------";
183 cout<<"\n| Power (Watts)\t\t\t | ";
184
185 ret = amdsmi_get_cpu_socket_power(plist[index], &socket_power);
186 if(ret != AMDSMI_STATUS_SUCCESS)
187 cout<<"Failed to get cpu socket power"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
188
189 if (!ret) {
190 cout<<fixed<<setprecision(3)<<static_cast<double>(socket_power)/1000<<"\t|";
191 } else {
192 err_bits |= 1 << ret;
193 cout<<" NA (Err:" <<ret<<" |";
194 }
195
196 uint32_t power_limit = 0;
197 cout<<"\n| PowerLimit (Watts)\t\t | ";
198
199 ret = amdsmi_get_cpu_socket_power_cap(plist[index], &power_limit);
200 if(ret != AMDSMI_STATUS_SUCCESS)
201 cout<<"Failed to get cpu socket power cap"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
202
203 if (!ret) {
204 cout<<fixed<<setprecision(3)<<static_cast<double>(power_limit)/1000<<"\t|";
205 } else {
206 err_bits |= 1 << ret;
207 cout<<" NA (Err:" <<ret<<" |";
208 }
209
210 uint32_t power_max = 0;
211 cout<<"\n| PowerLimitMax (Watts)\t\t | ";
212
213 ret = amdsmi_get_cpu_socket_power_cap_max(plist[index], &power_max);
214 if(ret != AMDSMI_STATUS_SUCCESS)
215 cout<<"Failed to get cpu socket power cap max"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
216
217 if (!ret) {
218 cout<<fixed<<setprecision(3)<<static_cast<double>(power_max)/1000<<"\t|";
219 } else {
220 err_bits |= 1 << ret;
221 cout<<" NA (Err:" <<ret<<" |";
222 }
223 cout<<"\n-------------------------------------------------\n";
224
225 uint32_t input_power;
226 power_max = 0;
227 cout<<"\nEnter the max power to be set:\n";
228 cin>>input_power;
229
230 ret = amdsmi_get_cpu_socket_power_cap_max(plist[index], &power_max);
231 if(ret != AMDSMI_STATUS_SUCCESS)
232 cout<<"Failed to get cpu socket power cap max"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
233
234 if ((ret == AMDSMI_STATUS_SUCCESS) && (input_power > power_max)) {
235 cout<<"Input power is more than max power limit,"
236 " limiting to "<<static_cast<double>(power_max)/1000<<"Watts\n";
237 input_power = power_max;
238 }
239
240 ret = amdsmi_set_cpu_socket_power_cap(plist[index], input_power);
241 if(ret != AMDSMI_STATUS_SUCCESS)
242 cout<<"Failed to set cpu socket power cap"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
243
244 if (!ret) {
245 cout<<"CPU ["<<index<<"] power_limit set to "
246 <<fixed<<setprecision(3)<<static_cast<double>(input_power)/1000<<" Watts successfully\n";
247 }
248
249 power_limit = 0;
250 cout<<"\n| PowerLimit (Watts) \t\t | ";
251
252 ret = amdsmi_get_cpu_socket_power_cap(plist[index], &power_limit);
253 if(ret != AMDSMI_STATUS_SUCCESS)
254 cout<<"Failed to get cpu socket power cap"<<"["<<index<<"] , Err["<<ret<<"] "<< endl;
255
256 if (!ret) {
257 cout<<fixed<<setprecision(3)<<static_cast<double>(power_limit)/1000<<"\t|";
258 } else {
259 err_bits |= 1 << ret;
260 cout<<" NA (Err:" <<ret<<" |";
261 }
262 cout<<"\n-------------------------------------------------\n";
263
264 double fraction_q10 = 1/pow(2,10);
265 double fraction_uq10 = fraction_q10;
266
267 amdsmi_hsmp_metrics_table_t mtbl = {};
268 ret = amdsmi_get_hsmp_metrics_table(plist[index], &mtbl);
269
270 if (ret != AMDSMI_STATUS_SUCCESS) {
271 cout<<"Failed to get Metrics Table for CPU["<<index<<"], Err["<<ret<<"]" << endl;
272 } else {
273 cout<<"\n| METRICS TABLE \t\t\t\t |\n";
274
275 cout<<"\n| ACCUMULATOR COUNTER | "<<mtbl.accumulation_counter<<"\t\t|";
276 cout<<"\n| SOCKET POWER LIMIT | "<<(mtbl.socket_power_limit * fraction_uq10)<<" W\t\t|";
277 cout<<"\n| MAX SOCKET POWER LIMIT | "<<(mtbl.max_socket_power_limit * fraction_uq10)<<" W\t\t|";
278 cout<<"\n| SOCKET POWER | "<<(mtbl.socket_power * fraction_uq10)<<" W\t\t|\n";
279
280 cout<<"\n| Effective frequency per AID: \t\t\t\t\t\t|";
281 cout<<"\n-------------------------------------------------------------------------";
282 cout<<"\n| AID | SOCCLK \t\t| VCLK \t\t| DCLK \t\t| LCLK \t\t|";
283 cout<<"\n-------------------------------------------------------------------------";
284 for(uint32_t j = 0; j < 4 ; j++){
285 cout<<fixed<<setprecision(3)<<"\n| ["<<j<<"] | "
286 <<(mtbl.socclk_frequency[j] * fraction_uq10)<<"MHz\t| "
287 <<(mtbl.vclk_frequency[j] * fraction_uq10)<<"MHz\t| "
288 <<(mtbl.dclk_frequency[j] * fraction_uq10)<<"MHz\t| "
289 <<(mtbl.lclk_frequency[j] * fraction_uq10)<<"MHz\t| ";
290 }
291 cout<<"\n-------------------------------------------------------------------------\n";
292 cout<<"\n-------------------------------------------------------------------------\n";
293 }
294 }
295 }
296 // Clean up resources allocated at amdsmi_init
297 ret = amdsmi_shut_down();
298 CHK_AMDSMI_RET(ret)
299
300 return 0;
301 }