Repository navigation
fix: process start time from /proc/<pid>/stat instead of taskstats (B8 of #369) #379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mayankpande88
wants to merge
4
commits into
main
Choose a base branch
from
port/upstream-b8-proc-start-time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−23
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eb3afc5
use os proc stat to fetch processStatTime for a PID instead of Taskst…
vishnukumarkvs 665682a
cache the bootTime of the host
vishnukumarkvs 75f0a19
proc: read the boot time once in init() instead of a lazily cached lo…
def dc03fca
test(proc): cover GetStartTime
mayankpande88 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 123 (my (odd) proc) S 1 4242 4242 0 -1 4194560 100 0 0 0 5 3 0 0 20 0 1 0 12345 1000000 200 18446744073709551615 0 0 0 0 0 0 0 0 0 0 0 0 17 0 0 0 0 0 0 |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package proc | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetStartTime(t *testing.T) { | ||
| saved := bootTime | ||
| defer func() { bootTime = saved }() | ||
| bootTime = 1700000000 | ||
|
|
||
| // starttime is field 22 of /proc/<pid>/stat, in clock ticks since boot: | ||
| // 12345 ticks = 123.45s. The comm contains spaces and ')', so fields | ||
| // must be counted from the last ')'. | ||
| assert.Equal(t, time.Unix(1700000000+123, 0), GetStartTime(123)) | ||
|
|
||
| assert.True(t, GetStartTime(999999).IsZero(), "missing pid") | ||
|
|
||
| bootTime = 0 | ||
| assert.True(t, GetStartTime(123).IsZero(), "unknown boot time") | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
GetStartTimeconverts the entire/proc/<pid>/statfile to a string and splits it into fields usingstrings.Fields. Since/proc/<pid>/statcontains around 50 fields, this approach triggers dozens of string allocations on every process start.Because
GetStartTimeis executed on every process start event, optimizing it to be allocation-free significantly reduces CPU and memory overhead in high-churn environments.We can achieve this by scanning the byte slice directly to find the 20th field after the last
)character, and parsing theuint64directly from the bytes without any string conversions or allocations.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I measured this, and I'm not changing it.
strings.Fieldsdoesn't allocate per field: the substrings share the original string's memory.testing.AllocsPerRun,GetStartTimemakes 10 allocations per call. 8 of them come fromPathplusos.ReadFile, which the suggested version keeps. Parsing adds 2: thestring(data)copy and theFieldsslice.