delay faster then KeDelayExecutionThread

is there way to make delay function that faster then KeDelayExecutionThread?

thats what im using right now

waitTime.QuadPart = -10000000; // 1 second
KeDelayExecutionThread(KernelMode, FALSE, &waitTime);

KeStallExecutionProcessor slow in my end

im thinking loop with rdtsc but i dont know how to convert it to x64 reading it through masm x64 its causing bsod

__declspec(naked) __forceinline void __fastcall Delay(unsigned int TicksC)
{
__asm
{
rdtsc
mov esi, eax
_loop:
rdtsc
sub eax, esi
cmp eax, ecx
jbe _loop
ret
}
}

@daniel02 said:
is there way to make delay function that faster then KeDelayExecutionThread?

Can you elaborate on what your intentions are? What do you mean by “faster”?

@Shane_Corbin said:

@daniel02 said:
is there way to make delay function that faster then KeDelayExecutionThread?

Can you elaborate on what your intentions are? What do you mean by “faster”?

im reading the rdtsc as main system clock so i need very good sleep function that have extreme nanoseconds KeDelayExecutionThread is the fastest on kernel ?

@daniel02 said:
im reading the rdtsc as main system clock so i need very good sleep function that have extreme nanoseconds KeDelayExecutionThread is the fastest on kernel ?

It’s still unclear what you’re trying to do.

rdtsc isn’t a system clock. It’s an instruction that returns the processor time stamp as a number of clock cycles since the last reset.

Are you trying to sleep for exactly one second so you can see how many tick counts there are for the processor in one second?

What’s an extreme nanosecond?

When you ask “is KeDelayExecutionThread() the fastest on kernel” are you wanting to know if it’s return from delay is deterministic? It’s hard to offer a recommendation without knowing more about what you’re trying to do. I’m assuming you’ve found the documentation for KeDelayExecutionThread which clearly states accuracy of expiration times are limited by the granularity of the system clock.

@Shane_Corbin said:

@daniel02 said:
im reading the rdtsc as main system clock so i need very good sleep function that have extreme nanoseconds KeDelayExecutionThread is the fastest on kernel ?

It’s still unclear what you’re trying to do.

rdtsc isn’t a system clock. It’s an instruction that returns the processor time stamp as a number of clock cycles since the last reset.

Are you trying to sleep for exactly one second so you can see how many tick counts there are for the processor in one second?

What’s an extreme nanosecond?

When you ask “is KeDelayExecutionThread() the fastest on kernel” are you wanting to know if it’s return from delay is deterministic? It’s hard to offer a recommendation without knowing more about what you’re trying to do. I’m assuming you’ve found the documentation for KeDelayExecutionThread which clearly states accuracy of expiration times are limited by the granularity of the system clock.

The behavior of the RDTSC instruction is implementation dependent. The TSC counts at a constant rate, but may be affected by power management events (such as frequency changes), depending on the processor implementation. If CPUID Fn8000_0007_EDX[TscInvariant] = 1, then the TSC rate is ensured to be invariant across all P-States, C-States, and stop-grant transitions (such as STPCLK Throttling); therefore, the TSC is suitable for use as a source of time. 

so rdtsc can be used as source of time

i found out KeDelayExecutionThread return to 100ns its still not fast enough then rdtsc loop i will try to found a way thanks for help !

Best of luck. You may find the following information about high resolution time stamps helpful in your quest.
https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps#direct-tsc-usage

@Shane_Corbin said:
Best of luck. You may find the following information about high resolution time stamps helpful in your quest.
https://learn.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps#direct-tsc-usage

thank you very much ZwYieldExecution fixed it

For the sake of clarity, ZwYieldExecution is not a function that delays thread execution in the same way as KeDelayExecutionThread or KeStallExecutionProcessor. KeDelayExecutionThread is like UM Sleep and interacts with the scheduler to make this thread non-runnable for at least the time requested. KeStallExecutionProcessor runs a busy loop for approximately the time requested using a hardware specific algorithm.

ZwYieldExecution interacts with the scheduler and might initiate a context switch if there is another thread ready to run. This is like the UM Sleep(0).

Possibly the OP is avoiding a crash by using ZwYieldExecution to avoid a watchdog timer, but it is hard to know based on the lack of detail