From 88b46a79fe7cb3cc5fddd4ef332f32ebff712798 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 18 May 2019 15:45:11 +1000 Subject: [PATCH] add graphs --- pkg/commands/container_stats.go | 134 ++++++++++++++++++++++++++++++++ pkg/gui/containers_panel.go | 69 +++++++++++++++- 2 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 pkg/commands/container_stats.go diff --git a/pkg/commands/container_stats.go b/pkg/commands/container_stats.go new file mode 100644 index 00000000..5b94b275 --- /dev/null +++ b/pkg/commands/container_stats.go @@ -0,0 +1,134 @@ +package commands + +import "time" + +// ContainerStats autogenerated at https://mholt.github.io/json-to-go/ +type ContainerStats struct { + Read time.Time `json:"read"` + Preread time.Time `json:"preread"` + PidsStats struct { + Current int `json:"current"` + } `json:"pids_stats"` + BlkioStats struct { + IoServiceBytesRecursive []struct { + Major int `json:"major"` + Minor int `json:"minor"` + Op string `json:"op"` + Value int `json:"value"` + } `json:"io_service_bytes_recursive"` + IoServicedRecursive []struct { + Major int `json:"major"` + Minor int `json:"minor"` + Op string `json:"op"` + Value int `json:"value"` + } `json:"io_serviced_recursive"` + IoQueueRecursive []interface{} `json:"io_queue_recursive"` + IoServiceTimeRecursive []interface{} `json:"io_service_time_recursive"` + IoWaitTimeRecursive []interface{} `json:"io_wait_time_recursive"` + IoMergedRecursive []interface{} `json:"io_merged_recursive"` + IoTimeRecursive []interface{} `json:"io_time_recursive"` + SectorsRecursive []interface{} `json:"sectors_recursive"` + } `json:"blkio_stats"` + NumProcs int `json:"num_procs"` + StorageStats struct { + } `json:"storage_stats"` + CPUStats struct { + CPUUsage struct { + TotalUsage int64 `json:"total_usage"` + PercpuUsage []int64 `json:"percpu_usage"` + UsageInKernelmode int64 `json:"usage_in_kernelmode"` + UsageInUsermode int64 `json:"usage_in_usermode"` + } `json:"cpu_usage"` + SystemCPUUsage int64 `json:"system_cpu_usage"` + OnlineCpus int `json:"online_cpus"` + ThrottlingData struct { + Periods int `json:"periods"` + ThrottledPeriods int `json:"throttled_periods"` + ThrottledTime int `json:"throttled_time"` + } `json:"throttling_data"` + } `json:"cpu_stats"` + PrecpuStats struct { + CPUUsage struct { + TotalUsage int64 `json:"total_usage"` + PercpuUsage []int64 `json:"percpu_usage"` + UsageInKernelmode int64 `json:"usage_in_kernelmode"` + UsageInUsermode int64 `json:"usage_in_usermode"` + } `json:"cpu_usage"` + SystemCPUUsage int64 `json:"system_cpu_usage"` + OnlineCpus int `json:"online_cpus"` + ThrottlingData struct { + Periods int `json:"periods"` + ThrottledPeriods int `json:"throttled_periods"` + ThrottledTime int `json:"throttled_time"` + } `json:"throttling_data"` + } `json:"precpu_stats"` + MemoryStats struct { + Usage int `json:"usage"` + MaxUsage int `json:"max_usage"` + Stats struct { + ActiveAnon int `json:"active_anon"` + ActiveFile int `json:"active_file"` + Cache int `json:"cache"` + Dirty int `json:"dirty"` + HierarchicalMemoryLimit int64 `json:"hierarchical_memory_limit"` + HierarchicalMemswLimit int64 `json:"hierarchical_memsw_limit"` + InactiveAnon int `json:"inactive_anon"` + InactiveFile int `json:"inactive_file"` + MappedFile int `json:"mapped_file"` + Pgfault int `json:"pgfault"` + Pgmajfault int `json:"pgmajfault"` + Pgpgin int `json:"pgpgin"` + Pgpgout int `json:"pgpgout"` + Rss int `json:"rss"` + RssHuge int `json:"rss_huge"` + TotalActiveAnon int `json:"total_active_anon"` + TotalActiveFile int `json:"total_active_file"` + TotalCache int `json:"total_cache"` + TotalDirty int `json:"total_dirty"` + TotalInactiveAnon int `json:"total_inactive_anon"` + TotalInactiveFile int `json:"total_inactive_file"` + TotalMappedFile int `json:"total_mapped_file"` + TotalPgfault int `json:"total_pgfault"` + TotalPgmajfault int `json:"total_pgmajfault"` + TotalPgpgin int `json:"total_pgpgin"` + TotalPgpgout int `json:"total_pgpgout"` + TotalRss int `json:"total_rss"` + TotalRssHuge int `json:"total_rss_huge"` + TotalUnevictable int `json:"total_unevictable"` + TotalWriteback int `json:"total_writeback"` + Unevictable int `json:"unevictable"` + Writeback int `json:"writeback"` + } `json:"stats"` + Limit int64 `json:"limit"` + } `json:"memory_stats"` + Name string `json:"name"` + ID string `json:"id"` + Networks struct { + Eth0 struct { + RxBytes int `json:"rx_bytes"` + RxPackets int `json:"rx_packets"` + RxErrors int `json:"rx_errors"` + RxDropped int `json:"rx_dropped"` + TxBytes int `json:"tx_bytes"` + TxPackets int `json:"tx_packets"` + TxErrors int `json:"tx_errors"` + TxDropped int `json:"tx_dropped"` + } `json:"eth0"` + } `json:"networks"` +} + +// CalculateContainerCPUPercentage calculates the cpu usage of the container as a percent of total CPU usage +// to calculate CPU usage, we take the increase in CPU time from the container since the last poll, divide that by the total increase in CPU time since the last poll, times by the number of cores, and times by 100 to get a percentage +// I'm not entirely sure why we need to multiply by the number of cores, but the numbers work +func (s *ContainerStats) CalculateContainerCPUPercentage() float64 { + cpuUsageDelta := s.CPUStats.CPUUsage.TotalUsage - s.PrecpuStats.CPUUsage.TotalUsage + cpuTotalUsageDelta := s.CPUStats.SystemCPUUsage - s.PrecpuStats.SystemCPUUsage + numberOfCores := len(s.CPUStats.CPUUsage.PercpuUsage) + + return float64(cpuUsageDelta*100) * float64(numberOfCores) / float64(cpuTotalUsageDelta) +} + +// CalculateContainerMemoryUsage calculates the memory usage of the container as a percent of total available memory +func (s *ContainerStats) CalculateContainerMemoryUsage() float64 { + return float64(s.MemoryStats.Usage*100) / float64(s.MemoryStats.Limit) +} diff --git a/pkg/gui/containers_panel.go b/pkg/gui/containers_panel.go index baecdae5..0b3578fd 100644 --- a/pkg/gui/containers_panel.go +++ b/pkg/gui/containers_panel.go @@ -1,11 +1,15 @@ package gui import ( + "bufio" + "context" "encoding/json" "fmt" + "math" "os/exec" "time" + "github.com/carlosms/asciigraph" "github.com/go-errors/errors" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazydocker/pkg/commands" @@ -15,7 +19,7 @@ import ( // list panel functions func (gui *Gui) getContainerContexts() []string { - return []string{"logs", "config"} + return []string{"logs", "config", "stats"} } func (gui *Gui) getSelectedContainer(g *gocui.Gui) (*commands.Container, error) { @@ -86,6 +90,10 @@ func (gui *Gui) handleContainerSelect(g *gocui.Gui, v *gocui.View, alreadySelect if err := gui.renderConfig(mainView, container, writerID); err != nil { return err } + case "stats": + if err := gui.renderStats(mainView, container, writerID); err != nil { + return err + } default: return errors.New("Unknown context for containers panel") } @@ -106,6 +114,65 @@ func (gui *Gui) renderConfig(mainView *gocui.View, container *commands.Container return nil } +func (gui *Gui) renderStats(mainView *gocui.View, container *commands.Container, writerID int) error { + mainView.Autoscroll = false + mainView.Title = "Stats" + + stats, err := gui.DockerCommand.Client.ContainerStats(context.Background(), container.ID, true) + if err != nil { + return err + } + + go func() { + cpuUsageHistory := []float64{} + memoryUsageHistory := []float64{} + scanner := bufio.NewScanner(stats.Body) + for scanner.Scan() { + data := scanner.Bytes() + var stats commands.ContainerStats + json.Unmarshal(data, &stats) + + if len(cpuUsageHistory) >= 20 { + cpuUsageHistory = cpuUsageHistory[1:] + } + + if len(memoryUsageHistory) >= 20 { + memoryUsageHistory = memoryUsageHistory[1:] + } + + percentMemory := stats.CalculateContainerMemoryUsage() + + memoryUsageHistory = append(memoryUsageHistory, percentMemory) + memoryGraph := asciigraph.Plot( + memoryUsageHistory, + asciigraph.Height(10), + asciigraph.Width(30), + asciigraph.Min(0), + asciigraph.Max(100), + asciigraph.Caption(fmt.Sprintf("%.2f%% Memory (%.2f/%.2f)", percentMemory, float64(stats.MemoryStats.Usage)/math.Pow(2, 20), float64(stats.MemoryStats.Limit)/math.Pow(2, 20))), + ) + + percentageCPU := stats.CalculateContainerCPUPercentage() + + cpuUsageHistory = append(cpuUsageHistory, percentageCPU) + cpuGraph := asciigraph.Plot( + cpuUsageHistory, + asciigraph.Height(10), + asciigraph.Width(30), + asciigraph.Min(0), + asciigraph.Max(100), + asciigraph.Caption(fmt.Sprintf("%.2f%% CPU", percentageCPU)), + ) + + gui.renderString(gui.g, "main", fmt.Sprintf("%s\n%s", cpuGraph, memoryGraph)) + } + + stats.Body.Close() + }() + + return nil +} + func (gui *Gui) renderLogs(mainView *gocui.View, container *commands.Container, writerID int) error { mainView.Autoscroll = true mainView.Title = "Logs"