brendangregg / FlameGraph

Stack trace visualizer
http://www.brendangregg.com/flamegraphs.html
17.28k stars 1.96k forks source link

Flamegraph handles multi-thread program abnormality #296

Open yuand2022 opened 1 year ago

yuand2022 commented 1 year ago

The command pipeline is as follows: perf record -g ./app perf script > app.perf cat app.perf | ./stackcollapse-perf.pl --all | ./flamegraph.pl > app.svg

My application is a multi-threaded application and the execution time is about 20 minutes. When I finished executing the above pipeline, I got an unexpected svg image, the flame graph does not truly reflect the actual situation。

out

ReadAlignChunk::mapChunk is a thread execution function, which should be a hotspot, and BarcodeMap::BarcodeMapping is a sub-function of a thread thread function, which should be a secondary hotspot. Another problem is that the call stack is too shallow, with less than 10 layers, far below expectations.

HarrySidhuz0008 commented 1 year ago

import threading import time import pyflamegraph

Function to calculate factorial

def calculate_factorial(n): if n == 0: return 1 else: return n * calculate_factorial(n - 1)

Function that simulates multi-threading

def worker(thread_id): for i in range(1, 5000): calculate_factorial(20)

def main():

Start profiling

with pyflamegraph.FlamegraphProfiler(output_file='flamegraph.svg'):
    threads = []
    for i in range(4):  # Create 4 threads
        thread = threading.Thread(target=worker, args=(i,))
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()

if name == 'main': main()