Score:0

What does top cpu percentage include?

bw flag

I have a program that reads data from shared memory and send it to a non-blocking socket in a single-threaded manner. When I'm hammering the traffic into that application, I can only see the TOP cpu percentage about 60% and not going higher, even the data I pump up into the shared memory is overflowing in the producer side.

I want to understand if this 60% is the true limit or some dubious behavior that needs to be optimized.

And here goes my breakdown of my questions:

  • Does top CPU include the kernel function execution times related to the socket send/receive API?
  • I'm using epoll which is blocking mode and does top CPU consider the blocked time as busy time? (Likely no but want to confirm)
  • (Not directly related) If I use blocking socket, does top CPU consider the blocked time as busy time?
Score:0
tc flag

Definition of "%CPU field" of processes from top's man page:

The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.

Definition "CPU time" from Wikipedia

CPU time (or process time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system.


Questions

  1. Does top CPU include the kernel function execution times related to the socket send/receive API?
  2. I'm using epoll which is blocking mode and does top CPU consider the blocked time as busy time? (Likely no but want to confirm)
  3. (Not directly related) If I use blocking socket, does top CPU consider the blocked time as busy time?

Answer to questions 2 and 3

Blocking time is not counted into %CPU as "CPU time" defined as time spent executing instructions on a CPU.

Answer to question 1

According to Wikipedia CPU time is the sum of both "user CPU time" and "system CPU time".

We would expect both kernel mode execution time on behalf of process and user mode execution of process to count up into the calculation of %CPU. In order to test if that is the case you can run a system time usage heavy process and check the %CPU of that process via top, which I have seen in a blog post.

$ dd if=/dev/zero of=/dev/null bs=65536

Cpu(s):  0.2%us, 12.8%sy,  0.0%ni, 87.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  16330524k total,  7869052k used,  8461472k free,   464240k buffers
Swap: 18563064k total,        0k used, 18563064k free,  6716724k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
29937 user      20   0  102m  724  564 R 99.7  0.0   0:13.40 dd

In the same blog post, the writer inspects source codes of implementations of top utility program for Linux and OpenBSD, I would suggest you to take a look at there too.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.