When an Erlang application drives CPU usage past 200%, the bottleneck is often hidden deep in the BEAM VM runtime. We profiled a live Erlang process with OpenResty XRay and traced the Erlang high CPU usage to PCRE regex backtracking inside check_resp_content — specifically the erts_pcre_exec C function that wraps the PCRE library in Erlang’s runtime. No code changes, no recompilation, and no restarts were required.

This walkthrough shows the complete profiling process — from observing an Erlang process consuming 200%+ CPU in top, through Erlang-level and C-level flame graph analysis, to pinpointing the exact source file and line number responsible.

Observing 200%+ CPU in an Erlang Process

Erlang high CPU usage typically surfaces in top or htop output as a busy BEAM VM process — often named beam.smp, or, as in this case, showing the name of the escript that launched it. Either way, the real question is which Erlang code path is responsible.

Run the top command to check the CPU usage.

top command showing an Erlang process consuming over 200% CPU

As shown, this process consumes more than 200% of CPU cores.

CPU usage details for the Erlang process exceeding 200%

It is called rebar3. rebar3 is a tool for managing and building Erlang projects. Here, we use it to start the project.

Process list showing the rebar3 process name

The ps command reveals more details about this process.

ps command displaying the full command line of the rebar3 Erlang process

We can see it is the standard rebar3 binary executable shipped with the Linux distribution. Naturally, this program is also compiled using the Erlang, which comes with the standard distribution.

ps output confirming the standard rebar3 binary and Erlang distribution

Profiling Erlang CPU Usage with OpenResty XRay

OpenResty XRay can analyze this unmodified Erlang process in real time — at both the Erlang language level and the underlying C level of the BEAM VM — without installing any modules or plugins in the target application. In the web console, navigate to Guided Analysis and select High CPU Usage as the problem type. Then select the Erlang application on the target machine.

Selecting the Erlang application from the discovered application list

Select the process that consumes almost 200% of CPU cores — the same one we observed in top.

Selecting the Erlang process consuming 200% CPU for analysis

OpenResty XRay can analyze multiple language levels at the same time. We keep both Erlang and C/C++ selected to see hotspots at both the Erlang code level and the BEAM VM internals, and leave the maximum analyzing time at its default of 300 seconds.

Selecting Erlang and C/C++ language levels for dual-level analysis

After starting the analysis, the system keeps performing different rounds of analysis. Two rounds are enough for this case.

It automatically generated an analysis report.

Automatically generated CPU analysis report

Erlang-Level CPU Flame Graph: Regex in check_resp_content

Look at the #1 hottest Erlang code path.

Hottest Erlang code path showing check_resp_content as the top CPU consumer

The check_resp_content function is a business function used to check the content of the response.

check_resp_content function highlighted in the Erlang CPU report

It calls the filter function of the lists module to filter a list.

lists:filter call inside check_resp_content shown in the code path

The filter condition is an anonymous function that uses regular expression matching within itself. These calls all occur within the check_resp_content function.

Anonymous function with re:run regex matching inside lists:filter

Click “More” to see details about this hottest code path.

Expanding the code path details for further inspection

This code path is automatically inferred from this Erlang-level CPU flame graph.

Erlang-level CPU flame graph showing the full call stack for check_resp_content

Here are more detailed explanations of the earlier code path.

Detailed explanation of the Erlang code path with function descriptions

Go back to the previous hot code path. Hover the mouse over the green box for the first function. You can see the source file of this function in the tooltip.

Tooltip showing the source file path for check_resp_content

The Erlang source line number is 15.

Source line number 15 displayed for the hotspot function

Click the icon to copy the source file path.

Copying the Erlang source file path from the OpenResty XRay report

Use the vim editor to open this Erlang source file. You can use any editors you like.

Opening the Erlang source file in vim to inspect the hotspot

Go to line 15, as OpenResty XRay suggested.

Navigating to line 15 in the Erlang source file

