mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-10-05 18:03:21 +08:00
Periodic tasks refactor (#62)
* Once every second the session run a list of periodic tasks on every instance concurrently. We use these tasks to do things like: - Collect mem and cpu stats - Check if instance is part of a swarm cluster - Broadcast information to connected clients
This commit is contained in:
committed by
GitHub
parent
07fee4c1bf
commit
afa47c0bfc
64
services/collect_stats_task.go
Normal file
64
services/collect_stats_task.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
units "github.com/docker/go-units"
|
||||
)
|
||||
|
||||
type collectStatsTask struct {
|
||||
mem float64
|
||||
memLimit float64
|
||||
memPercent float64
|
||||
v *types.StatsJSON
|
||||
|
||||
cpuPercent float64
|
||||
previousCPU uint64
|
||||
previousSystem uint64
|
||||
}
|
||||
|
||||
func (c *collectStatsTask) Run(i *Instance) {
|
||||
reader, err := GetContainerStats(i.Name)
|
||||
if err != nil {
|
||||
log.Println("Error while trying to collect instance stats", err)
|
||||
return
|
||||
}
|
||||
dec := json.NewDecoder(reader)
|
||||
e := dec.Decode(&c.v)
|
||||
if e != nil {
|
||||
log.Println("Error while trying to collect instance stats", e)
|
||||
return
|
||||
}
|
||||
// Memory
|
||||
if c.v.MemoryStats.Limit != 0 {
|
||||
c.memPercent = float64(c.v.MemoryStats.Usage) / float64(c.v.MemoryStats.Limit) * 100.0
|
||||
}
|
||||
c.mem = float64(c.v.MemoryStats.Usage)
|
||||
c.memLimit = float64(c.v.MemoryStats.Limit)
|
||||
|
||||
i.Mem = fmt.Sprintf("%.2f%% (%s / %s)", c.memPercent, units.BytesSize(c.mem), units.BytesSize(c.memLimit))
|
||||
|
||||
// cpu
|
||||
c.previousCPU = c.v.PreCPUStats.CPUUsage.TotalUsage
|
||||
c.previousSystem = c.v.PreCPUStats.SystemUsage
|
||||
c.cpuPercent = calculateCPUPercentUnix(c.previousCPU, c.previousSystem, c.v)
|
||||
i.Cpu = fmt.Sprintf("%.2f%%", c.cpuPercent)
|
||||
}
|
||||
|
||||
func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *types.StatsJSON) float64 {
|
||||
var (
|
||||
cpuPercent = 0.0
|
||||
// calculate the change for the cpu usage of the container in between readings
|
||||
cpuDelta = float64(v.CPUStats.CPUUsage.TotalUsage) - float64(previousCPU)
|
||||
// calculate the change for the entire system between readings
|
||||
systemDelta = float64(v.CPUStats.SystemUsage) - float64(previousSystem)
|
||||
)
|
||||
|
||||
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
||||
cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CPUStats.CPUUsage.PercpuUsage)) * 100.0
|
||||
}
|
||||
return cpuPercent
|
||||
}
|
Reference in New Issue
Block a user