From 6fda03ec9297a7dc822bed00fb6f15c3855da98f Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Wed, 27 Mar 2019 02:03:23 -0700 Subject: [PATCH] hypervisor: make getThreadIDs return vcpu to threadid mapping We need such mapping information to put vcpus in container cpuset properly. Fixes: #1435 Signed-off-by: Peng Tao --- virtcontainers/cgroups.go | 2 +- virtcontainers/fc.go | 4 ++-- virtcontainers/hypervisor.go | 7 ++++--- virtcontainers/mock_hypervisor.go | 6 +++--- virtcontainers/qemu.go | 13 +++++++------ 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/virtcontainers/cgroups.go b/virtcontainers/cgroups.go index 728e96c4b6..22f4316a71 100644 --- a/virtcontainers/cgroups.go +++ b/virtcontainers/cgroups.go @@ -227,7 +227,7 @@ func (s *Sandbox) constrainHypervisor(cgroup cgroups.Cgroup) error { if err != nil { return fmt.Errorf("failed to get thread ids from hypervisor: %v", err) } - if tids == nil || len(tids.vcpus) == 0 { + if len(tids.vcpus) == 0 { // If there's no tid returned from the hypervisor, this is not // a bug. It simply means there is nothing to constrain, hence // let's return without any error from here. diff --git a/virtcontainers/fc.go b/virtcontainers/fc.go index 75eb56a91a..461b6a480f 100644 --- a/virtcontainers/fc.go +++ b/virtcontainers/fc.go @@ -700,10 +700,10 @@ func (fc *firecracker) resizeVCPUs(reqVCPUs uint32) (currentVCPUs uint32, newVCP // Need to see if there's an easy way to ask firecracker for thread ids associated with // the vCPUs. Issue opened to ask for per vCPU thread IDs: // https://github.com/firecracker-microvm/firecracker/issues/718 -func (fc *firecracker) getThreadIDs() (*threadIDs, error) { +func (fc *firecracker) getThreadIDs() (vcpuThreadIDs, error) { //TODO: this may not be exactly supported in Firecracker. Closest is cpu-template as part // of get /machine-config - return nil, nil + return vcpuThreadIDs{}, nil } func (fc *firecracker) cleanup() error { diff --git a/virtcontainers/hypervisor.go b/virtcontainers/hypervisor.go index 24c56debf5..1157d42144 100644 --- a/virtcontainers/hypervisor.go +++ b/virtcontainers/hypervisor.go @@ -279,8 +279,9 @@ type HypervisorConfig struct { GuestHookPath string } -type threadIDs struct { - vcpus []int +// vcpu mapping from vcpu number to thread number +type vcpuThreadIDs struct { + vcpus map[int]int } func (conf *HypervisorConfig) checkTemplateConfig() error { @@ -597,7 +598,7 @@ type hypervisor interface { disconnect() capabilities() types.Capabilities hypervisorConfig() HypervisorConfig - getThreadIDs() (*threadIDs, error) + getThreadIDs() (vcpuThreadIDs, error) cleanup() error pid() int fromGrpc(ctx context.Context, hypervisorConfig *HypervisorConfig, store *store.VCStore, j []byte) error diff --git a/virtcontainers/mock_hypervisor.go b/virtcontainers/mock_hypervisor.go index b1efa6f670..780ae566fc 100644 --- a/virtcontainers/mock_hypervisor.go +++ b/virtcontainers/mock_hypervisor.go @@ -94,9 +94,9 @@ func (m *mockHypervisor) resizeVCPUs(cpus uint32) (uint32, uint32, error) { func (m *mockHypervisor) disconnect() { } -func (m *mockHypervisor) getThreadIDs() (*threadIDs, error) { - vcpus := []int{os.Getpid()} - return &threadIDs{vcpus}, nil +func (m *mockHypervisor) getThreadIDs() (vcpuThreadIDs, error) { + vcpus := map[int]int{0: os.Getpid()} + return vcpuThreadIDs{vcpus}, nil } func (m *mockHypervisor) cleanup() error { diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index 3280b04845..41baff4807 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -1509,28 +1509,29 @@ func genericMemoryTopology(memoryMb, hostMemoryMb uint64, slots uint8, memoryOff return memory } -func (q *qemu) getThreadIDs() (*threadIDs, error) { +func (q *qemu) getThreadIDs() (vcpuThreadIDs, error) { span, _ := q.trace("getThreadIDs") defer span.Finish() + tid := vcpuThreadIDs{} err := q.qmpSetup() if err != nil { - return nil, err + return tid, err } cpuInfos, err := q.qmpMonitorCh.qmp.ExecQueryCpus(q.qmpMonitorCh.ctx) if err != nil { q.Logger().WithError(err).Error("failed to query cpu infos") - return nil, err + return tid, err } - var tid threadIDs + tid.vcpus = make(map[int]int, len(cpuInfos)) for _, i := range cpuInfos { if i.ThreadID > 0 { - tid.vcpus = append(tid.vcpus, i.ThreadID) + tid.vcpus[i.CPU] = i.ThreadID } } - return &tid, nil + return tid, nil } func calcHotplugMemMiBSize(mem uint32, memorySectionSizeMB uint32) (uint32, error) {