Bug 4081

Summary: CPU usage for threads not displayed correctly in top (only with NPTL threads)
Product: Busybox Reporter: Ronald Wahl <ronald.wahl>
Component: OtherAssignee: unassigned
Status: RESOLVED FIXED    
Severity: normal CC: busybox-cvs
Priority: P5    
Version: 1.18.x   
Target Milestone: ---   
Hardware: Other   
OS: Linux   
Host: Target:
Build:
Attachments: small test programm
Proposal for a fix

Description Ronald Wahl 2011-08-24 15:00:49 UTC
Created attachment 3541 [details]
small test programm

When calling top and enabling the display of threads by pressing 'h' then the CPU usage for a multi-threaded process is distributed over all threads of the process equally. For example if I have a process with three threads where one thread is taking ~100% CPU and the other threads sleep then top shows ~33% CPU usage for each thread which is plainly wrong. The CPU usage reported for each thread should reflect the reality.

Checking /proc/<pid>/stat and /proc/<pid>/task/<tid> directly everything is fine, i.e. /proc/<pid>/stat shows the CPU usage of the whole process including all threads respective proc/<pid>/task/<tid> shows the CPU usage of each thread separately.

This whole issue exists only when using NPTL threads.

For quick testing you can use the attached test program which starts two extra threads - one thread eating all cpu time and one thread which is sleeping. The main thread is sleeping as well.
Comment 1 Ronald Wahl 2011-08-24 16:50:06 UTC
Created attachment 3547 [details]
Proposal for a fix

The original code only reads the /proc/<pid>/stat file instead of the /proc/<pid>/task/<tid>/stat file. The attached patch fixes this.
Comment 2 Denys Vlasenko 2011-08-28 09:58:35 UTC
Applied to git. Will be in 1.19.1. Thanks!