Much better algorithm for VM memory change.
This commit is contained in:
parent
015f6d2536
commit
844a10911a
@ -18,6 +18,16 @@ $js2xml = do ->
|
|||||||
}
|
}
|
||||||
builder.buildObject.bind builder
|
builder.buildObject.bind builder
|
||||||
|
|
||||||
|
$isVMRunning = do ->
|
||||||
|
states = {
|
||||||
|
'Halted': false
|
||||||
|
'Paused': true
|
||||||
|
'Running': true
|
||||||
|
'Suspended': false
|
||||||
|
}
|
||||||
|
|
||||||
|
(VM) -> states[VM.power_state]
|
||||||
|
|
||||||
#=====================================================================
|
#=====================================================================
|
||||||
|
|
||||||
exports.create = ->
|
exports.create = ->
|
||||||
@ -155,8 +165,7 @@ exports.migrate = ->
|
|||||||
catch
|
catch
|
||||||
@throw 'NO_SUCH_OBJECT'
|
@throw 'NO_SUCH_OBJECT'
|
||||||
|
|
||||||
# TODO: handles suspended.
|
unless $isVMRunning VM
|
||||||
if VM.power_state is 'Halted'
|
|
||||||
@throw 'INVALID_PARAMS', 'The VM can only be migrated when running'
|
@throw 'INVALID_PARAMS', 'The VM can only be migrated when running'
|
||||||
|
|
||||||
xapi = @getXAPI VM
|
xapi = @getXAPI VM
|
||||||
@ -191,32 +200,42 @@ exports.set = ->
|
|||||||
|
|
||||||
xapi = @getXAPI VM
|
xapi = @getXAPI VM
|
||||||
|
|
||||||
# Some settings can only be changed when the VM is halted.
|
{ref} = VM
|
||||||
if VM.power_state isnt 'Halted'
|
|
||||||
for param in ['memory']
|
|
||||||
if param of params
|
|
||||||
@throw(
|
|
||||||
'INVALID_PARAMS'
|
|
||||||
"cannot change #{param} when the VM is not halted"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
# The algorithm for the memory is a little complicated.
|
||||||
|
if 'memory' of params
|
||||||
|
{memory} = params
|
||||||
|
|
||||||
|
if memory < VM.memory.static[0]
|
||||||
|
@throw(
|
||||||
|
'INVALID_PARAMS'
|
||||||
|
"cannot set memory below the static minimum (#{VM.memory.static[0]})"
|
||||||
|
)
|
||||||
|
|
||||||
|
if ($isVMRunning VM) and memory > VM.memory.static[1]
|
||||||
|
@throw(
|
||||||
|
'INVALID_PARAMS'
|
||||||
|
"cannot set memory above the static maximum (#{VM.memory.static[0]}) "+
|
||||||
|
"for a running VM"
|
||||||
|
)
|
||||||
|
|
||||||
|
if memory < VM.memory.dynamic[0]
|
||||||
|
xapi.call 'VM.set_memory_dynamic_min', ref, "#{memory}"
|
||||||
|
else if memory > VM.memory.static[1]
|
||||||
|
xapi.call 'VM.set_memory_static_max', ref, "#{memory}"
|
||||||
|
xapi.call 'VM.set_memory_dynamic_max', ref, "#{memory}"
|
||||||
|
|
||||||
|
# Other fields.
|
||||||
for param, fields of {
|
for param, fields of {
|
||||||
CPUs:
|
CPUs:
|
||||||
if VM.power_state is 'Halted'
|
if $isVMRunning VM
|
||||||
['VCPUs_max', 'VCPUs_at_startup']
|
|
||||||
else
|
|
||||||
'VCPUs_number_live'
|
'VCPUs_number_live'
|
||||||
memory:
|
|
||||||
if params.memory > VM.memory.size
|
|
||||||
# Increase the memory.
|
|
||||||
['memory_static_max', 'memory_dynamic_max']
|
|
||||||
else
|
else
|
||||||
# Decrease the memory.
|
['VCPUs_max', 'VCPUs_at_startup']
|
||||||
['memory_dynamic_max', 'memory_static_max']
|
|
||||||
'name_label'
|
'name_label'
|
||||||
'name_description'
|
'name_description'
|
||||||
}
|
}
|
||||||
continue unless param of params
|
continue unless param of params
|
||||||
|
|
||||||
fields = [fields] unless $isArray fields
|
for field in (if $isArray fields then fields else [fields])
|
||||||
xapi.call "VM.set_#{field}", VM.ref, "#{params[param]}" for field in fields
|
xapi.call "VM.set_#{field}", ref, "#{params[param]}"
|
||||||
|
@ -441,22 +441,28 @@ module.exports = ->
|
|||||||
memory: ->
|
memory: ->
|
||||||
{metrics, guest_metrics} = @data
|
{metrics, guest_metrics} = @data
|
||||||
|
|
||||||
if not $isVMRunning.call this
|
memory = {
|
||||||
{
|
dynamic: [
|
||||||
size: +@genval.memory_static_max
|
+@genval.memory_dynamic_min
|
||||||
}
|
+@genval.memory_dynamic_max
|
||||||
else if (memory = guest_metrics?.memory)?.used
|
]
|
||||||
{
|
static: [
|
||||||
usage: +memory.used
|
+@genval.memory_static_min
|
||||||
size: +memory.total
|
+@genval.memory_static_max
|
||||||
}
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
memory.size = if not $isVMRunning.call this
|
||||||
|
+@genval.memory_static_max
|
||||||
|
else if (gmmemory = guest_metrics?.memory)?.used
|
||||||
|
memory.usage = +gmmemory.used
|
||||||
|
+gmmemory.total
|
||||||
|
else if metrics
|
||||||
|
+metrics.memory_actual
|
||||||
else
|
else
|
||||||
{
|
+@genval.memory_static_max
|
||||||
size: if metrics
|
|
||||||
+metrics.memory_actual
|
memory
|
||||||
else
|
|
||||||
+@genval.memory_static_max
|
|
||||||
}
|
|
||||||
|
|
||||||
power_state: -> @genval.power_state
|
power_state: -> @genval.power_state
|
||||||
|
|
||||||
|
@ -427,9 +427,17 @@ describe 'spec', ->
|
|||||||
# No data for this test.
|
# No data for this test.
|
||||||
}
|
}
|
||||||
|
|
||||||
$expect(vm.memory).to.be.an 'object'
|
$expect(vm.memory).to.deep.equal {
|
||||||
$expect(vm.memory.usage).to.be.undefined
|
dynamic: [
|
||||||
$expect(vm.memory.size).to.equal 536838144
|
536870912
|
||||||
|
536870912
|
||||||
|
]
|
||||||
|
static: [
|
||||||
|
134217728
|
||||||
|
536870912
|
||||||
|
]
|
||||||
|
size: 536838144
|
||||||
|
}
|
||||||
|
|
||||||
$expect(vm.messages).to.have.members [
|
$expect(vm.messages).to.have.members [
|
||||||
'OpaqueRef:a242799a-03bf-b55e-ecde-ddfe902fa69e'
|
'OpaqueRef:a242799a-03bf-b55e-ecde-ddfe902fa69e'
|
||||||
|
Loading…
Reference in New Issue
Block a user