From ab85b9a7d666c4b26b210074ca994e66a67ed184 Mon Sep 17 00:00:00 2001 From: Yu-Ju Hong Date: Tue, 22 Sep 2015 15:08:53 -0700 Subject: [PATCH] e2e: add a function to reset the stats in resource monitor The function can be called in a long-running test to clear exsiting stats (free the memory), and start over. --- test/e2e/kubelet_stats.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/e2e/kubelet_stats.go b/test/e2e/kubelet_stats.go index 26c2e971f9c..2e6c1a8b5e1 100644 --- a/test/e2e/kubelet_stats.go +++ b/test/e2e/kubelet_stats.go @@ -25,6 +25,7 @@ import ( "sort" "strconv" "strings" + "sync" "text/tabwriter" "time" @@ -346,6 +347,7 @@ func computeContainerResourceUsage(name string, oldStats, newStats *cadvisor.Con // list of containers, computes and cache resource usage up to // maxEntriesPerContainer for each container. type resourceCollector struct { + lock sync.RWMutex node string containers []string client *client.Client @@ -390,6 +392,8 @@ func (r *resourceCollector) collectStats(oldStats map[string]*cadvisor.Container Logf("Error getting container info on %q, err: %v", r.node, err) return } + r.lock.Lock() + defer r.lock.Unlock() for _, name := range r.containers { info, ok := infos[name] if !ok || len(info.Stats) < 1 { @@ -405,6 +409,8 @@ func (r *resourceCollector) collectStats(oldStats map[string]*cadvisor.Container // LogLatest logs the latest resource usage of each container. func (r *resourceCollector) LogLatest() { + r.lock.RLock() + defer r.lock.RUnlock() stats := make(map[string]*containerResourceUsage) for _, name := range r.containers { s := r.buffers[name][len(r.buffers)-1] @@ -417,6 +423,15 @@ func (r *resourceCollector) LogLatest() { Logf("\n%s", formatResourceUsageStats(r.node, stats)) } +// Reset frees the stats and start over. +func (r *resourceCollector) Reset() { + r.lock.Lock() + defer r.lock.Unlock() + for _, name := range r.containers { + r.buffers[name] = []*containerResourceUsage{} + } +} + type resourceUsageByCPU []*containerResourceUsage func (r resourceUsageByCPU) Len() int { return len(r) } @@ -428,6 +443,8 @@ var percentiles = [...]float64{0.05, 0.50, 0.90, 0.95} // GetBasicCPUStats returns the 5-th, 50-th, and 95-th, percentiles the cpu // usage in cores for containerName. This method examines all data currently in the buffer. func (r *resourceCollector) GetBasicCPUStats(containerName string) map[float64]float64 { + r.lock.RLock() + defer r.lock.RUnlock() result := make(map[float64]float64, len(percentiles)) usages := r.buffers[containerName] sort.Sort(resourceUsageByCPU(usages)) @@ -476,6 +493,12 @@ func (r *resourceMonitor) Stop() { } } +func (r *resourceMonitor) Reset() { + for _, collector := range r.collectors { + collector.Reset() + } +} + func (r *resourceMonitor) LogLatest() { for _, collector := range r.collectors { collector.LogLatest()