You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.cxx 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Copyright 2013-2014 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software 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 General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef WIN32
  22. #include <windows.h>
  23. #else
  24. #include <sys/resource.h>
  25. #include <sys/time.h>
  26. #endif
  27. #include "util.h"
  28. #ifdef WIN32
  29. typedef struct {
  30. FILETIME kernelTime;
  31. FILETIME userTime;
  32. } syscounter_t;
  33. #else
  34. typedef struct rusage syscounter_t;
  35. #endif
  36. static syscounter_t _globalCounter[2];
  37. static cpucounter_t globalCounter = _globalCounter;
  38. void startCpuCounter(void)
  39. {
  40. startCpuCounter(globalCounter);
  41. }
  42. void endCpuCounter(void)
  43. {
  44. endCpuCounter(globalCounter);
  45. }
  46. double getCpuCounter(void)
  47. {
  48. return getCpuCounter(globalCounter);
  49. }
  50. cpucounter_t newCpuCounter(void)
  51. {
  52. syscounter_t *c;
  53. c = (syscounter_t*)malloc(sizeof(syscounter_t) * 2);
  54. if (c == NULL)
  55. return NULL;
  56. memset(c, 0, sizeof(syscounter_t) * 2);
  57. return c;
  58. }
  59. void freeCpuCounter(cpucounter_t c)
  60. {
  61. free(c);
  62. }
  63. static void measureCpu(syscounter_t *counter)
  64. {
  65. #ifdef WIN32
  66. FILETIME dummy1, dummy2;
  67. GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
  68. &counter->kernelTime, &counter->userTime);
  69. #else
  70. getrusage(RUSAGE_SELF, counter);
  71. #endif
  72. }
  73. void startCpuCounter(cpucounter_t c)
  74. {
  75. syscounter_t *s = (syscounter_t*)c;
  76. measureCpu(&s[0]);
  77. }
  78. void endCpuCounter(cpucounter_t c)
  79. {
  80. syscounter_t *s = (syscounter_t*)c;
  81. measureCpu(&s[1]);
  82. }
  83. double getCpuCounter(cpucounter_t c)
  84. {
  85. syscounter_t *s = (syscounter_t*)c;
  86. double sysSeconds, userSeconds;
  87. #ifdef WIN32
  88. uint64_t counters[2];
  89. counters[0] = (uint64_t)s[0].kernelTime.dwHighDateTime << 32 |
  90. s[0].kernelTime.dwLowDateTime;
  91. counters[1] = (uint64_t)s[1].kernelTime.dwHighDateTime << 32 |
  92. s[1].kernelTime.dwLowDateTime;
  93. sysSeconds = (double)(counters[1] - counters[0]) / 10000000.0;
  94. counters[0] = (uint64_t)s[0].userTime.dwHighDateTime << 32 |
  95. s[0].userTime.dwLowDateTime;
  96. counters[1] = (uint64_t)s[1].userTime.dwHighDateTime << 32 |
  97. s[1].userTime.dwLowDateTime;
  98. userSeconds = (double)(counters[1] - counters[0]) / 10000000.0;
  99. #else
  100. sysSeconds = (double)(s[1].ru_stime.tv_sec -
  101. s[0].ru_stime.tv_sec);
  102. sysSeconds += (double)(s[1].ru_stime.tv_usec -
  103. s[0].ru_stime.tv_usec) / 1000000.0;
  104. userSeconds = (double)(s[1].ru_utime.tv_sec -
  105. s[0].ru_utime.tv_sec);
  106. userSeconds += (double)(s[1].ru_utime.tv_usec -
  107. s[0].ru_utime.tv_usec) / 1000000.0;
  108. #endif
  109. return sysSeconds + userSeconds;
  110. }
  111. #ifdef WIN32
  112. static LARGE_INTEGER timeStart, timeEnd;
  113. #else
  114. static struct timeval timeStart, timeEnd;
  115. #endif
  116. void startTimeCounter(void)
  117. {
  118. #ifdef WIN32
  119. QueryPerformanceCounter(&timeStart);
  120. #else
  121. gettimeofday(&timeStart, NULL);
  122. #endif
  123. }
  124. void endTimeCounter(void)
  125. {
  126. #ifdef WIN32
  127. QueryPerformanceCounter(&timeEnd);
  128. #else
  129. gettimeofday(&timeEnd, NULL);
  130. #endif
  131. }
  132. double getTimeCounter(void)
  133. {
  134. double time;
  135. #ifdef WIN32
  136. LARGE_INTEGER freq;
  137. QueryPerformanceFrequency(&freq);
  138. time = timeEnd.QuadPart - timeStart.QuadPart;
  139. time = time / freq.QuadPart;
  140. #else
  141. time = (double)timeEnd.tv_sec - timeStart.tv_sec;
  142. time += (double)(timeEnd.tv_usec - timeStart.tv_usec) / 1000000.0;
  143. #endif
  144. return time;
  145. }