|
43 | 43 | #include <vector> |
44 | 44 | #define PCM_DELAY_DEFAULT 1.0 // in seconds |
45 | 45 | #define PCM_DELAY_MIN 0.015 // 15 milliseconds is practical on most modern CPUs |
46 | | -#define PCM_CALIBRATION_INTERVAL 50 // calibrate clock only every 50th iteration |
47 | 46 | #define MAX_CORES 4096 |
48 | 47 |
|
49 | 48 | using namespace std; |
@@ -136,6 +135,7 @@ void print_usage(const string progname) |
136 | 135 | cerr << " [-e event1] [-e event2] [-e event3] .. => optional list of custom events to monitor\n"; |
137 | 136 | cerr << " event description example: cpu/umask=0x01,event=0x05,name=MISALIGN_MEM_REF.LOADS/ \n"; |
138 | 137 | cerr << " -yc | --yescores | /yc => enable specific cores to output\n"; |
| 138 | + cerr << " -i[=number] | /i[=number] => allow to determine number of iterations\n"; |
139 | 139 | print_help_force_rtm_abort_mode(41); |
140 | 140 | cerr << " Examples:\n"; |
141 | 141 | cerr << " " << progname << " 1 => print counters every second without core and socket output\n"; |
@@ -292,10 +292,8 @@ int main(int argc, char * argv[]) |
292 | 292 | char **sysArgv = NULL; |
293 | 293 | uint32 cur_event = 0; |
294 | 294 | bool csv = false; |
295 | | - long diff_usec = 0; // deviation of clock is useconds between measurements |
296 | 295 | uint64 txn_rate = 1; |
297 | | - int calibrated = PCM_CALIBRATION_INTERVAL - 2; // keeps track is the clock calibration needed |
298 | | - unsigned int numberOfIterations = 0; // number of iterations |
| 296 | + MainLoop mainLoop; |
299 | 297 | string program = string(argv[0]); |
300 | 298 | EventSelectRegister regs[PERF_MAX_COUNTERS]; |
301 | 299 | PCM::ExtendedCustomCoreEventDescription conf; |
@@ -335,17 +333,8 @@ int main(int argc, char * argv[]) |
335 | 333 | continue; |
336 | 334 | } |
337 | 335 | else |
338 | | - if (strncmp(*argv, "-i", 2) == 0 || |
339 | | - strncmp(*argv, "/i", 2) == 0) |
| 336 | + if (mainLoop.parseArg(*argv)) |
340 | 337 | { |
341 | | - string cmd = string(*argv); |
342 | | - size_t found = cmd.find('=', 2); |
343 | | - if (found != string::npos) { |
344 | | - string tmp = cmd.substr(found + 1); |
345 | | - if (!tmp.empty()) { |
346 | | - numberOfIterations = (unsigned int)atoi(tmp.c_str()); |
347 | | - } |
348 | | - } |
349 | 338 | continue; |
350 | 339 | } |
351 | 340 | else if (strncmp(*argv, "-c",2) == 0 || |
@@ -515,38 +504,12 @@ int main(int argc, char * argv[]) |
515 | 504 | } |
516 | 505 |
|
517 | 506 |
|
518 | | - unsigned int ic = 1; |
519 | | - |
520 | | - while ((ic <= numberOfIterations) || (numberOfIterations == 0)) |
| 507 | + mainLoop([&]() |
521 | 508 | { |
522 | 509 | if(!csv) cout << std::flush; |
523 | | - int delay_ms = int(delay * 1000); |
524 | | - int calibrated_delay_ms = delay_ms; |
525 | | -#ifdef _MSC_VER |
526 | | - // compensate slow Windows console output |
527 | | - if(AfterTime) delay_ms -= (int)(m->getTickCount() - BeforeTime); |
528 | | - if(delay_ms < 0) delay_ms = 0; |
529 | | -#else |
530 | | - // compensation of delay on Linux/UNIX |
531 | | - // to make the samling interval as monotone as possible |
532 | | - struct timeval start_ts, end_ts; |
533 | | - if(calibrated == 0) { |
534 | | - gettimeofday(&end_ts, NULL); |
535 | | - diff_usec = (end_ts.tv_sec-start_ts.tv_sec)*1000000.0+(end_ts.tv_usec-start_ts.tv_usec); |
536 | | - calibrated_delay_ms = delay_ms - diff_usec/1000.0; |
537 | | - } |
538 | | -#endif |
539 | | - if (sysCmd == NULL || numberOfIterations != 0 || m->isBlocked() == false) |
540 | | - { |
541 | | - MySleepMs(calibrated_delay_ms); |
542 | | - } |
543 | 510 |
|
544 | | -#ifndef _MSC_VER |
545 | | - calibrated = (calibrated + 1) % PCM_CALIBRATION_INTERVAL; |
546 | | - if(calibrated == 0) { |
547 | | - gettimeofday(&start_ts, NULL); |
548 | | - } |
549 | | -#endif |
| 511 | + calibratedSleep(delay, sysCmd, mainLoop, m); |
| 512 | + |
550 | 513 | AfterTime = m->getTickCount(); |
551 | 514 | m->getAllCounterStates(SysAfterState, DummySocketStates, AfterState); |
552 | 515 |
|
@@ -611,9 +574,9 @@ int main(int argc, char * argv[]) |
611 | 574 |
|
612 | 575 | if ( m->isBlocked() ) { |
613 | 576 | // in case PCM was blocked after spawning child application: break monitoring loop here |
614 | | - break; |
| 577 | + return false; |
615 | 578 | } |
616 | | - ++ic; |
617 | | - } |
| 579 | + return true; |
| 580 | + }); |
618 | 581 | exit(EXIT_SUCCESS); |
619 | 582 | } |
0 commit comments