Author Topic: QueryPerformanceCounter: Sleep slows down my code  (Read 646 times)


jj2007

  • Member
  • *****
  • Posts: 7889
  • Assembler is fun ;-)
    • MasmBasic
Best strategy for timings
« Reply #16 on: December 02, 2017, 02:38:45 PM »
Finally, a graph showing the results for the optimal timing strategy:
- discard first x timings to load the cache
- sort the results
- eliminate the highest 20%, i.e. the spikes

Note that the average is calculated before the top 20% got eliminated. The real average is 101.5 microseconds.

@nidud: yes, of course, mov edi, edi is the hot-patch instruction that you find at the entry of most API functions. But the rest is funny ;)

Siekmanski

  • Member
  • *****
  • Posts: 1169
Re: QueryPerformanceCounter: Sleep slows down my code
« Reply #17 on: December 02, 2017, 09:29:29 PM »
From the ntdll.dll these functions are interesting,

ZwQueryTimerResolution
ZwSetTimerResolution
ZwDelayExecution

Maybe we can create our own Sleep function with a 100 nanoseconds granularity and get below 1 millisecond?

more info: http://www.google.nl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0ahUKEwj0ioejiuvXAhUSpKQKHXRPBaUQFgg-MAM&url=http%3A%2F%2Fread.pudn.com%2Fdownloads137%2Fdoc%2F584096%2F%25E6%259C%25AA%25E6%2596%2587%25E6%25A1%25A3%25E5%258C%2596%25E5%2587%25BD%25E6%2595%25B0%2F10%2520Time.pdf&usg=AOvVaw2OY09pfo_iU8iIPP8gE0vM

jj2007

  • Member
  • *****
  • Posts: 7889
  • Assembler is fun ;-)
    • MasmBasic
Re: QueryPerformanceCounter: Sleep slows down my code
« Reply #18 on: December 02, 2017, 11:33:47 PM »
From the ntdll.dll these functions are interesting,

ZwQueryTimerResolution
ZwSetTimerResolution
ZwDelayExecution

These are for drivers; the equivalent Ntxx functions do the same.

Quote
Maybe we can create our own Sleep function with a 100 nanoseconds granularity and get below 1 millisecond?

In theory, yes, but it would increase power consumption.

I have another little project: a Delay2AbsoluteTime macro. Here are first results:

Code: [Select]
13:29:42.066  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:44.123    2057 ms passed

13:29:44.273  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:46.123    1850 ms passed

13:29:46.146  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:48.123    1977 ms passed

13:29:48.168  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:50.123    1955 ms passed

13:29:50.411  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:52.123    1712 ms passed

13:29:52.341  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:54.123    1782 ms passed

13:29:54.396  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:56.123    1727 ms passed

13:29:56.196  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:58.123    1927 ms passed

13:29:58.385  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:58.123    1 ms passed

13:29:58.393  - end should be two seconds later, at hh:mm:??.123 millisecs
13:29:58.123    1 ms passed

Interestingly enough, the granularity is not the usual 16 or so millisecs (1/64) of Sleep & friends but rather 1 ms.

P.S. - results from powercfg -energy duration 5 (google translated), it seems that Pale Moon is also greedy:
Code: [Select]
Platform timer resolution: Platform timer resolution
The default platform timer resolution is 15.6 ms (15625000 ns) and must be used whenever the system is idle. If the timer resolution increases, the processor's energy-saving technologies may not be effective. The timer resolution may increase in the case of multimedia playback or graphic animations.
Current timer resolution (100 ns unit) 10000
Max timer interval (100 ns unit) 156001

Platform timer resolution: Pending timer request
A program or service has requested a resolution of the timer that is less than the maximum resolution of the platform timer.
Requested range 10000
Request process ID 6028
Request process location \ Device \ HarddiskVolume3 \ Program Files (x86) \ Pale Moon \ palemoon.exe