This is the run function of the re module — the standard Erlang interface to the PCRE regex engine.

re:run function call at line 15 performing PCRE regex matching

This is the lists:filter function call we saw earlier in the report.

lists:filter function call in the source code matching the flame graph finding

This code path is indeed inside the check_resp_content function. The regex can be optimized to avoid costly backtracking operations, or you can consider using a non-backtracking regex engine.

check_resp_content function body confirming the regex hotspot location

The top two Erlang code paths are similar, and both execute regular expression matches.

Second hottest Erlang code path also showing regex matching in the report

The third Erlang code path is calculating the length of the input string prior to matching.

Third Erlang code path showing iolist size calculation

iolist is the input string to be matched, and erts_iolist_size is calculating the length of the string before passing it to the PCRE engine.

erts_iolist_size calculating the length of the input iolist

The Content variable is the input string to be matched.

Content variable as the input string for regex matching

C-Level Analysis: erts_pcre_exec and PCRE Backtracking

Switch back to the web console. The C-level analysis reveals performance hotspots inside the BEAM VM itself — confirming that the CPU bottleneck reaches all the way down into the PCRE library.

C-level code path showing PCRE match as the hottest C function

The function match in the PCRE library performs the actual regular expression matching.

PCRE match function identified as the leaf CPU consumer

The second function, erts_pcre_exec, is the Erlang runtime’s wrapper for the pcre_exec function in the PCRE library.

erts_pcre_exec wrapping pcre_exec in the BEAM VM runtime

The re_run function in the re module of Erlang is used for executing regular expressions.

re_run calling into erts_pcre_exec for regex execution

This hexadecimal address indicates that this code path is executed in JIT-compiled Erlang code.

Hexadecimal address in the call stack indicating JIT-compiled Erlang code

Why PCRE Regex Backtracking Is Expensive in the BEAM VM

The C-level flame graph confirms that the CPU time is spent inside erts_pcre_exec — the BEAM VM’s built-in binding to the PCRE library. Erlang’s re module delegates all regex work to PCRE, which uses a backtracking NFA engine. When a pattern contains quantifiers like .*, .+, or nested alternations, the engine may explore an exponential number of match paths before concluding that no match exists. This is known as catastrophic backtracking.

To fix PCRE regex bottlenecks like this one, you can optimize the regex to avoid costly backtracking operations by the regex engine, or consider using a non-backtracking regex engine.

Automatic Monitoring and Reports

For production workloads, OpenResty XRay monitors Erlang processes continuously and generates daily and weekly reports on the Insights page — no manual guided analysis required.

Insights page showing daily and weekly automatic analysis reports

FAQ: Erlang High CPU Usage

Why is my Erlang process using so much CPU?

A common cause of Erlang high CPU usage is an expensive operation in a hot code path — often regex matching, garbage collection pressure, or inefficient list processing. In this case, OpenResty XRay traced the CPU bottleneck to PCRE regex backtracking inside the check_resp_content function, where re:run/2 was called for every element in a lists:filter/2 loop. CPU flame graphs at both the Erlang and C levels reveal the full call chain down to erts_pcre_exec.

How do I find the Erlang code causing high CPU?

Use OpenResty XRay’s guided analysis to generate CPU flame graphs at both the Erlang and C language levels. The flame graph shows stacked function calls where width represents CPU time. Hover over a function box to see the source file path and line number, then open that file in any editor to inspect the exact code responsible. No code changes or restarts are needed — OpenResty XRay attaches to the running Erlang process directly.

Can PCRE regex patterns cause high CPU in Erlang?

Yes. Erlang’s re module delegates regex work to the PCRE library via erts_pcre_exec, and PCRE uses a backtracking NFA engine. Patterns with nested quantifiers or alternations can trigger catastrophic backtracking, consuming exponential CPU time. In this case, the PCRE match function appeared as the leaf CPU consumer in the C-level flame graph, confirming that regex backtracking was the root cause of the Erlang high CPU usage.

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.