[libvirt] Libvirt's DomainGetInfo Function Development

I am trying to get stats of Domains running under XEN hypervisor. I used Libvirt function for this purpose. I am trying to understand output for Memory. Domain running is Fedora-22 with 1GB Ram. Here is my code y = virDomainGetInfo(allDomain, &info); if (y == -1) printf("Errorl\n"); else { printf("Max Memory: %lu\n", info.maxMem); printf("Memory: %lu", info.memory); } I get output as *Max Memory: 1048576 Memory: 1048576* Now, as per Libvirt API <http://libvirt.org/html/libvirt-libvirt-domain.html#virDomainInfo>, info.memory should return the memory in KBytes used by the domain. My Question is, if info.memory returns memory used and my domain is using complete 100% of Max Memory or not? Also I tried to create Domain with memory varying from 128MB to 2GB with same effect. I also tried to run some applications in Domain but Memory usage return by given function does not change. Any help or guidance will be much appreciated. *Regards,* *Aleem Akhtar * *Research Assistant | HPC-Lab | SEECS* National University of Sciences & Technology Sector H-12, Islamabad Email: aleem.akhtar@seecs.nust.edu.pk <aleem.akhtar@seecs.nust.edu.pk> Website: aleemakhtar.com

On Thu, 2016-08-04 at 20:02 +0500, Aleem Akhtar wrote:
I am trying to get stats of Domains running under XEN hypervisor. I used Libvirt function for this purpose. I am trying to understand output for Memory. Domain running is Fedora-22 with 1GB Ram. Here is my code y = virDomainGetInfo(allDomain, &info); if (y == -1) printf("Errorl\n"); else { printf("Max Memory: %lu\n", info.maxMem); printf("Memory: %lu", info.memory); } I get output as Max Memory: 1048576 Memory: 1048576 Now, as per Libvirt API, info.memory should return the memory in KBytes used by the domain. My Question is, if info.memory returns memory used and my domain is using complete 100% of Max Memory or not? Also I tried to create Domain with memory varying from 128MB to 2GB with same effect. I also tried to run some applications in Domain but Memory usage return by given function does not change. Any help or guidance will be much appreciated.
The relevant virDomainInfo field is described as /* the memory in KBytes used by the domain */ unsigned long memory; but in this context, "used by" really means "available to". So the output above tells you that your Fedora guest has 1 GB of memory available; how much of that memory is actually being used by the guest OS is something that you can find out by running top inside the guest. -- Andrea Bolognani / Red Hat / Virtualization

Ok. So that's why I was getting 1 GB as a result. So is there any way I can get memory in used programmatically? I mean without going inside guest domain. On Aug 5, 2016 5:42 PM, "Andrea Bolognani" <abologna@redhat.com> wrote:
On Thu, 2016-08-04 at 20:02 +0500, Aleem Akhtar wrote:
I am trying to get stats of Domains running under XEN hypervisor. I used Libvirt function for this purpose. I am trying to understand output for Memory. Domain running is Fedora-22 with 1GB Ram. Here is my code
y = virDomainGetInfo(allDomain, &info); if (y == -1) printf("Errorl\n"); else { printf("Max Memory: %lu\n", info.maxMem); printf("Memory: %lu", info.memory); }
I get output as
Max Memory: 1048576 Memory: 1048576
Now, as per Libvirt API, info.memory should return the memory in KBytes used by the domain. My Question is, if info.memory returns memory used and my domain is using complete 100% of Max Memory or not? Also I tried to create Domain with memory varying from 128MB to 2GB with same effect. I also tried to run some applications in Domain but Memory usage return by given function does not change. Any help or guidance will be much appreciated.
The relevant virDomainInfo field is described as
/* the memory in KBytes used by the domain */ unsigned long memory;
but in this context, "used by" really means "available to".
So the output above tells you that your Fedora guest has 1 GB of memory available; how much of that memory is actually being used by the guest OS is something that you can find out by running top inside the guest.
-- Andrea Bolognani / Red Hat / Virtualization

Ok. So that's why I was getting 1 GB as a result. So is there any way I can get memory in used
On Fri, 2016-08-05 at 17:45 +0500, Aleem Akhtar wrote: programmatically?
I mean without going inside guest domain.
The only way I can come up with at the moment relies on qemu-guest-agent: $ virsh qemu-agent-command guest \ '{"execute": "guest-exec", "arguments": {"path": "/usr/bin/free", "capture-output": true}}' {"return":{"pid":1425}} $ virsh qemu-agent-command guest \ '{"execute": "guest-exec-status", "arguments": {"pid": 1425}}' {"return":{"exitcode":0,"out-data":"ICA...zIK","exited":true}} $ echo 'ICA...zIK' | base64 -d total used free Mem: 4020408 97244 3359560 Swap: 1048572 0 1048572 Of course you'll need to have qemu-guest-agent running inside the guest and process all the resulting information yourself. See the virDomainQemuAgentCommand() API. -- Andrea Bolognani / Red Hat / Virtualization

