RE: a problem on KeQuerySystemTime()

Query the time and print out both:

  1. Right before you goto sleep
  2. Right after you wakeup

It is likely that one of the following is happening:

  1. Your thread is either taking longer than you expect to execute
  2. Your driver is corrupting system memory and the locations associated with maintaining time

Duane.
-----Original Message-----
From: Dongli Wu [mailto:xxxxx@highstream.net]
Sent: Wednesday, December 25, 2002 9:40 PM
To: NT Developers Interest List
Subject: [ntdev] RE: a problem on KeQuerySystemTime()

Hi, Duane:
Thank you very much for your response.
The deviceExtension->PreviousTime was initialized to 0 in
DriverEntry. In the kernel thread there is only one path that don’t
update the
deviceExtension->PreviousTime: when timeDifference.QuadPart is large
than
TIME_LIMITATION and thread will stop.
To analysis the problem, I put KdPrint() after
KeQuerySystemTime(&currentSystemTime)
and print out every current time tick that returned from
KeQuerySystemTime().
The print out results make me confused: Originally, when
KeWaitForSingleObject is
trigged, the current time tick is correct. But after a while (less than
30 minutes)
the current time tick returned from KeQuerySystemTime suddenly change to
a
new unreasonable value so that timeDifference.QuadPart is larger than
TIME_LIMITATION and kernel thread stoped.

Based on the above scenarios, could you help me figure out what problem
in my driver?

Thanks

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of McCrory, Duane
Sent: Monday, December 23, 2002 3:32 PM
To: NT Developers Interest List
Subject: [ntdev] RE: a problem on KeQuerySystemTime()

I think your problem is not in the timer, but in your service routine.
You might want to check how long it took you do service your queue.

Other comments:

  1. Did you initialize deviceExtension->PreviousTime in DriverEntry?
  2. It looks like you have paths in DrvrSystemTimerThread where you do
    not update deviceExtension->PreviousTime.
  3. I suggest always updating deviceExtension->PreviousTime immediately
    before doing the KeWaitForSingleObjects in your while loop.

Duane.

-----Original Message-----
From: Dongli Wu [mailto:xxxxx@highstream.net]
Sent: Friday, December 20, 2002 7:38 PM
To: NT Developers Interest List
Subject: [ntdev] a problem on KeQuerySystemTime()

---------- Original Message ----------------------------------
Hi, All:
I have a problem on KeQuerySystemTime(). Does anybody can give some
help?

Here is a problem:
My driver has a kernel timer thread, which is trigged by a timer
object every 10 seconds. When the thread started, it walk through
a customized queue and remove some elements(the queue does not
have any relationship with any IRP). This queue is protected by
a mutex. At the same time, the Thread call KeQuerySystemTime()
to check the elapsed time. The problem is sooner or later, the
elapsed time check will fail because the calculated elapsed time
is lager than 10 seconds (actually it is about 30 seconds).

The code segment is :
/****** Initialize ******/
NTSTATUS DriverEntry(IN PDRIVER_OBJECT driverObject,
IN PUNICODE_STRING registryPath)
{
…?
KeInitializeMutex(&deviceExtension->QueueLock, 1);
InitializeListHead(&deviceExtension->ListHead);
KeInitializeTimerEx(&deviceExtension->SystemTimer,
SynchronizationTimer);

status = PsCreateSystemThread(&hSystemTimerThread,
(ACCESS_MASK)0,
NULL,
(HANDLE) 0,
NULL,
DrvrSystemTimerThread,
DeviceObject);

if (!NT_SUCCESS(status))
return status;

// get a pointer to the thread object.
ObReferenceObjectByHandle(hSystemTimerThread,
THREAD_ALL_ACCESS,
NULL,
KernelMode,
&deviceExtension->SystemTimerThread,
NULL);

// release the thread handle.
ZwClose(hSystemTimerThread);

// start system timer, due time is 0, period time 10 seconds
KeSetTimerEx(&deviceExtension->SystemTimer,
0,
10 * 1000,
NULL);
…?
return STATUS_SUCCESS;
}

/**** system thread ******/
VOID DrvrSystemTimerThread(PVOID Context)
{

while (TRUE) {
KeWaitForSingleObjects(deviceExtension->SystemTimer,
Executive,
KernelMode,
FALSE,
NULL);
// calculate the elapsed time, soon or later, this will fail
KeQuerySystemTime(&currentSystemTime);
timeDifference.QuadPart = currentSystemTime.QuadPart -
deviceExtension->PreviousTimeStamp.QuadPart;
if (timeDifference.QuadPart > TIME_LIMITATION /* 10 seconds */)
break;

deviceExtension->PreviousTime.QuadPart = currentSystemTime.QuadPart;

// acquire queue lock first.
KeWaitForSingleObject(&deviceExtension->QueueLock,
Executive,
KernelMode,
FALSE,
NULL);
?
// in this section, kernel thread walk through the queue and remove
// the element from queue

KeReleaseMutex(&deviceExtension->QueueLock, FALSE);
}
}

