You can change the CPU and memory settings of XenServer virtual machines using the API. XenServer allows you to set the priority of the virtual machine CPU (called weight), set a limit on the amount of CPU the virtual machine can use (called cap) and set the amount of memory the virtual machine gets.

CPU priority

You can set the weight parameter to a number between 1 and 65536. The default value is 256, meaning normal priority. A virtual machine with a weight of 512 will get twice as much CPU as a virtual machine with a weight of 256 on a contended XenServer host. The following code sets the weight to 128, meaning that it will get half as much CPU as a normal virtual machine.

Map<String, String> vcpuParameters = new HashMap<String, String>();
vcpuParameters.put("weight", "128");
vm.setVCPUsParams(connection, vcpuParameters);

CPU limit

You can set the cap parameter to a percentage number. The default value is 0, meaning there is no cap. The value 100 represents one virtual CPU, so half a virtual CPU can be set by using the value 50. The following code sets the cap to 35, meaning that it will not get more than 35 percent of one virtual CPU, even if the physical CPU is not used to its top.

Map<String, String> vcpuParameters = new HashMap<String, String>();
vcpuParameters.put("cap", "35");
vm.setVCPUsParams(connection, vcpuParameters);

Memory

You can set the amount of memory assigned to a virtual machine in bytes. The following code sets the amount of memory to 1 GB (1,073,741,824 bytes).

vm.setMemoryStaticMax(connection, new Long(1073741824));
Changing CPU and memory settings on XenServer VMs
Tagged on:     

10 thoughts on “Changing CPU and memory settings on XenServer VMs

  • 2010-07-17 at 14:46
    Permalink

    Good article!!

    Is there a way that the VMs ( lets say Win Server or RHEL) can start up with Xentools.iso ? I want to automate monitoring the IP that the machine gets through DHCP and the VM memory which can be done through Xentools.iso installation but this needs to be separate . I have tried installing xentools and converting it into a template and then instantiating a VM but this process doesn’t work.

    Can this be done through API ?

    Thoughts ?

    Thanks
    Madhurranjan

    Reply
  • 2010-09-12 at 10:38
    Permalink

    Hello, jansipke. I found the following information from the document of XenServer.

    “All writable VM parameter values can be changed while the VM is running, but the new parameters are not
    applied dynamically and will not be applied until the VM is rebooted.”

    Does it mean changing VM settings using comman line?If I change the CPU and memory settings of XenServer virtual machines using the API, do I have to reboot the VM to make the settings work?
    Looking forward to your reply.Please contact with me through email: kongshoubin@gmail.com

    Thank you very much.

    Reply
    • 2010-09-12 at 12:04
      Permalink

      It means that the VM settings can be changed while the VM is running, whether this is through the API or through a command line tool doesn’t matter. However, the changes are only applied after the VM has been rebooted.

      Reply
  • 2011-05-13 at 16:07
    Permalink

    Hi Jan,

    I am using the free version of XenServer. Is it possible to change the amount of memory assigned to a VM afterwards? I tried using XenCenter, but i did not suceed.

    Thanks.

    Karol.

    Reply
    • 2011-05-14 at 16:58
      Permalink

      I am using the free version of XenServer and it is indeed possible to change the amount of memory assigned to a VM after first creation. Just shutdown the VM, change the amount of memory and start again.

      Reply
      • 2011-05-20 at 07:49
        Permalink

        Hi Jan,

        I have tried it couple of times but without success. I can assign more memmory, but I can not assign less.

        –K.

        Reply
      • 2011-05-20 at 10:42
        Permalink

        Hi Jan,

        I have done some testing and it appears to be overwritten by the VM Template. Even when I try to create a new VM and set less memory, my setting gets overwritten to default 512Mb.

        –K.

        Reply
  • 2013-06-20 at 18:10
    Permalink

    Have you tested the cap command? I’ve tried setting the cap on xenserver 6.1 to various values (10, 50, 80, etc.) but the vcpu-utilization always goes beyond the cap. Is there other setting I need to set?
    Thanks

    Reply
  • 2014-05-28 at 02:15
    Permalink

    is now possible to increase the memory of a VM without rebooting it?

    Reply
  • 2014-09-07 at 18:32
    Permalink

    how can calculate the current load of virtual machine cpu..??

    Reply

Leave a Reply

Your email address will not be published.