Skip to content

Commit 626f8e9

Browse files
robhoesBob Ball
authored andcommitted
CA-270642, CA-265691: Copy metrics from snapshot when reverting
Do not copy the metrics reference from the `snapshot_metadata`, because this comes from the original VM (the one that the snapshot was taken of), and may have been destroyed meanwhile. The snapshot itself has a copy of the VM_metrics record of the original VM at the time the snapshot was taken, so copy that when restoring the VM. This applies to snapshots as well as checkpoints. Signed-off-by: Rob Hoes <[email protected]>
1 parent b66e46c commit 626f8e9

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

ocaml/database/db_names.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ let is_a_snapshot = "is_a_snapshot"
3333
let is_control_domain = "is_control_domain"
3434
let platform = "platform"
3535
let other_config = "other_config"
36+
let metrics = "metrics"
3637
let guest_metrics = "guest_metrics"
3738
let parent = "parent"
3839
let snapshot_of = "snapshot_of"

ocaml/xapi/xapi_vm_clone.ml

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -232,33 +232,7 @@ let copy_vm_record ?(snapshot_info_record) ~__context ~vm ~disk_op ~new_name ~ne
232232
then (Xapi_globs.base_template_name_key, all.Db_actions.vM_name_label) :: other_config
233233
else other_config
234234
in
235-
(* Copy the old metrics if available, otherwise generate a fresh one *)
236-
let m =
237-
if Db.is_valid_ref __context all.Db_actions.vM_metrics
238-
then Some (Db.VM_metrics.get_record_internal ~__context ~self:all.Db_actions.vM_metrics)
239-
else None
240-
in
241-
let metrics = Ref.make ()
242-
and metrics_uuid = Uuid.to_string (Uuid.make_uuid ()) in
243-
Db.VM_metrics.create ~__context
244-
~ref:metrics
245-
~uuid:metrics_uuid
246-
~memory_actual:(default 0L (may (fun x -> x.Db_actions.vM_metrics_memory_actual) m))
247-
~vCPUs_number:(default 0L (may (fun x -> x.Db_actions.vM_metrics_VCPUs_number) m))
248-
~vCPUs_utilisation:(default [(0L, 0.)] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_utilisation) m))
249-
~vCPUs_CPU:(default [] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_CPU) m))
250-
~vCPUs_params:(default [] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_params) m))
251-
~vCPUs_flags:(default [] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_flags) m))
252-
~start_time:(default Date.never (may (fun x -> x.Db_actions.vM_metrics_start_time) m))
253-
~install_time:(default Date.never (may (fun x -> x.Db_actions.vM_metrics_install_time) m))
254-
~state:(default [] (may (fun x -> x.Db_actions.vM_metrics_state) m))
255-
~last_updated:(default Date.never (may (fun x -> x.Db_actions.vM_metrics_last_updated) m))
256-
~other_config:(default [] (may (fun x -> x.Db_actions.vM_metrics_other_config) m))
257-
~nomigrate:(default false (may (fun x -> x.Db_actions.vM_metrics_nomigrate) m))
258-
~hvm:(default false (may (fun x -> x.Db_actions.vM_metrics_hvm) m))
259-
~nested_virt:(default false (may (fun x -> x.Db_actions.vM_metrics_nested_virt) m))
260-
;
261-
235+
let metrics = Xapi_vm_helpers.copy_metrics ~__context ~vm in
262236
let guest_metrics = Xapi_vm_helpers.copy_guest_metrics ~__context ~vm in
263237

264238
(* compute the parent VM *)

ocaml/xapi/xapi_vm_helpers.ml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ open Xapi_vm_memory_constraints
2222
open Xapi_network_attach_helpers
2323
open Listext
2424
open Fun
25+
open Pervasiveext
26+
2527
module XenAPI = Client.Client
2628

2729
module D=Debug.Make(struct let name="xapi" end)
@@ -852,7 +854,38 @@ let delete_guest_metrics ~__context ~self:vm =
852854
Db.VM.set_guest_metrics ~__context ~self:vm ~value:Ref.null;
853855
(try Db.VM_guest_metrics.destroy ~__context ~self:guest_metrics with _ -> ())
854856

