| 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 // On recent Intel chips (roughly, "has Core or Atom in its name") __rdtsc will
always tick |
| 38 // at the CPU's maximum rate, even while power management clocks the CPU up and
down. |
| 39 // That's great, because it makes measuring wall time super simple. |
| 40 |
| 41 void SysTimer::startWall() { |
| 42 fStartWall = __rdtsc(); |
| 43 } |
| 44 |
| 39 double SysTimer::endWall() { | 45 double SysTimer::endWall() { |
| 40 LARGE_INTEGER end_wall; | 46 unsigned __int64 end = __rdtsc(); |
| 41 if (0 == ::QueryPerformanceCounter(&end_wall)) { | |
| 42 end_wall.QuadPart = 0; | |
| 43 } | |
| 44 | 47 |
| 45 LARGE_INTEGER ticks_elapsed; | 48 // This seems to, weirdly, give the CPU frequency in kHz. That's exactly wh
at we want! |
| 46 ticks_elapsed.QuadPart = end_wall.QuadPart - fStartWall.QuadPart; | 49 LARGE_INTEGER freq_khz; |
| 50 QueryPerformanceFrequency(&freq_khz); |
| 47 | 51 |
| 48 LARGE_INTEGER frequency; | 52 return static_cast<double>(end - fStartWall) / static_cast<double>(freq_khz.
QuadPart); |
| 49 if (0 == ::QueryPerformanceFrequency(&frequency)) { | |
| 50 return 0.0L; | |
| 51 } else { | |
| 52 return static_cast<double>(ticks_elapsed.QuadPart) | |
| 53 / static_cast<double>(frequency.QuadPart) | |
| 54 * 1000.0L; | |
| 55 } | |
| 56 } | 53 } |
| OLD | NEW |