/ut/util/utt/uttTime.cpp

https://github.com/ALTIBASE/altibase · C++ · 503 lines · 294 code · 41 blank · 168 comment · 15 complexity · 35440dedfdb577e44c4b25dd79b55303 MD5 · raw file

  1. /**
  2. * Copyright (c) 1999~2017, Altibase Corp. and/or its affiliates. All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. NAME
  18. uttTime.cpp - QCU TimeCheck
  19. DESCRIPTION
  20. PUBLIC FUNCTION(S)
  21. PRIVATE FUNCTION(S)
  22. NOTES
  23. MODIFIED (MM/DD/YY)
  24. */
  25. #include <idl.h>
  26. #include <string.h>
  27. #include <uttTime.h>
  28. /*--------------------- uttTime ---------------------------*/
  29. /*
  30. NAME
  31. uttTime -
  32. DESCRIPTION
  33. ARGUMENTS
  34. RETURNS
  35. NOTES
  36. */
  37. uttTime::uttTime()
  38. {
  39. idlOS::strncpy( time_name, "Run Time", 60 );
  40. time_name[ 19 ] = 0;
  41. m_microseconds_wallclock = 0;
  42. m_microseconds_user = 0;
  43. m_microseconds_system = 0;
  44. }
  45. /*--------------------- uttTime ---------------------------*/
  46. /*
  47. NAME
  48. uttTime -
  49. DESCRIPTION
  50. ARGUMENTS
  51. RETURNS
  52. NOTES
  53. */
  54. void uttTime::setName( const char * t_name )
  55. {
  56. idlOS::strncpy( time_name, t_name, 60 );
  57. time_name[ 60 ] = 0;
  58. }
  59. /*--------------------- start ---------------------------*/
  60. /*
  61. NAME
  62. start -
  63. DESCRIPTION
  64. ARGUMENTS
  65. RETURNS
  66. NOTES
  67. */
  68. void uttTime::reset()
  69. {
  70. m_microseconds_wallclock = 0;
  71. m_microseconds_user = 0;
  72. m_microseconds_system = 0;
  73. }
  74. void uttTime::start()
  75. {
  76. /*BUGBUG_NT*/
  77. PDL_Time_Value pdl_start_time;
  78. idlOS::times(&start_time_tick);
  79. pdl_start_time = idlOS::gettimeofday();
  80. start_time.tv_sec = pdl_start_time.sec();
  81. start_time.tv_usec = pdl_start_time.usec();
  82. /*BUGBUG_NT*/
  83. }
  84. void uttTime::stop()
  85. {
  86. timeval v_timeval;
  87. /*BUGBUG_NT*/
  88. PDL_Time_Value pdl_end_time;
  89. idlOS::times(&end_time_tick);
  90. pdl_end_time = idlOS::gettimeofday();
  91. end_time.tv_sec = pdl_end_time.sec();
  92. end_time.tv_usec = pdl_end_time.usec();
  93. /*BUGBUG_NT*/
  94. /* Compute wall clock time */
  95. v_timeval.tv_sec = end_time.tv_sec - start_time.tv_sec;
  96. v_timeval.tv_usec = end_time.tv_usec - start_time.tv_usec;
  97. if ( v_timeval.tv_usec < 0 )
  98. {
  99. v_timeval.tv_sec -= 1;
  100. v_timeval.tv_usec = 1000000 + v_timeval.tv_usec;
  101. //v_timeval.tv_usec = 999999 - v_timeval.tv_usec * (-1);
  102. }
  103. m_seconds_wallclock += v_timeval.tv_sec;
  104. #if defined(VC_WIN32)
  105. m_microseconds_wallclock += (SLong)v_timeval.tv_sec * 1000000 + (SLong)v_timeval.tv_usec;
  106. /* Compute user clock time */
  107. m_microseconds_user +=
  108. (SLong)( end_time_tick.tms_utime - start_time_tick.tms_utime )
  109. * 1000000 / /*BUGBUG_NT*/(SLong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  110. /* Compute system clock time */
  111. m_microseconds_system +=
  112. (SLong)( end_time_tick.tms_stime - start_time_tick.tms_stime )
  113. * 1000000 / /*BUGBUG_NT*/(SLong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  114. #else
  115. m_microseconds_wallclock += (ULong)v_timeval.tv_sec * 1000000 + (ULong)v_timeval.tv_usec;
  116. /* Compute user clock time */
  117. m_microseconds_user +=
  118. (ULong)( end_time_tick.tms_utime - start_time_tick.tms_utime )
  119. * 1000000 / /*BUGBUG_NT*/(ULong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  120. /* Compute system clock time */
  121. m_microseconds_system +=
  122. (ULong)( end_time_tick.tms_stime - start_time_tick.tms_stime )
  123. * 1000000 / /*BUGBUG_NT*/(ULong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  124. #endif
  125. }
  126. /*--------------------- finish ---------------------------*/
  127. /*
  128. NAME
  129. finish -
  130. DESCRIPTION
  131. ARGUMENTS
  132. RETURNS
  133. NOTES
  134. */
  135. void uttTime::finish()
  136. {
  137. timeval v_timeval;
  138. /*BUGBUG_NT*/
  139. PDL_Time_Value pdl_end_time;
  140. idlOS::times(&end_time_tick);
  141. pdl_end_time = idlOS::gettimeofday();
  142. end_time.tv_sec = pdl_end_time.sec();
  143. end_time.tv_usec = pdl_end_time.usec();
  144. /*BUGBUG_NT*/
  145. /* Compute wall clock time */
  146. v_timeval.tv_sec = end_time.tv_sec - start_time.tv_sec;
  147. v_timeval.tv_usec = end_time.tv_usec - start_time.tv_usec;
  148. if ( v_timeval.tv_usec < 0 )
  149. {
  150. v_timeval.tv_sec -= 1;
  151. v_timeval.tv_usec = 1000000 + v_timeval.tv_usec;
  152. //v_timeval.tv_usec = 999999 - v_timeval.tv_usec * (-1);
  153. }
  154. m_seconds_wallclock = v_timeval.tv_sec;
  155. #if defined(VC_WIN32)
  156. m_microseconds_wallclock = (SLong)v_timeval.tv_sec * 1000000 + (SLong)v_timeval.tv_usec;
  157. /* Compute user clock time */
  158. m_microseconds_user =
  159. (SLong)( end_time_tick.tms_utime - start_time_tick.tms_utime )
  160. * 1000000 / /*BUGBUG_NT*/(SLong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  161. /* Compute system clock time */
  162. m_microseconds_system =
  163. (SLong)( end_time_tick.tms_stime - start_time_tick.tms_stime )
  164. * 1000000 / /*BUGBUG_NT*/(SLong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  165. #else
  166. m_microseconds_wallclock = (ULong)v_timeval.tv_sec * 1000000 + (ULong)v_timeval.tv_usec;
  167. /* Compute user clock time */
  168. m_microseconds_user =
  169. (ULong)( end_time_tick.tms_utime - start_time_tick.tms_utime )
  170. * 1000000 / /*BUGBUG_NT*/(ULong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  171. /* Compute system clock time */
  172. m_microseconds_system =
  173. (ULong)( end_time_tick.tms_stime - start_time_tick.tms_stime )
  174. * 1000000 / /*BUGBUG_NT*/(ULong)/*sysconf*/idlOS::sysconf/*BUGBUG_NT*/( _SC_CLK_TCK );
  175. #endif
  176. }
  177. /*--------------------- show ---------------------------*/
  178. /*
  179. NAME
  180. show -
  181. DESCRIPTION
  182. ARGUMENTS
  183. RETURNS
  184. NOTES
  185. */
  186. void uttTime::show()
  187. {
  188. idlOS::fprintf( stdout,
  189. "%s: %" ID_UINT64_FMT "\n",
  190. time_name,
  191. ( ULong ) m_microseconds_wallclock );
  192. }
  193. void uttTime::print( UTTTimeScale ts )
  194. {
  195. int scale;
  196. switch ( ts )
  197. {
  198. case UTT_SEC:
  199. scale = 1000000;
  200. idlOS::fprintf( stdout,
  201. "%s: %" ID_UINT64_FMT ,
  202. time_name,
  203. ( ULong ) m_seconds_wallclock );
  204. break;
  205. case UTT_MSEC:
  206. scale = 1000;
  207. idlOS::fprintf( stdout,
  208. "%s: %" ID_UINT64_FMT ,
  209. time_name,
  210. ( ULong ) m_microseconds_wallclock / scale );
  211. break;
  212. case UTT_USEC:
  213. scale = 1;
  214. idlOS::fprintf( stdout,
  215. "%s: %" ID_UINT64_FMT ,
  216. time_name,
  217. ( ULong ) m_microseconds_wallclock / scale );
  218. break;
  219. default:
  220. scale = 1;
  221. }
  222. /*
  223. idlOS::fprintf( stdout,
  224. "%s: %" ID_UINT64_FMT ,
  225. time_name,
  226. (ULong)m_microseconds_wallclock/scale);
  227. */
  228. /*
  229. idlOS::fprintf( stdout,
  230. "%s: %.4f(start:%.4f sec %.4f usec end:%.4f sec %.4f usec ",
  231. time_name,
  232. m_microseconds_wallclock/(double)scale,
  233. (double)start_time.tv_sec, (double)start_time.tv_usec,
  234. (double)end_time.tv_sec, (double)end_time.tv_usec);
  235. */
  236. }
  237. void uttTime::println( UTTTimeScale ts )
  238. {
  239. int scale;
  240. switch ( ts )
  241. {
  242. case UTT_SEC:
  243. scale = 1000000;
  244. idlOS::fprintf( stdout,
  245. "%s: %" ID_UINT64_FMT,
  246. time_name,
  247. ( ULong ) m_seconds_wallclock );
  248. break;
  249. case UTT_MSEC:
  250. scale = 1000;
  251. idlOS::fprintf( stdout,
  252. "%s: %" ID_UINT64_FMT,
  253. time_name,
  254. ( ULong ) m_microseconds_wallclock / scale );
  255. break;
  256. case UTT_USEC:
  257. scale = 1;
  258. idlOS::fprintf( stdout,
  259. "%s: %" ID_UINT64_FMT,
  260. time_name,
  261. ( ULong ) m_microseconds_wallclock / scale );
  262. break;
  263. default:
  264. scale = 1;
  265. }
  266. /*
  267. idlOS::fprintf( stdout,
  268. "%s: %" ID_UINT64_FMT "\n",
  269. time_name,
  270. (ULong)m_microseconds_wallclock/scale);
  271. */
  272. }
  273. void uttTime::showAutoScale()
  274. {
  275. if ( m_microseconds_wallclock >= 1000000 )
  276. {
  277. idlOS::fprintf( stderr,
  278. "%s:(W) %.4f sec, (U) %.4f sec, (S) %.4f sec\n",
  279. time_name,
  280. m_microseconds_wallclock / ( double ) 1000000,
  281. m_microseconds_user / ( double ) 1000000,
  282. m_microseconds_system / ( double ) 1000000 );
  283. }
  284. else if ( m_microseconds_wallclock >= 1000 )
  285. {
  286. idlOS::fprintf( stderr,
  287. "%s:(W) %.4f msec, (U) %.4f msec, (S) %.4f msec\n",
  288. time_name,
  289. m_microseconds_wallclock / ( double ) 1000,
  290. m_microseconds_user / ( double ) 1000,
  291. m_microseconds_system / ( double ) 1000 );
  292. }
  293. else
  294. {
  295. idlOS::fprintf( stderr,
  296. "%s:(W) %.4f usec, (U) %.4f usec, (S) %.4f usec\n",
  297. time_name,
  298. (double)m_microseconds_wallclock,
  299. (double)m_microseconds_user,
  300. (double)m_microseconds_system );
  301. }
  302. }
  303. // BUG-24096 : iloader °æ°ú ½Ã°£ Ç¥½Ã
  304. void uttTime::showAutoScale4Wall()
  305. {
  306. if ( m_microseconds_wallclock >= 1000000 )
  307. {
  308. idlOS::fprintf( stderr,
  309. "%s : %.4f sec\n",
  310. time_name,
  311. m_microseconds_wallclock / ( double ) 1000000 );
  312. }
  313. else if ( m_microseconds_wallclock >= 1000 )
  314. {
  315. idlOS::fprintf( stderr,
  316. "%s : %.4f msec\n",
  317. time_name,
  318. m_microseconds_wallclock / ( double ) 1000 );
  319. }
  320. else
  321. {
  322. idlOS::fprintf( stderr,
  323. "%s : %.4f usec\n",
  324. time_name,
  325. (double)m_microseconds_wallclock );
  326. }
  327. }
  328. /*--------------------- getMicroseconds ---------------------------*/
  329. /*
  330. NAME
  331. getMicroseconds -
  332. DESCRIPTION
  333. ARGUMENTS
  334. RETURNS
  335. NOTES
  336. */
  337. SInt uttTime::getMicroseconds()
  338. {
  339. return ( SInt ) m_microseconds_wallclock;
  340. }
  341. double uttTime::getTime( UTTTimeClass tc, UTTTimeScale ts )
  342. {
  343. int scale;
  344. switch ( ts )
  345. {
  346. case UTT_SEC:
  347. scale = 1000000;
  348. break;
  349. case UTT_MSEC:
  350. scale = 1000;
  351. break;
  352. case UTT_USEC:
  353. scale = 1;
  354. break;
  355. default:
  356. scale = 1;
  357. }
  358. switch ( tc )
  359. {
  360. case UTT_WALL_TIME:
  361. return m_microseconds_wallclock / ( double ) scale;
  362. case UTT_USER_TIME:
  363. return m_microseconds_user / ( double ) scale;
  364. case UTT_SYS_TIME:
  365. return m_microseconds_system / ( double ) scale;
  366. default:
  367. return 0.0;
  368. }
  369. }
  370. double uttTime::getMicroSeconds( UTTTimeClass tc )
  371. {
  372. switch ( tc )
  373. {
  374. case UTT_WALL_TIME:
  375. return ( double ) m_microseconds_wallclock;
  376. case UTT_USER_TIME:
  377. return ( double ) m_microseconds_user;
  378. case UTT_SYS_TIME:
  379. return ( double ) m_microseconds_system;
  380. default:
  381. return 0.0;
  382. }
  383. }
  384. double uttTime::getMilliSeconds( UTTTimeClass tc )
  385. {
  386. switch ( tc )
  387. {
  388. case UTT_WALL_TIME:
  389. return m_microseconds_wallclock / ( double ) 1000;
  390. case UTT_USER_TIME:
  391. return m_microseconds_user / ( double ) 1000;
  392. case UTT_SYS_TIME:
  393. return m_microseconds_system / ( double ) 1000;
  394. default:
  395. return 0.0;
  396. }
  397. }
  398. double uttTime::getSeconds( UTTTimeClass ts )
  399. {
  400. switch ( ts )
  401. {
  402. case UTT_WALL_TIME:
  403. return m_microseconds_wallclock / ( double ) 1000000;
  404. case UTT_USER_TIME:
  405. return m_microseconds_user / ( double ) 1000000;
  406. case UTT_SYS_TIME:
  407. return m_microseconds_system / ( double ) 1000000;
  408. default:
  409. return 0.0;
  410. }
  411. }
  412. /*
  413. SInt uttTime::getMicrosecondsUser()
  414. {
  415. return (SInt)m_microseconds_user;
  416. }
  417. SInt uttTime::getMicrosecondsSystem()
  418. {
  419. return (SInt)m_microseconds_system;
  420. }
  421. float uttTime::getMilliseconds()
  422. {
  423. return (float)m_microseconds_wallclock/1000;
  424. }
  425. float uttTime::getMillisecondsUser()
  426. {
  427. return (float)m_microseconds_user/1000;
  428. }
  429. float uttTime::getMillisecondsSystem()
  430. {
  431. return (float)m_microseconds_system/1000;
  432. }
  433. */