func libcontainerToContainerStats(s *cgroups.Stats, mi *info.MachineInfo) *info.ContainerStats { ret := new(info.ContainerStats) ret.Timestamp = time.Now() 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) ret.Cpu.Usage.PerCpu = make([]uint64, n) ret.Cpu.Usage.Total = 0 for i := 0; i < n; i++ { ret.Cpu.Usage.PerCpu[i] = s.CpuStats.CpuUsage.PercpuUsage[i] ret.Cpu.Usage.Total += s.CpuStats.CpuUsage.PercpuUsage[i] } ret.Memory = new(info.MemoryStats) ret.Memory.Usage = s.MemoryStats.Usage if v, ok := s.MemoryStats.Stats["pgfault"]; ok { ret.Memory.ContainerData.Pgfault = v ret.Memory.HierarchicalData.Pgfault = v } if v, ok := s.MemoryStats.Stats["pgmajfault"]; ok { ret.Memory.ContainerData.Pgmajfault = v ret.Memory.HierarchicalData.Pgmajfault = v } return ret }
func buildTrace(cpu, mem []uint64, duration time.Duration) []*info.ContainerStats { if len(cpu) != len(mem) { panic("len(cpu) != len(mem)") } ret := make([]*info.ContainerStats, len(cpu)) currentTime := time.Now() var cpuTotalUsage uint64 = 0 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) stats.Cpu.Usage.Total = cpuTotalUsage stats.Cpu.Usage.User = stats.Cpu.Usage.Total stats.Cpu.Usage.System = 0 stats.Cpu.Usage.PerCpu = []uint64{cpuUsage} stats.Memory.Usage = mem[i] ret[i] = stats } return ret }
func GenerateRandomStats(numStats, numCores int, duration time.Duration) []*info.ContainerStats { ret := make([]*info.ContainerStats, numStats) perCoreUsages := make([]uint64, numCores) currentTime := time.Now() for i := range perCoreUsages { perCoreUsages[i] = uint64(rand.Int63n(1000)) } 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) percore := make([]uint64, numCores) for i := range perCoreUsages { perCoreUsages[i] += uint64(rand.Int63n(1000)) percore[i] = perCoreUsages[i] stats.Cpu.Usage.Total += percore[i] } stats.Cpu.Usage.PerCpu = percore stats.Cpu.Usage.User = stats.Cpu.Usage.Total stats.Cpu.Usage.System = 0 stats.Memory.Usage = uint64(rand.Int63n(4096)) ret[i] = stats } return ret }
func (self *randomStatsContainer) GetStats() (*info.ContainerStats, error) { stats := new(info.ContainerStats) stats.Cpu = new(info.CpuStats) stats.Memory = new(info.MemoryStats) stats.Memory.Usage = uint64(rand.Intn(2048)) return stats, nil }
func (self *replayTrace) GetStats() (*info.ContainerStats, error) { stats := new(info.ContainerStats) stats.Cpu = new(info.CpuStats) stats.Memory = new(info.MemoryStats) if len(self.memTrace) > 0 { stats.Memory.Usage = self.memTrace[0] self.memTrace = self.memTrace[1:] } self.lock.Lock() defer self.lock.Unlock() cpuTrace := self.totalUsage if len(self.cpuTrace) > 0 { cpuTrace += self.cpuTrace[0] self.cpuTrace = self.cpuTrace[1:] } self.totalUsage = cpuTrace stats.Timestamp = self.currenttime self.currenttime = self.currenttime.Add(self.duration) stats.Cpu.Usage.Total = cpuTrace stats.Cpu.Usage.PerCpu = []uint64{cpuTrace} stats.Cpu.Usage.User = cpuTrace stats.Cpu.Usage.System = 0 return stats, nil }
func protobufToContainerStats(pstats *ContainerStats) *info.ContainerStats { ret := new(info.ContainerStats) if pstats.GetCpu() != nil { pcpu := pstats.GetCpu() cpustats := new(info.CpuStats) cpustats.Usage.Total = pcpu.GetUsage().GetTotal() percpu := pcpu.GetUsage().GetPerCpu() if len(percpu) > 0 { cpustats.Usage.PerCpu = make([]uint64, len(percpu)) for i, p := range percpu { cpustats.Usage.PerCpu[i] = uint64(p) } } cpustats.Usage.User = uint64(pcpu.GetUsage().GetUser()) cpustats.Usage.System = uint64(pcpu.GetUsage().GetSystem()) cpustats.Load = pcpu.GetLoad() ret.Cpu = cpustats } if pstats.GetMemory() != nil { pmem := pstats.GetMemory() memstats := new(info.MemoryStats) memstats.Limit = uint64(pmem.GetLimit()) memstats.Usage = uint64(pmem.GetUsage()) protobufToMemoryData(pmem.GetContainerData(), &memstats.ContainerData) protobufToMemoryData(pmem.GetHierarchicalData(), &memstats.HierarchicalData) ret.Memory = memstats } return ret }
// Convert libcontainer stats to info.ContainerStats. func toContainerStats(libcontainerStats *libcontainer.ContainerStats) *info.ContainerStats { s := libcontainerStats.CgroupStats ret := new(info.ContainerStats) 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) ret.Cpu.Usage.PerCpu = make([]uint64, n) ret.Cpu.Usage.Total = 0 for i := 0; i < n; i++ { ret.Cpu.Usage.PerCpu[i] = s.CpuStats.CpuUsage.PercpuUsage[i] ret.Cpu.Usage.Total += s.CpuStats.CpuUsage.PercpuUsage[i] } ret.Memory = new(info.MemoryStats) ret.Memory.Usage = s.MemoryStats.Usage if v, ok := s.MemoryStats.Stats["pgfault"]; ok { ret.Memory.ContainerData.Pgfault = v ret.Memory.HierarchicalData.Pgfault = v } if v, ok := s.MemoryStats.Stats["pgmajfault"]; ok { ret.Memory.ContainerData.Pgmajfault = v ret.Memory.HierarchicalData.Pgmajfault = v } if v, ok := s.MemoryStats.Stats["total_inactive_anon"]; ok { ret.Memory.WorkingSet = ret.Memory.Usage - v if v, ok := s.MemoryStats.Stats["total_active_file"]; ok { ret.Memory.WorkingSet -= v } } } // TODO(vishh): Perform a deep copy or alias libcontainer network stats. if libcontainerStats.NetworkStats != nil { ret.Network = (*info.NetworkStats)(libcontainerStats.NetworkStats) } return ret }