/trunk/Source/PublicLibs/SystemLibs/Time/Time_Posix.ipp

https://github.com/Mysticial/y-cruncher · C++ Header · 122 lines · 83 code · 7 blank · 32 comment · 6 complexity · 4e41e6737e78176a19fa76e7529137e3 MD5 · raw file

  1. /* Time_Linux.ipp
  2. *
  3. * Author : Alexander J. Yee
  4. * Date Created : 09/17/2014
  5. * Last Modified : 03/07/2016
  6. *
  7. */
  8. ////////////////////////////////////////////////////////////////////////////////
  9. ////////////////////////////////////////////////////////////////////////////////
  10. ////////////////////////////////////////////////////////////////////////////////
  11. ////////////////////////////////////////////////////////////////////////////////
  12. // Dependencies
  13. #include <time.h>
  14. #include <unistd.h>
  15. #include <sys/times.h>
  16. #include "PublicLibs/ConsoleIO/Label.h"
  17. #include "Time_Posix.h"
  18. namespace ymp{
  19. namespace Time{
  20. ////////////////////////////////////////////////////////////////////////////////
  21. ////////////////////////////////////////////////////////////////////////////////
  22. ////////////////////////////////////////////////////////////////////////////////
  23. ////////////////////////////////////////////////////////////////////////////////
  24. void CompileOptions(){
  25. Console::println_labelm("Time", "POSIX", 'G');
  26. }
  27. ////////////////////////////////////////////////////////////////////////////////
  28. ////////////////////////////////////////////////////////////////////////////////
  29. ////////////////////////////////////////////////////////////////////////////////
  30. ////////////////////////////////////////////////////////////////////////////////
  31. YM_NO_INLINE WallClock WallClock::Now(){
  32. WallClock out;
  33. if (gettimeofday(&out.m_time, NULL)){
  34. Console::Warning("Unable to access gettimeofday().");
  35. Console::Quit(1);
  36. }
  37. return out;
  38. }
  39. double WallClock::operator-(const WallClock& x) const{
  40. u64_t isec = (u64_t)m_time.tv_sec - (u64_t)x.m_time.tv_sec;
  41. s32_t usec = m_time.tv_usec - x.m_time.tv_usec;
  42. if (usec < 0){
  43. usec += 1000000;
  44. isec--;
  45. }
  46. return (double)isec + (double)usec * .000001;
  47. }
  48. ////////////////////////////////////////////////////////////////////////////////
  49. ////////////////////////////////////////////////////////////////////////////////
  50. ////////////////////////////////////////////////////////////////////////////////
  51. ////////////////////////////////////////////////////////////////////////////////
  52. PerformanceTimeStamp PerformanceTimeStamp::now(){
  53. PerformanceTimeStamp out;
  54. out.wall_clock = WallClock::Now();
  55. static double ratio = 1. / sysconf(_SC_CLK_TCK);
  56. struct tms timers;
  57. if (times(&timers) == (clock_t)-1){
  58. out.user_clock = 0;
  59. out.kernel_clock = 0;
  60. }else{
  61. out.user_clock = timers.tms_utime * ratio;
  62. out.kernel_clock = timers.tms_stime * ratio;
  63. }
  64. return out;
  65. }
  66. void PerformanceTimeDuration::reset(){
  67. wall_time = 0;
  68. user_time = 0;
  69. kernel_time = 0;
  70. }
  71. PerformanceTimeDuration PerformanceTimeDuration::time_since(const PerformanceTimeStamp& timestamp){
  72. return PerformanceTimeStamp::now() - timestamp;
  73. }
  74. void PerformanceTimeDuration::operator+=(const PerformanceTimeDuration& duration){
  75. wall_time += duration.wall_time;
  76. user_time += duration.user_time;
  77. kernel_time += duration.kernel_time;
  78. }
  79. PerformanceTimeDuration operator-(const PerformanceTimeStamp& end, const PerformanceTimeStamp& start){
  80. PerformanceTimeDuration out;
  81. out.wall_time = end.wall_clock - start.wall_clock;
  82. out.user_time = end.user_clock - start.user_clock;
  83. out.kernel_time = end.kernel_clock - start.kernel_clock;
  84. return out;
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////
  87. ////////////////////////////////////////////////////////////////////////////////
  88. ////////////////////////////////////////////////////////////////////////////////
  89. ////////////////////////////////////////////////////////////////////////////////
  90. YM_NO_INLINE void get_process_times(double& user, double& kernel){
  91. user = 0;
  92. kernel = 0;
  93. }
  94. YM_NO_INLINE std::string tostr_now(){
  95. time_t rawtime = time(nullptr);
  96. char buffer[32];
  97. const char* current_time = asctime(localtime(&rawtime));
  98. char ch;
  99. upL_t c = 0;
  100. do{
  101. ch = *current_time;
  102. buffer[c++] = *current_time++;
  103. }while (ch != '\0');
  104. c = 0;
  105. while (buffer[c] >= 32)
  106. c++;
  107. buffer[c] = '\0';
  108. return buffer;
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////
  111. ////////////////////////////////////////////////////////////////////////////////
  112. ////////////////////////////////////////////////////////////////////////////////
  113. ////////////////////////////////////////////////////////////////////////////////
  114. }
  115. }