Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

util.cxx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #endif
  26. #include "util.h"
  27. #ifdef WIN32
  28. typedef FILETIME syscounter_t;
  29. #else
  30. typedef struct rusage syscounter_t;
  31. #endif
  32. static syscounter_t _globalCounter[2];
  33. static cpucounter_t globalCounter = _globalCounter;
  34. void startCpuCounter(void)
  35. {
  36. startCpuCounter(globalCounter);
  37. }
  38. void endCpuCounter(void)
  39. {
  40. endCpuCounter(globalCounter);
  41. }
  42. double getCpuCounter(void)
  43. {
  44. return getCpuCounter(globalCounter);
  45. }
  46. cpucounter_t newCpuCounter(void)
  47. {
  48. syscounter_t *c;
  49. c = (syscounter_t*)malloc(sizeof(syscounter_t) * 2);
  50. if (c == NULL)
  51. return NULL;
  52. memset(c, 0, sizeof(syscounter_t) * 2);
  53. return c;
  54. }
  55. void freeCpuCounter(cpucounter_t c)
  56. {
  57. free(c);
  58. }
  59. static void measureCpu(syscounter_t *counter)
  60. {
  61. #ifdef WIN32
  62. FILETIME dummy1, dummy2, dummy3;
  63. GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
  64. &dummy3, counter);
  65. #else
  66. getrusage(RUSAGE_SELF, counter);
  67. #endif
  68. }
  69. void startCpuCounter(cpucounter_t c)
  70. {
  71. syscounter_t *s = (syscounter_t*)c;
  72. measureCpu(&s[0]);
  73. }
  74. void endCpuCounter(cpucounter_t c)
  75. {
  76. syscounter_t *s = (syscounter_t*)c;
  77. measureCpu(&s[1]);
  78. }
  79. double getCpuCounter(cpucounter_t c)
  80. {
  81. syscounter_t *s = (syscounter_t*)c;
  82. double seconds;
  83. #ifdef WIN32
  84. uint64_t counters[2];
  85. counters[0] = (uint64_t)s[0].dwHighDateTime << 32 |
  86. s[0].dwLowDateTime;
  87. counters[1] = (uint64_t)s[1].dwHighDateTime << 32 |
  88. s[1].dwLowDateTime;
  89. seconds = (double)(counters[1] - counters[0]) / 10000000.0;
  90. #else
  91. seconds = (double)(s[1].ru_utime.tv_sec -
  92. s[0].ru_utime.tv_sec);
  93. seconds += (double)(s[1].ru_utime.tv_usec -
  94. s[0].ru_utime.tv_usec) / 1000000.0;
  95. #endif
  96. return seconds;
  97. }