runtime-rs: QEMU get_thread_ids() returns real vCPU's tids

The information is obtained through QMP query_cpus_fast.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu
2025-07-17 19:57:53 +08:00
parent acae4480ac
commit 79f29bc523
3 changed files with 65 additions and 8 deletions

View File

@@ -22,7 +22,6 @@ use kata_types::{
};
use persist::sandbox_persist::Persist;
use std::cmp::Ordering;
use std::collections::HashMap;
use std::convert::TryInto;
use std::path::Path;
use std::process::Stdio;
@@ -288,13 +287,14 @@ impl QemuInner {
todo!()
}
pub(crate) async fn get_thread_ids(&self) -> Result<VcpuThreadIds> {
pub(crate) async fn get_thread_ids(&mut self) -> Result<VcpuThreadIds> {
info!(sl!(), "QemuInner::get_thread_ids()");
//todo!()
let vcpu_thread_ids: VcpuThreadIds = VcpuThreadIds {
vcpus: HashMap::new(),
};
Ok(vcpu_thread_ids)
Ok(self
.qmp
.as_mut()
.and_then(|qmp| qmp.get_vcpu_thread_ids().ok())
.unwrap_or_default())
}
pub(crate) async fn get_vmm_master_tid(&self) -> Result<u32> {

View File

@@ -135,7 +135,7 @@ impl Hypervisor for Qemu {
}
async fn get_thread_ids(&self) -> Result<VcpuThreadIds> {
let inner = self.inner.read().await;
let mut inner = self.inner.write().await;
inner.get_thread_ids().await
}

View File

@@ -5,9 +5,11 @@
use crate::device::pci_path::PciPath;
use crate::qemu::cmdline_generator::{DeviceVirtioNet, Netdev};
use crate::VcpuThreadIds;
use anyhow::{anyhow, Context, Result};
use nix::sys::socket::{sendmsg, ControlMessage, MsgFlags};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt::{Debug, Error, Formatter};
use std::io::BufReader;
@@ -659,6 +661,61 @@ impl Qmp {
Ok(Some(pci_path))
}
/// Get vCPU thread IDs through QMP query_cpus_fast.
pub fn get_vcpu_thread_ids(&mut self) -> Result<VcpuThreadIds> {
let vcpu_info = self
.qmp
.execute(&qmp::query_cpus_fast {})
.map_err(|e| anyhow!("query_cpus_fast failed: {:?}", e))?;
let vcpus: HashMap<u32, u32> = vcpu_info
.iter()
.map(|info| match info {
qmp::CpuInfoFast::aarch64(cpu_info)
| qmp::CpuInfoFast::alpha(cpu_info)
| qmp::CpuInfoFast::arm(cpu_info)
| qmp::CpuInfoFast::avr(cpu_info)
| qmp::CpuInfoFast::cris(cpu_info)
| qmp::CpuInfoFast::hppa(cpu_info)
| qmp::CpuInfoFast::i386(cpu_info)
| qmp::CpuInfoFast::loongarch64(cpu_info)
| qmp::CpuInfoFast::m68k(cpu_info)
| qmp::CpuInfoFast::microblaze(cpu_info)
| qmp::CpuInfoFast::microblazeel(cpu_info)
| qmp::CpuInfoFast::mips(cpu_info)
| qmp::CpuInfoFast::mips64(cpu_info)
| qmp::CpuInfoFast::mips64el(cpu_info)
| qmp::CpuInfoFast::mipsel(cpu_info)
| qmp::CpuInfoFast::nios2(cpu_info)
| qmp::CpuInfoFast::or1k(cpu_info)
| qmp::CpuInfoFast::ppc(cpu_info)
| qmp::CpuInfoFast::ppc64(cpu_info)
| qmp::CpuInfoFast::riscv32(cpu_info)
| qmp::CpuInfoFast::riscv64(cpu_info)
| qmp::CpuInfoFast::rx(cpu_info)
| qmp::CpuInfoFast::sh4(cpu_info)
| qmp::CpuInfoFast::sh4eb(cpu_info)
| qmp::CpuInfoFast::sparc(cpu_info)
| qmp::CpuInfoFast::sparc64(cpu_info)
| qmp::CpuInfoFast::tricore(cpu_info)
| qmp::CpuInfoFast::x86_64(cpu_info)
| qmp::CpuInfoFast::xtensa(cpu_info)
| qmp::CpuInfoFast::xtensaeb(cpu_info) => {
let vcpu_id = cpu_info.cpu_index as u32;
let thread_id = cpu_info.thread_id as u32;
(vcpu_id, thread_id)
}
qmp::CpuInfoFast::s390x { base, .. } => {
let vcpu_id = base.cpu_index as u32;
let thread_id = base.thread_id as u32;
(vcpu_id, thread_id)
}
})
.collect();
Ok(VcpuThreadIds { vcpus })
}
}
fn vcpu_id_from_core_id(core_id: i64) -> String {