diff options
| author | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 01:05:03 +0100 |
|---|---|---|
| committer | Paul Buetow (europa) <paul@buetow.org> | 2015-05-31 01:05:03 +0100 |
| commit | 006e450462a64ea89feb8bc100941d3c0a0c6a95 (patch) | |
| tree | 531c5f0e29ac05940a8576166444f98866023824 | |
| parent | f459cec5e073de22019de4ae160d91cbaa926b63 (diff) | |
display mode and interval in header
| -rw-r--r-- | gstat/main.go | 34 | ||||
| -rw-r--r-- | utils/utils.go | 4 |
2 files changed, 26 insertions, 12 deletions
diff --git a/gstat/main.go b/gstat/main.go index c5ee164..ed28444 100644 --- a/gstat/main.go +++ b/gstat/main.go @@ -22,6 +22,7 @@ var config struct { interval time.Duration binary *bool mode *int + modeName string } type twoP struct { @@ -62,7 +63,7 @@ func sortP(lastP *mapP) *list.List { for _, val := range *lastP { nowTimestamp := int32(time.Now().Unix()) - if val.first.Timestamp+int32(config.interval)*2 < nowTimestamp { + if val.first.Timestamp+2 < nowTimestamp { // Schedule remove obsolete pids from lastP remove.PushBack(val.first.Id) @@ -94,7 +95,6 @@ func sortP(lastP *mapP) *list.List { } func printP(sortedP *list.List) { - tWidth, tHeight, err := terminal.GetSize(0) if err != nil { log.Fatal(err) @@ -102,11 +102,12 @@ func printP(sortedP *list.List) { // Clear the screen + print header fmt.Printf("\033[H\033[2J") + fmt.Printf("Mode: %s, Interval: %s\n", config.modeName, config.interval) fmt.Printf("(Hit Ctr-C to quit, re-run with -h for flags)\n") - fmt.Printf("%6s %6s %6s %s\n", "WRITES", "READS", "PID", "COMMAND") + fmt.Printf("%5s/%5s %5s %s\n", "WRITE", "READS", "PID", "COMMAND") // Print the results - row := 2 + row := 3 for e := sortedP.Front(); e != nil; e = e.Next() { row++ if row > tHeight { @@ -122,7 +123,7 @@ func printP(sortedP *list.List) { humanW, humanR = utils.Human(val.diffW), utils.Human(val.diffR) } - outstr := fmt.Sprintf("%6s %6s %6d %s", humanW, humanR, first.Pid, first.Cmdline) + outstr := fmt.Sprintf("%5s %5s %5d %s", humanW, humanR, first.Pid, first.Cmdline) l := len(outstr) if l > tWidth { @@ -136,10 +137,23 @@ func receiveP(pRxChan <-chan process.Process) { lastP := make(mapP) flag := false + var readKey, writeKey, modeName string + + switch *config.mode { + case 0: + readKey, writeKey, modeName = "read_bytes", "write_bytes", "bytes" + case 1: + readKey, writeKey, modeName = "syscr", "syscw", "syscalls" + case 2: + readKey, writeKey, modeName = "rchar", "wchar", "chars" + } + + config.modeName = modeName + makeDiff := func(first, second process.Process) twoP { // TODO: make "rchar,wchar" configurable - firstValR, firstValW := first.Count["rchar"], first.Count["wchar"] - secondValR, secondValW := second.Count["rchar"], second.Count["wchar"] + firstValR, firstValW := first.Count[readKey], first.Count[writeKey] + secondValR, secondValW := second.Count[readKey], second.Count[writeKey] diffR, diffW := utils.Abs(firstValR-secondValR), utils.Abs(firstValW-secondValW) diff := diffR + diffW return twoP{first, second, diff, diffR, diffW} @@ -170,7 +184,7 @@ func parseFlags() { interF := flag.Int("i", 2, "Update interval in seconds") config.binary = flag.Bool("b", false, "Use binary instead of deciman (e.g. kiB an not kB") - config.mode = flag.Int("m", 0, "The stats mode: 0:bytes 1:syscalls 2:chars") + config.mode = flag.Int("m", 1, "The stats mode: 0:bytes 1:syscalls 2:chars") flag.Parse() @@ -181,7 +195,7 @@ func parseFlags() { os.Exit(0) } - config.interval = time.Duration(*interF) + config.interval = time.Duration(*interF) * time.Second } func main() { @@ -209,6 +223,6 @@ func main() { for { tChan <- true - time.Sleep(time.Second * config.interval) + time.Sleep(config.interval) } } diff --git a/utils/utils.go b/utils/utils.go index 28532ab..1e9ce71 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -24,7 +24,7 @@ func Abs(x int) int { } func Human(x int) string { - units := []string{"kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} + units := []string{"k", "M", "G", "T", "P", "E", "Z", "Y"} f := float32(x) i := 0 @@ -37,7 +37,7 @@ func Human(x int) string { } func HumanBinary(x int) string { - units := []string{"ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"} + units := []string{"k", "M", "G", "T", "P", "E", "Z", "Y"} f := float32(x) i := 0 |
