diff options
| author | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 00:00:13 +0100 |
|---|---|---|
| committer | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 00:00:13 +0100 |
| commit | 285d29d9e34a906f11b15388c637739c0d630cf7 (patch) | |
| tree | c0e9df625e747879880e15893769fef0693829c3 | |
| parent | 3d924b60339016201b56c6f31be73e1efa63f8b1 (diff) | |
Convert numbers to human readable
| -rw-r--r-- | gstat/main.go | 42 | ||||
| -rw-r--r-- | process/process.go | 1 | ||||
| -rw-r--r-- | utils/utils.go | 39 |
3 files changed, 62 insertions, 20 deletions
diff --git a/gstat/main.go b/gstat/main.go index 0b34eef..f2fdae7 100644 --- a/gstat/main.go +++ b/gstat/main.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/buetow/gstat/diskstats" "github.com/buetow/gstat/process" + "github.com/buetow/gstat/utils" "golang.org/x/crypto/ssh/terminal" "log" "os" @@ -13,13 +14,15 @@ import ( "time" ) -var interval time.Duration -var footer string +var interval_ time.Duration +var banner_ string type twoP struct { first process.Process second process.Process diff int + diffR int + diffW int } type mapP map[string]twoP @@ -55,7 +58,7 @@ func sortP(lastP *mapP) *list.List { for _, val := range *lastP { nowTimestamp := int32(time.Now().Unix()) - if val.first.Timestamp+int32(interval)*2 < nowTimestamp { + if val.first.Timestamp+int32(interval_)*2 < nowTimestamp { // Schedule remove obsolete pids from lastP remove.PushBack(val.first.Id) @@ -87,6 +90,7 @@ func sortP(lastP *mapP) *list.List { } func printP(sortedP *list.List) { + tWidth, tHeight, err := terminal.GetSize(0) if err != nil { log.Fatal(err) @@ -94,7 +98,7 @@ func printP(sortedP *list.List) { // Clear the screen + print header fmt.Println("\033[H\033[2J") - fmt.Printf("%5s %5s %s\n", "Value", "PID", "Command") + fmt.Printf("%6s %6s %6s %s\n", "WRITES", "READS", "PID", "COMMAND") // Print the results row := 2 @@ -104,7 +108,9 @@ func printP(sortedP *list.List) { break } val := e.Value.(twoP) - outstr := fmt.Sprintf("%5d %5d %s", val.diff, val.first.Pid, val.first.Cmdline) + first := val.first + outstr := fmt.Sprintf("%6s %6s %6d %s", utils.Human(val.diffW), utils.Human(val.diffR), first.Pid, first.Cmdline) + l := len(outstr) if l > tWidth { l = tWidth @@ -112,11 +118,11 @@ func printP(sortedP *list.List) { fmt.Printf("%s\n", outstr[0:l]) } - // Fill up the other rows + print footer - for ; row < tHeight; row++ { + // Fill up the other rows + print banner + for ; row < tHeight-1; row++ { fmt.Println() } - fmt.Printf(footer) + fmt.Print(banner_) } func receiveP(pRxChan <-chan process.Process) { @@ -124,14 +130,12 @@ func receiveP(pRxChan <-chan process.Process) { flag := false makeDiff := func(first, second process.Process) twoP { - // TODO: make "syscr" choosable/configurable - firstVal := first.Count["syscr"] - secondVal := second.Count["syscr"] - diff := firstVal - secondVal - if diff < 0 { - diff = -diff - } - return twoP{first, second, diff} + // TODO: make "rchar,wchar" configurable + firstValR, firstValW := first.Count["rchar"], first.Count["wchar"] + secondValR, secondValW := second.Count["rchar"], second.Count["wchar"] + diffR, diffW := utils.Abs(firstValR-secondValR), utils.Abs(firstValW-secondValW) + diff := diffR + diffW + return twoP{first, second, diff, diffR, diffW} } for p := range pRxChan { @@ -159,8 +163,8 @@ func main() { dRxChan := make(chan diskstats.Diskstats) pRxChan := make(chan process.Process) - interval = 2 - footer = "gstat 0.1 (C) 2015 Paul Buetow <http://github.com/buetow/gstat>" + interval_ = 2 + banner_ = "gstat 0.1" go timedGather(timerChan, dRxChan, pRxChan) go receiveD(dRxChan) @@ -179,6 +183,6 @@ func main() { for { timerChan <- true - time.Sleep(time.Second * interval) + time.Sleep(time.Second * interval_) } } diff --git a/process/process.go b/process/process.go index c4f2953..53db8b7 100644 --- a/process/process.go +++ b/process/process.go @@ -107,5 +107,6 @@ func Gather(pTxChan chan<- Process) { } } } + pTxChan <- Process{Last: true} } diff --git a/utils/utils.go b/utils/utils.go index e314367..65461f4 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,6 +1,9 @@ package utils -import "io/ioutil" +import ( + "fmt" + "io/ioutil" +) func Slurp(what *string, path string) error { bytes, err := ioutil.ReadFile(path) @@ -11,3 +14,37 @@ func Slurp(what *string, path string) error { } return nil } + +func Abs(x int) int { + if x < 0 { + return -x + } else { + return x + } +} + +func Human(x int) string { + units := []string{"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} + + f := float32(x) + i := 0 + + for ; f >= 1000; i++ { + f /= 1000 + } + + return fmt.Sprintf("%d%s", int(f), units[i]) +} + +func HumanMebi(x int) string { + units := []string{"ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"} + + f := float32(x) + i := 0 + + for ; f >= 1024; i++ { + f /= 1024 + } + + return fmt.Sprintf("%d%s", int(f), units[i]) +} |
