scc  2022.4.0
SystemC components library
perf_estimator.cpp
1 /*******************************************************************************
2  * Copyright 2018 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #include "perf_estimator.h"
18 #include "report.h"
19 
20 #if defined(_WIN32)
21 #include <Windows.h>
22 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__MACH__) && defined(__APPLE__))
23 #include <ctime>
24 #include <sys/resource.h>
25 #include <sys/times.h>
26 #include <unistd.h>
27 #else
28 #error "Cannot compile file because of an unknown method to retrieve OS time."
29 #endif
30 #include <malloc.h>
31 
32 namespace scc {
33 using namespace sc_core;
34 
35 SC_HAS_PROCESS(perf_estimator);
36 
37 perf_estimator::perf_estimator(const sc_module_name& nm, sc_time beat_delay_)
38 : sc_module(nm)
39 , beat_delay(beat_delay_) {
40  soc.set();
41  if(beat_delay.value()) {
42  SC_METHOD(beat);
43  }
44 }
45 
46 perf_estimator::~perf_estimator() {
47  time_stamp eod;
48  eod.set();
49  SCCINFO("perf_estimator") << "constr & elab time: " << (eoe.proc_clock_stamp - soc.proc_clock_stamp) << "s";
50  SCCINFO("perf_estimator") << "simulation time: " << (eos.proc_clock_stamp - sos.proc_clock_stamp) << "s";
51  if(cycle_period.value()) {
52  uint64_t cycles = sc_time_stamp().value() / cycle_period.value();
53  SCCINFO("perf_estimator") << "simulation speed: "
54  << (sc_time_stamp().value() ? cycles / (eos.proc_clock_stamp - soc.proc_clock_stamp) : 0.0)
55  << " cycles/s";
56  }
57  SCCINFO("perf_estimator") << "max resident memory: " << max_memory << "kB";
58 }
59 
60 void perf_estimator::end_of_elaboration() { eoe.set(); }
61 
62 void perf_estimator::start_of_simulation() {
63  sos.set();
64  get_memory();
65 }
66 
67 void perf_estimator::end_of_simulation() {
68  eos.set();
69  sc_time now = sc_time_stamp();
70  unsigned long long elapsed_wall = (eos.wall_clock_stamp - sos.wall_clock_stamp).total_microseconds();
71  auto elapsed_proc = (unsigned long long)((eos.proc_clock_stamp - sos.proc_clock_stamp) * 1000000);
72  auto elapsed_sim = (unsigned long long)(now.to_seconds() * 1000000.);
73  if(elapsed_sim > 0) {
74  double wall_perf = elapsed_wall / elapsed_sim;
75  double proc_perf = elapsed_proc / elapsed_sim;
76  SCCINFO("perf_estimator") << "Wall clock (process clock) based simulation real time factor is " << wall_perf << "(" << proc_perf
77  << ")";
78  }
79  get_memory();
80 }
81 
82 void perf_estimator::beat() {
83  if(sc_time_stamp().value())
84  SCCINFO("perf_estimator") << "Heart beat, rss mem: " << get_memory() << "kB";
85  next_trigger(beat_delay);
86 #ifndef _MSC_VER
87  malloc_trim(0);
88 #endif
89 }
90 } /* namespace scc */
91 
92 auto scc::perf_estimator::time_stamp::get_cpu_time() -> double {
93 #if defined(_WIN32)
94  FILETIME create_time;
95  FILETIME exit_time;
96  FILETIME kernel_time;
97  FILETIME user_time;
98  if(GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time) != -1) {
99  SYSTEMTIME system_time;
100  if(FileTimeToSystemTime(&user_time, &system_time) != -1)
101  return (double)system_time.wHour * 3600.0 + (double)system_time.wMinute * 60.0 + (double)system_time.wSecond +
102  (double)system_time.wMilliseconds / 1000.;
103  }
104 #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__MACH__) && defined(__APPLE__))
105 #if _POSIX_TIMERS > 0
106  {
107  clockid_t id;
108  struct timespec stamp {};
109 #if _POSIX_CPUTIME > 0
110  if(clock_getcpuclockid(0, &id) == -1)
111 #endif
112 #if defined(CLOCK_PROCESS_CPUTIME_ID)
113  id = CLOCK_PROCESS_CPUTIME_ID;
114 #elif defined(CLOCK_VIRTUAL)
115  id = CLOCK_VIRTUAL;
116 #else
117  id = (clockid_t)-1;
118 #endif
119  if(id != (clockid_t)-1 && clock_gettime(id, &stamp) != -1)
120  return (double)stamp.tv_sec + (double)stamp.tv_nsec / 1000000000.0;
121  }
122 #endif
123 #if defined(RUSAGE_SELF)
124  {
125  struct rusage usage {};
126  if(getrusage(RUSAGE_SELF, &usage) != -1)
127  return (double)usage.ru_utime.tv_sec + (double)usage.ru_utime.tv_usec / 1000000.0;
128  }
129 #endif
130 #if defined(_SC_CLK_TICK)
131  {
132  const double ticks = (double)sysconf(_SC_CLK_TCK);
133  struct tms s;
134  if(times(&s) != (clock_t)-1)
135  return (double)s.tms_utime / ticks;
136  }
137 #endif
138 #if defined(CLOCKS_PER_SEC)
139  {
140  clock_t c = clock();
141  if(c != (clock_t)-1)
142  return (double)c / (double)CLOCKS_PER_SEC;
143  }
144 #endif
145 #endif
146  return 1.0;
147 }
148 
149 long scc::perf_estimator::get_memory() {
150 #if defined(RUSAGE_SELF)
151  {
152  struct rusage usage {};
153  if(getrusage(RUSAGE_SELF, &usage) != -1) {
154  max_memory = std::max(max_memory, usage.ru_maxrss);
155  return usage.ru_maxrss;
156  }
157  }
158 #endif
159  return 0L;
160 }
perf_estimator()
default constructor creating an unnamed perf_estimator
SCC SystemC utilities.