Analyzers
Talk to fellow users of Intel Analyzer tools (Intel VTune™ Profiler, Intel Advisor)
4991 Discussions

New to Vtune, how to understand Wait Count for a Sleep Object.

brettgrant99
Beginner
1,333 Views

I am fairly new to Vtune and multithreaded stuff.  Running C++11 on Windows in Visual Studio.

 

I have a thread that is basically a while-loop that runs a few commands and then puts the thread to sleep for 50 msec.  Basically, it is a thread to run every 50 msec (I know, it doesn't really account for the few commands, but I inherited the code and it isn't that important).

 

For a test, I run a known set of test vectors for 90 seconds, and the analysis Summary always says that Sleep is the top waiting object.  I'm probably not thinking about it correctly, because that particular sleep command I know about and don't really care about.  I care about the commands in the thread that are running when it is not asleep.

It is hard to explain to management why "Wait Time with poor CPU Utilization" has the top object as "Sleep" and that it is expected and intended.  I guess that this is more of a threading question, but I thought that putting a thread to sleep was the right thing to do, freeing up the OS to work on other things.  It's not like I put in a busy-wait loop or something.

 

Back to the Vtune question, when I look at this thread, it says that it has a wait count of ~1800.  I think that it is counting the number of times that the thread goes to sleep.  (1800 / 50 ms ~= 90 s).  It seems strange to me that it is counting the number of times the thread has to wait upon itself.

 

Am I thinking about this correctly?  Any insights?

Thanks,

Brett 

Labels (1)
0 Kudos
9 Replies
AthiraM_Intel
Moderator
1,312 Views

Hi,

 

Thank you for posting in Intel Communities.

 

Could you please share the sample reproducer code and exact steps to reproduce the same from our side.

 

We try to analyze a similar c++ code in Windows using VTune 2023.2 and got the below results. Please refer the below screenshots:

AthiraM_Intel_0-1699012186369.pngAthiraM_Intel_1-1699012213262.png

 

 

The following aspects of Threading Analysis provide possible reasons for poor CPU utilization:

 

Thread count: a quick glance at the application thread count can give clues to threading inefficiencies, such as a fixed number of threads that might prevent the application from scaling to a larger number of cores or lead to thread oversubscription.

 

Wait time (trace-based or context switch-based): analyze threads waiting on synchronization objects or I/O.

 

Spin and overhead time: Threading analysis shows how much time the application spends in threading runtimes either because of busy waits or overhead on parallel work arrangement. 

 

Threading analysis helps to analyze thread wait time and find synchronization bottlenecks. A high thread wait time can cause poor CPU utilization. In the Bottom-up window you can identify the most performance critical synchronization objects. Although it varies, often there are non-interesting threads waiting for a long time on objects infrequently. Usually VTune recommended to focus your tuning efforts on the waits with both high Wait Time and Wait Count values, especially if they have poor utilization/concurrency.

 

Please refer the below links for more detailed information:

https://www.intel.com/content/www/us/en/docs/vtune-profiler/user-guide/2023-0/threading-efficiency-view.html#WAIT:~:text=of%20performance%20issues.-,Wait%20Time,-A%20high%20thread

https://www.intel.com/content/www/us/en/docs/vtune-profiler/user-guide/2023-0/threading-analysis.html

 

If you have any further doubts, please let us know.

 

Thanks

 

 

0 Kudos
brettgrant99
Beginner
1,297 Views

Thanks for the input.  It will probably take me some time to get a representative sample to run, but I can show a few things.  I think we are talking about the same thing.

 

Here is the summary 

brettgrant99_0-1699020540310.pngbrettgrant99_1-1699020773118.png

The soundsThread is a simple one line call to sends an event to a external sound board.  What I don't understand is why the soundThread ever has a wait, since I am telling it to go to sleep and then it should wake up X milliseconds later.  It is not like something else is calling the soundsThread while it is sleeping, which is what this seems to imply to me.

Also, how does it determine the gray and red bars.  If it is asleep, I would assume that it would be idle the entire time.  If I drill down, I do not see any time being consumed by sending my event to the sound board.

 

Thanks,

Brett

0 Kudos
AthiraM_Intel
Moderator
1,232 Views

Hi,


Thank you for the update. To assist you further, could you please share the below details:


  1. sample reproducer code and exact steps you followed
  2. OS and Hardware details
  3. VTune version



Thanks


0 Kudos
AthiraM_Intel
Moderator
1,148 Views

Hi,


We have not heard back from you. Could you please give us an update?



Thanks


0 Kudos
brettgrant99
Beginner
1,125 Views

I am running Vtune 2023.0.0 build 624757 with VS 2019 version 16.11.9 on an Intel i7-11850H @ 2.5GHz - 8 cores.

Here is some sample code which I run for 60 seconds:

#include <iostream>
#include <thread>

int main()
{
unsigned int counter{0};
while (1) {
counter++;
std::cout << counter << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}

When I run it for 60 seconds, it records:

brettgrant99_0-1699981534231.png

So, why are there waits on operator<< and sleep_for() functions?  The thread shouldn't be sleeping while doing output, so what is is it waiting for? 

Similarly, I tell the thread to go to sleep, so why is there a wait count of 117 for the sleep_for function?

I would expect the wait count to be zero for both of those.

Thanks,

Brett

0 Kudos
AthiraM_Intel
Moderator
1,039 Views

Hi,


Thank you for sharing the details. We are checking on this internally, will get back to you with an update.



Thanks


0 Kudos
AthiraM_Intel
Moderator
977 Views

Hi,


The Intel VTune profiler reports wait counts for functions like std::this_thread::sleep_for, which might seem counterintuitive since these functions intentionally put threads to sleep. However, VTune profiles system activity comprehensively, tracking time spent in various thread states, including waiting periods.

When a thread enters a sleep state via functions like sleep_for, it's intentionally not performing active computations, allowing the operating system to allocate CPU resources to other tasks. Despite the thread being inactive during sleep, VTune tracks time spent in different thread states, including sleep or wait states, to provide a holistic view of an application's behavior and resource utilization.

Ref: https://www.intel.com/content/www/us/en/docs/vtune-profiler/user-guide/2023-0/threading-efficiency-view.html


If you need any further clarification, please let us know.



Thanks


0 Kudos
AthiraM_Intel
Moderator
889 Views

Hi,


We have not heard back from you. Could you please give us an update?



Thanks


0 Kudos
AthiraM_Intel
Moderator
810 Views

Hi,


Since we didn't hear back from you, we assume that your issue is resolved. If you need any additional information, please post a new question as this thread will no longer be monitored by Intel.


Thanks



0 Kudos
Reply