diff --git a/container/libcontainer/helpers.go b/container/libcontainer/helpers.go index c0fcbaac..2d28e6f1 100644 --- a/container/libcontainer/helpers.go +++ b/container/libcontainer/helpers.go @@ -95,7 +95,6 @@ func toContainerStats(libcontainerStats *libcontainer.ContainerStats) *info.Cont ret.Timestamp = time.Now() if s != nil { - ret.Cpu = new(info.CpuStats) ret.Cpu.Usage.User = s.CpuStats.CpuUsage.UsageInUsermode ret.Cpu.Usage.System = s.CpuStats.CpuUsage.UsageInKernelmode n := len(s.CpuStats.CpuUsage.PercpuUsage) @@ -116,7 +115,6 @@ func toContainerStats(libcontainerStats *libcontainer.ContainerStats) *info.Cont ret.DiskIo.IoMerged = DiskStatsCopy(s.BlkioStats.IoMergedRecursive) ret.DiskIo.IoTime = DiskStatsCopy(s.BlkioStats.IoTimeRecursive) - ret.Memory = new(info.MemoryStats) ret.Memory.Usage = s.MemoryStats.Usage if v, ok := s.MemoryStats.Stats["pgfault"]; ok { ret.Memory.ContainerData.Pgfault = v @@ -135,7 +133,7 @@ func toContainerStats(libcontainerStats *libcontainer.ContainerStats) *info.Cont } // TODO(vishh): Perform a deep copy or alias libcontainer network stats. if libcontainerStats.NetworkStats != nil { - ret.Network = (*info.NetworkStats)(libcontainerStats.NetworkStats) + ret.Network = *(*info.NetworkStats)(libcontainerStats.NetworkStats) } return ret diff --git a/info/container.go b/info/container.go index 627e3b8c..115c74e9 100644 --- a/info/container.go +++ b/info/container.go @@ -302,51 +302,16 @@ type FsStats struct { type ContainerStats struct { // The time of this stat point. - Timestamp time.Time `json:"timestamp"` - Cpu *CpuStats `json:"cpu,omitempty"` - DiskIo DiskIoStats `json:"diskio,omitempty"` - Memory *MemoryStats `json:"memory,omitempty"` - Network *NetworkStats `json:"network,omitempty"` + Timestamp time.Time `json:"timestamp"` + Cpu CpuStats `json:"cpu,omitempty"` + DiskIo DiskIoStats `json:"diskio,omitempty"` + Memory MemoryStats `json:"memory,omitempty"` + Network NetworkStats `json:"network,omitempty"` + // Filesystem statistics Filesystem []FsStats `json:"filesystem,omitempty"` } -// Makes a deep copy of the ContainerStats and returns a pointer to the new -// copy. Copy() will allocate a new ContainerStats object if dst is nil. -func (self *ContainerStats) Copy(dst *ContainerStats) *ContainerStats { - if dst == nil { - dst = new(ContainerStats) - } - dst.Timestamp = self.Timestamp - if self.Cpu != nil { - if dst.Cpu == nil { - dst.Cpu = new(CpuStats) - } - // To make a deep copy of a slice, we need to copy every value - // in the slice. To make less memory allocation, we would like - // to reuse the slice in dst if possible. - percpu := dst.Cpu.Usage.PerCpu - if len(percpu) != len(self.Cpu.Usage.PerCpu) { - percpu = make([]uint64, len(self.Cpu.Usage.PerCpu)) - } - dst.Cpu.Usage = self.Cpu.Usage - dst.Cpu.Load = self.Cpu.Load - copy(percpu, self.Cpu.Usage.PerCpu) - dst.Cpu.Usage.PerCpu = percpu - } else { - dst.Cpu = nil - } - if self.Memory != nil { - if dst.Memory == nil { - dst.Memory = new(MemoryStats) - } - *dst.Memory = *self.Memory - } else { - dst.Memory = nil - } - return dst -} - func timeEq(t1, t2 time.Time, tolerance time.Duration) bool { // t1 should not be later than t2 if t1.After(t2) { @@ -386,12 +351,22 @@ func (a *ContainerStats) Eq(b *ContainerStats) bool { // Checks equality of the stats values. func (a *ContainerStats) StatsEq(b *ContainerStats) bool { + // TODO(vmarmol): Consider using this through reflection. if !reflect.DeepEqual(a.Cpu, b.Cpu) { return false } if !reflect.DeepEqual(a.Memory, b.Memory) { return false } + if !reflect.DeepEqual(a.DiskIo, b.DiskIo) { + return false + } + if !reflect.DeepEqual(a.Network, b.Network) { + return false + } + if !reflect.DeepEqual(a.Filesystem, b.Filesystem) { + return false + } return true } diff --git a/info/container_test.go b/info/container_test.go index bd730c19..2ff38e69 100644 --- a/info/container_test.go +++ b/info/container_test.go @@ -15,7 +15,6 @@ package info import ( - "reflect" "testing" "time" ) @@ -69,10 +68,7 @@ func TestStatsEndTime(t *testing.T) { } func createStats(cpuUsage, memUsage uint64, timestamp time.Time) *ContainerStats { - stats := &ContainerStats{ - Cpu: &CpuStats{}, - Memory: &MemoryStats{}, - } + stats := &ContainerStats{} stats.Cpu.Usage.PerCpu = []uint64{cpuUsage} stats.Cpu.Usage.Total = cpuUsage stats.Cpu.Usage.System = 0 @@ -81,21 +77,3 @@ func createStats(cpuUsage, memUsage uint64, timestamp time.Time) *ContainerStats stats.Timestamp = timestamp return stats } - -func TestContainerStatsCopy(t *testing.T) { - stats := createStats(100, 101, time.Now()) - shadowStats := stats.Copy(nil) - if !reflect.DeepEqual(stats, shadowStats) { - t.Errorf("Copy() returned different object") - } - stats.Cpu.Usage.PerCpu[0] = shadowStats.Cpu.Usage.PerCpu[0] + 1 - stats.Cpu.Load = shadowStats.Cpu.Load + 1 - stats.Memory.Usage = shadowStats.Memory.Usage + 1 - if reflect.DeepEqual(stats, shadowStats) { - t.Errorf("Copy() did not deeply copy the object") - } - stats = shadowStats.Copy(stats) - if !reflect.DeepEqual(stats, shadowStats) { - t.Errorf("Copy() returned different object") - } -} diff --git a/info/test/datagen.go b/info/test/datagen.go index bb0b2ff4..519e28c1 100644 --- a/info/test/datagen.go +++ b/info/test/datagen.go @@ -31,8 +31,6 @@ func GenerateRandomStats(numStats, numCores int, duration time.Duration) []*info } for i := 0; i < numStats; i++ { stats := new(info.ContainerStats) - stats.Cpu = new(info.CpuStats) - stats.Memory = new(info.MemoryStats) stats.Timestamp = currentTime currentTime = currentTime.Add(duration) diff --git a/manager/container.go b/manager/container.go index 856b6447..9e911b79 100644 --- a/manager/container.go +++ b/manager/container.go @@ -171,15 +171,8 @@ func (c *containerData) housekeeping() { } else if len(stats) < 2 { // Ignore, not enough stats yet. } else { - usageCpuNs := uint64(0) - usageMemory := uint64(0) - - if stats[0].Cpu != nil && stats[1].Cpu != nil { - usageCpuNs = stats[1].Cpu.Usage.Total - stats[0].Cpu.Usage.Total - } - if stats[1].Memory != nil { - usageMemory = stats[1].Memory.Usage - } + usageCpuNs := stats[1].Cpu.Usage.Total - stats[0].Cpu.Usage.Total + usageMemory := stats[1].Memory.Usage usageInCores := float64(usageCpuNs) / float64(stats[1].Timestamp.Sub(stats[0].Timestamp).Nanoseconds()) usageInHuman := units.HumanSize(int64(usageMemory)) diff --git a/storage/bigquery/bigquery.go b/storage/bigquery/bigquery.go index 7d344391..4f8eb032 100644 --- a/storage/bigquery/bigquery.go +++ b/storage/bigquery/bigquery.go @@ -223,13 +223,11 @@ func (self *bigqueryStorage) containerStatsToRows( // hierarchical major page fault row[colMemoryHierarchicalPgmajfault] = stats.Memory.HierarchicalData.Pgmajfault - // Optional: Network stats. - if stats.Network != nil { - row[colRxBytes] = stats.Network.RxBytes - row[colRxErrors] = stats.Network.RxErrors - row[colTxBytes] = stats.Network.TxBytes - row[colTxErrors] = stats.Network.TxErrors - } + // Network stats. + row[colRxBytes] = stats.Network.RxBytes + row[colRxErrors] = stats.Network.RxErrors + row[colTxBytes] = stats.Network.TxBytes + row[colTxErrors] = stats.Network.TxErrors // TODO(jnagal): Handle per-cpu stats. @@ -288,9 +286,6 @@ func convertToUint64(v interface{}) (uint64, error) { func (self *bigqueryStorage) valuesToContainerStats(columns []string, values []interface{}) (*info.ContainerStats, error) { stats := &info.ContainerStats{ - Cpu: &info.CpuStats{}, - Memory: &info.MemoryStats{}, - Network: &info.NetworkStats{}, Filesystem: make([]info.FsStats, 0), } var err error @@ -383,7 +378,7 @@ func (self *bigqueryStorage) valuesToContainerStats(columns []string, values []i } func (self *bigqueryStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error { - if stats == nil || stats.Cpu == nil || stats.Memory == nil { + if stats == nil { return nil } rows := make([]map[string]interface{}, 0) diff --git a/storage/influxdb/influxdb.go b/storage/influxdb/influxdb.go index 0374032d..b4697d6d 100644 --- a/storage/influxdb/influxdb.go +++ b/storage/influxdb/influxdb.go @@ -123,20 +123,18 @@ func (self *influxdbStorage) containerStatsToValues( columns = append(columns, colMemoryWorkingSet) values = append(values, stats.Memory.WorkingSet) - // Optional: Network stats. - if stats.Network != nil { - columns = append(columns, colRxBytes) - values = append(values, stats.Network.RxBytes) + // Network stats. + columns = append(columns, colRxBytes) + values = append(values, stats.Network.RxBytes) - columns = append(columns, colRxErrors) - values = append(values, stats.Network.RxErrors) + columns = append(columns, colRxErrors) + values = append(values, stats.Network.RxErrors) - columns = append(columns, colTxBytes) - values = append(values, stats.Network.TxBytes) + columns = append(columns, colTxBytes) + values = append(values, stats.Network.TxBytes) - columns = append(columns, colTxErrors) - values = append(values, stats.Network.TxErrors) - } + columns = append(columns, colTxErrors) + values = append(values, stats.Network.TxErrors) return columns, values } @@ -176,9 +174,6 @@ func convertToUint64(v interface{}) (uint64, error) { func (self *influxdbStorage) valuesToContainerStats(columns []string, values []interface{}) (*info.ContainerStats, error) { stats := &info.ContainerStats{ - Cpu: &info.CpuStats{}, - Memory: &info.MemoryStats{}, - Network: &info.NetworkStats{}, Filesystem: make([]info.FsStats, 0), } var err error @@ -261,7 +256,7 @@ func (self *influxdbStorage) defaultReadyToFlush() bool { } func (self *influxdbStorage) AddStats(ref info.ContainerReference, stats *info.ContainerStats) error { - if stats == nil || stats.Cpu == nil || stats.Memory == nil { + if stats == nil { return nil } var seriesToFlush []*influxdb.Series diff --git a/storage/test/storagetests.go b/storage/test/storagetests.go index 128688a6..1b6ec67f 100644 --- a/storage/test/storagetests.go +++ b/storage/test/storagetests.go @@ -41,8 +41,6 @@ func buildTrace(cpu, mem []uint64, duration time.Duration) []*info.ContainerStat for i, cpuUsage := range cpu { cpuTotalUsage += cpuUsage stats := new(info.ContainerStats) - stats.Cpu = new(info.CpuStats) - stats.Memory = new(info.MemoryStats) stats.Timestamp = currentTime currentTime = currentTime.Add(duration) @@ -53,7 +51,6 @@ func buildTrace(cpu, mem []uint64, duration time.Duration) []*info.ContainerStat stats.Memory.Usage = mem[i] - stats.Network = new(info.NetworkStats) stats.Network.RxBytes = uint64(rand.Intn(10000)) stats.Network.RxErrors = uint64(rand.Intn(1000)) stats.Network.TxBytes = uint64(rand.Intn(100000))