Changing CPU and memory settings on XenServer VMs

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));

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

  1. Permalink  ⋅ Reply

    Madhurranjan

    July 17, 2010 at 2:46pm

    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

  2. Permalink  ⋅ Reply

    kongshoubin

    September 12, 2010 at 10:38am

    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.

    • Permalink  ⋅ Reply

      jansipke

      September 12, 2010 at 12:04pm

      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.

  3. Permalink  ⋅ Reply

    Karol

    May 13, 2011 at 4:07pm

    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.

    • Permalink  ⋅ Reply

      jansipke

      May 14, 2011 at 4:58pm

      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.

      • Permalink  ⋅ Reply

        Karol

        May 20, 2011 at 7:49am

        Hi Jan,

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

        –K.

      • Permalink  ⋅ Reply

        Karol

        May 20, 2011 at 10:42am

        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.

  4. Permalink  ⋅ Reply

    vance

    June 20, 2013 at 6:10pm

    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

  5. Permalink  ⋅ Reply

    Fábio Barboza

    May 28, 2014 at 2:15am

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

  6. Permalink  ⋅ Reply

    dharmesh kashyap

    September 7, 2014 at 6:32pm

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

Leave a Reply

Your email will not be published. Name and Email fields are required.