forked from xapi-project/xen-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xapi_vm_memory_constraints.ml
107 lines (87 loc) · 4.15 KB
/
xapi_vm_memory_constraints.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(*
* Copyright (C) 2006-2009 Citrix Systems Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)
(**
* @group Virtual-Machine Management
*)
(** An extension of Vm_memory_constraints that provides additional database and API operations. *)
module type T = sig
include Vm_memory_constraints.T
(** Asserts for the given set of constraints [c], that
[c.static_min] ≤ [c.dynamic_min] ≤ [c.dynamic_max] ≤ [c.static_max]. *)
val assert_valid : constraints:t -> unit
(** Asserts for the given set of constraints [c], that
[c.static_min] ≤ [c.dynamic_min] = [c.dynamic_max] = [c.static_max]. *)
val assert_valid_and_pinned_at_static_max : constraints:t -> unit
(** Asserts that the given set of constraints [c] is valid for the current
context. *)
val assert_valid_for_current_context :
__context:Context.t -> vm:API.ref_VM -> constraints:t -> unit
(** Extracts memory constraints from the given VM record. *)
val extract : vm_record:API.vM_t -> t
(** Reads memory constraints for the given VM, from the database. *)
val get : __context:Context.t -> vm_ref:[`VM] Ref.t -> t
(** Reads memory constraints effective for the given running VM, from the database. *)
val get_live : __context:Context.t -> vm_ref:[`VM] Ref.t -> t
(** Writes memory constraints for the given VM, to the database. *)
val set : __context:Context.t -> vm_ref:[`VM] Ref.t -> constraints:t -> unit
end
module Vm_memory_constraints : T = struct
include Vm_memory_constraints.Vm_memory_constraints
let assert_valid ~constraints =
if not (are_valid ~constraints)
then raise (Api_errors.Server_error (
Api_errors.memory_constraint_violation,
["Memory limits must satisfy: \
static_min ≤ dynamic_min ≤ dynamic_max ≤ static_max"]))
let assert_valid_and_pinned_at_static_max ~constraints =
if not (are_valid_and_pinned_at_static_max ~constraints)
then raise (Api_errors.Server_error (
Api_errors.memory_constraint_violation,
["Memory limits must satisfy: \
static_min ≤ dynamic_min = dynamic_max = static_max"]))
let assert_valid_for_current_context ~__context ~vm ~constraints =
(* NB we don't want to prevent dom0 ballooning even if we do want to prevent
domU ballooning. *)
(if Db.VM.get_is_control_domain ~__context ~self:vm || (Pool_features.is_enabled ~__context Features.DMC)
then assert_valid
else assert_valid_and_pinned_at_static_max)
~constraints
let extract ~vm_record =
{
static_min = vm_record.API.vM_memory_static_min;
dynamic_min = vm_record.API.vM_memory_dynamic_min;
target = vm_record.API.vM_memory_target;
dynamic_max = vm_record.API.vM_memory_dynamic_max;
static_max = vm_record.API.vM_memory_static_max;
}
let get ~__context ~vm_ref =
let vm_record = Db.VM.get_record ~__context ~self:vm_ref in
extract vm_record
let get_live ~__context ~vm_ref =
let live_record = Db.VM.get_record ~__context ~self:vm_ref in
let boot_record = Helpers.get_boot_record ~__context ~self:vm_ref in
{
static_min = boot_record.API.vM_memory_static_min;
dynamic_min = live_record.API.vM_memory_dynamic_min;
target = live_record.API.vM_memory_target;
dynamic_max = live_record.API.vM_memory_dynamic_max;
static_max = boot_record.API.vM_memory_static_max;
}
let set ~__context ~vm_ref ~constraints =
Db.VM.set_memory_static_min ~__context ~self:vm_ref ~value:constraints.static_min;
Db.VM.set_memory_dynamic_min ~__context ~self:vm_ref ~value:constraints.dynamic_min;
Db.VM.set_memory_target ~__context ~self:vm_ref ~value:constraints.target;
Db.VM.set_memory_dynamic_max ~__context ~self:vm_ref ~value:constraints.dynamic_max;
Db.VM.set_memory_static_max ~__context ~self:vm_ref ~value:constraints.static_max
end