Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include <pj/os.h> |
| 20 | #include <pj/errno.h> |
| 21 | #include <windows.h> |
| 22 | |
Benny Prijono | 99683ae | 2005-11-21 16:59:47 +0000 | [diff] [blame] | 23 | #if defined(PJ_TIMESTAMP_USE_RDTSC) && PJ_TIMESTAMP_USE_RDTSC!=0 && \ |
| 24 | defined(PJ_M_I386) && PJ_M_I386 != 0 && \ |
Benny Prijono | 33723ce | 2005-11-22 01:05:59 +0000 | [diff] [blame] | 25 | defined(PJ_HAS_PENTIUM) && PJ_HAS_PENTIUM!=0 && \ |
Benny Prijono | 99683ae | 2005-11-21 16:59:47 +0000 | [diff] [blame] | 26 | defined(_MSC_VER) |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 27 | |
Benny Prijono | 99683ae | 2005-11-21 16:59:47 +0000 | [diff] [blame] | 28 | /* |
| 29 | * Use rdtsc to get the OS timestamp. |
| 30 | */ |
| 31 | static LONG CpuMhz; |
| 32 | static pj_int64_t CpuHz; |
| 33 | |
| 34 | static pj_status_t GetCpuHz(void) |
| 35 | { |
| 36 | HKEY key; |
| 37 | LONG rc; |
| 38 | DWORD size; |
| 39 | |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 40 | #if defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0 |
| 41 | rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, |
| 42 | L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", |
| 43 | 0, 0, &key); |
| 44 | #else |
Benny Prijono | 99683ae | 2005-11-21 16:59:47 +0000 | [diff] [blame] | 45 | rc = RegOpenKey( HKEY_LOCAL_MACHINE, |
| 46 | "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", |
| 47 | &key); |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Benny Prijono | 99683ae | 2005-11-21 16:59:47 +0000 | [diff] [blame] | 50 | if (rc != ERROR_SUCCESS) |
| 51 | return PJ_RETURN_OS_ERROR(rc); |
| 52 | |
| 53 | size = sizeof(CpuMhz); |
| 54 | rc = RegQueryValueEx(key, "~MHz", NULL, NULL, (BYTE*)&CpuMhz, &size); |
| 55 | RegCloseKey(key); |
| 56 | |
| 57 | if (rc != ERROR_SUCCESS) { |
| 58 | return PJ_RETURN_OS_ERROR(rc); |
| 59 | } |
| 60 | |
| 61 | CpuHz = CpuMhz; |
| 62 | CpuHz = CpuHz * 1000000; |
| 63 | |
| 64 | return PJ_SUCCESS; |
| 65 | } |
| 66 | |
| 67 | /* __int64 is nicely returned in EDX:EAX */ |
| 68 | __declspec(naked) __int64 rdtsc() |
| 69 | { |
| 70 | __asm |
| 71 | { |
| 72 | RDTSC |
| 73 | RET |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) |
| 78 | { |
| 79 | ts->u64 = rdtsc(); |
| 80 | return PJ_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) |
| 84 | { |
| 85 | pj_status_t status; |
| 86 | |
| 87 | if (CpuHz == 0) { |
| 88 | status = GetCpuHz(); |
| 89 | if (status != PJ_SUCCESS) |
| 90 | return status; |
| 91 | } |
| 92 | |
| 93 | freq->u64 = CpuHz; |
| 94 | return PJ_SUCCESS; |
| 95 | } |
| 96 | |
| 97 | #else |
| 98 | /* |
| 99 | * Use QueryPerformanceCounter and QueryPerformanceFrequency. |
| 100 | */ |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 101 | PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts) |
| 102 | { |
| 103 | LARGE_INTEGER val; |
| 104 | |
| 105 | if (!QueryPerformanceCounter(&val)) |
| 106 | return PJ_RETURN_OS_ERROR(GetLastError()); |
| 107 | |
| 108 | ts->u64 = val.QuadPart; |
| 109 | return PJ_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | PJ_DEF(pj_status_t) pj_get_timestamp_freq(pj_timestamp *freq) |
| 113 | { |
| 114 | LARGE_INTEGER val; |
| 115 | |
| 116 | if (!QueryPerformanceFrequency(&val)) |
| 117 | return PJ_RETURN_OS_ERROR(GetLastError()); |
| 118 | |
| 119 | freq->u64 = val.QuadPart; |
| 120 | return PJ_SUCCESS; |
| 121 | } |
| 122 | |
Benny Prijono | 99683ae | 2005-11-21 16:59:47 +0000 | [diff] [blame] | 123 | #endif /* PJ_TIMESTAMP_USE_RDTSC */ |
| 124 | |