The logical is very simple, but after a while, the time check will fail
and the system thread will stop. The calculated elapsed time is about 30
seconds. That doesn??t make any sense. To narrow down the problem, I cut
off all other part, just leave the system timer thread in there. Even
though I just add two elements in the queue and let thread remove it.
Eventually, the time check can still fail ( the calculated time is bout
30 seconds).

Does anybody have similar experience? Any information will be
appreciated.

Thanks


You are currently subscribed to ntdev as: xxxxx@infiniconsys.com To
unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@highstream.net To
unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@infiniconsys.com
To unsubscribe send a blank email to %%email.unsub%%

This will be my last post to this thread.

You probably have a DPC or interrupt handler that is consuming the processor (or an interrupt handler that keeps queuing DPCs). It seems that eventually you recover, but there is some timing hole in your code where you are getting stuck for an extremely long period of time.

Duane.
-----Original Message-----
From: Dongli Wu [mailto:xxxxx@highstream.net]
Sent: Thursday, December 26, 2002 2:29 PM
To: NT Developers Interest List
Subject: [ntdev] RE: a problem on KeQuerySystemTime()

Hi, Duane:
I query the time and print out both right before the thread goto sleep
and right after the thread wake up. I still got a chance to see the wrong time tick between right before sleep and right after wake up.
I got 96 seconds one time even though I did not add any thing in the queue yet. That means the queue was empty, and thread didn’t do any
thing. The actuall time looks OK. I mean about every 10 seconds, I can
see the debug information print out.
Is this possible that I didn’t clear the currentSystemTime before I
called KeQuerySystemTime(&currentSystemTime) to query the system
time tick and that cause the problem?

BTW, the problem was hard to reproduce. I still have a chance to see the problem even though the kernel thread almost empty(no element in queue, so no memory allocated).

Thanks

---------- Original Message ----------------------------------
From: “McCrory, Duane”
Reply-To: “NT Developers Interest List”
Date: Thu, 26 Dec 2002 09:27:25 -0500

