PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/opencascade-6.5.1/ros/src/OSD/OSD_Chronometer.cxx

https://github.com/AsherBond/MondocosmOS
C++ | 272 lines | 159 code | 40 blank | 73 comment | 17 complexity | 6c0f32c670c395ffb8c9be48ce8edd4b MD5 | raw file
  1. // File: OSD_Chronometer.cxx
  2. // Created: Mon Nov 16 15:20:41 1992
  3. // Author: Mireille MERCIEN
  4. // <mip@sdsun3>
  5. #include <OSD_Chronometer.ixx>
  6. #include <Standard_Stream.hxx>
  7. // ====================== PLATFORM-SPECIFIC PART ========================
  8. #ifndef WNT
  9. //---------- Systemes autres que WNT : ----------------------------------
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. #ifdef HAVE_SYS_TYPES_H
  14. # include <sys/types.h>
  15. #endif
  16. #ifdef HAVE_UNISTD_H
  17. # include <unistd.h>
  18. #endif
  19. #ifdef HAVE_SYS_TIMES_H
  20. # include <sys/times.h>
  21. #endif
  22. #ifdef SOLARIS
  23. # include <sys/resource.h>
  24. #endif
  25. //=======================================================================
  26. //Selon les plateformes on doit avoir le nombre de clicks par secondes
  27. //qui est l unite de mesure du temps.
  28. //=======================================================================
  29. #ifndef sysconf
  30. # define _sysconf sysconf
  31. #endif
  32. #if defined(HAVE_TIME_H) || defined(WNT) || defined(DECOSF1)
  33. # include <time.h>
  34. #endif
  35. # ifndef CLK_TCK
  36. # define CLK_TCK CLOCKS_PER_SEC
  37. # endif
  38. #ifdef HAVE_LIMITS
  39. # include <limits>
  40. #elif defined (HAVE_LIMITS_H)
  41. # include <limits.h>
  42. #endif
  43. //=======================================================================
  44. //function : GetProcessCPU
  45. //purpose :
  46. //=======================================================================
  47. void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
  48. {
  49. #if defined(LIN) || defined(linux) || defined(__FreeBSD__)
  50. static const long aCLK_TCK = sysconf(_SC_CLK_TCK);
  51. #else
  52. static const long aCLK_TCK = CLK_TCK;
  53. #endif
  54. tms CurrentTMS;
  55. times (&CurrentTMS);
  56. UserSeconds = (Standard_Real)CurrentTMS.tms_utime / aCLK_TCK;
  57. SystemSeconds = (Standard_Real)CurrentTMS.tms_stime / aCLK_TCK;
  58. }
  59. //=======================================================================
  60. //function : GetThreadCPU
  61. //purpose :
  62. //=======================================================================
  63. void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
  64. {
  65. UserSeconds = SystemSeconds = 0.;
  66. #if defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
  67. // on Linux, only user times are available for threads via clock_gettime()
  68. struct timespec t;
  69. if ( ! clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t) )
  70. {
  71. UserSeconds = t.tv_sec + 0.000000001 * t.tv_nsec;
  72. }
  73. #elif defined(SOLARIS)
  74. // on Solaris, both user and system times are available as LWP times
  75. struct rusage rut;
  76. if ( ! getrusage (RUSAGE_LWP, &rut) )
  77. {
  78. UserSeconds = rut.ru_utime.tv_sec + 0.000001 * rut.ru_utime.tv_usec;
  79. SystemSeconds = rut.ru_stime.tv_sec + 0.000001 * rut.ru_stime.tv_usec;
  80. }
  81. #else
  82. #pragma error "OS is not supported yet; code to be ported"
  83. #endif
  84. }
  85. #else
  86. //---------------------------- Systeme WNT --------------------------------
  87. #define STRICT
  88. #include <windows.h>
  89. //=======================================================================
  90. //function : EncodeFILETIME
  91. //purpose : Encode time defined by FILETIME structure
  92. // (100s nanoseconds since January 1, 1601) to 64-bit integer
  93. //=======================================================================
  94. static inline __int64 EncodeFILETIME (PFILETIME pFt)
  95. {
  96. __int64 qw;
  97. qw = pFt -> dwHighDateTime;
  98. qw <<= 32;
  99. qw |= pFt -> dwLowDateTime;
  100. return qw;
  101. }
  102. //=======================================================================
  103. //function : GetProcessCPU
  104. //purpose :
  105. //=======================================================================
  106. void OSD_Chronometer::GetProcessCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
  107. {
  108. FILETIME ftStart, ftExit, ftKernel, ftUser;
  109. ::GetProcessTimes (GetCurrentProcess(), &ftStart, &ftExit, &ftKernel, &ftUser);
  110. UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
  111. SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
  112. }
  113. //=======================================================================
  114. //function : GetThreadCPU
  115. //purpose :
  116. //=======================================================================
  117. void OSD_Chronometer::GetThreadCPU (Standard_Real& UserSeconds, Standard_Real& SystemSeconds)
  118. {
  119. FILETIME ftStart, ftExit, ftKernel, ftUser;
  120. ::GetThreadTimes (GetCurrentThread(), &ftStart, &ftExit, &ftKernel, &ftUser);
  121. UserSeconds = 0.0000001 * EncodeFILETIME (&ftUser);
  122. SystemSeconds = 0.0000001 * EncodeFILETIME (&ftKernel);
  123. }
  124. #endif /* WNT */
  125. // ====================== PLATFORM-INDEPENDENT PART ========================
  126. //=======================================================================
  127. //function : OSD_Chronometer
  128. //purpose :
  129. //=======================================================================
  130. OSD_Chronometer::OSD_Chronometer(const Standard_Boolean ThisThreadOnly)
  131. {
  132. ThreadOnly = ThisThreadOnly;
  133. Start_user = Start_sys = 0.;
  134. Cumul_user = Cumul_sys = 0.;
  135. Stopped = Standard_True;
  136. }
  137. //=======================================================================
  138. //function : Destroy
  139. //purpose :
  140. //=======================================================================
  141. void OSD_Chronometer::Destroy ()
  142. {
  143. }
  144. //=======================================================================
  145. //function : Reset
  146. //purpose :
  147. //=======================================================================
  148. void OSD_Chronometer::Reset ()
  149. {
  150. Stopped = Standard_True;
  151. Start_user = Start_sys = 0.;
  152. Cumul_user = Cumul_sys = 0.;
  153. }
  154. //=======================================================================
  155. //function : Stop
  156. //purpose :
  157. //=======================================================================
  158. void OSD_Chronometer::Stop ()
  159. {
  160. if ( !Stopped ) {
  161. Standard_Real Curr_user, Curr_sys;
  162. if ( ThreadOnly )
  163. GetThreadCPU (Curr_user, Curr_sys);
  164. else
  165. GetProcessCPU (Curr_user, Curr_sys);
  166. Cumul_user += Curr_user - Start_user;
  167. Cumul_sys += Curr_sys - Start_sys;
  168. Stopped = Standard_True;
  169. }
  170. // else cerr << "WARNING: OSD_Chronometer already stopped !\n" << flush;
  171. }
  172. //=======================================================================
  173. //function : Start
  174. //purpose :
  175. //=======================================================================
  176. void OSD_Chronometer::Start ()
  177. {
  178. if ( Stopped ) {
  179. if ( ThreadOnly )
  180. GetThreadCPU (Start_user, Start_sys);
  181. else
  182. GetProcessCPU (Start_user, Start_sys);
  183. Stopped = Standard_False;
  184. }
  185. // else cerr << "WARNING: OSD_Chronometer already running !\n" << flush;
  186. }
  187. //=======================================================================
  188. //function : Show
  189. //purpose :
  190. //=======================================================================
  191. void OSD_Chronometer::Show ()
  192. {
  193. Show (cout);
  194. }
  195. //=======================================================================
  196. //function : Show
  197. //purpose :
  198. //=======================================================================
  199. void OSD_Chronometer::Show (Standard_OStream& os)
  200. {
  201. Standard_Boolean StopSav = Stopped;
  202. if (!StopSav) Stop();
  203. std::streamsize prec = os.precision (12);
  204. os << "CPU user time: " << Cumul_user << " seconds " << endl;
  205. os << "CPU system time: " << Cumul_sys << " seconds " << endl;
  206. os.precision (prec);
  207. if (!StopSav) Start();
  208. }
  209. //=======================================================================
  210. //function : Show
  211. //purpose : Returns cpu user time
  212. //=======================================================================
  213. void OSD_Chronometer::Show (Standard_Real& second)
  214. {
  215. Standard_Boolean StopSav = Stopped;
  216. if (!StopSav) Stop();
  217. second = Cumul_user;
  218. if (!StopSav) Start();
  219. }
  220. //=======================================================================
  221. //function : Show
  222. //purpose : Returns both user and system cpu times
  223. //=======================================================================
  224. void OSD_Chronometer::Show (Standard_Real& user,
  225. Standard_Real& system)
  226. {
  227. Standard_Boolean StopSav = Stopped;
  228. if (!StopSav) Stop();
  229. user = Cumul_user;
  230. system = Cumul_sys;
  231. if (!StopSav) Start();
  232. }