mirror of
https://github.com/bcicen/ctop.git
synced 2025-12-06 15:16:41 +08:00
add runc metric collector
This commit is contained in:
@@ -1,25 +1,32 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/opencontainers/runc/libcontainer"
|
||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||
)
|
||||
|
||||
// Runc collector
|
||||
type Runc struct {
|
||||
Metrics
|
||||
id string
|
||||
libc libcontainer.Container
|
||||
stream chan Metrics
|
||||
done bool
|
||||
running bool
|
||||
aggression int64
|
||||
interval int // collection interval, in seconds
|
||||
lastCpu float64
|
||||
lastSysCpu float64
|
||||
}
|
||||
|
||||
func NewRunc(a int64) *Runc {
|
||||
func NewRunc(libc libcontainer.Container) *Runc {
|
||||
c := &Runc{
|
||||
Metrics: Metrics{},
|
||||
aggression: a,
|
||||
Metrics: Metrics{},
|
||||
id: libc.ID(),
|
||||
libc: libc,
|
||||
interval: 1,
|
||||
}
|
||||
c.MemLimit = 2147483648
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -43,22 +50,19 @@ func (c *Runc) Stream() chan Metrics {
|
||||
|
||||
func (c *Runc) run() {
|
||||
c.running = true
|
||||
rand.Seed(int64(time.Now().Nanosecond()))
|
||||
defer close(c.stream)
|
||||
|
||||
for {
|
||||
c.CPUUtil += rand.Intn(2) * int(c.aggression)
|
||||
if c.CPUUtil >= 100 {
|
||||
c.CPUUtil = 0
|
||||
stats, err := c.libc.Stats()
|
||||
if err != nil {
|
||||
log.Errorf("failed to collect stats for container %s:\n%s", c.id, err)
|
||||
break
|
||||
}
|
||||
|
||||
c.NetTx += rand.Int63n(60) * c.aggression
|
||||
c.NetRx += rand.Int63n(60) * c.aggression
|
||||
c.MemUsage += rand.Int63n(c.MemLimit/512) * c.aggression
|
||||
if c.MemUsage > c.MemLimit {
|
||||
c.MemUsage = 0
|
||||
}
|
||||
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
|
||||
c.ReadCPU(stats.CgroupStats)
|
||||
c.ReadMem(stats.CgroupStats)
|
||||
c.ReadNet(stats.Interfaces)
|
||||
|
||||
c.stream <- c.Metrics
|
||||
if c.done {
|
||||
break
|
||||
@@ -68,3 +72,46 @@ func (c *Runc) run() {
|
||||
|
||||
c.running = false
|
||||
}
|
||||
|
||||
func (c *Runc) ReadCPU(stats *cgroups.Stats) {
|
||||
u := stats.CpuStats.CpuUsage
|
||||
ncpus := float64(len(u.PercpuUsage))
|
||||
total := float64(u.TotalUsage)
|
||||
system := float64(getSysCPUUsage())
|
||||
|
||||
cpudiff := total - c.lastCpu
|
||||
syscpudiff := system - c.lastSysCpu
|
||||
|
||||
c.CPUUtil = round((cpudiff / syscpudiff * 100) * ncpus)
|
||||
c.lastCpu = total
|
||||
c.lastSysCpu = system
|
||||
c.Pids = int(stats.PidsStats.Current)
|
||||
}
|
||||
|
||||
func (c *Runc) ReadMem(stats *cgroups.Stats) {
|
||||
c.MemUsage = int64(stats.MemoryStats.Usage.Usage)
|
||||
c.MemLimit = int64(stats.MemoryStats.Usage.Limit)
|
||||
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
|
||||
}
|
||||
|
||||
func (c *Runc) ReadNet(interfaces []*libcontainer.NetworkInterface) {
|
||||
var rx, tx int64
|
||||
for _, network := range interfaces {
|
||||
rx += int64(network.RxBytes)
|
||||
tx += int64(network.TxBytes)
|
||||
}
|
||||
c.NetRx, c.NetTx = rx, tx
|
||||
}
|
||||
|
||||
func (c *Runc) ReadIO(stats *cgroups.Stats) {
|
||||
var read, write int64
|
||||
for _, blk := range stats.BlkioStats.IoServiceBytesRecursive {
|
||||
if blk.Op == "Read" {
|
||||
read = int64(blk.Value)
|
||||
}
|
||||
if blk.Op == "Write" {
|
||||
write = int64(blk.Value)
|
||||
}
|
||||
}
|
||||
c.IOBytesRead, c.IOBytesWrite = read, write
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user