So my system is running at 1 ms resolution if the browser is open :(
« Last Edit: December 03, 2017, 01:04:36 AM by jj2007 »

Siekmanski

  • Member
  • *****
  • Posts: 1169
Re: QueryPerformanceCounter: Sleep slows down my code
« Reply #19 on: December 03, 2017, 01:24:40 AM »
From the ntdll.dll these functions are interesting,

ZwQueryTimerResolution
ZwSetTimerResolution
ZwDelayExecution

These are for drivers; the equivalent Ntxx functions do the same.

Forgot to mention that...

Did a quick test with NtQueryTimerResolution:

CurrentResolution =  10003
MaximumResolution =   5000
MinimumResolution = 156250

Seems we can have a "Sleep" function of 0.5 milliseconds.
I'll have to check first if it is useful or not in a timer interrupt, and how it behaves on different computers. ( cpu load etc. )

Quote
I have another little project: a Delay2AbsoluteTime macro.

Timing in windows is a real challenge.

jj2007

  • Member
  • *****
  • Posts: 7889
  • Assembler is fun ;-)
    • MasmBasic
Re: QueryPerformanceCounter: Sleep slows down my code
« Reply #20 on: December 03, 2017, 01:31:32 AM »
> NtQueryTimerResolution
Nice - I used powercfg -energy duration 5 which is much clumsier :t

Further analysis shows that my browser Pale Moon is indeed guilty of setting the resolution to 1ms, thus causing increased CPU load and power consumption, BUT: it doesn't so automatically. If I open the browser with a static page, granularity is as usual 1/64; only if I visit Google's baby called "YouTube", the resolution suddenly jumps up, and granularity goes to 1ms. Good to know :bgrin:

P.S.: Can I get some info, please?
Code: [Select]
This is Windows version 6.1, build 7601
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz

initial res:    100000

current res
esi             0
edi             5000

current res
esi             10000
edi             10000

current res
esi             12500
edi             12500

current res
esi             25000
edi             25000

current res
esi             50000
edi             50000

current res
esi             100000
edi             100000
Code: [Select]
This is Windows version 5.1, build 2600
Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz

initial res:    100144

current res
esi             0
edi             10032

Weird results for Win10, it seems to be stuck at 10000:
Code: [Select]
This is Windows version 10.0, build 15063
Intel(R) Celeron(R) CPU  N2840  @ 2.16GHz

initial res:    10014

current res
esi             0
edi             5007

lowest, highes, current res
eax             5000
edx             5007
ecx             156250

current res
esi             10000
edi             10014

lowest, highes, current res
eax             5000
edx             10014
ecx             156250

lowest, highes, current res
eax             5000
edx             10014
ecx             156250
« Last Edit: December 03, 2017, 04:40:28 AM by jj2007 »

Siekmanski

  • Member
  • *****
  • Posts: 1169
Re: QueryPerformanceCounter: Sleep slows down my code
« Reply #21 on: December 03, 2017, 07:37:14 AM »
I've implemented the 0.5 ms "Sleep" routine in my timer interrupt.
It works perfect.
Now I can keep track with the write/play position message with more accuracy and precision. ( within 4 samples at a samplerate of 44100Hz )
Without losing a message or producing clicks in the sound, I'm happy !  :biggrin:
And the cpu load is very small 0.0 - 0.2 % on my computer.
Always thought that it was impossible without a big cpu load.

Jochen, thanks for the inspiring thread.



jj2007

  • Member
  • *****
  • Posts: 7889
  • Assembler is fun ;-)
    • MasmBasic
Re: QueryPerformanceCounter: Sleep slows down my code
« Reply #22 on: December 03, 2017, 09:09:21 AM »
And the cpu load is very small 0.0 - 0.2 % on my computer.

For the program, or the whole computer? The setting is global...

Quote
Jochen, thanks for the inspiring thread.

Still struggling to understand everything. For my Win7-64 machine, it is pretty clear, but the XP VM and the Win10 notebook show an entirely different behaviour for NtQueryTimerResolution :(

Siekmanski

  • Member
  • *****
  • Posts: 1169
Re: QueryPerformanceCounter: Sleep slows down my code
« Reply #23 on: December 03, 2017, 10:05:41 AM »
For the program only 0.1 % ( see task manager "AudioDSwav.exe" in the top right of the posted image )

I have to test it on other computers, and see how it behaves.