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