5

I'm totally behind this topic.

Yesterday I was doing profiling for some script I'm working on, and the unit for time spent was a 'CPU second'. Can anyone remind me with the definition of it?

For example for some profiling I got: 200.750 CPU seconds. What does that supposed to mean? At other case and for time consuming process I got: -347.977 CPU seconds, a negative number!

Is there anyway I can convert that time, to calendar time?

Cheers,

dude
  • 53
  • 1
  • 1
  • 3

3 Answers3

7

A CPU-second is one second of time on a CPU.

Process execution time has two measurements:

  • CPU time, or the amount of time that the CPU spent actively running the process; and
  • Wall time, or the amount of time that passes between you starting the process and the process ending.

Assuming a single CPU core, they are related as so:

(Wall time) - (CPU time) = (time the CPU spent doing other things)

So in a simple example, if you run a process and it takes 10 seconds but you get a measurement of 7 CPU-seconds, then during that 10 seconds the CPU spent 3 seconds doing things other than execute your process.

CPU time is generally split into two types:

  • "sys" or "kernel" time, or time that the kernel spent doing things on your process' behalf -- running I/O, timers, mutex's, whatever
  • "user" time, or execution run by a process in user space

So for example:

$ time sleep 1

real    0m1.005s
user    0m0.000s
sys     0m0.002s

In this case, we are sleeping for one second. The system spent 2/1000 seconds in kernel space, probably setting up timers and interrupts for us, no time in user space, and the whole thing took 1.005 seconds to run, which means that during that time period the computer spent an additional 0.003 seconds doing things other than dealing with our process.

Off the top of my head I don't know how or why you'd get an apparently negative number for CPU time.

David Mackintosh
  • 14,293
  • 7
  • 49
  • 78
1

CPU seconds is, in UNIX terms, the user time plus the system time of the process itself, as opposed to the real (wallclock) time and the time spent by the child processes.

I saw something bizzare with negative cpu times a few years back under AMD dual core CPUs. There was a processer driver update that fixed the issue. Basicaly the timing between the 2 processors drifted and caused issues like negative cpu times and negative ping times.

Jim B
  • 24,081
  • 4
  • 36
  • 60
0

A "CPU second" is a second's worth of CPU cycles running the process or thread. You can convert it to wall time by dividing it by the CPU usage:

15 CPU seconds / 50% CPU usage = 30 wall seconds

Negative CPU times are a glitch in the measurement process, possibly an overflow.

Ignacio Vazquez-Abrams
  • 45,939
  • 6
  • 79
  • 84
  • 1
    Note that since, with many CPUs, clock rates will vary with load (and mains vs. battery), so it isn't a precise measurement. – Richard May 05 '10 at 08:49
  • @Ignacio Do you mean: actual second of processing time assigned for this process? – dude May 05 '10 at 09:08
  • @Richard are you mentioning the equation above? – dude May 05 '10 at 09:09
  • @dude: no. More than the number of cycles a processor (and thus the number of instructions executed) will vary depending on environmental factors. And depending how many cache misses (which I forgot in the last answer) there could be a huge difference in the amount of work: cache misses (causing the processor to idle) can dominate the time. – Richard May 06 '10 at 11:40