17#include "perf_estimator.h"
22#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__MACH__) && defined(__APPLE__))
24#include <sys/resource.h>
28#error "Cannot compile file because of an unknown method to retrieve OS time."
30#if !defined(WIN32) and !defined(__APPLE__)
35using namespace sc_core;
37#if SYSTEMC_VERSION < 20250221
42, beat_delay(beat_delay_) {
44 if(beat_delay.value()) {
52 SCCINFO(
"perf_estimator") <<
"constr & elab time: " << (eoe.proc_clock_stamp -
soc.proc_clock_stamp) <<
"s";
53 SCCINFO(
"perf_estimator") <<
"simulation time: " << (eos.proc_clock_stamp - sos.proc_clock_stamp) <<
"s";
54 if(cycle_period.value()) {
55 uint64_t cycles = sc_time_stamp().value() / cycle_period.value();
56 SCCINFO(
"perf_estimator") <<
"simulation speed: "
57 << (sc_time_stamp().value() ? cycles / (eos.proc_clock_stamp -
soc.proc_clock_stamp) : 0.0)
60 SCCINFO(
"perf_estimator") <<
"max resident memory: " << max_memory <<
"kB";
65void perf_estimator::start_of_simulation() {
70void perf_estimator::end_of_simulation() {
72 sc_time now = sc_time_stamp();
73 unsigned long long elapsed_wall = (eos.wall_clock_stamp - sos.wall_clock_stamp).total_microseconds();
74 auto elapsed_proc = (
unsigned long long)((eos.proc_clock_stamp - sos.proc_clock_stamp) * 1000000);
75 auto elapsed_sim = (
unsigned long long)(now.to_seconds() * 1000000.);
77 double wall_perf = elapsed_wall / elapsed_sim;
78 double proc_perf = elapsed_proc / elapsed_sim;
79 SCCINFO(
"perf_estimator") <<
"Wall clock (process clock) based simulation real time factor is " << wall_perf <<
"(" << proc_perf
85void perf_estimator::beat() {
86 if(sc_time_stamp().value())
87 SCCINFO(
"perf_estimator") <<
"Heart beat, rss mem: " << get_memory() <<
"kB";
88 next_trigger(beat_delay);
89#if !defined(WIN32) and !defined(__APPLE__)
95auto scc::perf_estimator::time_stamp::get_cpu_time() ->
double {
101 if(GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time) != -1) {
102 SYSTEMTIME system_time;
103 if(FileTimeToSystemTime(&user_time, &system_time) != -1)
104 return (
double)system_time.wHour * 3600.0 + (double)system_time.wMinute * 60.0 + (
double)system_time.wSecond +
105 (double)system_time.wMilliseconds / 1000.;
107#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__MACH__) && defined(__APPLE__))
111 struct timespec stamp {};
112#if _POSIX_CPUTIME > 0
113 if(clock_getcpuclockid(0, &
id) == -1)
115#if defined(CLOCK_PROCESS_CPUTIME_ID)
116 id = CLOCK_PROCESS_CPUTIME_ID;
117#elif defined(CLOCK_VIRTUAL)
122 if(
id != (clockid_t)-1 && clock_gettime(
id, &stamp) != -1)
123 return (
double)stamp.tv_sec + (double)stamp.tv_nsec / 1000000000.0;
126#if defined(RUSAGE_SELF)
128 struct rusage usage {};
129 if(getrusage(RUSAGE_SELF, &usage) != -1)
130 return (
double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000.0;
133#if defined(_SC_CLK_TICK)
135 const double ticks = (double)sysconf(_SC_CLK_TCK);
137 if(times(&s) != (clock_t)-1)
138 return (
double)s.tms_utime / ticks;
141#if defined(CLOCKS_PER_SEC)
145 return (
double)c / (double)CLOCKS_PER_SEC;
152long scc::perf_estimator::get_memory() {
153#if defined(RUSAGE_SELF)
155 struct rusage usage {};
156 if(getrusage(RUSAGE_SELF, &usage) != -1) {
157 max_memory = std::max(max_memory, usage.ru_maxrss);
158 return usage.ru_maxrss;
perf_estimator()
default constructor creating an unnamed perf_estimator
void end_of_elaboration() override
SystemC callbacks.
virtual ~perf_estimator()
destructor
time_stamp soc
the recorded time stamps