Thanks. But I am using XEN. And I also don't want to go inside guest. I mean I want to get memory usage at the same level I am getting max memory and total memory. I have another question. If Memory attribute of GetDomainInfo gives total memory I.e. memory in use plus remaining memory than what is the use of Max Memory? I also programmatically updated Memory using libvirt function I.e. 1GB to 512mb. When tried to get domain statistics again I got Max Memory 512mb and Memory 1GB. How is this possible? On Aug 5, 2016 6:36 PM, "Andrea Bolognani" <abologna@redhat.com> wrote:
Ok. So that's why I was getting 1 GB as a result. So is there any way I can get memory in used
On Fri, 2016-08-05 at 17:45 +0500, Aleem Akhtar wrote: programmatically?
I mean without going inside guest domain.
The only way I can come up with at the moment relies on qemu-guest-agent:
$ virsh qemu-agent-command guest \ '{"execute": "guest-exec", "arguments": {"path": "/usr/bin/free", "capture-output": true}}' {"return":{"pid":1425}}
$ virsh qemu-agent-command guest \ '{"execute": "guest-exec-status", "arguments": {"pid": 1425}}' {"return":{"exitcode":0,"out-data":"ICA...zIK","exited":true}}
$ echo 'ICA...zIK' | base64 -d total used free Mem: 4020408 97244 3359560 Swap: 1048572 0 1048572
Of course you'll need to have qemu-guest-agent running inside the guest and process all the resulting information yourself.
See the virDomainQemuAgentCommand() API.
-- Andrea Bolognani / Red Hat / Virtualization

On Fri, 2016-08-05 at 18:49 +0500, Aleem Akhtar wrote:
Thanks. But I am using XEN.
I see. qemu-guest-agent is definitely not an option then.
And I also don't want to go inside guest. I mean I want to get memory usage at the same level I am getting max memory and total memory.
The problem with that is that the host OS has zero knowledge of how the guest OS is using the memory it's been assigned.
I have another question. If Memory attribute of GetDomainInfo gives total memory I.e. memory in use plus remaining memory than what is the use of Max Memory?
You can configure your guest like <memory unit='KiB'>4194304</memory> <currentMemory unit='KiB'>2097152</currentMemory> Doing so would give you 2 GB of initial memory and the ability to increase that up to 4 GB at runtime. So in the virDomainInfo structure, maxMem corresponds to <memory> and memory corresponds to <currentMemory>. Confusing, I know :)
I also programmatically updated Memory using libvirt function I.e. 1GB to 512mb. When tried to get domain statistics again I got Max Memory 512mb and Memory 1GB. How is this possible?
You mean memory = 512 MB and maxMem = 1 GB, right? PS: This whole thread would have been more suitable for the libvirt-users mailing list - libvir-devel is targeted at the development of libvirt itself. Not a big deal, but maybe keep that in mind the next time :) -- Andrea Bolognani / Red Hat / Virtualization

On Fri, Aug 05, 2016 at 06:12:25PM +0200, Andrea Bolognani wrote:
On Fri, 2016-08-05 at 18:49 +0500, Aleem Akhtar wrote:
Thanks. But I am using XEN.
I see. qemu-guest-agent is definitely not an option then.
And I also don't want to go inside guest. I mean I want to get memory usage at the same level I am getting max memory and total memory.
The problem with that is that the host OS has zero knowledge of how the guest OS is using the memory it's been assigned.
You could try using virDomainMemoryStats(), but I'm not sure how much functionality Xen provides in this regard. Meybe none, Meybe everything needed. Idon't have Xen, so you have to try yourself. To see how it is used (or just to try it out) check out 'virsh dommemstat'.
I have another question. If Memory attribute of GetDomainInfo gives total memory I.e. memory in use plus remaining memory than what is the use of Max Memory?
You can configure your guest like
<memory unit='KiB'>4194304</memory> <currentMemory unit='KiB'>2097152</currentMemory>
Doing so would give you 2 GB of initial memory and the ability to increase that up to 4 GB at runtime.
So in the virDomainInfo structure, maxMem corresponds to <memory> and memory corresponds to <currentMemory>. Confusing, I know :)
I also programmatically updated Memory using libvirt function I.e. 1GB to 512mb. When tried to get domain statistics again I got Max Memory 512mb and Memory 1GB. How is this possible?
You mean memory = 512 MB and maxMem = 1 GB, right?
PS: This whole thread would have been more suitable for the libvirt-users mailing list - libvir-devel is targeted at the development of libvirt itself. Not a big deal, but maybe keep that in mind the next time :) -- Andrea Bolognani / Red Hat / Virtualization
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (3)
-
Aleem Akhtar
-
Andrea Bolognani
-
Martin Kletzander