This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Add process collector #24
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13ea871
Add process collector
olivielpeau 30a5863
Implement `set` as `map[string]struct{}`
olivielpeau d47cff2
Initialize `options` in `init` function
olivielpeau f6929f2
[processes][linux] Detect kernel threads by looking at ancestors' PIDs
olivielpeau ca33b90
[processes][linux] Avoid possible infinite recursion on ppid hash rec…
olivielpeau 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 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 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 |
---|---|---|
@@ -1,9 +1,4 @@ | ||
package gops | ||
|
||
import ( | ||
"github.com/shirou/gopsutil/process" | ||
) | ||
|
||
func pickName(p *process.Process) (string, error) { | ||
return p.Name() | ||
} | ||
// Do nothing for now | ||
func postProcess(processInfos []*ProcessInfo) {} |
This file contains 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 |
---|---|---|
@@ -1,20 +1,40 @@ | ||
package gops | ||
|
||
import ( | ||
"strings" | ||
"fmt" | ||
|
||
"github.com/shirou/gopsutil/process" | ||
) | ||
|
||
func pickName(p *process.Process) (string, error) { | ||
cmdline, err := p.Cmdline() | ||
if err != nil { | ||
return "", err | ||
// Build a hash pid -> ppid | ||
func buildPIDHash(processInfos []*ProcessInfo) (hash map[int32]int32) { | ||
hash = make(map[int32]int32) | ||
for _, processInfo := range processInfos { | ||
hash[processInfo.PID] = processInfo.PPID | ||
} | ||
// Assume that the process is a kernel process when it has no args | ||
if strings.TrimSpace(cmdline) == "" { | ||
return "kernel", nil | ||
return | ||
} | ||
|
||
// Return whether the PID is of a kernel thread, based on whether it has | ||
// the init process (PID 1) as ancestor | ||
func isKernelThread(pid int32, pidHash map[int32]int32) bool { | ||
if pid == 1 { | ||
return false | ||
} | ||
ppid, ok := pidHash[pid] | ||
if !ok { | ||
return true | ||
} | ||
|
||
return p.Name() | ||
return isKernelThread(ppid, pidHash) | ||
} | ||
|
||
// Name processes "kernel" if they're a kernel thread | ||
func postProcess(processInfos []*ProcessInfo) { | ||
pidHash := buildPIDHash(processInfos) | ||
for _, processInfo := range processInfos { | ||
if isKernelThread(processInfo.PID, pidHash) { | ||
processInfo.Name = "kernel" | ||
} | ||
} | ||
} |
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.
Because the processInfo's are not precisely a snapshot of the process table due to the fact that while we iterate the table to get the info - this table might change, I'm therefore (quite mildly) concerned that it could leave a slightly tainted table that wouldn't always lead to
1
or what would be worse some weird loop.We could maybe add a maximum depth
isKernelThread
might go before deciding it's NOT a kernel thread. In general kernel threads would have a depth of 2 maximum. I'm sure if we go 4 or 5 levels into the recursion we can be certain it's not a kernel thread.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.
Thanks, hadn't thought about that.
Addressed this by adding a recursion limit, let me know what you think.