Install necessary software packages

  • Linux perf must be installed

            sudo apt install linux-tools-common linux-tools-generic linux-tools-`uname -r` -y
  • Allow user access to PMU (Performance Monitoring Unit) registers ( more information ) :

            sudo sh -c "echo '1' > /proc/sys/kernel/perf_event_paranoid"

Detailed Steps

The previous section explained how to run a Python program to compress large files and increase performance with zlib-cloudflare. Now use perf to look at the performance.

Continue with the same program as the previous section. Make sure to start with and largefile available. Confirm the application is working and largefile.gz is created when it is run.


            python ./

Run the example with perf using the default zlib

Run with the default zlib and time the execution.


            perf stat python ./

The perf stat command will display counts of a few selected PMU events.

Use perf record and generate the flame graph

You can also record the application activity with perf record. -F specifies the sampling frequency and -g enables to collect the backtrace:


            perf record -F 99 -g python ./

To visualize the results, you can generate an image with FlameGraph. Install it with:


            sudo apt install git -y
git clone

Generate the graph:


            perf script | ./FlameGraph/ > out.perf-folded && ./FlameGraph/ out.perf-folded > flamegraph1.svg

Copy the file flamegraph1.svg to your computer and open it in a browser or another image application.

Look at the report

As an alternative, use perf report to inspect the profiling data:


            perf report

Note that the zlib and the deflate function are taking significant time.

Run the example again with perf stat and zlib-cloudflare

This time use LD_PRELOAD to change to zlib-cloudflare instead and check the performance difference.

Adjust the path to the Cloudflare as needed.


            LD_PRELOAD=/usr/local/lib/  perf stat python ./

Generate the new flame graph


            LD_PRELOAD=/usr/local/lib/ perf record -F 99 -g python ./

            perf script | ./FlameGraph/ > out.perf-folded && ./FlameGraph/ out.perf-folded > flamegraph2.svg

Copy the file flamegraph2.svg to your computer. Open it in a browser or other image application and compare it to flamegraph1.svg.