3 * Politecnico di Torino. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the Politecnico
13 * di Torino, and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include "time_calls.h"
27 void TIME_DESYNCHRONIZE(struct time_conv *data)
30 data->start.tv_sec = 0;
31 data->start.tv_usec = 0;
36 /* KeQueryPerformanceCounter TimeStamps */
38 VOID TIME_SYNCHRONIZE(struct time_conv *data)
41 LARGE_INTEGER SystemTime;
44 LARGE_INTEGER TimeFreq,PTime;
46 if (data->reference!=0)
49 // get the absolute value of the system boot time.
50 PTime=KeQueryPerformanceCounter(&TimeFreq);
51 KeQuerySystemTime(&SystemTime);
53 tmp.tv_sec=(LONG)(SystemTime.QuadPart/10000000-11644473600);
54 tmp.tv_usec=(LONG)((SystemTime.QuadPart%10000000)/10);
55 tmp.tv_sec-=(ULONG)(PTime.QuadPart/TimeFreq.QuadPart);
56 tmp.tv_usec-=(LONG)((PTime.QuadPart%TimeFreq.QuadPart)*1000000/TimeFreq.QuadPart);
68 void FORCE_TIME(struct timeval *src, struct time_conv *dest)
73 void GET_TIME(struct timeval *dst, struct time_conv *data)
75 LARGE_INTEGER PTime, TimeFreq;
78 PTime=KeQueryPerformanceCounter(&TimeFreq);
80 tmp=(LONG)(PTime.QuadPart/TimeFreq.QuadPart);
81 dst->tv_sec=data->start.tv_sec+tmp;
82 dst->tv_usec=data->start.tv_usec+(LONG)((PTime.QuadPart%TimeFreq.QuadPart)*1000000/TimeFreq.QuadPart);
86 if (dst->tv_usec>=1000000) {
88 dst->tv_usec-=1000000;
96 /* callers must be at IRQL=PASSIVE_LEVEL */
97 VOID TIME_SYNCHRONIZE(struct time_conv *data)
100 LARGE_INTEGER system_time;
101 ULONGLONG curr_ticks;
103 LARGE_INTEGER start_kqpc,stop_kqpc,start_freq,stop_freq;
104 ULONGLONG start_ticks,stop_ticks;
105 ULONGLONG delta,delta2;
110 if (data->reference!=0)
113 KeInitializeEvent(&event,NotificationEvent,FALSE);
115 KeRaiseIrql(HIGH_LEVEL,&old);
116 start_kqpc=KeQueryPerformanceCounter(&start_freq);
137 "mov %%edx,(%%ecx+4);"
142 :"=c"(start_ticks): );
145 KeWaitForSingleObject(&event,UserRequest,KernelMode,TRUE ,&i);
146 KeRaiseIrql(HIGH_LEVEL,&old);
147 stop_kqpc=KeQueryPerformanceCounter(&stop_freq);
168 "mov %%edx,(%%ecx+4);"
173 :"=c"(stop_ticks): );
176 delta=stop_ticks-start_ticks;
177 delta2=stop_kqpc.QuadPart-start_kqpc.QuadPart;
178 if (delta>10000000000) {
182 reference=delta*(start_freq.QuadPart)/delta2;
183 data->reference=reference/1000;
184 if (reference%1000>500)
186 data->reference*=1000;
187 reference=data->reference;
188 KeQuerySystemTime(&system_time);
209 "mov %%edx,(%%ecx+4);"
214 :"=c"(curr_ticks): );
216 tmp.tv_sec=-(LONG)(curr_ticks/reference);
217 tmp.tv_usec=-(LONG)((curr_ticks%reference)*1000000/reference);
218 system_time.QuadPart-=116444736000000000;
219 tmp.tv_sec+=(LONG)(system_time.QuadPart/10000000);
220 tmp.tv_usec+=(LONG)((system_time.QuadPart%10000000)/10);
223 tmp.tv_usec+=1000000;
226 IF_LOUD(DbgPrint("Frequency %I64u MHz\n",data->reference);)
229 void FORCE_TIME(struct timeval *src, struct time_conv *dest)
234 void GET_TIME(struct timeval *dst, struct time_conv *data)
257 "mov %%edx,(%%ecx+4);"
264 if (data->reference==0) {
267 dst->tv_sec=(LONG)(tmp/data->reference);
268 dst->tv_usec=(LONG)((tmp-dst->tv_sec*data->reference)*1000000/data->reference);
269 dst->tv_sec+=data->start.tv_sec;
270 dst->tv_usec+=data->start.tv_usec;
271 if (dst->tv_usec>=1000000) {
273 dst->tv_usec-=1000000;