mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add percent helper method to metrics
This commit is contained in:
parent
b49e174483
commit
389dee0f3c
@ -86,7 +86,7 @@ func (c *Docker) ReadCPU(stats *api.Stats) {
|
||||
func (c *Docker) ReadMem(stats *api.Stats) {
|
||||
c.MemUsage = int64(stats.MemoryStats.Usage)
|
||||
c.MemLimit = int64(stats.MemoryStats.Limit)
|
||||
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
|
||||
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
|
||||
}
|
||||
|
||||
func (c *Docker) ReadNet(stats *api.Stats) {
|
||||
|
@ -22,14 +22,14 @@ type Metrics struct {
|
||||
|
||||
func NewMetrics() Metrics {
|
||||
return Metrics{
|
||||
CPUUtil: -1,
|
||||
NetTx: -1,
|
||||
NetRx: -1,
|
||||
MemUsage: -1,
|
||||
MemPercent: -1,
|
||||
IOBytesRead: -1,
|
||||
IOBytesWrite: -1,
|
||||
Pids: -1,
|
||||
CPUUtil: -1,
|
||||
NetTx: -1,
|
||||
NetRx: -1,
|
||||
MemUsage: -1,
|
||||
MemPercent: -1,
|
||||
IOBytesRead: -1,
|
||||
IOBytesWrite: -1,
|
||||
Pids: -1,
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,3 +43,11 @@ type Collector interface {
|
||||
func round(num float64) int {
|
||||
return int(num + math.Copysign(0.5, num))
|
||||
}
|
||||
|
||||
// return rounded percentage
|
||||
func percent(val float64, total float64) int {
|
||||
if total <= 0 {
|
||||
return 0
|
||||
}
|
||||
return round((val / total) * 100)
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ func (c *Mock) run() {
|
||||
if c.MemUsage > c.MemLimit {
|
||||
c.MemUsage = 0
|
||||
}
|
||||
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
|
||||
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
|
||||
c.stream <- c.Metrics
|
||||
if c.done {
|
||||
break
|
||||
|
@ -19,6 +19,7 @@ func getSysMemTotal() int64 {
|
||||
return int64(stat.MemTotal * 1024)
|
||||
}
|
||||
|
||||
// return cumulative system cpu usage in nanoseconds
|
||||
func getSysCPUUsage() uint64 {
|
||||
stat, err := linuxproc.ReadStat("/proc/stat")
|
||||
if err != nil {
|
||||
|
@ -95,7 +95,7 @@ func (c *Runc) ReadMem(stats *cgroups.Stats) {
|
||||
if c.MemLimit > sysMemTotal && sysMemTotal > 0 {
|
||||
c.MemLimit = sysMemTotal
|
||||
}
|
||||
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
|
||||
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
|
||||
}
|
||||
|
||||
func (c *Runc) ReadNet(interfaces []*libcontainer.NetworkInterface) {
|
||||
|
Loading…
Reference in New Issue
Block a user