forked from autowarefoundation/autoware_universe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsr_reader.cpp
309 lines (269 loc) · 9.27 KB
/
msr_reader.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2020 Autoware Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file msr_reader.cpp
* @brief MSR read class
*/
#include "system_monitor/msr_reader/msr_reader.hpp"
#include <boost/archive/text_oarchive.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <syslog.h>
#include <unistd.h>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <vector>
namespace fs = boost::filesystem;
// 7634-7647 Unassigned
constexpr int PORT = 7634;
/**
* @brief Package Thermal Status Information
* For details please see the documents below.
* - Intel® 64 and IA-32 ArchitecturesSoftware Developer’s Manual
* https://software.intel.com/sites/default/files/managed/39/c5/325462-sdm-vol-1-2abcd-3abcd.pdf
*/
typedef struct
{
uint64_t pkg_thermal_status_ : 1; //!< @brief 0 Pkg Thermal Status (RO)
// cppcheck-suppress unusedStructMember
uint64_t pkg_thermal_status_log_ : 1; //!< @brief 1 Pkg Thermal Status Log (R/W)
// cppcheck-suppress unusedStructMember
uint64_t pkg_prochot_event_ : 1; //!< @brief 2 Pkg PROCHOT # event (RO)
// cppcheck-suppress unusedStructMember
uint64_t pkg_prochot_log_ : 1; //!< @brief 3 Pkg PROCHOT # log (R/WC0)
// cppcheck-suppress unusedStructMember
uint64_t pkg_critical_temperature_status_ : 1; //!< @brief 4 Pkg Critical Temperature Status (RO)
uint64_t //!< @brief 5 Pkg Critical Temperature
// cppcheck-suppress unusedStructMember
pkg_critical_temperature_status_log_ : 1; //!< Status Log (R/WC0)
// cppcheck-suppress unusedStructMember
uint64_t pkg_thermal_threshold_1_status_ : 1; //!< @brief 6 Pkg Thermal Threshold #1 Status (RO)
// cppcheck-suppress unusedStructMember
uint64_t pkg_thermal_threshold_1_log_ : 1; //!< @brief 7 Pkg Thermal Threshold #1 log (R/WC0)
// cppcheck-suppress unusedStructMember
uint64_t pkg_thermal_threshold_2_status_ : 1; //!< @brief 8 Pkg Thermal Threshold #2 Status (RO)
// cppcheck-suppress unusedStructMember
uint64_t pkg_thermal_threshold_2_log_ : 1; //!< @brief 9 Pkg Thermal Threshold #2 log (R/WC0)
// cppcheck-suppress unusedStructMember
uint64_t pkg_power_limitation_status_ : 1; //!< @brief 10 Pkg Power Limitation Status (RO)
// cppcheck-suppress unusedStructMember
uint64_t pkg_power_limitation_log_ : 1; //!< @brief 11 Pkg Power Limitation log (R/WC0)
// cppcheck-suppress unusedStructMember
uint64_t reserved1_ : 4; //!< @brief 15:12 Reserved
// cppcheck-suppress unusedStructMember
uint64_t pkg_digital_readout_ : 7; //!< @brief 22:16 Pkg Digital Readout (RO)
// cppcheck-suppress unusedStructMember
uint64_t reserved2_ : 41; //!< @brief 63:23 Reserved
} PackageThermalStatus;
/**
* @brief print usage
*/
void usage()
{
printf("Usage: msr_reader [options]\n");
printf(" -h --help : Display help\n");
printf(" -p --port # : Port number to listen to.\n");
printf("\n");
}
/**
* @brief check CPU thermal throttling
* @param [in] port port to listen
* @param [in] list list of path to msr
*/
void run(int port, const std::vector<std::string> & list)
{
// Create a new socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
syslog(LOG_ERR, "Failed to create a new socket. %s\n", strerror(errno));
return;
}
// Allow address reuse
int ret = 0;
int opt = 1;
ret = setsockopt(
sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&opt), (socklen_t)sizeof(opt));
if (ret < 0) {
syslog(LOG_ERR, "Failed to set socket FD's option. %s\n", strerror(errno));
close(sock);
return;
}
// Give the socket FD the local address ADDR
sockaddr_in addr;
memset(&addr, 0, sizeof(sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
// cppcheck-suppress cstyleCast
ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) {
syslog(LOG_ERR, "Failed to give the socket FD the local address ADDR. %s\n", strerror(errno));
close(sock);
return;
}
// Prepare to accept connections on socket FD
ret = listen(sock, 5);
if (ret < 0) {
syslog(LOG_ERR, "Failed to prepare to accept connections on socket FD. %s\n", strerror(errno));
close(sock);
return;
}
sockaddr_in client;
socklen_t len = sizeof(client);
while (true) {
// Await a connection on socket FD
int new_sock = accept(sock, reinterpret_cast<sockaddr *>(&client), &len);
if (new_sock < 0) {
syslog(
LOG_ERR, "Failed to prepare to accept connections on socket FD. %s\n", strerror(errno));
close(sock);
return;
}
ret = 0;
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
MSRInfo msr{0, {}};
for (auto itr = list.begin(); itr != list.end(); ++itr) {
// Open a file
int fd = open(itr->c_str(), O_RDONLY);
if (fd < 0) {
msr.error_code_ = errno;
syslog(LOG_ERR, "Failed to open a file. %s\n", strerror(msr.error_code_));
break;
}
// Read from a file descriptor
PackageThermalStatus val;
ret = pread(fd, &val, sizeof(uint64_t), 0x1b1);
if (ret < 0) {
msr.error_code_ = errno;
syslog(LOG_ERR, "Failed to read from a file descriptor. %s\n", strerror(msr.error_code_));
close(fd);
break;
}
// Close the file descriptor FD
ret = close(fd);
if (ret < 0) {
msr.error_code_ = errno;
syslog(LOG_ERR, "Failed to close the file descriptor FD. %s\n", strerror(msr.error_code_));
break;
}
msr.pkg_thermal_status_.push_back(val.pkg_thermal_status_);
}
oa << msr;
// Write N bytes of BUF to FD
ret = write(new_sock, oss.str().c_str(), oss.str().length());
if (ret < 0) {
syslog(LOG_ERR, "Failed to write N bytes of BUF to FD. %s\n", strerror(errno));
}
// Close the file descriptor FD
ret = close(new_sock);
if (ret < 0) {
syslog(LOG_ERR, "Failed to close the file descriptor FD. %s\n", strerror(errno));
}
}
close(sock);
}
int main(int argc, char ** argv)
{
try {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};
// Parse command-line options
int c = 0;
int option_index = 0;
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;
case 'p':
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());
return EXIT_FAILURE;
}
break;
default:
break;
}
}
if (!fs::exists("/dev/cpu")) {
printf("Failed to access /dev/cpu.\n");
return EXIT_FAILURE;
}
std::vector<std::string> list;
const fs::path root("/dev/cpu");
for (const fs::path & path : boost::make_iterator_range(
fs::recursive_directory_iterator(root), fs::recursive_directory_iterator())) {
if (fs::is_directory(path)) {
continue;
}
std::cmatch match;
const char * msr = path.generic_string().c_str();
// /dev/cpu/[0-9]/msr ?
if (!std::regex_match(msr, match, std::regex(".*msr"))) {
continue;
}
list.push_back(path.generic_string());
}
std::sort(list.begin(), list.end(), [](const std::string & c1, const std::string & c2) {
std::cmatch match;
const std::regex filter(".*/(\\d+)/msr");
int n1 = 0;
int n2 = 0;
if (std::regex_match(c1.c_str(), match, filter)) {
n1 = std::stoi(match[1].str());
}
if (std::regex_match(c2.c_str(), match, filter)) {
n2 = std::stoi(match[1].str());
}
return n1 < n2;
}); // NOLINT
if (list.empty()) {
printf("No msr found in /dev/cpu.\n");
return EXIT_FAILURE;
}
// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;
}
// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);
run(port, list);
// Close descriptor used to write to system logger
closelog();
} catch (const std::exception & e) {
std::cerr << "Exception in main(): " << e.what() << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cerr << "Unknown exception in main()" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}