Search code examples
c++profilingverysleepy

VerySleepy Profiling c++ code


While profiling my code to find what is going slow, I have 3functions that are taking forever apparently, well thats what very sleepy says.

These functions are:

ZwDelayExecution    20.460813   20.460813   19.987685   19.987685
MsgWaitForMultipleObjects   20.460813   20.460813   19.987685   19.987685
WaitForSingleObject 20.361805   20.361805   19.890967   19.890967

Can anybody tell me what these functions are? Why they are taking so long, and how to fix them.

Thanks


Solution

  • Probably that functions are used to make thread 'sleeping' in Win32 API. Also they might be used as thread synchronization so check these thing.

    They are taking so much CPU time because they are designed for that.


    The WaitForSingleObject function can wait for the following objects:

    • Change notification
    • Console input
    • Event
    • Memory resource notification
    • Mutex
    • Process
    • Semaphore
    • Thread
    • Waitable timer

    So the other possible thing where it can be used for is console user input waiting.


    ZwDelayExecution is an internal function of Windows. As it can be seen it is used to realize Sleep function. Here is call stack for Sleep function so you can see it with your own eyes:

    0  ntdll.dll        ZwDelayExecution    
    1  kernel32.dll     SleepEx     
    2  kernel32.dll     Sleep   
    

    It probaly uses Assembly low-level features to realize that so it can delay thread with precision of 100ns.


    MsgWaitForMultipleObjects has a similar to WaitForSingleObject goal.