857+
let copy_metrics ~__context ~vm =
858+
(* Copy the old metrics if available, otherwise generate a fresh one *)
859+
let m =
860+
let mref = Db.VM.get_metrics ~__context ~self:vm in
861+
if Db.is_valid_ref __context mref
862+
then Some (Db.VM_metrics.get_record_internal ~__context ~self:mref)
863+
else None
864+
in
865+
let metrics = Ref.make ()
866+
and metrics_uuid = Uuid.to_string (Uuid.make_uuid ()) in
867+
Db.VM_metrics.create ~__context
868+
~ref:metrics
869+
~uuid:metrics_uuid
870+
~memory_actual:(default 0L (may (fun x -> x.Db_actions.vM_metrics_memory_actual) m))
871+
~vCPUs_number:(default 0L (may (fun x -> x.Db_actions.vM_metrics_VCPUs_number) m))
872+
~vCPUs_utilisation:(default [(0L, 0.)] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_utilisation) m))
873+
~vCPUs_CPU:(default [] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_CPU) m))
874+
~vCPUs_params:(default [] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_params) m))
875+
~vCPUs_flags:(default [] (may (fun x -> x.Db_actions.vM_metrics_VCPUs_flags) m))
876+
~start_time:(default Date.never (may (fun x -> x.Db_actions.vM_metrics_start_time) m))
877+
~install_time:(default Date.never (may (fun x -> x.Db_actions.vM_metrics_install_time) m))
878+
~state:(default [] (may (fun x -> x.Db_actions.vM_metrics_state) m))
879+
~last_updated:(default Date.never (may (fun x -> x.Db_actions.vM_metrics_last_updated) m))
880+
~other_config:(default [] (may (fun x -> x.Db_actions.vM_metrics_other_config) m))
881+
~nomigrate:(default false (may (fun x -> x.Db_actions.vM_metrics_nomigrate) m))
882+
~hvm:(default false (may (fun x -> x.Db_actions.vM_metrics_hvm) m))
883+
~nested_virt:(default false (may (fun x -> x.Db_actions.vM_metrics_nested_virt) m))
884+
;
885+
metrics
886+
855887
let copy_guest_metrics ~__context ~vm =
888+
(* Copy the old metrics if available, otherwise return Ref.null *)
856889
try
857890
let gm = Db.VM.get_guest_metrics ~__context ~self:vm in
858891
let all = Db.VM_guest_metrics.get_record ~__context ~self:gm in

ocaml/xapi/xapi_vm_snapshot.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,17 @@ let update_guest_metrics ~__context ~vm ~snapshot =
398398
Db.VM.set_guest_metrics ~__context ~self:vm ~value:new_gm
399399
end
400400

401+
let update_metrics ~__context ~vm ~snapshot =
402+
let snap_m = Db.VM.get_metrics ~__context ~self:snapshot in
403+
let vm_m = Db.VM.get_metrics ~__context ~self:vm in
404+
405+
debug "Reverting the metrics";
406+
if Db.is_valid_ref __context vm_m then Db.VM_metrics.destroy ~__context ~self:vm_m;
407+
if Db.is_valid_ref __context snap_m then begin
408+
let new_m = Xapi_vm_helpers.copy_metrics ~__context ~vm:snapshot in
409+
Db.VM.set_metrics ~__context ~self:vm ~value:new_m
410+
end
411+
401412
let update_parent ~__context ~vm ~snapshot =
402413
Db.VM.set_parent ~__context ~self:vm ~value:snapshot
403414

@@ -409,6 +420,7 @@ let do_not_copy = [
409420
Db_names.parent;
410421
Db_names.current_operations;
411422
Db_names.allowed_operations;
423+
Db_names.metrics;
412424
Db_names.guest_metrics;
413425
Db_names.resident_on;
414426
Db_names.domid;
@@ -465,6 +477,7 @@ let revert ~__context ~snapshot ~vm =
465477

466478
update_vifs_vbds_vgpus_and_vusbs ~__context ~snapshot ~vm;
467479
update_guest_metrics ~__context ~snapshot ~vm;
480+
update_metrics ~__context ~snapshot ~vm;
468481
update_parent ~__context ~snapshot ~vm;
469482
TaskHelper.set_progress ~__context 1.;
470483

0 commit comments

Comments
 (0)