Hi,
Im writing a delay routine for a 2.6 Ghz machine.I want a ealy of
around 5 milli seconds. I dont know whats going wrong with this code.
#define CYCLES_REQURIED 2600000 // 1millisec requires so many cycles
__declspec(naked) unsigned __int64 GetCycles()
{
__asm
{
rdtsc
ret
}
}
__declspec(naked) void Delay(unsigned __int64 milliseconds)
{
unsigned __int64 cyclesrequired,cyclesstart,cyclesend;
cyclesrequired = CYCLES_REQURIED*milliseconds*;
cyclesstart = GetCycles();
cyclesend = GetCycles();
while(cyclesend-cyclesstart < cyclesrequired)
{
cyclesend = GetCycles();
}
}
Regards,
Manohara
- your assumption about CYCLES_REQURIED = 2600000 on a 2.6 Ghz is theoretically not correct .
- the last asterisk is a typing error I presume : cyclesrequired = CYCLES_REQURIED*milliseconds*;
- you have to care about wrapping of the rdtsc value from max to zero
Why do you use this instead of “KeStallExecutionProcessor(…)” ?
----- Original Message -----
From: “Manohara.K”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Wednesday, August 04, 2004 3:36 PM
Subject: [ntdev] RDTSC
> Hi,
>
> Im writing a delay routine for a 2.6 Ghz machine.I want a ealy of
> around 5 milli seconds. I dont know whats going wrong with this code.
>
> #define CYCLES_REQURIED 2600000 // 1millisec requires so many cycles
>
> __declspec(naked) unsigned__int64 GetCycles()
> {
> asm
> {
> rdtsc
> ret
> }
> }
>
> declspec(naked) void Delay(unsigned __int64 milliseconds)
> {
> unsigned__int64 cyclesrequired,cyclesstart,cyclesend;
> cyclesrequired = CYCLES_REQURIEDmilliseconds;
> cyclesstart = GetCycles();
> cyclesend = GetCycles();
> while(cyclesend-cyclesstart < cyclesrequired)
> {
> cyclesend = GetCycles();
> }
>
> }
>
>
> Regards,
> Manohara
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
RDTSC lies a lot if processor speed control for power management is
employed.
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com
----- Original Message -----
From: “Christiaan Ghijselinck”
To: “Windows System Software Devs Interest List”
Sent: Wednesday, August 04, 2004 6:31 PM
Subject: Re: [ntdev] RDTSC
>
> 1. your assumption about CYCLES_REQURIED = 2600000 on a 2.6 Ghz is
theoretically not correct .
> 2. the last asterisk is a typing error I presume : cyclesrequired =
CYCLES_REQURIEDmilliseconds;
> 3. you have to care about wrapping of the rdtsc value from max to zero
>
> Why do you use this instead of “KeStallExecutionProcessor(…)” ?
>
>
>
> ----- Original Message -----
> From: “Manohara.K”
> Newsgroups: ntdev
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, August 04, 2004 3:36 PM
> Subject: [ntdev] RDTSC
>
>
> > Hi,
> >
> > Im writing a delay routine for a 2.6 Ghz machine.I want a ealy of
> > around 5 milli seconds. I dont know whats going wrong with this code.
> >
> > #define CYCLES_REQURIED 2600000 // 1millisec requires so many cycles
> >
> > __declspec(naked) unsigned__int64 GetCycles()
> > {
> > asm
> > {
> > rdtsc
> > ret
> > }
> > }
> >
> > declspec(naked) void Delay(unsigned __int64 milliseconds)
> > {
> > unsigned__int64 cyclesrequired,cyclesstart,cyclesend;
> > cyclesrequired = CYCLES_REQURIEDmilliseconds;
> > cyclesstart = GetCycles();
> > cyclesend = GetCycles();
> > while(cyclesend-cyclesstart < cyclesrequired)
> > {
> > cyclesend = GetCycles();
> > }
> >
> > }
> >
> >
> > Regards,
> > Manohara
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
xxxxx@compaqnet.be
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
Also, I wouldn’t worry about the wrap from max to zero problem that was mentioned. It would take nearly 225 years to wrap the counter on a 2.6 Ghz system if you could keep your box up and running that long. That would equate to a 584.94 Ghz system to wrap the counter in a year! LOL
(1.0 E+63 / (60 Sec * 60 Min * 24 Hrs * 365)) / 1.0 E+9 = 584.94 Ghz
>> xxxxx@storagecraft.com 08/04/04 10:01AM >>>
RDTSC lies a lot if processor speed control for power management is
employed.
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com
----- Original Message -----
From: “Christiaan Ghijselinck”
To: “Windows System Software Devs Interest List”
Sent: Wednesday, August 04, 2004 6:31 PM
Subject: Re: [ntdev] RDTSC
>
> 1. your assumption about CYCLES_REQURIED = 2600000 on a 2.6 Ghz is
theoretically not correct .
> 2. the last asterisk is a typing error I presume : cyclesrequired =
CYCLES_REQURIEDmilliseconds;
> 3. you have to care about wrapping of the rdtsc value from max to zero
>
> Why do you use this instead of “KeStallExecutionProcessor(…)” ?
>
>
>
> ----- Original Message -----
> From: “Manohara.K”
> Newsgroups: ntdev
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, August 04, 2004 3:36 PM
> Subject: [ntdev] RDTSC
>
>
> > Hi,
> >
> > Im writing a delay routine for a 2.6 Ghz machine.I want a ealy of
> > around 5 milli seconds. I dont know whats going wrong with this code.
> >
> > #define CYCLES_REQURIED 2600000 // 1millisec requires so many cycles
> >
> > __declspec(naked) unsigned__int64 GetCycles()
> > {
> > asm
> > {
> > rdtsc
> > ret
> > }
> > }
> >
> > declspec(naked) void Delay(unsigned __int64 milliseconds)
> > {
> > unsigned__int64 cyclesrequired,cyclesstart,cyclesend;
> > cyclesrequired = CYCLES_REQURIEDmilliseconds;
> > cyclesstart = GetCycles();
> > cyclesend = GetCycles();
> > while(cyclesend-cyclesstart < cyclesrequired)
> > {
> > cyclesend = GetCycles();
> > }
> >
> > }
> >
> >
> > Regards,
> > Manohara
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
xxxxx@compaqnet.be
> > To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
>
> —
> Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@storagecraft.com
> To unsubscribe send a blank email to xxxxx@lists.osr.com
—
Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
You are currently subscribed to ntdev as: xxxxx@dcainc.com
To unsubscribe send a blank email to xxxxx@lists.osr.com
What exactly do you mean by “going wrong”?
Chuck
----- Original Message -----
From: “Manohara.K”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Wednesday, August 04, 2004 8:36 PM
Subject: [ntdev] RDTSC
> Hi,
>
> Im writing a delay routine for a 2.6 Ghz machine.I want a ealy of
> around 5 milli seconds. I dont know whats going wrong with this code.
>
> #define CYCLES_REQURIED 2600000 // 1millisec requires so many cycles
>
> __declspec(naked) unsigned__int64 GetCycles()
> {
> asm
> {
> rdtsc
> ret
> }
> }
>
> declspec(naked) void Delay(unsigned __int64 milliseconds)
> {
> unsigned__int64 cyclesrequired,cyclesstart,cyclesend;
> cyclesrequired = CYCLES_REQURIEDmilliseconds;
> cyclesstart = GetCycles();
> cyclesend = GetCycles();
> while(cyclesend-cyclesstart < cyclesrequired)
> {
> cyclesend = GetCycles();
> }
>
> }
>
>
> Regards,
> Manohara
Probably because he thinks that he has to stall the processor for 5 whole
milliseconds.
–
Jake Oshins
Windows Kernel Group
This posting is provided “AS IS” with no warranties, and confers no rights.
“Christiaan Ghijselinck” wrote in
message news:xxxxx@ntdev…
>
> 1. your assumption about CYCLES_REQURIED = 2600000 on a 2.6 Ghz is
> theoretically not correct .
> 2. the last asterisk is a typing error I presume : cyclesrequired =
> CYCLES_REQURIEDmilliseconds;
> 3. you have to care about wrapping of the rdtsc value from max to zero
>
> Why do you use this instead of “KeStallExecutionProcessor(…)” ?
>
>
>
> ----- Original Message -----
> From: “Manohara.K”
> Newsgroups: ntdev
> To: “Windows System Software Devs Interest List”
> Sent: Wednesday, August 04, 2004 3:36 PM
> Subject: [ntdev] RDTSC
>
>
>> Hi,
>>
>> Im writing a delay routine for a 2.6 Ghz machine.I want a ealy of
>> around 5 milli seconds. I dont know whats going wrong with this code.
>>
>> #define CYCLES_REQURIED 2600000 // 1millisec requires so many cycles
>>
>> __declspec(naked) unsigned__int64 GetCycles()
>> {
>> asm
>> {
>> rdtsc
>> ret
>> }
>> }
>>
>> declspec(naked) void Delay(unsigned __int64 milliseconds)
>> {
>> unsigned__int64 cyclesrequired,cyclesstart,cyclesend;
>> cyclesrequired = CYCLES_REQURIEDmilliseconds;
>> cyclesstart = GetCycles();
>> cyclesend = GetCycles();
>> while(cyclesend-cyclesstart < cyclesrequired)
>> {
>> cyclesend = GetCycles();
>> }
>>
>> }
>>
>>
>> Regards,
>> Manohara
>>
>> —
>> Questions? First check the Kernel Driver FAQ at
>> http://www.osronline.com/article.cfm?id=256
>>
>> You are currently subscribed to ntdev as:
>> xxxxx@compaqnet.be
>> To unsubscribe send a blank email to xxxxx@lists.osr.com
>>
>
In addition to the problems mentioned, writing an __declspec(naked) function
with C code inside and having no prolog/epilog is very bad. It is probably
going wrong… i.e. you blow the stack of the calling thread and it probably
crash after return.
“Manohara.K” wrote in message news:xxxxx@ntdev…
>
> __declspec(naked) void Delay(unsigned__int64 milliseconds)
> {
> unsigned __int64 cyclesrequired,cyclesstart,cyclesend;
> cyclesrequired = CYCLES_REQURIEDmilliseconds;
> cyclesstart = GetCycles();
> cyclesend = GetCycles();
> while(cyclesend-cyclesstart < cyclesrequired)
> {
> cyclesend = GetCycles();
> }
>
> }
>
>
> Regards,
> Manohara
>
Of course, because EBP still points to the callers stack area !
I had overseen that …
----- Original Message -----
From: “Marc-Antoine Ruel”
Newsgroups: ntdev
To: “Windows System Software Devs Interest List”
Sent: Wednesday, August 04, 2004 7:29 PM
Subject: Re:[ntdev] RDTSC
> In addition to the problems mentioned, writing an __declspec(naked) function
> with C code inside and having no prolog/epilog is very bad. It is probably
> going wrong… i.e. you blow the stack of the calling thread and it probably
> crash after return.
>
> “Manohara.K” wrote in message news:xxxxx@ntdev…
> >
> >__declspec(naked) void Delay(unsigned __int64 milliseconds)
> > {
> > unsigned__int64 cyclesrequired,cyclesstart,cyclesend;
> > cyclesrequired = CYCLES_REQURIEDmilliseconds;
> > cyclesstart = GetCycles();
> > cyclesend = GetCycles();
> > while(cyclesend-cyclesstart < cyclesrequired)
> > {
> > cyclesend = GetCycles();
> > }
> >
> > }
> >
> >
> > Regards,
> > Manohara
> >
>
>
> —
> Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@compaqnet.be
> To unsubscribe send a blank email to xxxxx@lists.osr.com
>
Hogging the CPU like this and at the same time trying to hold up a
4.4 MByte Isochronous USB pipe might be contra productive( see post
'IoCallDriver').
AFAIK the setup of new Iso-Transfers takes place at DpcForIsr in the
USB Hostcontroller driver. You may generate overflows in the HC.
Norbert.
"When all else fails, read the instructions."
---- snip ----
The other guy who needs to poll his USB device every 2ms is going to be
mighty upset by a driver with a 5ms delay loop.

Chuck
----- Original Message -----
From: “Norbert Kawulski”
To: “Windows System Software Devs Interest List”
Sent: Thursday, August 05, 2004 2:42 PM
Subject: Re:[ntdev] RDTSC
> Hogging the CPU like this and at the same time trying to hold up a
> 4.4 MByte Isochronous USB pipe might be contra productive( see post
> ‘IoCallDriver’).
>
> AFAIK the setup of new Iso-Transfers takes place at DpcForIsr in the
> USB Hostcontroller driver. You may generate overflows in the HC.
>
>
> Norbert.
> --------
> “When all else fails, read the instructions.”
> ---- snip ----