blob: 7cd391e37f8765a1e8156a8a4e3496782d290b3e [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $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 Prijono99683ae2005-11-21 16:59:47 +000023#if defined(PJ_TIMESTAMP_USE_RDTSC) && PJ_TIMESTAMP_USE_RDTSC!=0 && \
24 defined(PJ_M_I386) && PJ_M_I386 != 0 && \
Benny Prijono33723ce2005-11-22 01:05:59 +000025 defined(PJ_HAS_PENTIUM) && PJ_HAS_PENTIUM!=0 && \
Benny Prijono99683ae2005-11-21 16:59:47 +000026 defined(_MSC_VER)
Benny Prijono9cf138e2006-01-19 03:58:29 +000027
Benny Prijono99683ae2005-11-21 16:59:47 +000028/*
29 * Use rdtsc to get the OS timestamp.
30 */
31static LONG CpuMhz;
32static pj_int64_t CpuHz;
33
34static pj_status_t GetCpuHz(void)
35{
36 HKEY key;
37 LONG rc;
38 DWORD size;
39
Benny Prijono9cf138e2006-01-19 03:58:29 +000040#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 Prijono99683ae2005-11-21 16:59:47 +000045 rc = RegOpenKey( HKEY_LOCAL_MACHINE,
46 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
47 &key);
Benny Prijono9cf138e2006-01-19 03:58:29 +000048#endif
49
Benny Prijono99683ae2005-11-21 16:59:47 +000050 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
77PJ_DEF(pj_status_t) pj_get_timestamp(pj_timestamp *ts)
78{
79 ts->u64 = rdtsc();
80 return PJ_SUCCESS;
81}
82
83PJ_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 Prijono9033e312005-11-21 02:08:39 +0000101PJ_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
112PJ_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 Prijono99683ae2005-11-21 16:59:47 +0000123#endif /* PJ_TIMESTAMP_USE_RDTSC */
124