diff options
author | Pierre Ossman <ossman@cendio.se> | 2015-02-06 13:50:36 +0100 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2015-02-13 11:13:53 +0100 |
commit | 8ac31113cc6c0327b40bb40770d83a544f84e8b5 (patch) | |
tree | 2aeb1fb7725c2619d3d5894decb0a034462955aa /tests | |
parent | e1f2545fa18497c3395121881d9de8892228f08f (diff) | |
download | tigervnc-8ac31113cc6c0327b40bb40770d83a544f84e8b5.tar.gz tigervnc-8ac31113cc6c0327b40bb40770d83a544f84e8b5.zip |
Make it possible to measure CPU usage in parellel
Diffstat (limited to 'tests')
-rw-r--r-- | tests/util.cxx | 79 | ||||
-rw-r--r-- | tests/util.h | 10 |
2 files changed, 71 insertions, 18 deletions
diff --git a/tests/util.cxx b/tests/util.cxx index c2685abb..52a2c610 100644 --- a/tests/util.cxx +++ b/tests/util.cxx @@ -17,6 +17,8 @@ */ #include <stdint.h> +#include <stdlib.h> +#include <string.h> #ifdef WIN32 #include <windows.h> @@ -24,52 +26,93 @@ #include <sys/resource.h> #endif +#include "util.h" + #ifdef WIN32 -static FILETIME cpuCounters[2]; +typedef FILETIME syscounter_t; #else -struct rusage cpuCounters[2]; +typedef struct rusage syscounter_t; #endif -static void measureCpu(void *counter) +static syscounter_t _globalCounter[2]; +static cpucounter_t globalCounter = _globalCounter; + +void startCpuCounter(void) +{ + startCpuCounter(globalCounter); +} + +void endCpuCounter(void) +{ + endCpuCounter(globalCounter); +} + +double getCpuCounter(void) +{ + return getCpuCounter(globalCounter); +} + +cpucounter_t newCpuCounter(void) +{ + syscounter_t *c; + + c = (syscounter_t*)malloc(sizeof(syscounter_t) * 2); + if (c == NULL) + return NULL; + + memset(c, 0, sizeof(syscounter_t) * 2); + + return c; +} + +void freeCpuCounter(cpucounter_t c) +{ + free(c); +} + +static void measureCpu(syscounter_t *counter) { #ifdef WIN32 FILETIME dummy1, dummy2, dummy3; GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2, - &dummy3, (FILETIME*)counter); + &dummy3, counter); #else - getrusage(RUSAGE_SELF, (struct rusage*)counter); + getrusage(RUSAGE_SELF, counter); #endif } -void startCpuCounter(void) +void startCpuCounter(cpucounter_t c) { - measureCpu(&cpuCounters[0]); + syscounter_t *s = (syscounter_t*)c; + measureCpu(&s[0]); } -void endCpuCounter(void) +void endCpuCounter(cpucounter_t c) { - measureCpu(&cpuCounters[1]); + syscounter_t *s = (syscounter_t*)c; + measureCpu(&s[1]); } -double getCpuCounter(void) +double getCpuCounter(cpucounter_t c) { + syscounter_t *s = (syscounter_t*)c; double seconds; #ifdef WIN32 uint64_t counters[2]; - counters[0] = (uint64_t)cpuCounters[0].dwHighDateTime << 32 | - cpuCounters[0].dwLowDateTime; - counters[1] = (uint64_t)cpuCounters[1].dwHighDateTime << 32 | - cpuCounters[1].dwLowDateTime; + counters[0] = (uint64_t)s[0].dwHighDateTime << 32 | + s[0].dwLowDateTime; + counters[1] = (uint64_t)s[1].dwHighDateTime << 32 | + s[1].dwLowDateTime; seconds = (double)(counters[1] - counters[2]) / 10000000.0; #else - seconds = (double)(cpuCounters[1].ru_utime.tv_sec - - cpuCounters[0].ru_utime.tv_sec); - seconds += (double)(cpuCounters[1].ru_utime.tv_usec - - cpuCounters[0].ru_utime.tv_usec) / 1000000.0; + seconds = (double)(s[1].ru_utime.tv_sec - + s[0].ru_utime.tv_sec); + seconds += (double)(s[1].ru_utime.tv_usec - + s[0].ru_utime.tv_usec) / 1000000.0; #endif return seconds; diff --git a/tests/util.h b/tests/util.h index ebeadeba..16f2ba2f 100644 --- a/tests/util.h +++ b/tests/util.h @@ -19,9 +19,19 @@ #ifndef __TESTS_UTIL_H__ #define __TESTS_UTIL_H__ +typedef void* cpucounter_t; + void startCpuCounter(void); void endCpuCounter(void); double getCpuCounter(void); +cpucounter_t newCpuCounter(void); +void freeCpuCounter(cpucounter_t c); + +void startCpuCounter(cpucounter_t c); +void endCpuCounter(cpucounter_t c); + +double getCpuCounter(cpucounter_t c); + #endif |