This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract function to return all current process ids
This implementation should be more efficient because it only uses the names under /proc, whereas the previous implementation fetched the full directory entry for each one, and sorted them too.
- Loading branch information
Showing
2 changed files
with
32 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package common | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func AllPids(procRoot string) ([]int, error) { | ||
fh, err := os.Open(procRoot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer fh.Close() | ||
dirNames, err := fh.Readdirnames(-1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pids := make([]int, len(dirNames)) | ||
for _, dirName := range dirNames { | ||
pid, err := strconv.Atoi(dirName) | ||
if err != nil { // Only interested in numeric entries - skip /proc/stat, etc. | ||
continue | ||
} | ||
pids = append(pids, pid) | ||
} | ||
return pids, err | ||
} |
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