>
>Query the time and print out both:
>1) Right before you goto sleep
>2) Right after you wakeup
>
>It is likely that one of the following is happening:
>1) Your thread is either taking longer than you expect to execute
>2) Your driver is corrupting system memory and the locations associated with maintaining time
>
>Duane.
>-----Original Message-----
>From: Dongli Wu [mailto:xxxxx@highstream.net]
>Sent: Wednesday, December 25, 2002 9:40 PM
>To: NT Developers Interest List
>Subject: [ntdev] RE: a problem on KeQuerySystemTime()
>
>
>Hi, Duane:
>Thank you very much for your response.
>The deviceExtension->PreviousTime was initialized to 0 in
>DriverEntry. In the kernel thread there is only one path that don’t
>update the
>deviceExtension->PreviousTime: when timeDifference.QuadPart is large
>than
>TIME_LIMITATION and thread will stop.
>To analysis the problem, I put KdPrint() after
>KeQuerySystemTime(&currenttSystemTime)
>and print out every current time tick that returned from
>KeQuerySystemTime().
>The print out results make me confused: Originally, when
>KeWaitForSingleObject is
>trigged, the current time tick is correct. But after a while (less than
>30 minutes)
>the current time tick returned from KeQuerySystemTime suddenly change to
>a
>new unreasonable value so that timeDifference.QuadPart is larger than
>TIME_LIMITATION and kernel thread stoped.
>
>Based on the above scenarios, could you help me figure out what problem
>in my driver?
>
>Thanks
>
>-----Original Message-----
>From: xxxxx@lists.osr.com
>[mailto:xxxxx@lists.osr.com] On Behalf Of McCrory, Duane
>Sent: Monday, December 23, 2002 3:32 PM
>To: NT Developers Interest List
>Subject: [ntdev] RE: a problem on KeQuerySystemTime()
>
>
>I think your problem is not in the timer, but in your service routine.
>You might want to check how long it took you do service your queue.
>
>Other comments:
>1) Did you initialize deviceExtension->PreviousTime in DriverEntry?
>2) It looks like you have paths in DrvrSystemTimerThread where you do
>not update deviceExtension->PreviousTime.
>3) I suggest always updating deviceExtension->PreviousTime immediately
>before doing the KeWaitForSingleObjects in your while loop.
>
>Duane.
>
>-----Original Message-----
>From: Dongli Wu [mailto:xxxxx@highstream.net]
>Sent: Friday, December 20, 2002 7:38 PM
>To: NT Developers Interest List
>Subject: [ntdev] a problem on KeQuerySystemTime()
>
>
>
>---------- Original Message ----------------------------------
>Hi, All:
>I have a problem on KeQuerySystemTime(). Does anybody can give some
>help?
>
>Here is a problem:
>My driver has a kernel timer thread, which is trigged by a timer
>object every 10 seconds. When the thread started, it walk through
>a customized queue and remove some elements(the queue does not
>have any relationship with any IRP). This queue is protected by
>a mutex. At the same time, the Thread call KeQuerySystemTime()
>to check the elapsed time. The problem is sooner or later, the
>elapsed time check will fail because the calculated elapsed time
>is lager than 10 seconds (actually it is about 30 seconds).
>
>The code segment is :
>/ Initialize /
>NTSTATUS DriverEntry(IN PDRIVER_OBJECT driverObject,
> IN PUNICODE_STRING registryPath)
>{
> …?
> KeInitializeMutex(&deviceExtension->QueueLock, 1);
> InitializeListHead(&deviceExtension->ListHead);
> KeInitializeTimerEx(&deviceExtension->SystemTimer,
> SynchronizationTimer);
>
> status = PsCreateSystemThread(&hSystemTimerThread,
> (ACCESS_MASK)0,
> NULL,
> (HANDLE) 0,
> NULL,
> DrvrSystemTimerThread,
> DeviceObject);
>
> if (!NT_SUCCESS(status))
> return status;
>
> // get a pointer to the thread object.
> ObReferenceObjectByHandle(hSystemTimerThread,
> THREAD_ALL_ACCESS,
> NULL,
> KernelMode,
> &deviceExtension->SystemTimerThread,
> NULL);
>
> // release the thread handle.
> ZwClose(hSystemTimerThread);
>
> // start system timer, due time is 0, period time 10 seconds
> KeSetTimerEx(&deviceExtension->SystemTimer,
> 0,
> 10 * 1000,
> NULL);
> …?
> return STATUS_SUCCESS;
>}
>
>/ system thread** /
>VOID DrvrSystemTimerThread(PVOID Context)
>{
> …
> while (TRUE) {
> KeWaitForSingleObjects(deviceExtension->SystemTimer,
> Executive,
> KernelMode,
> FALSE,
> NULL);
> // calculate the elapsed time, soon or later, this will fail
> KeQuerySystemTime(??tSystemTime);
> timeDifference.QuadPart = currentSystemTime.QuadPart -
> deviceExtension->PreviousTimeStamp.QuadPart;
> if (timeDifference.QuadPart > TIME_LIMITATION /* 10 seconds */)
> break;
>
> deviceExtension->PreviousTime.QuadPart = currentSystemTime.QuadPart;
>
> // acquire queue lock first.
> KeWaitForSingleObject(&deviceExtension->QueueLock,
> Executive,
> KernelMode,
> FALSE,
> NULL);
> ?
> // in this section, kernel thread walk through the queue and remove
> // the element from queue
> …
>
> KeReleaseMutex(&deviceExtension->QueueLock, FALSE);
> }
> }
>
>The logical is very simple, but after a while, the time check will fail
>and the system thread will stop. The calculated elapsed time is about 30
>seconds. That doesn??t make any sense. To narrow down the problem, I cut
>off all other part, just leave the system timer thread in there. Even
>though I just add two elements in the queue and let thread remove it.
>Eventually, the time check can still fail ( the calculated time is bout
>30 seconds).
>
>Does anybody have similar experience? Any information will be
>appreciated.
>
>Thanks
>
>
>
>—
>You are currently subscribed to ntdev as: xxxxx@infiniconsys.com To
>unsubscribe send a blank email to %%email.unsub%%
>
>
>—
>You are currently subscribed to ntdev as: xxxxx@highstream.net To
>unsubscribe send a blank email to %%email.unsub%%
>
>
>
>—
>You are currently subscribed to ntdev as: xxxxx@infiniconsys.com
>To unsubscribe send a blank email to %%email.unsub%%
>
>
>—
>You are currently subscribed to ntdev as: xxxxx@highstream.net
>To unsubscribe send a blank email to %%email.unsub%%
>


You are currently subscribed to ntdev as: xxxxx@infiniconsys.com
To unsubscribe send a blank email to %%email.unsub%%