blob: d24a6aae71bbf9a9353ae0fb3fed6cbc74338188 [file] [log] [blame]
mtklein9ac68ee2014-06-20 11:29:20 -07001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SysTimer_windows.h"
8
Mike Klein7edaeb52014-07-15 19:04:14 -04009#include <intrin.h>
10
mtklein9ac68ee2014-06-20 11:29:20 -070011static ULONGLONG win_cpu_time() {
12 FILETIME createTime;
13 FILETIME exitTime;
14 FILETIME usrTime;
15 FILETIME sysTime;
16 if (0 == GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &sysTime, &usrTime)) {
17 return 0;
18 }
19 ULARGE_INTEGER start_cpu_sys;
20 ULARGE_INTEGER start_cpu_usr;
21 start_cpu_sys.LowPart = sysTime.dwLowDateTime;
22 start_cpu_sys.HighPart = sysTime.dwHighDateTime;
23 start_cpu_usr.LowPart = usrTime.dwLowDateTime;
24 start_cpu_usr.HighPart = usrTime.dwHighDateTime;
25 return start_cpu_sys.QuadPart + start_cpu_usr.QuadPart;
26}
27
mtklein9ac68ee2014-06-20 11:29:20 -070028void SysTimer::startCpu() {
29 fStartCpu = win_cpu_time();
30}
31
32double SysTimer::endCpu() {
33 ULONGLONG end_cpu = win_cpu_time();
34 return static_cast<double>(end_cpu - fStartCpu) / 10000.0L;
35}
Mike Klein7edaeb52014-07-15 19:04:14 -040036
37static void wall_timestamp(LARGE_INTEGER* now) {
38 _ReadWriteBarrier();
39 if (0 == ::QueryPerformanceCounter(now)) {
40 now->QuadPart = 0;
41 }
42 _ReadWriteBarrier();
43}
44
45void SysTimer::startWall() {
46 wall_timestamp(&fStartWall);
47}
48
mtklein9ac68ee2014-06-20 11:29:20 -070049double SysTimer::endWall() {
50 LARGE_INTEGER end_wall;
Mike Klein7edaeb52014-07-15 19:04:14 -040051 wall_timestamp(&end_wall);
mtklein9ac68ee2014-06-20 11:29:20 -070052
53 LARGE_INTEGER ticks_elapsed;
54 ticks_elapsed.QuadPart = end_wall.QuadPart - fStartWall.QuadPart;
55
56 LARGE_INTEGER frequency;
57 if (0 == ::QueryPerformanceFrequency(&frequency)) {
58 return 0.0L;
59 } else {
60 return static_cast<double>(ticks_elapsed.QuadPart)
61 / static_cast<double>(frequency.QuadPart)
62 * 1000.0L;
63 }
64}