How does getrusage() report time spent hibernating

2019-08-12 02:44发布

问题:

I am currently using getrusage to tell me how much time I spend in my application's event loop.

I wonder how this will be affected by hibernating. Is hibernation time reported at all? Or perhaps as system time? Is this specified somewhere in Posix or is this system dependent?

Edit Asking the same question for Windows here.

回答1:

I don't think POSIX mentions hibernation anywhere, so it is technically platform dependent.

I can only speak for Linux, but other UNIX variants - and perhaps even Windows - probably behave similarly, since this is what makes most sense.

In Linux, the exact details of what happens under the hood with hibernation are described in Documentation/power/swsusp.txt under the kernel source. In short, user processes are sent a fake signal that causes them to switch to TASK_UNINTERRUPTIBLE state. A process in the TASK_UNINTERRUPTIBLE state is taken out of the run queue and put to sleep until the condition it is waiting for comes true, so time spent in TASK_UNINTERRUPTIBLE is neither counted as user time nor as system time. If you were to measure runtimes with time(1), you would at most see that it contributes to wall clock time.

With getrusage(2), you won't see a difference in any of the reported CPU times because the process wasn't runnable.

So, to answer your question: hibernation time is not reported at all.