views:

294

answers:

8

I am running some profiling tests, and usleep is an useful function. But while my program is sleeping, this time does not appear in the profile.

eg. if I have a function as :

void f1() {
    for (i = 0; i < 1000; i++)
        usleep(1000);
}

With profile tools as gprof, f1 does not seems to consume any time.

What I am looking is a method nicer than an empty while loop for doing an active sleep, like:

while (1) {
    if (gettime()  == whatiwant)
        break;
}
+3  A: 

Because when you call usleep the CPU is put to work to something else for 1 second. So the current thread does not use any processor resources, and that's a very clever thing to do.

An active sleep is something to absolutely avoid because it's a waste of resources (ultimately damaging the environment by converting electricity to heat ;) ).

Anyway if you really want to do that you must give some real work to do to the processor, something that will not be factored out by compiler optimizations. For example

for (i = 0; i < 1000; i++)
    time(NULL);
IlDan
+2  A: 

I assume you want to find out the total amount of time (wall-clock time, real-world time, the time you are sitting watching your app run) f1() is taking, as opposed to CPU time. I'd investigate to see if gprof can give you a wall-clock-time instead of a processing-time.

I imagine it depends upon your OS, but the reason you aren't seeing usleep as taking any process time in the profile is because it technically isn't using any during that time - other running processes are (assuming this is running on a *nix platform).

sheepsimulator
I think you mean real-time/wall-clock time. Sleeping shouldn't use user time.
Nick
@Nick - True. Edit made.
sheepsimulator
Hm, nice response. Already searched in gprof, it is not possible.
Jérôme
@ Jérôme - Bummer.
sheepsimulator
+1  A: 
for (int i = i; i < SOME_BIG_NUMBER; ++i);

The entire point in "sleep" functions is that your application is not running. It is put in a sleep queue, and the OS transfers control to another process. If you want your application to run, but do nothing, an empty loop is a simple solution. But you lose all the benefits of sleep (letting other applications run, saving CPU usage/power consumption)

So what you're asking makes no sense. You can't have your application sleep, but still be running.

jalf
If that compiled, it would be a mighty long loop indeed. :) You're missing the "i++" part.
unwind
haha, oops
jalf
Apart from that typo, he is basically right (or c++ly??) I'm sure someone with a big rep will fix it if he doesn't !
Chris Huang-Leaver
told you so :-)
Chris Huang-Leaver
yeah, fixed it after unwind pointed it out. :)
jalf
A: 

AFAIK the only option is to do a while loop. The operating system generally assumes that if you want to wait for a period of time that you will want to be yielding to the operating system.

Being able to get a microsecond accurate timer is also a potential issue. AFAIK there isn't a cross-platform way of doing timing (please someone correct me on this because i'd love a cross-platform sub-microsecond timer! :D). Under Win32, You could surround a loop with some QueryPerformanceCounter calls to work out when you have spent enough time in the loop and then exit.

e.g

void USleepEatCycles( __int64 uSecs )
{
    __int64 frequency;
    QueryPerformanceFrequency( (LARGE_INTEGER*)&frequency );
    __int64 counter;
    QueryPerformanceCounter( (LARGE_INTEGER*)&counter );

    double dStart = (double)counter / (double)frequency;
    double dEnd   = dStart;
    while( (dEnd - dStart) < uSecs )
    {
        QueryPerformanceCounter( (LARGE_INTEGER*)&counter );
        dEnd = (double)counter / (double)frequency;
    }
}
Goz
+2  A: 

What kind of a system are you on? In UNIX-like systems you can use setitimer() to send a signal to a process after a specified period of time. This is the facility you would need to implement the type of "active sleep" you're looking for.

Set the timer, then loop until you receive the signal.

Keith Smith
Wonderful, exactly what I am looking for!! Thanks
Jérôme
A: 

That's why it's important when profiling to look at the "Switched Out %" time. Basically, while your function's exclusive time may be little, if it performs e.g. I/O, DB, etc, waiting for external resources, then "Switched Out %" is the metric to watch out.

Ariel
A: 

This is the kind of confusion you get with gprof, since what you care about is wall-clock time. I use this.

Mike Dunlavey
A: 

This is called a spin-lock.

I did not want to say spinlock because I don't care threads at all. I did not want to confuse.
Jérôme