3

I'm looking for a simple operation / routine which can "waste" time if repeated continuously.

I'm researching how gprof profiles applications, so this "time waster" needs to waste time in the user space and should not require external libraries. IE, calling sleep(20) will "waste" 20 seconds of time, but gprof will not record this time because it occurred within another library.

Any recommendations for simple tasks which can be repeated to waste time?

Charles
  • 50,943
  • 13
  • 104
  • 142
BSchlinker
  • 3,401
  • 11
  • 51
  • 82

4 Answers4

6

The simplest way to "waste" time without yielding CPU is a tight loop.

If you don't need to restrict the duration of your waste (say, you control it by simply terminating the process when done), then go C style*:

for (;;) {}

(Be aware, though, that the standard allows the implementation to assume that programs will eventually terminate, so technically speaking this loop — at least in C++0x — has Undefined Behaviour and could be optimised out!**

Otherwise, you could time it manually:

time_t s = time(0);
while (time(0) - s < 20) {}

Or, instead of repeatedly issuing the time syscall (which will lead to some time spent in the kernel), if on a GNU-compatible system you could make use of signal.h "alarms" to end the loop:

alarm(20);
while (true) {}

There's even a very similar example on the documentation page for "Handler Returns".

(Of course, these approaches will all send you to 100% CPU for the intervening time and make fluffy unicorns fall out of your ears.)


* {} rather than trailing ; used deliberately, for clarity. Ultimately, there's no excuse for writing a semicolon in a context like this; it's a terrible habit to get into, and becomes a maintenance pitfall when you use it in "real" code.

** See [n3290: 1.10/2] and [n3290: 1.10/24].

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
6

Another variant on Tomalak's solution is to set up an alarm, and so in your busy-wait loop, you don't need to keep issuing a system call, but instead just check if the signal has been sent.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
michel-slm
  • 9,438
  • 3
  • 32
  • 31
  • +1: That's a good idea! (BTW I removed the consecutive-post referntial issue from your answer; there is no inherent ordering in SO answers.) – Lightness Races in Orbit Aug 18 '11 at 10:08
  • I had considered setting up an alarm. This would not be considered "yielding" though, correct? Would the profiler still be "counting" this time? – BSchlinker Aug 18 '11 at 10:09
  • 2
    If you busy-wait in a loop waiting for the alarm to be raised, that busy-wait should be counted towards the busy-waiting process? It has the benefit of spending less time in the kernel because it does not issue the ``time()`` syscall repeatedly. – michel-slm Aug 18 '11 at 10:11
0

Here's a busy loop which runs at one cycle per iteration on modern hardware, at least as compiled by clang or gcc or probably any reasonable compiler with at least some optimization flag:

void busy_loop(uint64_t iters) {
    volatile int sink;
    do {
        sink = 0;
    } while (--iters > 0);
    (void)sink;
}

The idea is just to store to the volatile sink every iteration. This prevents the loop from being optimized away, and makes each iteration have a predictable amount of work (at least one store). Modern hardware can do one store per cycle, and the loop overhead generally can complete in parallel in that same cycle, so usually achieves one cycle per iteration. So you can ballpark the wall-clock time in nanoseconds a given number of iters will take by dividing by your CPU speed in GHz. For example, a 3 GHz CPU will take about 2 seconds (2 billion nanos) to busy_loop when iters == 6,000,000,000.

BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
0

A simple loop would do. If you're researching how gprof works, I assume you've read the paper, slowly and carefully. I also assume you're familiar with these issues.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
  • I've read that paper and many others. Thanks for the links though. – BSchlinker Aug 18 '11 at 16:59
  • @BSchlinker: In that case, you might find [this link](http://stackoverflow.com/questions/4387895/if-profiler-is-not-the-answer-what-other-choices-do-we-have/4390868#4390868) interesting. – Mike Dunlavey Aug 18 '11 at 19:13
  • Read your comment there about a month ago when starting my research. I knew I recognized your name from somewhere.. – BSchlinker Aug 19 '11 at 06:24
  • @BSchlinker: Then you know how it works, which is PC samples credited to routines, which is "self" time. Also, calls from any routine A to B is counted. Everything else comes from that. What amazes me is the ideas still assumed (30 years later) that self time, CPU-only time, functions, call graphs, and measurement accuracy matter, as opposed to locating problems in non-trivial software. – Mike Dunlavey Aug 19 '11 at 13:13