Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to diagnose a python process chewing CPU in linux

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 ?

like image 413
Feru Avatar asked May 23 '14 21:05

Feru


People also ask

How can I tell what is consuming my CPU Linux?

Check CPU Usage with Iostat Command Run the iostat command without any option will display the information about CPU utilization, device utilization, and network file system utilization. Use the -c option to break the CPU utilization into user processes, system processes, I/O wait, and idle time.

How do I check CPU usage in Python?

Get current CPU usage using psutil The function psutil. cpu_percent() provides the current system-wide CPU utilization in the form of a percentage. It takes a parameter which is the time interval (seconds). Since CPU utilization is calculated over a period of time it is recommended to provide a time interval.

How do I troubleshoot CPU usage in Linux?

How to check Linux CPU usage and utilization using the iostat command. You can also use iostat command which report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions. It can be use to find out your system's average CPU utilization since the last reboot.


1 Answers

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/executable [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
like image 73
Feru Avatar answered Sep 21 '22 10:09

Feru