usge container stats sample
This commit is contained in:
parent
0625caffa3
commit
205a379887
@ -37,6 +37,28 @@ func (self *statsSummaryContainerHandlerWrapper) GetSpec() (*info.ContainerSpec,
|
||||
return self.handler.GetSpec()
|
||||
}
|
||||
|
||||
func (self *statsSummaryContainerHandlerWrapper) updatePrevStats(stats *info.ContainerStats) {
|
||||
if stats == nil || stats.Cpu == nil || stats.Memory == nil {
|
||||
// discard incomplete stats
|
||||
self.prevStats = nil
|
||||
return
|
||||
}
|
||||
if self.prevStats == nil {
|
||||
self.prevStats = &info.ContainerStats{
|
||||
Cpu: &info.CpuStats{},
|
||||
Memory: &info.MemoryStats{},
|
||||
}
|
||||
}
|
||||
// make a deep copy.
|
||||
self.prevStats.Timestamp = stats.Timestamp
|
||||
*self.prevStats.Cpu = *stats.Cpu
|
||||
self.prevStats.Cpu.Usage.PerCpu = make([]uint64, len(stats.Cpu.Usage.PerCpu))
|
||||
for i, perCpu := range stats.Cpu.Usage.PerCpu {
|
||||
self.prevStats.Cpu.Usage.PerCpu[i] = perCpu
|
||||
}
|
||||
*self.prevStats.Memory = *stats.Memory
|
||||
}
|
||||
|
||||
func (self *statsSummaryContainerHandlerWrapper) GetStats() (*info.ContainerStats, error) {
|
||||
stats, err := self.handler.GetStats()
|
||||
if err != nil {
|
||||
@ -49,7 +71,11 @@ func (self *statsSummaryContainerHandlerWrapper) GetStats() (*info.ContainerStat
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
|
||||
self.sampler.Update(stats)
|
||||
sample, err := info.NewSample(self.prevStats, stats)
|
||||
if err == nil && sample != nil {
|
||||
self.sampler.Update(sample)
|
||||
}
|
||||
self.updatePrevStats(stats)
|
||||
if self.currentSummary == nil {
|
||||
self.currentSummary = new(info.ContainerStatsSummary)
|
||||
}
|
||||
@ -87,9 +113,9 @@ func (self *statsSummaryContainerHandlerWrapper) ListProcesses(listType ListType
|
||||
func (self *statsSummaryContainerHandlerWrapper) StatsSummary() (*info.ContainerStatsSummary, error) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
samples := make([]*info.ContainerStats, 0, self.sampler.Len())
|
||||
samples := make([]*info.ContainerStatsSample, 0, self.sampler.Len())
|
||||
self.sampler.Map(func(d interface{}) {
|
||||
stats := d.(*info.ContainerStats)
|
||||
stats := d.(*info.ContainerStatsSample)
|
||||
samples = append(samples, stats)
|
||||
})
|
||||
self.currentSummary.Samples = samples
|
||||
|
@ -31,15 +31,15 @@ func init() {
|
||||
rand.Seed(seed)
|
||||
}
|
||||
|
||||
type randomStatsContainer struct {
|
||||
type randomMemoryUsageContainer struct {
|
||||
NoStatsSummary
|
||||
}
|
||||
|
||||
func (self *randomStatsContainer) GetSpec() (*info.ContainerSpec, error) {
|
||||
func (self *randomMemoryUsageContainer) GetSpec() (*info.ContainerSpec, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *randomStatsContainer) GetStats() (*info.ContainerStats, error) {
|
||||
func (self *randomMemoryUsageContainer) GetStats() (*info.ContainerStats, error) {
|
||||
stats := new(info.ContainerStats)
|
||||
stats.Cpu = new(info.CpuStats)
|
||||
stats.Memory = new(info.MemoryStats)
|
||||
@ -47,21 +47,21 @@ func (self *randomStatsContainer) GetStats() (*info.ContainerStats, error) {
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
func (self *randomStatsContainer) ListContainers(listType ListType) ([]string, error) {
|
||||
func (self *randomMemoryUsageContainer) ListContainers(listType ListType) ([]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *randomStatsContainer) ListThreads(listType ListType) ([]int, error) {
|
||||
func (self *randomMemoryUsageContainer) ListThreads(listType ListType) ([]int, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *randomStatsContainer) ListProcesses(listType ListType) ([]int, error) {
|
||||
func (self *randomMemoryUsageContainer) ListProcesses(listType ListType) ([]int, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func TestAvgMaxMemoryUsage(t *testing.T) {
|
||||
handler, err := AddStatsSummary(
|
||||
&randomStatsContainer{},
|
||||
&randomMemoryUsageContainer{},
|
||||
&StatsParameter{
|
||||
Sampler: "uniform",
|
||||
NumSamples: 10,
|
||||
|
@ -156,7 +156,8 @@ type ContainerStats struct {
|
||||
}
|
||||
|
||||
type ContainerStatsSample struct {
|
||||
Cpu struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Cpu struct {
|
||||
// number of nanoseconds of CPU time used by the container
|
||||
// within one second.
|
||||
Usage uint64 `json:"usage"`
|
||||
@ -204,6 +205,7 @@ func NewSample(prev, current *ContainerStats) (*ContainerStatsSample, error) {
|
||||
sample.Cpu.Usage = current.Cpu.Usage.Total - prev.Cpu.Usage.Total
|
||||
// Memory usage is current memory usage
|
||||
sample.Memory.Usage = current.Memory.Usage
|
||||
sample.Timestamp = current.Timestamp
|
||||
|
||||
return sample, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user