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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifdef WIN32
  25. #include <windows.h>
  26. #else
  27. #include <sys/resource.h>
  28. #include <sys/time.h>
  29. #endif
  30. #include "util.h"
  31. #ifdef WIN32
  32. typedef struct {
  33. FILETIME kernelTime;
  34. FILETIME userTime;
  35. } syscounter_t;
  36. #else
  37. typedef struct rusage syscounter_t;
  38. #endif
  39. static syscounter_t _globalCounter[2];
  40. static cpucounter_t globalCounter = _globalCounter;
  41. void startCpuCounter(void)
  42. {
  43. startCpuCounter(globalCounter);
  44. }
  45. void endCpuCounter(void)
  46. {
  47. endCpuCounter(globalCounter);
  48. }
  49. double getCpuCounter(void)
  50. {
  51. return getCpuCounter(globalCounter);
  52. }
  53. cpucounter_t newCpuCounter(void)
  54. {
  55. syscounter_t *c;
  56. c = (syscounter_t*)malloc(sizeof(syscounter_t) * 2);
  57. if (c == NULL)
  58. return NULL;
  59. memset(c, 0, sizeof(syscounter_t) * 2);
  60. return c;
  61. }
  62. void freeCpuCounter(cpucounter_t c)
  63. {
  64. free(c);
  65. }
  66. static void measureCpu(syscounter_t *counter)
  67. {
  68. #ifdef WIN32
  69. FILETIME dummy1, dummy2;
  70. GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
  71. &counter->kernelTime, &counter->userTime);
  72. #else
  73. getrusage(RUSAGE_SELF, counter);
  74. #endif
  75. }
  76. void startCpuCounter(cpucounter_t c)
  77. {
  78. syscounter_t *s = (syscounter_t*)c;
  79. measureCpu(&s[0]);
  80. }
  81. void endCpuCounter(cpucounter_t c)
  82. {
  83. syscounter_t *s = (syscounter_t*)c;
  84. measureCpu(&s[1]);
  85. }
  86. double getCpuCounter(cpucounter_t c)
  87. {
  88. syscounter_t *s = (syscounter_t*)c;
  89. double sysSeconds, userSeconds;
  90. #ifdef WIN32
  91. uint64_t counters[2];
  92. counters[0] = (uint64_t)s[0].kernelTime.dwHighDateTime << 32 |
  93. s[0].kernelTime.dwLowDateTime;
  94. counters[1] = (uint64_t)s[1].kernelTime.dwHighDateTime << 32 |
  95. s[1].kernelTime.dwLowDateTime;
  96. sysSeconds = (double)(counters[1] - counters[0]) / 10000000.0;
  97. counters[0] = (uint64_t)s[0].userTime.dwHighDateTime << 32 |
  98. s[0].userTime.dwLowDateTime;
  99. counters[1] = (uint64_t)s[1].userTime.dwHighDateTime << 32 |
  100. s[1].userTime.dwLowDateTime;
  101. userSeconds = (double)(counters[1] - counters[0]) / 10000000.0;
  102. #else
  103. sysSeconds = (double)(s[1].ru_stime.tv_sec -
  104. s[0].ru_stime.tv_sec);
  105. sysSeconds += (double)(s[1].ru_stime.tv_usec -
  106. s[0].ru_stime.tv_usec) / 1000000.0;
  107. userSeconds = (double)(s[1].ru_utime.tv_sec -
  108. s[0].ru_utime.tv_sec);
  109. userSeconds += (double)(s[1].ru_utime.tv_usec -
  110. s[0].ru_utime.tv_usec) / 1000000.0;
  111. #endif
  112. return sysSeconds + userSeconds;
  113. }
  114. #ifdef WIN32
  115. static LARGE_INTEGER timeStart, timeEnd;
  116. #else
  117. static struct timeval timeStart, timeEnd;
  118. #endif
  119. void startTimeCounter(void)
  120. {
  121. #ifdef WIN32
  122. QueryPerformanceCounter(&timeStart);
  123. #else
  124. gettimeofday(&timeStart, NULL);
  125. #endif
  126. }
  127. void endTimeCounter(void)
  128. {
  129. #ifdef WIN32
  130. QueryPerformanceCounter(&timeEnd);
  131. #else
  132. gettimeofday(&timeEnd, NULL);
  133. #endif
  134. }
  135. double getTimeCounter(void)
  136. {
  137. double time;
  138. #ifdef WIN32
  139. LARGE_INTEGER freq;
  140. QueryPerformanceFrequency(&freq);
  141. time = timeEnd.QuadPart - timeStart.QuadPart;
  142. time = time / freq.QuadPart;
  143. #else
  144. time = (double)timeEnd.tv_sec - timeStart.tv_sec;
  145. time += (double)(timeEnd.tv_usec - timeStart.tv_usec) / 1000000.0;
  146. #endif
  147. return time;
  148. }