What specifically are wall-clock-time, user-cpu-time, and system-cpu-time in Unix?
Asked Answered
T

4

213

I can take a guess based on the names, but what specifically are wall-clock-time, user-cpu-time, and system-cpu-time in Unix?

Is user-cpu time the amount of time spent executing user-code while kernel-cpu time the amount of time spent in the kernel due to the need of privileged operations (like I/O to disk)?

What unit of time is this measurement in?

And is wall-clock time really the number of seconds the process has spent on the CPU or is the name just misleading?

Tallowy answered 7/9, 2011 at 14:50 Comment(1)
Possible duplicate of What do 'real', 'user' and 'sys' mean in the output of time(1)?Unjust
A
229

Wall-clock time is the time that a clock on the wall (or a stopwatch in hand) would measure as having elapsed between the start of the process and 'now'.

The user-cpu time and system-cpu time are pretty much as you said - the amount of time spent in user code and the amount of time spent in kernel code.

The units are seconds (and subseconds, which might be microseconds or nanoseconds).

The wall-clock time is not the number of seconds that the process has spent on the CPU; it is the elapsed time, including time spent waiting for its turn on the CPU (while other processes get to run).

Athodyd answered 7/9, 2011 at 14:53 Comment(6)
So does this mean that the wall-clock time will always be greater than the cpu time?Amusing
@Pacerier: on a single core machine, yes, but multi-core machines and multi-threaded programs can use more than 1 CPU second per elapsed second.Athodyd
@JonathanLeffler thank you for the answer, I wanted to get the number of nanoseconds that has been elapsed but calculalting the CPU time using the formula CPUtime = #clock_cycles / clock_rate cannot be the same as calculating the elapsed time. Do you know if I can get the elapsed time from the CPU time?Dusen
@Bionix1441: You cannot derive elapsed time from CPU time for a number of reasons. First, a process can be idle, not consuming any CPU time, for arbitrary periods (for example, a daemon process waiting for a client to connect to it over the network), so it may do nothing for days at a time of elapsed time. Second, if it is running, it may have multiple threads, and if it has, say, 4 threads and there are 4 or more cores on the system, it might rack up 4 CPU seconds of expended effort per second of elapsed time. These show that there's no simple (or even complex) formula that you could use.Athodyd
@JonathanLeffler: I'm still confused about "user code" and "kernel code". Can you please make it clearer. ThanksSpanish
@Catbuilts: Are you aware that the Unix kernel runs separately from user programs. When your program makes a system call (for example, read() or getpid()), the kernel executes code on behalf of your program. The kernel also handles pre-emptive multi-tasking so that other programs get their turn to run, and does some general housekeeping work to keep the system running smoothly. This code is executed in 'kernel code' (also in 'kernel mode'). This is distinct from the code you wrote, and the user libraries (including the system C library) that you run.Athodyd
F
49

Wall clock time: time elapsed according to the computer's internal clock, which should match time in the outside world. This has nothing to do with CPU usage; it's given for reference.

User CPU time and system time: exactly what you think. System calls, which include I/O calls such as read, write, etc. are executed by jumping into kernel code and executing that.

If wall clock time < CPU time, then you're executing a program in parallel. If wall clock time > CPU time, you're waiting for disk, network or other devices.

All are measured in seconds, per the SI.

Farina answered 7/9, 2011 at 14:52 Comment(0)
H
14
time [WHAT-EVER-COMMAND]

real    7m2.444s
user    76m14.607s
sys 2m29.432s

$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                24

real or wall-clock

real 7m2.444s

On a system with a 24 core-processor, this cmd/process took more than 7 minutes to complete. That by utilizing the most possible parallelism with all given cores.

user

user 76m14.607s

The cmd/process has utilized this much amount of CPU time.

In other words, on machine with single core CPU, the real and user will be nearly equal, so the same command will take approximately 76 minutes to complete.

sys

sys 2m29.432s

This is the time taken by the kernel to execute all the basic/system level operations to run this cmd, including context switching, resource allocation, etc.

Note: The example assumes that your command utilizes parallelism/threads.

Detailed man page: https://linux.die.net/man/1/time

Halitosis answered 14/3, 2018 at 3:35 Comment(2)
Given the times you posted, isn't it roughly half the parallelism done? (im just doing (user + sys) / real to represent that.Longevous
I'm a bit late here, but real > (user + sys)/(num cpus), always, because the program will also spend time idle or sharing with other programs, and additionally it may not utilise all of the cores 100% of the time (or even at any time). It can be very close though if it's e.g. a dedicated machine with low overhead.Helen
R
7

Wall clock time is exactly what it says, the time elapsed as measured by the clock on your wall (or wristwatch)

User CPU time is the time spent in "user land", that is time spent on non-kernel processes.

System CPU time is time spent in the kernel, usually time spent servicing system calls.

Raccoon answered 7/9, 2011 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.