My python process at certain point in automated scripts starts chewing CPU on Linux based System (Ubuntu). I’m trying to debug this issue in GDB. I'm fairly new to GDB. Are there any GDB commands to give information on which thread is using most of the cpu. Looking at the thread stack doesn't really give that away.

On windows windbg world the command '!runaway' did give the info on time consumed by each thread in a process. Do we've an equivalent command here ? Any other suggestions to debug issue ?

share|improve this question
    
What you want, at this point, is a profiler. I'd start with one implemented in and for Python -- you'll get better information that way with lower-level tools. Jumping straight to gdb is almost certainly the wrong place to start. – Charles Duffy Sep 17 '14 at 22:49
    
...and while "which profiler should I use for Python?" is a tool recommendation request and off-topic under SO's current rules, that question has been asked and answered back before those rules went into effect, so you'll find plenty of information in the archives. – Charles Duffy Sep 17 '14 at 22:50

Just to clarify all the steps required to diagnose this issue. (thanks everyone for postings) :

Following command shows the list of process with their CPU / Memory usage :

ps auxf

Following command gives the list of all threads of a process sorted with CPU usage.

top -H -p [PID]

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

1654 root 20 0 1416m 1.2g 24m t 100 36.8 21:26.23 python

1687 root 20 0 1416m 1.2g 24m t 0 36.8 0:05.07 python

Thread 1654 is chewing CPU. Attach gdb to the process

gdb /path/of/process [pid]

Following command in gdb to get list of threads

(gdb) info threads

2 Thread 0xa7bffb40 (LWP 20736) "python" 0xb7736424 in __kernel_vsyscall ()

1 Thread 0xb73a56c0 (LWP 1654) "python" 0xb7736424 in __kernel_vsyscall ()

in gdb switch to the thread to check its stack.

(gdb) thread 1

(gdb) bt

share|improve this answer
    
Excellent. Is there a way to tell which tread is eating the CPU? – Bryce Mar 19 '17 at 4:49

One possible solution is to use the command top with the option to display all threads:

> top -H

The tasks will be sorted by CPU usage by default.

Alternate solutions can be found in the previous thread here.

share|improve this answer
    
top -H -p PID is better. -p : Monitor only processes with given process id. This flag can be given up to twenty times. – Sergei Kurenkov May 24 '14 at 7:50
    
thank you guys. this was helpfull figure out which thread chewed cpu. – Feru May 25 '14 at 1:49
    
is there a gdb command to detect thread usage in a core-dump file. The assumption is that the process is not in the state where the process is chewing CPU. In fact this could be a core-dump from a customer. – Feru Sep 17 '14 at 22:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.