aboutsummaryrefslogtreecommitdiffstats
path: root/tests/perf/util.cxx
blob: 17a8369888cde0aa3ad7987e9cc9bfd07986fb02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright 2013-2014 Pierre Ossman <ossman@cendio.se> for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#include <windows.h>
#else
#include <sys/resource.h>
#include <sys/time.h>
#endif

#include "util.h"

#ifdef WIN32
typedef struct {
  FILETIME kernelTime;
  FILETIME userTime;
} syscounter_t;
#else
typedef struct rusage syscounter_t;
#endif

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;

  GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
                  &counter->kernelTime, &counter->userTime);
#else
  getrusage(RUSAGE_SELF, counter);
#endif
}

void startCpuCounter(cpucounter_t c)
{
  syscounter_t *s = (syscounter_t*)c;
  measureCpu(&s[0]);
}

void endCpuCounter(cpucounter_t c)
{
  syscounter_t *s = (syscounter_t*)c;
  measureCpu(&s[1]);
}

double getCpuCounter(cpucounter_t c)
{
  syscounter_t *s = (syscounter_t*)c;
  double sysSeconds, userSeconds;

#ifdef WIN32
  uint64_t counters[2];

  counters[0] = (uint64_t)s[0].kernelTime.dwHighDateTime << 32 |
                s[0].kernelTime.dwLowDateTime;
  counters[1] = (uint64_t)s[1].kernelTime.dwHighDateTime << 32 |
                s[1].kernelTime.dwLowDateTime;

  sysSeconds = (double)(counters[1] - counters[0]) / 10000000.0;

  counters[0] = (uint64_t)s[0].userTime.dwHighDateTime << 32 |
                s[0].userTime.dwLowDateTime;
  counters[1] = (uint64_t)s[1].userTime.dwHighDateTime << 32 |
                s[1].userTime.dwLowDateTime;

  userSeconds = (double)(counters[1] - counters[0]) / 10000000.0;
#else
  sysSeconds = (double)(s[1].ru_stime.tv_sec -
                        s[0].ru_stime.tv_sec);
  sysSeconds += (double)(s[1].ru_stime.tv_usec -
                         s[0].ru_stime.tv_usec) / 1000000.0;

  userSeconds = (double)(s[1].ru_utime.tv_sec -
                         s[0].ru_utime.tv_sec);
  userSeconds += (double)(s[1].ru_utime.tv_usec -
                          s[0].ru_utime.tv_usec) / 1000000.0;
#endif

  return sysSeconds + userSeconds;
}

#ifdef WIN32
static LARGE_INTEGER timeStart, timeEnd;
#else
static struct timeval timeStart, timeEnd;
#endif

void startTimeCounter(void)
{
#ifdef WIN32
  QueryPerformanceCounter(&timeStart);
#else
  gettimeofday(&timeStart, NULL);
#endif
}

void endTimeCounter(void)
{
#ifdef WIN32
  QueryPerformanceCounter(&timeEnd);
#else
  gettimeofday(&timeEnd, NULL);
#endif
}

double getTimeCounter(void)
{
  double time;

#ifdef WIN32
  LARGE_INTEGER freq;

  QueryPerformanceFrequency(&freq);

  time = timeEnd.QuadPart - timeStart.QuadPart;
  time = time / freq.QuadPart;
#else
  time = (double)timeEnd.tv_sec - timeStart.tv_sec;
  time += (double)(timeEnd.tv_usec - timeStart.tv_usec) / 1000000.0;
#endif

  return time;
}