5

In Linux Kernel when processor context switches from one thread to another , the state of the registers are saved into PCB and some more bookkeeping is done to ensure that the exact state can be loaded again.

This whole saving and loading of the registers from kernel memory might take some CPU cycles. So does this times comes under User CPU / System CPU or somewhere else

gjain
  • 4,468
  • 5
  • 39
  • 47
mridul_verma
  • 265
  • 1
  • 10

2 Answers2

2

Think of it like this:

  • a task is running in user-space, but something happens (syscall, exception, IRQ, ...) causing the task to switch to kernel-space

  • kernel calculates a "time spent in user-space" (now - last_time) and updates a "user time" counter for the task, and sets "last time" for later (last_time = now).

  • kernel does stuff (initially depending on what caused the switch to kernel-space), and while doing stuff it might or might not decide to do one or more task switches. When each task switch occurs kernel works out how much time the previous task spent in kernel (now - last time) and adds it to the task's "system time" and sets "last time" for later (last_time = now)

  • kernel eventually decides that the currently running task should return to user-space, and immediately before that happens it does a final update of the task's system time (now - last time again) and sets "last time" again for later (last_time = now) so that kernel can figure out "time spent in user-space" later.

  • after the task switches back to user-space, go back to the first step above and do it all again.

Brendan
  • 35,656
  • 2
  • 39
  • 66
  • This context switch time ( now - last_time ) is counted against which process. Process / Thread which was running before or Process / Thread which was running after the context switch. – mridul_verma Oct 18 '18 at 04:22
  • @mridul_verma: If the cost of a task switch is `Q`; then during the task switch a fraction of `Q` (that I'll call `R`) will be attributed to the previous task and then later the remainder (`Q - R`) will be attributed to the next task. If a task stops using CPU and then starts using CPU again; the total time attributed to the task will be `(R) + (Q - R) = Q`; and it makes no difference how large or small `R` is. – Brendan Oct 18 '18 at 04:56
  • @mridul_verma: Note that (for modern multi-GHz CPUs) the total cost of a task switch (`Q`) is probably in the tens of nanoseconds range, so any temporary imbalances are lost in the noise. Also note that I'm talking about "direct costs" here. In reality the "indirect costs" (the cost of TLB misses and cache misses that occur after a task switch, after kernel returns to user-space) are much more significant than the "direct costs"; and these indirect costs are attributed to "user time" and not "system time" (because that's when the TLB misses and cache misses happen). – Brendan Oct 18 '18 at 05:02
  • That answers my question completely. Thanks for the information. – mridul_verma Oct 19 '18 at 13:17
0

That time is definitely supposed to be under System CPU. Any time spent in System Calls and Interrupts should be under System CPU, not User CPU. User CPU is time spent running assembly in the ELF that's actively executing and any supporting libraries - nothing else. Even I/O counts as System CPU.

Looking at the documentation in Section 1.8, we see

- system: processes executing in kernel mode

Of course, context switches access kernel-level data and not userland data. Thus this code is run under kernel mode, and we can be sure up to the legitimacy of their documentation that this is counted as system time.

Nicholas Pipitone
  • 4,002
  • 4
  • 24
  • 39