2018-01-31 17:05:54 +00:00
|
|
|
// +build linux
|
2017-06-14 13:11:40 +00:00
|
|
|
|
2017-06-12 14:12:03 +00:00
|
|
|
package collector
|
2017-06-08 18:33:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2017-06-09 16:07:25 +00:00
|
|
|
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2020-10-25 14:22:47 +00:00
|
|
|
"github.com/opencontainers/runc/types"
|
|
|
|
|
|
|
|
"github.com/bcicen/ctop/models"
|
2017-06-08 18:33:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Runc collector
|
|
|
|
type Runc struct {
|
2017-06-27 15:46:03 +00:00
|
|
|
models.Metrics
|
2017-06-09 16:07:25 +00:00
|
|
|
id string
|
|
|
|
libc libcontainer.Container
|
2017-06-27 15:46:03 +00:00
|
|
|
stream chan models.Metrics
|
2017-06-08 18:33:34 +00:00
|
|
|
done bool
|
|
|
|
running bool
|
2017-06-09 16:07:25 +00:00
|
|
|
interval int // collection interval, in seconds
|
|
|
|
lastCpu float64
|
|
|
|
lastSysCpu float64
|
2017-06-08 18:33:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 16:07:25 +00:00
|
|
|
func NewRunc(libc libcontainer.Container) *Runc {
|
2017-06-08 18:33:34 +00:00
|
|
|
c := &Runc{
|
2017-06-27 15:46:03 +00:00
|
|
|
Metrics: models.Metrics{},
|
2017-06-09 16:07:25 +00:00
|
|
|
id: libc.ID(),
|
|
|
|
libc: libc,
|
|
|
|
interval: 1,
|
2017-06-08 18:33:34 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Runc) Running() bool {
|
|
|
|
return c.running
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Runc) Start() {
|
|
|
|
c.done = false
|
2017-06-27 15:46:03 +00:00
|
|
|
c.stream = make(chan models.Metrics)
|
2017-06-08 18:33:34 +00:00
|
|
|
go c.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Runc) Stop() {
|
2020-11-13 21:43:11 +00:00
|
|
|
c.running = false
|
2017-06-08 18:33:34 +00:00
|
|
|
c.done = true
|
|
|
|
}
|
|
|
|
|
2017-06-27 15:46:03 +00:00
|
|
|
func (c *Runc) Stream() chan models.Metrics {
|
2017-06-08 18:33:34 +00:00
|
|
|
return c.stream
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:18:17 +00:00
|
|
|
func (c *Runc) Logs() LogCollector {
|
|
|
|
return nil
|
2017-06-27 16:21:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:33:34 +00:00
|
|
|
func (c *Runc) run() {
|
|
|
|
c.running = true
|
|
|
|
defer close(c.stream)
|
2017-06-10 12:36:34 +00:00
|
|
|
log.Debugf("collector started for container: %s", c.id)
|
2017-06-08 18:33:34 +00:00
|
|
|
|
|
|
|
for {
|
2017-06-09 16:07:25 +00:00
|
|
|
stats, err := c.libc.Stats()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to collect stats for container %s:\n%s", c.id, err)
|
|
|
|
break
|
2017-06-08 18:33:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 16:07:25 +00:00
|
|
|
c.ReadCPU(stats.CgroupStats)
|
|
|
|
c.ReadMem(stats.CgroupStats)
|
|
|
|
c.ReadNet(stats.Interfaces)
|
|
|
|
|
2017-06-08 18:33:34 +00:00
|
|
|
c.stream <- c.Metrics
|
|
|
|
if c.done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.running = false
|
|
|
|
}
|
2017-06-09 16:07:25 +00:00
|
|
|
|
|
|
|
func (c *Runc) ReadCPU(stats *cgroups.Stats) {
|
|
|
|
u := stats.CpuStats.CpuUsage
|
2020-11-20 21:08:19 +00:00
|
|
|
ncpus := uint8(len(u.PercpuUsage))
|
2017-06-09 16:07:25 +00:00
|
|
|
total := float64(u.TotalUsage)
|
|
|
|
system := float64(getSysCPUUsage())
|
|
|
|
|
|
|
|
cpudiff := total - c.lastCpu
|
|
|
|
syscpudiff := system - c.lastSysCpu
|
|
|
|
|
2020-11-20 21:08:19 +00:00
|
|
|
c.NCpus = ncpus
|
|
|
|
c.CPUUtil = percent(cpudiff, syscpudiff)
|
2017-06-09 16:07:25 +00:00
|
|
|
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)
|
2017-06-09 17:15:12 +00:00
|
|
|
if c.MemLimit > sysMemTotal && sysMemTotal > 0 {
|
|
|
|
c.MemLimit = sysMemTotal
|
|
|
|
}
|
2017-06-10 13:00:54 +00:00
|
|
|
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
|
2017-06-09 16:07:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 14:22:47 +00:00
|
|
|
func (c *Runc) ReadNet(interfaces []*types.NetworkInterface) {
|
2017-06-09 16:07:25 +00:00
|
|
|
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
|
|
|
|
}
|