PandA-2024.02
cpu_time.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
44 #ifndef CPU_TIME_HPP
45 #define CPU_TIME_HPP
46 
47 #include "config_HAVE_OPENMP.hpp"
48 
49 #if HAVE_OPENMP
50 #include "omp.h"
51 #endif
52 
53 #ifdef _WIN32
54 #include <windows.h>
55 #undef IN
56 #undef OUT
57 #else
58 #include <sys/times.h>
59 #endif
60 
61 #include "dbgPrintHelper.hpp"
62 #include <unistd.h>
63 
64 #if defined(_SC_CLK_TCK)
65 #define TIMES_TICKS_PER_SEC sysconf(_SC_CLK_TCK)
66 #elif defined(CLK_TCK)
67 #define TIMES_TICKS_PER_SEC CLK_TCK
68 #elif defined(HZ)
69 #define TIMES_TICKS_PER_SEC HZ
70 #else // !CLK_TCK && !_SC_CLK_TCK && !HZ
71 #define TIMES_TICKS_PER_SEC 60
72 #endif // !CLK_TCK && !_SC_CLK_TCK && !HZ
73 
78 inline long int p_cpu_time()
79 {
80 #ifdef _WIN32
81  FILETIME creationTime, exitTime, kernelTime, userTime;
82  if(GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime))
83  {
84  ULARGE_INTEGER integerTime;
85  integerTime.u.LowPart = userTime.dwLowDateTime;
86  integerTime.u.HighPart = userTime.dwHighDateTime;
87  return (long)(integerTime.QuadPart / 10000);
88  }
89  else
90  return 0;
91 #else
92  long t;
93  struct tms now;
94  clock_t ret = times(&now);
95  if(ret == static_cast<clock_t>(-1))
96  {
97  // cppcheck-suppress unreadVariable
98  now.tms_utime = now.tms_stime = now.tms_cutime = now.tms_cstime = 0;
99  }
100  // cppcheck-suppress ConfigurationNotChecked
101  t = (long(now.tms_utime) * 1000) / (TIMES_TICKS_PER_SEC) + (long(now.tms_cutime) * 1000) / (TIMES_TICKS_PER_SEC);
102  return t;
103 #endif
104 }
105 
110 inline std::string print_cpu_time(long int t)
111 {
112  std::string ost;
113  ost = std::to_string(t / 1000) + ".";
114  long centisec = (t % 1000) / 10;
115  if(centisec < 10)
116  {
117  ost += "0" + std::to_string(centisec);
118  }
119  else
120  {
121  ost += std::to_string(centisec);
122  }
123  return ost;
124 }
125 
126 void inline dump_exec_time(const std::string& thing, long et)
127 {
128  // cppcheck-suppress duplicateExpression
129  INDENT_OUT_MEX(0, 0, thing + ": " + print_cpu_time(et) + " seconds;");
130 }
131 
133 #define START_TIME(time_var) time_var = p_cpu_time()
134 
136 #define STOP_TIME(time_var) time_var = p_cpu_time() - (time_var)
137 
142 inline long int p_cpu_wtime()
143 {
144 #if HAVE_OPENMP
145  return static_cast<long int>(1000 * omp_get_wtime());
146 #else
147  return p_cpu_time();
148 #endif
149 }
150 
152 #define START_WTIME(time_var) time_var = p_cpu_wtime()
153 
155 #define STOP_WTIME(time_var) time_var = p_cpu_wtime() - (time_var)
156 
157 #endif
int times(int x, int y)
Definition: operations.c:3
File containing functions and utilities to support the printing of debug messagges.
long int p_cpu_wtime()
return a long which represents the elapsed wall processor time in milliseconds since some constant re...
Definition: cpu_time.hpp:142
#define INDENT_OUT_MEX(outLevel, curOutLevel, mex)
#define TIMES_TICKS_PER_SEC
Definition: cpu_time.hpp:71
void dump_exec_time(const std::string &thing, long et)
Definition: cpu_time.hpp:126
std::string print_cpu_time(long int t)
massage a long which represents a time interval in milliseconds, into a string suitable for output ...
Definition: cpu_time.hpp:110
long int p_cpu_time()
return a long which represents the elapsed processor time in milliseconds since some constant referen...
Definition: cpu_time.hpp:78

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13