You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow the linux perf profiler to see Python calls #96143
The linux perf profiler is a very powerful tool but unfortunately is not able to see Python calls (only the C stack) and therefore it cannot be used (neither its very complete ecosystem) to profile Python applications and extensions.
Turns out that node and the JVM have developed a way to leverage the perf profiler for the Java and javascript frames. They use their JIT compilers to generate a unique area in memory where they place assembly code that in turn calls the frame evaluator function. This JIT compiled areas are unique per function/code object. They use the perf maps (perf allows to place a map in /temp/perf-PID.map with information mapping the JIT-ed areas to a string that identifies them and this allows perf to map java/javascript names to the JIT-ed areas, basically showing the non-native function names on the stack.
We can do a simple version of this idea in Python by using a very simple JIT compiler that compiles a assembly template that is the used to jump to PyEval_EvalFrameDefault and we can place the code names and filenames in the special perf file. This allows perf to see Python calls as well:
And this works with all the tools in the perf ecosystem, like flamegraphs:
Something needs to garbage collect it's /tmp/perf-$pid.map files as well.
That's really up to the user unfortunately. The files must be available after the process finishes and at report time so I don't see what we can do that automatically cleans these files because we don't know when the user has finished with them.
Or do you mean in the buildbot? In that case, tests are deleting created files already so they should not be polluting the machine so these won't pile up in buildbots.
Or do you mean in the buildbot? In that case, tests are deleting created files already so they should not be polluting the machine so these won't pile up in buildbots.
Yes I was talking about the desired buildbot config. A tmpwatcher of some form set to tmp files over a few hours old is likely sufficient. With the environment variable set to enable perf everywhere, a single test run probably has hundreds if not thousands of PIDs. ;)
(This is where the buildbot design really shows age. A fresh container per buildbot worker test session would make sense.)
Yes I was talking about the desired buildbot config. A tmpwatcher of some form set to tmp files over a few hours old is likely sufficient. With the environment variable set to enable perf everywhere, a single test run probably has hundreds if not thousands of PIDs. ;)
Ah in that case I don't think is needed. Tests check the perf files before and after the tests and deleted any new file that matches PIDs that have been spawned during tests. I will revise this logic to ensure it works correctly when running parallel test suites but I think that should be enough.
FYI - our TensorFlow folks internally just re-worked their equivalent parallel perf-trampoline-hook Python profile enabling work to do what it needs using our backport of these changes to our internal 3.9 soon to be 3.10 runtime. Once they start using that hopefully to helps shake any strange issues out (of which I'm sure we all hope there are none). 😄
marking closed as I think everything for this is done at this point?
marking closed as I think everything for this is done at this point?
Nothing critical that I can think. I will probably add some extra examples to the doc but I can open more issues for that.
FYI - our TensorFlow folks internally just re-worked their equivalent parallel perf-trampoline-hook Python profile enabling work to do what it needs using our backport of these changes to our internal 3.9 soon to be 3.10 runtime. Once they start using that hopefully to helps shake any strange issues out (of which I'm sure we all hope there are none).
That's wonderful. This will allow us to test this more heavily before the final release 👍
Activity
pablogsal commentedon Aug 21, 2022
Is also very easy to transform these into python-only flamegraphs by filtering the

py::prefix:gh-96143: Allow Linux perf profiler to see Python calls (GH-96123)
gh-96143: Add some comments and minor fixes missed in the original PR (…
pythongh-96143: Improve perf profiler docs
gpshead commentedon Aug 31, 2022
PYTHONPERFSUPPORT=1and relevantCFLAGS=-fno-omit-frame-pointerneeds to be setup./tmp/perf-$pid.mapfiles as well.gh-96143: subprocess API %s/universal_newlines=/text=/g. (GH-96468)
pablogsal commentedon Sep 1, 2022
That's really up to the user unfortunately. The files must be available after the process finishes and at report time so I don't see what we can do that automatically cleans these files because we don't know when the user has finished with them.
Or do you mean in the buildbot? In that case, tests are deleting created files already so they should not be polluting the machine so these won't pile up in buildbots.
pythongh-96143: Clear instruction cache after mprotect call
gpshead commentedon Sep 2, 2022
Yes I was talking about the desired buildbot config. A tmpwatcher of some form set to tmp files over a few hours old is likely sufficient. With the environment variable set to enable perf everywhere, a single test run probably has hundreds if not thousands of PIDs. ;)
(This is where the buildbot design really shows age. A fresh container per buildbot worker test session would make sense.)
pablogsal commentedon Sep 2, 2022
Ah in that case I don't think is needed. Tests check the perf files before and after the tests and deleted any new file that matches PIDs that have been spawned during tests. I will revise this logic to ensure it works correctly when running parallel test suites but I think that should be enough.
gh-96143: Clear instruction cache after mprotect call (#96476)
pythongh-96143: Move the perf trampoline files to the Python directory
gh-96143: Move the perf trampoline files to the Python directory (#98675
gh-96143: Improve perf profiler docs (#96445)
pythongh-96143: Improve perf profiler docs (python#96445)
pablogsal commentedon Nov 1, 2022
@gpshead We have a perf buildbot now:
https://buildbot.python.org/all/#/builders/1078
here is the relevant log from the last build:
gpshead commentedon Nov 1, 2022
sweet!
FYI - our TensorFlow folks internally just re-worked their equivalent parallel perf-trampoline-hook Python profile enabling work to do what it needs using our backport of these changes to our internal 3.9 soon to be 3.10 runtime. Once they start using that hopefully to helps shake any strange issues out (of which I'm sure we all hope there are none). 😄
marking closed as I think everything for this is done at this point?
pablogsal commentedon Nov 1, 2022
Nothing critical that I can think. I will probably add some extra examples to the doc but I can open more issues for that.
That's wonderful. This will allow us to test this more heavily before the final release 👍
pythongh-96143: Improve perf profiler docs (python#96445)
made changes according to 1f737ed: pythongh-96143: Move the perf tram…