I am trying to get the information of vcpus running on my machine and for the same I am using libvirt. I am not able to understand how to use the api virDomainGetVcpus which has arguments cpumaps and maplen.
I am using C. Please let me know if you have some insight.
Thanks.
You need to use virDomainGetInfo and virNodeGetInfo to guest the number of guest CPUs and number of host CPUs. Then you can allocate a map of the right size. This code would do the trick:
virNodeInfo nodeinfo;
virDomainInfo dominfo;
int nhostcpus;
if (virNodeGetInfo(conn, &nodeinfo) < 0)
return -1;
nhostcpus = VIR_NODEINFO_MAXCPUS(nodeinfo);
if (virDomainGetInfo(dom, &dominfo) != 0)
return -1;
cpuinfo = malloc(sizeof(virVcpuInfo)*dominfo.nrVirtCpu);
cpumaplen = VIR_CPU_MAPLEN(nhostcpu);
cpumaps = vshMalloc(ctl, dominfo.nrVirtCpu * cpumaplen);
if ((ncpus = virDomainGetVcpus(dom,
cpuinfo, dominfo.nrVirtCpu,
cpumaps, cpumaplen)) < 0)
return -1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With