cloud-hypervisor: Fix GetThreadIDs function

Get vcpu thread-ids by reading cloud-hypervisor process tasks information.

Fixes: #5568

Signed-off-by: Guanglu Guo <guoguanglu@qiyi.com>
(cherry picked from commit daeee26a1e)
This commit is contained in:
Guanglu Guo 2022-11-03 11:59:54 +08:00 committed by Fabiano Fidêncio
parent 9141acd94c
commit 8fbf862fa6

View File

@ -21,6 +21,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@ -721,6 +722,55 @@ func (clh *cloudHypervisor) GetThreadIDs(ctx context.Context) (VcpuThreadIDs, er
vcpuInfo.vcpus = make(map[int]int) vcpuInfo.vcpus = make(map[int]int)
getVcpus := func(pid int) (map[int]int, error) {
vcpus := make(map[int]int)
dir := fmt.Sprintf("/proc/%d/task", pid)
files, err := os.ReadDir(dir)
if err != nil {
return vcpus, err
}
pattern, err := regexp.Compile(`^vcpu\d+$`)
if err != nil {
return vcpus, err
}
for _, file := range files {
comm, err := os.ReadFile(fmt.Sprintf("%s/%s/comm", dir, file.Name()))
if err != nil {
return vcpus, err
}
pName := strings.TrimSpace(string(comm))
if !pattern.MatchString(pName) {
continue
}
cpuID := strings.TrimPrefix(pName, "vcpu")
threadID := file.Name()
k, err := strconv.Atoi(cpuID)
if err != nil {
return vcpus, err
}
v, err := strconv.Atoi(threadID)
if err != nil {
return vcpus, err
}
vcpus[k] = v
}
return vcpus, nil
}
if clh.state.PID == 0 {
return vcpuInfo, nil
}
vcpus, err := getVcpus(clh.state.PID)
if err != nil {
return vcpuInfo, err
}
vcpuInfo.vcpus = vcpus
return vcpuInfo, nil return vcpuInfo, nil
} }