When a Perl process keeps receiving requests but its CPU usage stays low — around 13% in our case — some code path is blocking the OS thread, typically synchronous I/O such as an HTTP call waiting on select. This tutorial shows how to find the exact blocking Perl code path, down to the source file and line number, using OpenResty XRay’s off-CPU flame graphs — with no code changes and no process restarts.

The Symptom: Perl Process CPU Usage Stays Low Under Heavy Requests

Let’s run the top command to check the CPU usage of each process.

Take a look at this perl process. The CPU usage is only around 13%. The usage does not increase even as many requests continue to come in.

top command output showing a Perl process stuck at around 13% CPU usage

Run the ps command to check all Perl processes.

We can see it is the standard perl binary executable shipped with the Linux distribution.

ps command output showing the standard perl binary shipped with the Linux distribution

Let’s take a look at the access log file for this Perl application.

We can see many client request are coming in, but the CPU usage remains low. This means that there is something blocking the Perl code from running efficiently. But we don’t know what it is. How can we find out?

Access log of the Perl application showing many incoming client requests

Access log entries continuing to grow while the CPU usage remains low

Find the Blocking Perl Code Paths with OpenResty XRay’s Off-CPU Flame Graphs

Let’s use OpenResty XRay to check this unmodified process. You can analyze it in real-time and figure out what is happening.

Open the OpenResty XRay web console in the web browser, make sure you are on the right machine, and go to the “Guided Analysis” page.

Guided Analysis page in the OpenResty XRay web console

Among the problem types you can diagnose here, select “Low CPU usage and cannot go up”.

Selecting the “Low CPU usage and cannot go up” problem type in Guided Analysis

Then follow the wizard: select the Perl application, pick the process consuming 14% of the CPU resources (the one we saw earlier in top), and keep the defaults for the remaining steps — the application type, both Perl and C/C++ language levels, and the 300-second maximum analysis time.

Selecting the Perl process consuming 14% CPU in the Guided Analysis wizard

Start analyzing. The system will keep performing multiple rounds of analysis; the first round is enough for this case, so we stop there.

Guided Analysis running its first round of analysis on the Perl process

It automatically generated a report.

Automatically generated Guided Analysis report

This is the problem type we’re analyzing, “off-CPU”.

Guided Analysis report showing the off-CPU problem type

This is the C code path that blocks operating system threads the most.

The C code path that blocks operating system threads the most

The first function is select, a system call.

The select system call at the top of the blocking C code path

Perl_pp_select is a built-in function that handles Perl’s select function. It is a part of Perl’s internal for monitoring and waiting for I/O events on sockets and other files.

Perl_pp_select built-in function handling Perl’s select in the C code path

We can see from this C function that Perl code is currently executing.

C function frame showing that Perl code is currently executing

Next, we look at the hottest Perl code path blocking the OS thread from using the CPU.

The hottest Perl code path blocking the OS thread from using the CPU

The top C function, select, is the blocking point in the C code path we just saw.

The select function shown as the blocking point of the Perl code path

The can_read Perl function of the Net::HTTP::Methods module waits until the socket has received new data for reading.

The can_read function of Net::HTTP::Methods waiting for socket data

Along the caller chain, we can see that it is blocked when reading the response headers in this read_response_headers function.

The read_response_headers function blocking while reading HTTP response headers

remote_fetch is a function in our business-level code, it belongs to our own Perl module, Service::Processor.

The remote_fetch function of the business-level Service::Processor Perl module

The most critical blocking code path was automatically inferred from this Perl-land off-CPU flame graph. Off-CPU analysis like this is also the standard way to diagnose latency problems — see how we pinpointed a 244ms latency spike in a 500k QPS OpenResty gateway.

Perl-land off-CPU flame graph highlighting the most critical blocking code path

Below are more explanations and suggestions about the issue.

Report explanations and suggestions about the off-CPU issue

It refers to the select function we saw earlier.

Report text referring to the select function

It mentions that this function uses the select call to proceed to the next step based on the data.

Report explaining that the function uses the select call to proceed

This is also a reference to the remote_fetch function we saw earlier.

Report reference to the remote_fetch function

Let’s go back to the code path.

Hover the mouse over the green box for the remote_fetch Perl function.

Hovering over the green frame of the remote_fetch Perl function in the flame graph

You can see the source file and line number of this function in the tooltip.

Tooltip showing the source file and line number of the remote_fetch function

Click the icon to copy the perl source file path.

Copying the Perl source file path from the flame graph tooltip

On the terminal, use the vim editor to open the source file. Paste the file path we just copied. You can use any editor you like.

Opening the Perl source file in the vim editor on the terminal

Go to line 66, as OpenResty XRay suggested.

Line 66 of the Perl source file, as suggested by OpenResty XRay

We can see it is sending out an HTTP GET request and waiting for its response.

Perl code sending an HTTP GET request and waiting for its response

To avoid blocking HTTP requests in Perl, you can consider use nonblocking frameworks like Coro. The same off-CPU approach also works for Go and Python processes.

Suggestion to use nonblocking Perl frameworks like Coro

Automatic Analysis and Reports in the Insights Page

OpenResty XRay can also monitor online processes automatically and generate analysis reports for daily and weekly periods in the “Insights” page — so you don’t have to use the “Guided Analysis” feature by hand, though it remains useful for application development and demonstration purposes.

Daily and weekly analysis reports in the OpenResty XRay Insights page

OpenResty XRay is a non-invasive diagnostic system based on our own dynamic-tracing technology. It can monitor and scan performance problems, behavioral issues, and security vulnerabilities in real-time.

Overview of the OpenResty XRay non-invasive diagnostic system

FAQ

Why is my Perl process not using full CPU even under heavy load?

Because some code path is blocking the OS thread instead of doing work on the CPU. In the case above, the can_read function of the Net::HTTP::Methods module waits in the select system call until the socket receives new data, so the process sits mostly idle while client requests keep piling up.

How do I find which Perl code is blocking, without modifying the code?

Run OpenResty XRay’s “Guided Analysis” against the unmodified running process and choose “Low CPU usage and cannot go up”. The generated report automatically infers the most critical blocking code path, and hovering over a function in the off-CPU flame graph reveals its exact source file and line number.

What does the off-CPU flame graph show in this case?

It shows the code paths where the OS thread spends its time blocked rather than running on the CPU. In this case, the Perl-land off-CPU flame graph points directly at our remote_fetch business function, which blocks in read_response_headers on the select call while waiting for an HTTP response.

What is OpenResty XRay

OpenResty XRay is a dynamic-tracing product that automatically analyzes your running applications to troubleshoot performance problems, behavioral issues, and security vulnerabilities with actionable suggestions. Under the hood, OpenResty XRay is powered by our Y language targeting various runtimes like Stap+, eBPF+, GDB, and ODB, depending on the contexts.

If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!

About The Author

Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.

OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.

As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.