33 using tstring = std::basic_string<TCHAR>;
35 static auto& tcerr = std::wcerr;
37 static auto& tcerr = std::cerr;
41 typedef void (* print_usage_func)(
const std::string & progname);
42 double parse_delay(
const char * arg,
const std::string & progname, print_usage_func print_usage_func);
43 bool extract_argument_value(
const char * arg, std::initializer_list<const char*> arg_names, std::string & value);
44 bool check_argument_equals(
const char * arg, std::initializer_list<const char*> arg_names);
46 void exit_cleanup(
void);
47 void set_signal_handlers(
void);
48 void set_real_time_priority(
const bool & silent);
49 void restore_signal_handlers(
void);
51 void sigINT_handler(
int signum);
52 void sigHUP_handler(
int signum);
53 void sigUSR_handler(
int signum);
54 void sigSTOP_handler(
int signum);
55 void sigCONT_handler(
int signum);
58 void set_post_cleanup_callback(
void(*cb)(
void));
60 inline void MySleep(
int delay)
63 if (delay) Sleep(delay * 1000);
69 inline void MySleepMs(
int delay_ms)
72 if (delay_ms) Sleep((DWORD)delay_ms);
74 struct timespec sleep_intrval;
75 double complete_seconds;
76 sleep_intrval.tv_nsec =
static_cast<long>(1000000000.0 * (::modf(delay_ms / 1000.0, &complete_seconds)));
77 sleep_intrval.tv_sec =
static_cast<time_t
>(complete_seconds);
78 ::nanosleep(&sleep_intrval, NULL);
82 void MySystem(
char * sysCmd,
char ** argc);
85 #pragma warning (disable : 4068 ) // disable unknown pragma warning 89 #pragma GCC diagnostic push 90 #pragma GCC diagnostic ignored "-Woverloaded-virtual" 91 #elif defined __clang__ 92 #pragma clang diagnostic push 93 #pragma clang diagnostic ignored "-Woverloaded-virtual" 97 int_type overflow(int_type)
override {
return {}; }
100 #pragma GCC diagnostic pop 101 #elif defined __clang__ 102 #pragma clang diagnostic pop 105 template <
class IntType>
106 inline std::string unit_format(IntType n)
111 snprintf(buffer, 1024,
"%4d ", int32(n));
112 return std::string{buffer};
116 snprintf(buffer, 1024,
"%4d K", int32(n / 1000ULL));
117 return std::string{buffer};
119 if (n <= 9999999999ULL)
121 snprintf(buffer, 1024,
"%4d M", int32(n / 1000000ULL));
122 return std::string{buffer};
124 if (n <= 9999999999999ULL)
126 snprintf(buffer, 1024,
"%4d G", int32(n / 1000000000ULL));
127 return std::string{buffer};
130 snprintf(buffer, 1024,
"%4d T", int32(n / (1000000000ULL * 1000ULL)));
131 return std::string{buffer};
134 void print_cpu_details();
136 #define PCM_UNUSED(x) (void)(x) 138 #define PCM_COMPILE_ASSERT(condition) \ 139 typedef char pcm_compile_assert_failed[(condition) ? 1 : -1]; \ 140 pcm_compile_assert_failed pcm_compile_assert_failed_; \ 141 PCM_UNUSED(pcm_compile_assert_failed_); 144 class ThreadGroupTempAffinity
146 GROUP_AFFINITY PreviousGroupAffinity;
149 ThreadGroupTempAffinity();
150 ThreadGroupTempAffinity(
const ThreadGroupTempAffinity &);
151 ThreadGroupTempAffinity & operator = (
const ThreadGroupTempAffinity &);
154 ThreadGroupTempAffinity(uint32 core_id,
bool checkStatus =
true,
const bool restore_ =
false);
155 ~ThreadGroupTempAffinity();
165 checked_uint64(
const uint64 d,
const uint64 o) : data(d), overflows(o) {}
169 overflows += o.overflows;
176 constexpr uint64 counter_width = 48;
177 return data + overflows * (1ULL << counter_width) - o.data;
180 uint64 getRawData_NoOverflowProtection()
const {
return data; }
185 typedef std::istringstream pcm_sscanf;
190 explicit s_expect(
const char * s) : std::string(s) {}
191 explicit s_expect(
const std::string & s) : std::string(s) {}
192 friend std::istream & operator >> (std::istream & istr,
s_expect && s);
193 friend std::istream & operator >> (std::istream && istr,
s_expect && s);
196 void match(std::istream & istr)
const 198 istr >> std::noskipws;
199 const auto len = length();
200 char * buffer =
new char[len + 2];
202 istr.get(buffer, len+1);
203 if (*
this != std::string(buffer))
205 istr.setstate(std::ios_base::failbit);
211 inline std::istream & operator >> (std::istream & istr,
s_expect && s)
217 inline std::istream & operator >> (std::istream && istr,
s_expect && s)
223 inline std::pair<tm, uint64> pcm_localtime()
225 const auto durationSinceEpoch = std::chrono::system_clock::now().time_since_epoch();
226 const auto durationSinceEpochInSeconds = std::chrono::duration_cast<std::chrono::seconds>(durationSinceEpoch);
227 time_t now = durationSinceEpochInSeconds.count();
230 localtime_s(&result, &now);
232 localtime_r(&now, &result);
234 return std::make_pair(result, std::chrono::duration_cast<std::chrono::milliseconds>(durationSinceEpoch- durationSinceEpochInSeconds).count());
246 template <
class H1,
class H2,
class D>
247 inline void choose(
const CsvOutputType outputType, H1 h1Func, H2 h2Func, D dataFunc)
263 std::cerr <<
"PCM internal error: wrong CSvOutputType\n";
267 inline void printDateForCSV(
const CsvOutputType outputType, std::string separator = std::string(
","))
271 std::cout << separator << separator;
274 std::cout <<
"Date" << separator <<
"Time" << separator;
277 std::pair<tm, uint64> tt{ pcm_localtime() };
278 std::cout.precision(3);
279 char old_fill = std::cout.fill(
'0');
281 std::setw(4) << 1900 + tt.first.tm_year <<
'-' <<
282 std::setw(2) << 1 + tt.first.tm_mon <<
'-' <<
283 std::setw(2) << tt.first.tm_mday << separator <<
284 std::setw(2) << tt.first.tm_hour <<
':' <<
285 std::setw(2) << tt.first.tm_min <<
':' <<
286 std::setw(2) << tt.first.tm_sec <<
'.' <<
287 std::setw(3) << tt.second << separator;
288 std::cout.fill(old_fill);
289 std::cout.setf(std::ios::fixed);
290 std::cout.precision(2);
294 inline void printDateForJson(
const std::string& separator,
const std::string &jsonSeparator)
296 std::pair<tm, uint64> tt{ pcm_localtime() };
297 std::cout.precision(3);
298 char old_fill = std::cout.fill(
'0');
300 "Date" << jsonSeparator <<
"\"" <<
301 std::setw(4) << 1900 + tt.first.tm_year <<
'-' <<
302 std::setw(2) << 1 + tt.first.tm_mon <<
'-' <<
303 std::setw(2) << tt.first.tm_mday <<
"\"" << separator <<
304 "Time" << jsonSeparator <<
"\"" <<
305 std::setw(2) << tt.first.tm_hour <<
':' <<
306 std::setw(2) << tt.first.tm_min <<
':' <<
307 std::setw(2) << tt.first.tm_sec <<
'.' <<
308 std::setw(3) << tt.second <<
"\"" << separator;
309 std::cout.fill(old_fill);
310 std::cout.setf(std::ios::fixed);
311 std::cout.precision(2);
314 std::vector<std::string> split(
const std::string & str,
const char delim);
317 bool CheckAndForceRTMAbortMode(
const char * argv,
PCM * m);
319 void print_help_force_rtm_abort_mode(
const int alignment);
322 void parseParam(
int argc,
char* argv[],
const char* param, F f)
328 if ((std::string(
"-") + param == *argv) || (std::string(
"/") + param == *argv))
334 std::cerr <<
"ERROR: no parameter provided for option " << param <<
"\n";
345 unsigned numberOfIterations = 0;
348 bool parseArg(
const char * arg)
350 std::string arg_value;
351 if (extract_argument_value(arg, {
"-i",
"/i"}, arg_value))
353 numberOfIterations = (
unsigned int)atoi(arg_value.c_str());
359 unsigned getNumberOfIterations()
const 361 return numberOfIterations;
363 template <
class Body>
364 void operator ()(
const Body & body)
368 while ((i <= numberOfIterations) || (numberOfIterations == 0))
380 FILE * tryOpen(
const char * path,
const char * mode);
381 std::string readSysFS(
const char * path,
bool silent);
382 bool writeSysFS(
const char * path,
const std::string & value,
bool silent);
385 int calibratedSleep(
const double delay,
const char* sysCmd,
const MainLoop& mainLoop,
PCM* m);
388 double fraction{0.0};
389 std::string label{
""};
393 const std::string & label_,
394 char fill_) : fraction(fraction_), label(label_), fill(fill_) {}
397 void drawStackedBar(
const std::string & label, std::vector<StackedBarItem> & h,
const int width = 80);
400 bool match(
const std::string& subtoken,
const std::string& sname, uint64* result);
402 uint64 read_number(
const char* str);
407 struct {
unsigned int eax, ebx, ecx, edx; } reg;
414 __cpuid(info.array, leaf);
416 __asm__ __volatile__(
"cpuid" : \
417 "=a" (info.reg.eax),
"=b" (info.reg.ebx),
"=c" (info.reg.ecx),
"=d" (info.reg.edx) :
"a" (leaf));
421 inline void clear_screen() {
425 std::cout <<
"\033[2J\033[0;0H";
429 inline uint32 build_bit_ui(uint32 beg, uint32 end)
439 myll = (1 << (end + 1)) - 1;
445 inline uint32 extract_bits_ui(uint32 myin, uint32 beg, uint32 end)
462 myll = myll & build_bit_ui(beg1, end1);
466 inline uint64 build_bit(uint32 beg, uint32 end)
471 myll =
static_cast<uint64
>(-1);
475 myll = (1LL << (end + 1)) - 1;
481 inline uint64 extract_bits(uint64 myin, uint32 beg, uint32 end)
498 myll = myll & build_bit(beg1, end1);
502 std::string safe_getenv(
const char* env);
505 inline HANDLE openMSRDriver()
507 return CreateFile(TEXT(
"\\\\.\\RDMSR"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
513 void check_and_set_silent(
int argc,
char * argv[],
null_stream &nullStream2);
515 void print_pid_collection_message(
int pid);
517 inline bool isPIDOption(
char * argv [])
519 return check_argument_equals(*argv, {
"-pid",
"/pid"});
522 inline void parsePID(
int argc,
char* argv[],
int& pid)
524 parseParam(argc, argv,
"pid", [&pid](
const char* p) {
if (p) pid = atoi(p); });
Internal type and constant definitions.
CPU Performance Monitor.
Definition: cpucounters.h:543
Definition: pcm-iio.cpp:146