Replace popen('uname -ap') with uname() syscall in bvar kernel_version - #3517
Open
weim0000 wants to merge 1 commit into
Open
Replace popen('uname -ap') with uname() syscall in bvar kernel_version#3517weim0000 wants to merge 1 commit into
weim0000 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Problem Summary:
When a brpc server has allocated a large amount of memory, the first request to the
/varsendpoint can cause a significant latency stall. This is because thekernel_versionbvar variable is lazily initialized on first access, and its constructor callspopen("uname -ap")to read the kernel version.Internally,
popen()callsfork()to spawn a child process. On Linux,fork()needs to duplicate the parent process's page tables. For a server with a large memory footprint (e.g., tens of GBs), this can take hundreds of milliseconds or even longer, effectively blocking the bthread that handles the/varsrequest.This caused a production incident in our environment, where the service appeared to hang when the monitoring system first scraped the
/varsendpoint after the server had been running for a while with heavy memory usage.What is changed and the side effects?
Changed:
Replace
butil::read_command_output(oss, "uname -ap")(which shells out viapopen→fork→exec) with the POSIXuname()syscall insrc/bvar/default_variables.cpp. Theuname()syscall reads the same kernel information directly viastruct utsname, without creating any child process. The output format remains equivalent touname -ap. A unit test is added intest/bvar_variable_unittest.cpp.Side effects:
Performance effects: Eliminates the
fork()overhead entirely. Theuname()syscall completes in microseconds regardless of the process's memory usage, whereas the previouspopen()approach could stall for 100ms+ on large-memory processes.Breaking backward compatibility: No. The output format of the
kernel_versionbvar remains the same.