17 #include "perf_estimator.h"
22 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__MACH__) && defined(__APPLE__))
24 #include <sys/resource.h>
25 #include <sys/times.h>
28 #error "Cannot compile file because of an unknown method to retrieve OS time."
30 #if !defined(WIN32) and !defined(__APPLE__)
35 using namespace sc_core;
37 SC_HAS_PROCESS(perf_estimator);
41 , beat_delay(beat_delay_) {
43 if(beat_delay.value()) {
48 perf_estimator::~perf_estimator() {
51 SCCINFO(
"perf_estimator") <<
"constr & elab time: " << (eoe.proc_clock_stamp - soc.proc_clock_stamp) <<
"s";
52 SCCINFO(
"perf_estimator") <<
"simulation time: " << (eos.proc_clock_stamp - sos.proc_clock_stamp) <<
"s";
53 if(cycle_period.value()) {
54 uint64_t cycles = sc_time_stamp().value() / cycle_period.value();
55 SCCINFO(
"perf_estimator") <<
"simulation speed: "
56 << (sc_time_stamp().value() ? cycles / (eos.proc_clock_stamp - soc.proc_clock_stamp) : 0.0)
59 SCCINFO(
"perf_estimator") <<
"max resident memory: " << max_memory <<
"kB";
62 void perf_estimator::end_of_elaboration() { eoe.set(); }
64 void perf_estimator::start_of_simulation() {
69 void perf_estimator::end_of_simulation() {
71 sc_time now = sc_time_stamp();
72 unsigned long long elapsed_wall = (eos.wall_clock_stamp - sos.wall_clock_stamp).total_microseconds();
73 auto elapsed_proc = (
unsigned long long)((eos.proc_clock_stamp - sos.proc_clock_stamp) * 1000000);
74 auto elapsed_sim = (
unsigned long long)(now.to_seconds() * 1000000.);
76 double wall_perf = elapsed_wall / elapsed_sim;
77 double proc_perf = elapsed_proc / elapsed_sim;
78 SCCINFO(
"perf_estimator") <<
"Wall clock (process clock) based simulation real time factor is " << wall_perf <<
"(" << proc_perf
84 void perf_estimator::beat() {
85 if(sc_time_stamp().value())
86 SCCINFO(
"perf_estimator") <<
"Heart beat, rss mem: " << get_memory() <<
"kB";
87 next_trigger(beat_delay);
88 #if !defined(WIN32) and !defined(__APPLE__)
94 auto scc::perf_estimator::time_stamp::get_cpu_time() ->
double {
100 if(GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time) != -1) {
101 SYSTEMTIME system_time;
102 if(FileTimeToSystemTime(&user_time, &system_time) != -1)
103 return (
double)system_time.wHour * 3600.0 + (double)system_time.wMinute * 60.0 + (
double)system_time.wSecond +
104 (double)system_time.wMilliseconds / 1000.;
106 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__MACH__) && defined(__APPLE__))
107 #if _POSIX_TIMERS > 0
110 struct timespec stamp {};
111 #if _POSIX_CPUTIME > 0
112 if(clock_getcpuclockid(0, &
id) == -1)
114 #
if defined(CLOCK_PROCESS_CPUTIME_ID)
115 id = CLOCK_PROCESS_CPUTIME_ID;
116 #elif defined(CLOCK_VIRTUAL)
121 if(
id != (clockid_t)-1 && clock_gettime(
id, &stamp) != -1)
122 return (
double)stamp.tv_sec + (double)stamp.tv_nsec / 1000000000.0;
125 #if defined(RUSAGE_SELF)
127 struct rusage usage {};
128 if(getrusage(RUSAGE_SELF, &usage) != -1)
129 return (
double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000.0;
132 #if defined(_SC_CLK_TICK)
134 const double ticks = (double)sysconf(_SC_CLK_TCK);
136 if(times(&s) != (clock_t)-1)
137 return (
double)s.tms_utime / ticks;
140 #if defined(CLOCKS_PER_SEC)
144 return (
double)c / (double)CLOCKS_PER_SEC;
151 long scc::perf_estimator::get_memory() {
152 #if defined(RUSAGE_SELF)
154 struct rusage usage {};
155 if(getrusage(RUSAGE_SELF, &usage) != -1) {
156 max_memory = std::max(max_memory, usage.ru_maxrss);
157 return usage.ru_maxrss;
perf_estimator()
default constructor creating an unnamed perf_estimator