summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2017-01-26 01:16:05 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2017-01-26 01:16:05 -0500
commit29bbc978760a4a56e27d5fdb41e78fee0f62e186 (patch)
tree7183fadbafce0bd884b8abc3a51f343b21883d6e
parent3f9c979830b72bb30ca6dcc0384a18be6565d04d (diff)
cgswap: tidy
-rw-r--r--cgswap.go78
1 files changed, 49 insertions, 29 deletions
diff --git a/cgswap.go b/cgswap.go
index 6fab8ae..696265a 100644
--- a/cgswap.go
+++ b/cgswap.go
@@ -6,6 +6,7 @@ package main
import (
"bufio"
"fmt"
+ "io"
"os"
"sort"
"strconv"
@@ -29,7 +30,7 @@ func (l cginfos) Len() int { return len(l) }
func (l cginfos) Less(i, j int) bool { return l[i].VmSwap < l[j].VmSwap }
func (l cginfos) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
-func in_array(needle string, haystack []string) bool {
+func inArray(needle string, haystack []string) bool {
for _, straw := range haystack {
if needle == straw {
return true
@@ -38,6 +39,38 @@ func in_array(needle string, haystack []string) bool {
return false
}
+func getVmSwap(statusFile io.Reader) (int, bool) {
+ buf := bufio.NewScanner(statusFile)
+ for buf.Scan() {
+ line := buf.Text()
+ if strings.HasPrefix(line, "VmSwap:") {
+ swap, err := strconv.Atoi(strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(line, "VmSwap:"), "kB")))
+ if err != nil {
+ return 0, false
+ }
+ return swap, true
+ }
+ }
+ return 0, false
+}
+
+func getCgroup(cgroupFile io.Reader) (string, bool) {
+ buf := bufio.NewScanner(cgroupFile)
+ for buf.Scan() {
+ parts := strings.SplitN(buf.Text(), ":", 3)
+ if len(parts) != 3 {
+ continue
+ }
+ heir := parts[0]
+ controllers := strings.Split(parts[1], ",")
+ cgroup := parts[2]
+ if heir == "0" || inArray("name=systemd", controllers) {
+ return cgroup, true
+ }
+ }
+ return "", false
+}
+
func main() {
dir, err := os.Open("/proc")
if err != nil {
@@ -79,35 +112,22 @@ func main() {
return
}
cgroupFile, err := os.Open(fmt.Sprintf("/proc/%d/cgroup", pid))
-
- buf := bufio.NewScanner(statusFile)
- for buf.Scan() {
- line := buf.Text()
- if strings.HasPrefix(line, "VmSwap:") {
- swap, err := strconv.Atoi(strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(line, "VmSwap:"), "kB")))
- if err != nil || swap == 0 {
- return
- }
- buf := bufio.NewScanner(cgroupFile)
- for buf.Scan() {
- parts := strings.SplitN(buf.Text(), ":", 3)
- if len(parts) != 3 {
- continue
- }
- heir := parts[0]
- controllers := strings.Split(parts[1], ",")
- cgroup := parts[2]
- if heir == "0" || in_array("name=systemd", controllers) {
- ch <- cginfo{VmSwap: swap, Cgroup: cgroup}
- return
- }
- }
- return
- }
+ if err != nil {
+ return
}
+ swap, ok := getVmSwap(statusFile)
+ if !ok || swap == 0 {
+ return
+ }
+ cgroup, ok := getCgroup(cgroupFile)
+ if !ok {
+ return
+ }
+ ch <- cginfo{VmSwap: swap, Cgroup: cgroup}
}(pid)
}
}
+
producers.Wait()
close(ch)
consumers.Wait()
@@ -118,10 +138,10 @@ func main() {
for _, info := range infos {
total += info.VmSwap
}
- vmswap_width := len(strconv.Itoa(total))
+ vmswapWidth := len(strconv.Itoa(total))
for _, info := range infos {
- fmt.Printf("%[1]*d kB %s\n", vmswap_width, info.VmSwap, info.Cgroup)
+ fmt.Printf("%[1]*d kB %s\n", vmswapWidth, info.VmSwap, info.Cgroup)
}
- fmt.Printf("%[1]*d kB %s\n", vmswap_width, total, "total")
+ fmt.Printf("%[1]*d kB %s\n", vmswapWidth, total, "total")
}