In this tutorial, we will walk through how to use OpenResty XRay to quantitatively analyze the CPU time consumption in the Envoy servers online. We will show the C++ code paths that consume the most CPU time. These hot code paths are obtained by OpenResty XRay automatically analyzing and interpreting the C++ language-level CPU flame graphs. It tries hard to minimize the performance impact, thus making it ideal for production environments sensitive to performance overhead and latency. At least for simple scenarios, we can see that Envoy is much less efficient than OpenResty and Nginx.

Problem: high CPU usage

Use the cat command to check this Envoy server configuration file.

Screenshot

You can see that it listens on port 1088.

Screenshot

It replies with the “Hello world” response body.

Screenshot

Test the response of this /hello interface.

Screenshot

The response is “hello world” indeed.

Screenshot

Run the top command to check the CPU usage. Look at this process named envoy.

Screenshot

As shown, it consumes more than 90% of a CPU core.

Screenshot

Run the ps command to see the full command line for this process. It is downloaded and installed from the Envoy official binary package repository.

Screenshot

Use the guidede analysis feature of OpenResty XRay to spot the most CPU-intensive C++ code paths inside Envoy Servers

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.

Screenshot

Make sure it is the right machine you are watching.

Screenshot

You can choose the right machine from the list below if the current one is incorrect.

Screenshot

Go to the “Guided Analysis” page.

Screenshot

Here, you can see different types of problems that you can diagnose.

Screenshot

Let’s select “High CPU usage”.

Screenshot

Click on “Next”.

Screenshot

Select the Envoy application.

Screenshot

Select the process that consumes over 90% of the CPU resources. This is what we saw previously in top.

Screenshot

Make sure that the application type is right. Usually, the default should be correct.

Screenshot

The language level here is just “C/C++”.

Screenshot

We can set the maximum analyzing time. We’ll leave it as 300 seconds, which is the default value.

Screenshot

Let’s start analyzing.

Screenshot

The system will keep performing different rounds of analysis. Now, it’s executing the first round.

Screenshot

The first round is done, and it’s already on to the second one. That’s enough for this case.

Screenshot

Let’s stop analyzing.

Screenshot

We can see it automatically created a report.

Screenshot

This is the type of problem we diagnose. It’s CPU.

Screenshot

This is the #1 hottest C++ code path for the CPU time.

Screenshot

This is an overloaded operator of the SchedulableCallbackImpl class.

Screenshot

Click “More” to see details.

Screenshot

The hot code path was automatically derived from this C-land CPU flame graph.

Screenshot

Click the icon to enlarge the flame graph.

Screenshot

Continue to zoom in invoke_impl function.

Screenshot

The write method of the Envoy network socket class performs socket writing. It sends out the HTTP response data.

Screenshot

The drain method of the Envoy buffer class frees unused memory in the write buffer, and performs other cleanup work.

Screenshot

The clearDeferredDeletedList method of the Envoy dispatcher class frees up all the resources associated with the current request. And does all the cleanup work.

Screenshot

Check the #2 hottest C++ code path for the CPU time.

Screenshot

The function emitLog in the Envoy Proxy is used to write the access logs to a file.

Screenshot

Enlarge the flame graph.

Screenshot

Zoom in the emitLog function.

Screenshot

Most of the emitLog CPU time is spent formatting the log message string, not on writing file operations.

Screenshot

Here is the third hottest code path that costs the most CPU time.

Screenshot

This prepareLocalReplayViaFilterChain function is in the chain of Envoy response output filters. Each filter in the chain has the potential to modify the response.

Screenshot

Enlarge the flame graph.

Screenshot

Zoom in the prepareLocalReplayViaFilterChain function.

Screenshot

The createHeaderMap function is hot. It’s mainly for allocating new hash table for HTTP headers.

Screenshot

The newUri function is mainly for allocating and formatting the URI string.

Screenshot

The setStatus function sets the response status code.

Screenshot

The format method of the BodyFormatter formats the responses body data.

Screenshot

setContentLength method is used to set the response length header.

Screenshot

setReferenceContentType method is for setting the Content-Type response header.

Screenshot

This is the performance comparison chart between Envoy servers and OpenResty. You can see that the throughput of OpenResty is more than 200% higher than Envoy servers.

Screenshot

Automatic analysis and reports

OpenResty XRay can also monitor online processes automatically and show analysis reports. Go to the “Insights” page.

Screenshot

You can find the reports on the Insights page for daily and weekly periods. For this reason, you don’t have to use the “Guided Analysis” feature.

Screenshot

Though “Guided analysis” is useful for application development and demonstration purposes.

Screenshot

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.