cpu hot plugging

This commit is contained in:
Nan Deng 2014-06-24 16:01:13 -07:00
parent ecf2346443
commit 900a3f76c8
2 changed files with 59 additions and 12 deletions

View File

@ -119,7 +119,7 @@ type CpuStats struct {
// Per CPU/core usage of the container. // Per CPU/core usage of the container.
// Unit: nanoseconds. // Unit: nanoseconds.
PerCpu []uint64 `json:"per_cpu,omitempty"` PerCpu []uint64 `json:"per_cpu_usage,omitempty"`
// Time spent in user space. // Time spent in user space.
// Unit: nanoseconds // Unit: nanoseconds
@ -175,7 +175,7 @@ type ContainerStatsSample struct {
Usage uint64 `json:"usage"` Usage uint64 `json:"usage"`
// Per-core usage of the container. (unit: nanoseconds) // Per-core usage of the container. (unit: nanoseconds)
PerCpu []uint64 `json:"per_cpu,omitempty"` PerCpuUsage []uint64 `json:"per_cpu_usage,omitempty"`
} `json:"cpu"` } `json:"cpu"`
Memory struct { Memory struct {
// Units: Bytes. // Units: Bytes.
@ -218,24 +218,24 @@ func NewSample(prev, current *ContainerStats) (*ContainerStatsSample, error) {
var percpu []uint64 var percpu []uint64
if len(current.Cpu.Usage.PerCpu) > 0 { if len(current.Cpu.Usage.PerCpu) > 0 {
if len(current.Cpu.Usage.PerCpu) != len(prev.Cpu.Usage.PerCpu) { curNumCpus := len(current.Cpu.Usage.PerCpu)
return nil, fmt.Errorf("current number of cores is %v; but there are %v cores in previous stats", percpu = make([]uint64, curNumCpus)
len(current.Cpu.Usage.PerCpu), len(prev.Cpu.Usage.PerCpu))
}
percpu = make([]uint64, len(current.Cpu.Usage.PerCpu))
for i, curUsage := range current.Cpu.Usage.PerCpu { for i, currUsage := range current.Cpu.Usage.PerCpu {
prevUsage := prev.Cpu.Usage.PerCpu[i] var prevUsage uint64 = 0
if curUsage < prevUsage { if i < len(prev.Cpu.Usage.PerCpu) {
prevUsage = prev.Cpu.Usage.PerCpu[i]
}
if currUsage < prevUsage {
return nil, fmt.Errorf("current per-core CPU usage is less than prev per-core CPU usage (cumulative).") return nil, fmt.Errorf("current per-core CPU usage is less than prev per-core CPU usage (cumulative).")
} }
percpu[i] = curUsage - prevUsage percpu[i] = currUsage - prevUsage
} }
} }
sample := new(ContainerStatsSample) sample := new(ContainerStatsSample)
// Calculate the diff to get the CPU usage within the time interval. // Calculate the diff to get the CPU usage within the time interval.
sample.Cpu.Usage = current.Cpu.Usage.Total - prev.Cpu.Usage.Total sample.Cpu.Usage = current.Cpu.Usage.Total - prev.Cpu.Usage.Total
sample.Cpu.PerCpu = percpu sample.Cpu.PerCpuUsage = percpu
// Memory usage is current memory usage // Memory usage is current memory usage
sample.Memory.Usage = current.Memory.Usage sample.Memory.Usage = current.Memory.Usage
sample.Timestamp = current.Timestamp sample.Timestamp = current.Timestamp

View File

@ -230,3 +230,50 @@ func TestAddSampleWrongCpuUsage(t *testing.T) {
t.Errorf("generated an unexpected sample: %+v", sample) t.Errorf("generated an unexpected sample: %+v", sample)
} }
} }
func TestAddSampleHotPluggingCpu(t *testing.T) {
cpuPrevUsage := uint64(10)
cpuCurrentUsage := uint64(15)
memCurrentUsage := uint64(200)
prevTime := time.Now()
prev := createStats(cpuPrevUsage, memCurrentUsage, prevTime)
current := createStats(cpuCurrentUsage, memCurrentUsage, prevTime.Add(1*time.Second))
current.Cpu.Usage.PerCpu = append(current.Cpu.Usage.PerCpu, 10)
sample, err := NewSample(prev, current)
if err != nil {
t.Errorf("should be able to generate a sample. but received error: %v", err)
}
if len(sample.Cpu.PerCpuUsage) != 2 {
t.Fatalf("Should have 2 cores.")
}
if sample.Cpu.PerCpuUsage[0] != cpuCurrentUsage-cpuPrevUsage {
t.Errorf("First cpu usage is %v. should be %v", sample.Cpu.PerCpuUsage[0], cpuCurrentUsage-cpuPrevUsage)
}
if sample.Cpu.PerCpuUsage[1] != 10 {
t.Errorf("Second cpu usage is %v. should be 10", sample.Cpu.PerCpuUsage[1])
}
}
func TestAddSampleHotUnpluggingCpu(t *testing.T) {
cpuPrevUsage := uint64(10)
cpuCurrentUsage := uint64(15)
memCurrentUsage := uint64(200)
prevTime := time.Now()
prev := createStats(cpuPrevUsage, memCurrentUsage, prevTime)
current := createStats(cpuCurrentUsage, memCurrentUsage, prevTime.Add(1*time.Second))
prev.Cpu.Usage.PerCpu = append(prev.Cpu.Usage.PerCpu, 10)
sample, err := NewSample(prev, current)
if err != nil {
t.Errorf("should be able to generate a sample. but received error: %v", err)
}
if len(sample.Cpu.PerCpuUsage) != 1 {
t.Fatalf("Should have 1 cores.")
}
if sample.Cpu.PerCpuUsage[0] != cpuCurrentUsage-cpuPrevUsage {
t.Errorf("First cpu usage is %v. should be %v", sample.Cpu.PerCpuUsage[0], cpuCurrentUsage-cpuPrevUsage)
}
}