NRP Core  1.4.1
time_utils.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef TIME_UTILS_H
23 #define TIME_UTILS_H
24 
25 #include <chrono>
26 #include <map>
27 #include <fstream>
28 #include <filesystem>
29 #include <unistd.h>
30 
31 using SimulationTime = std::chrono::nanoseconds;
32 
40 template <class vartype, class ratio>
41 static SimulationTime toSimulationTime(vartype time)
42 {
43  return std::chrono::duration_cast<SimulationTime>(std::chrono::duration<vartype, ratio>(time));
44 }
45 
53 
57 template <class vartype, class ratio>
58 static vartype fromSimulationTime(SimulationTime time)
59 {
60  return std::chrono::duration_cast<std::chrono::duration<vartype, ratio>>(time).count();
61 }
62 
63 
74 double getRoundedRunTimeMs(const SimulationTime runTime, const float simulationResolutionMs);
75 
80 std::string getTimestamp();
81 
82 
84 
85 #ifdef TIME_PROFILE
86 
87 // macro used to generate unique (within a file) anonymous var names
88 #define _CONCAT_(x,y) x ## y
89 #define CONCAT(x,y) _CONCAT_(x,y)
90 #define ANONYMOUS_VAR CONCAT(_anonymous, __LINE__)
91 
95 #define NRP_LOG_TIME_SET_START TimeProfiler::setStartTime()
96 
100 #define NRP_LOG_TIME(filename) TimeProfiler::recordTimePoint(filename)
101 #define NRP_LOG_TIME_WITH_COMMENT(filename, comment) TimeProfiler::recordTimePoint(filename, comment)
102 
106 #define NRP_LOG_TIME_BLOCK(filename) auto ANONYMOUS_VAR = BlockProfiler(filename)
107 #define NRP_LOG_TIME_BLOCK_WITH_COMMENT(filename, comment) auto ANONYMOUS_VAR = BlockProfiler(filename, comment)
108 
112 struct TimeProfiler {
113 
114  static std::map<std::string, std::ofstream> files;
115  static std::chrono::time_point<std::chrono::high_resolution_clock> start;
116  static std::string timeLogsDir;
117 
121  static void setStartTime();
122 
130  static void recordTimePoint(const std::string& filename, const std::string& comment = "", bool newLine = true);
131 
139  static void recordDuration(const std::string& filename, const std::chrono::microseconds& duration, const std::string& comment = "", bool newLine = true);
140 };
141 
145 struct BlockProfiler {
146  BlockProfiler() = delete;
147 
154  BlockProfiler(const std::string& filename, const std::string& comment="");
155 
156  ~BlockProfiler();
157 
158  std::chrono::time_point<std::chrono::high_resolution_clock> _start;
159  std::string _filename;
160  std::string _comment;
161 };
162 
163 #else
164 #define NRP_LOG_TIME_SET_START
165 #define NRP_LOG_TIME(filename)
166 #define NRP_LOG_TIME_WITH_COMMENT(filename, comment)
167 #define NRP_LOG_TIME_BLOCK(filename)
168 #define NRP_LOG_TIME_BLOCK_WITH_COMMENT(filename, comment)
169 #endif
170 
171 #endif // TIME_UTILS_H
172 
173 // EOF
getTimestamp
std::string getTimestamp()
returns the current local time as a string in the format: YYMMDD-hhmmss-pid, being pid the pid of the...
Definition: time_utils.cpp:78
toSimulationTimeFromSeconds
SimulationTime toSimulationTimeFromSeconds(double time)
Converts floating-point seconds into SimulationTime.
Definition: time_utils.cpp:4
getRoundedRunTimeMs
double getRoundedRunTimeMs(const SimulationTime runTime, const float simulationResolutionMs)
Calculates simulation run time rounded to milliseconds, accounting for given resolution.
Definition: time_utils.cpp:55
SimulationTime
std::chrono::nanoseconds SimulationTime
Definition: time_utils.h:31