Skip to content

Commit

Permalink
Scale cpu graph based on num of cpus
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Nov 6, 2015
1 parent 0c90393 commit 659c00b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
23 changes: 14 additions & 9 deletions probe/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ func (c *container) ports(localAddrs []net.IP) report.StringSet {
return report.MakeStringSet(ports...)
}

func (c *container) memoryUsageMetric() report.Metric {
result := report.MakeMetric()
for _, s := range c.pendingStats {
result = result.Add(s.Read, float64(s.MemoryStats.Usage))
}
return result
}

func (c *container) cpuPercentMetric() report.Metric {
result := report.MakeMetric()
if len(c.pendingStats) < 2 {
Expand All @@ -238,22 +246,18 @@ func (c *container) cpuPercentMetric() report.Metric {
cpuPercent = (cpuDelta / systemDelta) * float64(len(s.CPUStats.CPUUsage.PercpuUsage)) * 100.0
}
result = result.Add(s.Read, cpuPercent)
available := float64(len(s.CPUStats.CPUUsage.PercpuUsage)) * 100.0
if available >= result.Max {
result.Max = available
}
previous = s
}
return result
}

func (c *container) metric(f func(*docker.Stats) float64) report.Metric {
result := report.MakeMetric()
for _, s := range c.pendingStats {
result = result.Add(s.Read, f(s))
}
return result
}

func (c *container) metrics() report.Metrics {
result := report.Metrics{
MemoryUsage: c.metric(func(s *docker.Stats) float64 { return float64(s.MemoryStats.Usage) }),
MemoryUsage: c.memoryUsageMetric(),
CPUTotalUsage: c.cpuPercentMetric(),
}

Expand Down Expand Up @@ -287,6 +291,7 @@ func (c *container) GetNode(hostID string, localAddrs []net.IP) report.Node {
ContainerIPs: report.MakeStringSet(ips...),
ContainerIPsWithScopes: report.MakeStringSet(ipsWithScopes...),
}).WithMetrics(c.metrics())

AddLabels(result, c.container.Config.Labels)

if c.latestStats == nil {
Expand Down
46 changes: 22 additions & 24 deletions render/detailed_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ func processOriginTable(nmd report.Node, addHostTag bool, addContainerTag bool)
}, len(rows) > 0 || commFound || pidFound
}

func sparklineRow(human, format string, scale float64, metric report.Metric) Row {
lastStr := ""
if last := metric.LastSample(); last != nil {
lastStr = fmt.Sprintf(format, last.Value/scale)
}
return Row{Key: human, ValueMajor: lastStr, Metric: metric, ValueType: "sparkline"}
}

func containerOriginTable(nmd report.Node, addHostTag bool) (Table, bool) {
rows := []Row{}
for _, tuple := range []struct{ key, human string }{
Expand All @@ -328,20 +336,11 @@ func containerOriginTable(nmd report.Node, addHostTag bool) (Table, bool) {
rows = append([]Row{{Key: "Host", ValueMajor: report.ExtractHostID(nmd)}}, rows...)
}

for _, tuple := range []struct {
key, human, format string
scale float64
}{
{docker.MemoryUsage, "Memory Usage", "%0.2f MB", 1024 * 1024.},
{docker.CPUTotalUsage, "CPU Usage", "%0.2f%%", 1.},
} {
if val, ok := nmd.Metrics[tuple.key]; ok {
lastStr := ""
if last := val.LastSample(); last != nil {
lastStr = fmt.Sprintf(tuple.format, last.Value/tuple.scale)
}
rows = append(rows, Row{Key: tuple.human, ValueMajor: lastStr, Metric: val, ValueType: "sparkline"})
}
if val, ok := nmd.Metrics[docker.MemoryUsage]; ok {
rows = append(rows, sparklineRow("Memory Usage", "%0.2f MB", 1024*1024, val))
}
if val, ok := nmd.Metrics[docker.CPUTotalUsage]; ok {
rows = append(rows, sparklineRow("CPU Usage", "%0.2f%%", 1, val))
}

var (
Expand Down Expand Up @@ -423,25 +422,24 @@ func hostOriginTable(nmd report.Node) (Table, bool) {

rows := []Row{}
for _, tuple := range []struct{ key, human string }{
// TODO(paulbellamy): render this as a sparkline and number
{host.Load1, "Load (1m)"},
{host.Load5, "Load (5m)"},
{host.Load15, "Load (15m)"},
} {
if val, ok := nmd.Metrics[tuple.key]; ok {
val.Max = maxLoad
val.First = lastLoad.Add(-15 * time.Second) // TODO(paulbellamy): This should be based on the duration flag, maybe? or just auto-scale to data.
val.Last = lastLoad
rows = append(rows, sparklineRow(tuple.human, "%0.2f", 1, val))
}
}
for _, tuple := range []struct{ key, human string }{
{host.OS, "Operating system"},
{host.KernelVersion, "Kernel version"},
{host.Uptime, "Uptime"},
} {
if val, ok := nmd.Metadata[tuple.key]; ok {
rows = append(rows, Row{Key: tuple.human, ValueMajor: val, ValueMinor: ""})
} else if val, ok := nmd.Metrics[tuple.key]; ok {
lastStr := ""
if last := val.LastSample(); last != nil {
lastStr = fmt.Sprint(last.Value)
}
val.Max = maxLoad
val.First = lastLoad.Add(-15 * time.Second) // TODO(paulbellamy): This should be based on the duration flag, maybe? or just auto-scale to data.
val.Last = lastLoad
rows = append(rows, Row{Key: tuple.human, ValueMajor: lastStr, Metric: val, ValueType: "sparkline"})
}
}

Expand Down
12 changes: 12 additions & 0 deletions report/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ func (m Metric) Merge(other Metric) Metric {
for _, sample := range other.Samples {
m = m.Add(sample.Timestamp, sample.Value)
}
if !other.First.IsZero() && other.First.Before(m.First) {
m.First = other.First
}
if !other.Last.IsZero() && other.Last.After(m.Last) {
m.Last = other.Last
}
if other.Min < m.Min {
m.Min = other.Min
}
if other.Max > m.Max {
m.Max = other.Max
}
return m
}

Expand Down
7 changes: 7 additions & 0 deletions report/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func TestMetricFirstLastMinMax(t *testing.T) {
t2 := time.Now().Add(1 * time.Minute)
t3 := time.Now().Add(2 * time.Minute)
t4 := time.Now().Add(3 * time.Minute)
other := report.MakeMetric()
other.Max = 5
other.Min = -5
other.First = t1.Add(-1 * time.Minute)
other.Last = t4.Add(1 * time.Minute)

tests := []struct {
f func(report.Metric) report.Metric
first, last time.Time
Expand All @@ -84,6 +90,7 @@ func TestMetricFirstLastMinMax(t *testing.T) {
{func(m report.Metric) report.Metric { return m.Add(t1, 1) }, t1, t2, 0, 2},
{func(m report.Metric) report.Metric { return m.Add(t3, -1) }, t1, t3, -1, 2},
{func(m report.Metric) report.Metric { return m.Add(t4, 3) }, t1, t4, -1, 3},
{func(m report.Metric) report.Metric { return m.Merge(other) }, t1.Add(-1 * time.Minute), t4.Add(1 * time.Minute), -5, 5},
}
for _, test := range tests {
if test.f != nil {
Expand Down

0 comments on commit 659c00b

Please sign in to comment.