views:

258

answers:

4

I have a wxPython app which has many worker threads, idle event cycles, and many other such event handling code which can consume CPU, for now when app is not being interacted with consumes about 8-10% CPU.

Question:

Is there a tool which can tell which part/threads of my app is consuming most CPU? If there are no such generic tools, I am willing to know the approaches you usually take to tackle such scenarios? e.g. disabling part of app, trace etc

Edit: May be my question's language is ambiguous, I do not want to know which function or code block in my code takes up most resources, for that I can use profiler. What I want to know is when I run my app, and I see cpu usage it is 8-10%, now is there a way to know what different parts, threads of my app are using up that 10% cpu? Basically at that instant i want to know which part(s) of code is running?

+1  A: 

If all your threads have unique start methods you could use the profiler that comes with Python.

If you're on a Mac you should check out the Instruments app. You could also use dtrace for Linux.

Georg
how will profiler help here? and is dtrace available for linux(ubuntu) ?
Anurag Uniyal
@Anurag Uniyal: Are you unclear on what a profiler does? It tells what part of the program is using up all the time. If each thread has a unique start method, the method name using all the time will tell you which thread is using all the time. Please read up on profilers.
S.Lott
I may not be as knowledgeable as you are but yes I have used profilers a lot, may be my question's language is ambiguous, I have edited it, see if make sense now?
Anurag Uniyal
A: 

This isn't very practical at a language-agnostic level. Take away the language and all you have left is a load of machine code instructions sprinkled around with system calls. You could use strace on Linux or ProcessExplorer on Windows to try and guess what is going on from those system calls, but it would make far more sense to just use a profiler. If you do have access to the language, then there are a variety of things you could do (extra logging, random pausing in the debugger) but in that situation the profiler is still your best tool.

Kylotan
ok removed language-agnostic, but I am still not sure if profiler can help me much
Anurag Uniyal
Several profilers are capable of giving you not just output on individual functions (eg. sampling) but on the entire call stack (eg. function attribution or 'call graph'). That tells you exactly where most of your application's time is being spent.
Kylotan
I understand that, but profiling threads and main thread will give lots of data and may be false alerts e.g. most of the time is spent in the correct function, but while no activity is going on there is base cpu usage due to lots of events, timer, threads in the whole system, i wanted to track only those
Anurag Uniyal
You will track exactly what is there. If there's 'no activity', then what shows up in the profiler will be exactly the things you talk about.
Kylotan
A: 

I am able to solve my problem by writing a modifed version of python trace module , which can be enabled disabled.

Enabling it at the critical points helps me to trace line by line which code statements are executing most.

Anurag Uniyal
A: 

On Windows XP and higher, Process Explorer will show all processes, and you can view the properties of each process and see open threads. It shows thread ID, start time, state, kernel time, user time, and more.

Nick T