1

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

Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
Chris Condy
  • 626
  • 2
  • 9
  • 25
  • You don't fix them. You find out why in your code you're causing them to be called. For that you [*sample the stack*](http://stackoverflow.com/a/378024/23771). Just one sample should be enough. – Mike Dunlavey May 02 '13 at 11:50

4 Answers4

7

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.

43l0v3k
  • 337
  • 2
  • 9
1

Judging on the names, all 3 functions seem to block, so they take a long time because they are designed to do so, but they shouldn't use any CPU while waiting.

stefaanv
  • 14,072
  • 2
  • 31
  • 53
1

One of the first steps should always be to check the documentation:

Anyway, the name already reveals part of it. "Wait" and "Delay"-functions are supposed to take time. If you want to reduce the waiting time you have to find out what is calling these functions.

To give you an example:
If you start a new thread and then wait for it to finish in your main thread, you will call WaitForSingleObject one way or another in WINAPI-programming. It doesn't even have to be you who is starting the thread - it could be the runtime itself. The function will wait until the thread finishes. Therefore it will take time and block the program in WaitForSingleObject until thread is done or a timeout occurs. This is nothing bad, this is intended behaviour.

Excelcius
  • 1,680
  • 1
  • 14
  • 31
0

Before you start zooming in on these functions, you might first want to determine what kind of slowness your program is suffering from. It is pretty normal for a Windows program to have one or more threads spending most of their time in blocking functions.

You would first need to determine whether your actual critical thread is CPU bound. In that case you don't want to zoom in on the functions that take a lot off wall clock time, you want to find those functions that take CPU time.

I don't have much experience with Very Sleepy, but IIRC it is a sampling profiler, and those are typically not so good at measuring CPU usage.

Only after you've determined that your program is not CPU bound, then you should zoom in on the functions that wait a lot.

dhavenith
  • 2,028
  • 13
  • 14