This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: make cProfile multi-stack aware
Type: enhancement Stage: patch review
Components: Extension Modules Versions: Python 3.2
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, georg.brandl, kristjan.jonsson, stutzbach, twhitema
Priority: normal Keywords: patch

Created on 2010-08-15 15:09 by kristjan.jonsson, last changed 2022-04-11 14:57 by admin.

File name Uploaded Description Edit
_lsprof.patch kristjan.jonsson, 2010-08-15 15:09
cprofile2.patch kristjan.jonsson, 2010-09-09 15:50
Messages (4)
msg113964 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2010-08-15 15:09
One of the problems with the profiling modules provided with Python is that they are not useful in the presence of multiple threads.  This is because time spent in a different thread may be falsely attributed to some random place in a thread being profiled.

This patch helps fix that, by making the _lsprof.c module (the engine behind cProfile) multi-stack aware.  At every entry into the profiler, a check is made to see which stack is in operation (by looking at the thread state).  The previous stack is then paused and profiling commences on the new stack.

Time spent on other stacks is then subtracted from the measured time on each stack.

A complication arises because it is no longer possible to determine the recursion level of each function (or subcall instance) on each stack by looking at the function's entry alone.  For this reason, it becomes necessary to walk the stack in cases where there are multiple stacks and multiple total recursions seen for the entries.

This patch has been successfully used, with a modifiaction for stackless python, in production at CCP (the modification uses the Tasklet ID rather than the TLS pointer as a key to the stack map).

To be useful, it is important that all threads in the process are set to use the same cProfile.Profiler() instance.  Currently there is no easy way to do that and this patch doesn't attempt to fix that.  But is is possible that an application designed for profiling would attach the profiler at each thread start point.  (In the version of Stackless Python that this is used on, it is possible to enable tracing/profiling of all tasklets simultaneously)
msg115954 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2010-09-09 15:50
Here is a new patch.  When 'allthreads' is specified to cProfile.Profile.enable(), profling is enabled on all threads.
The testsuite tests to see that all threads do indeed register, it does not attempt to validate the timings.

It turns it on for all threads (in all interpreter states).  Maybe this is overdoing it.  Interpreter states is something new to me, a py3k feature?

Now, the problem with this approach is that it only works with threads already in existence when called.  The "global" appcoach that we used to solve a similar problem in Stackless Python, however, will invoke the tracing/profiling functionality for all tasklets, even if they were new.  The same would be useful for threads in regular python, especially in long running server applications.

However, this patch stands on its own.  This particular shortcoming could be fixed later.
msg156777 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2012-03-25 21:36
Is there progress on it?
msg156824 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2012-03-26 14:49
I've presented the patch, but no one has voiced an opinion on it.
Date User Action Args
2022-04-11 14:57:05adminsetgithub: 53818
2012-03-26 14:49:16kristjan.jonssonsetmessages: + msg156824
2012-03-25 21:36:39asvetlovsetmessages: + msg156777
2010-09-27 16:31:31twhitemasetnosy: + twhitema
2010-09-09 16:51:28stutzbachsetkeywords: patch, patch
nosy: + georg.brandl

stage: patch review
2010-09-09 15:50:43kristjan.jonssonsetkeywords: patch, patch
files: + cprofile2.patch
messages: + msg115954
2010-09-09 13:24:34stutzbachsetkeywords: patch, patch
nosy: + stutzbach
2010-09-09 07:42:05asvetlovsetnosy: + asvetlov
2010-08-15 15:09:35kristjan.jonssoncreate