@@ -1276,6 +1276,63 @@ let update_vm ~__context id =
12761276 let a = Opt. map (fun x -> f (snd x)) info in
12771277 let b = Opt. map f previous in
12781278 a <> b in
1279+ (* Helpers to create and update guest metrics when needed *)
1280+ let lookup state key =
1281+ if List. mem_assoc key state.guest_agent then Some (List. assoc key state.guest_agent) else None in
1282+ let list state dir =
1283+ let dir = if dir.[0 ] = '/' then String. sub dir 1 (String. length dir - 1 ) else dir in
1284+ let results = Listext.List. filter_map (fun (path , value ) ->
1285+ if String. startswith dir path then begin
1286+ let rest = String. sub path (String. length dir) (String. length path - (String. length dir)) in
1287+ match List. filter (fun x -> x <> " " ) (String. split '/' rest) with
1288+ | x :: _ -> Some x
1289+ | _ -> None
1290+ end else None
1291+ ) state.guest_agent |> Listext.List. setify in
1292+ results in
1293+ let create_guest_metrics_if_needed () =
1294+ let gm = Db.VM. get_guest_metrics ~__context ~self in
1295+ if gm = Ref. null then
1296+ Opt. iter
1297+ (fun (_ , state ) ->
1298+ List. iter
1299+ (fun domid ->
1300+ try
1301+ let new_gm_ref = Xapi_guest_agent. create_and_set_guest_metrics (lookup state) (list state) ~__context ~domid ~uuid: id in
1302+ debug " xenopsd event: created guest metrics %s for VM %s" (Ref. string_of new_gm_ref) id
1303+ with e ->
1304+ error " Caught %s: while creating VM %s guest metrics" (Printexc. to_string e) id
1305+ ) state.domids
1306+ ) info in
1307+ let check_guest_agent () =
1308+ Opt. iter
1309+ (fun (_ , state ) ->
1310+ Opt. iter (fun oldstate ->
1311+ let old_ga = oldstate.guest_agent in
1312+ let new_ga = state.guest_agent in
1313+
1314+ (* Remove memory keys *)
1315+ let ignored_keys = [ " data/meminfo_free" ; " data/updated" ; " data/update_cnt" ] in
1316+ let remove_ignored ga =
1317+ List. fold_left (fun acc k -> List. filter (fun x -> fst x <> k) acc) ga ignored_keys in
1318+ let old_ga = remove_ignored old_ga in
1319+ let new_ga = remove_ignored new_ga in
1320+ if new_ga <> old_ga then begin
1321+ debug " Will update VM.allowed_operations because guest_agent has changed." ;
1322+ should_update_allowed_operations := true
1323+ end else begin
1324+ debug " Supressing VM.allowed_operations update because guest_agent data is largely the same"
1325+ end
1326+ ) previous;
1327+ List. iter
1328+ (fun domid ->
1329+ try
1330+ debug " xenopsd event: Updating VM %s domid %d guest_agent" id domid;
1331+ Xapi_guest_agent. all (lookup state) (list state) ~__context ~domid ~uuid: id
1332+ with e ->
1333+ error " Caught %s: while updating VM %s guest_agent" (Printexc. to_string e) id
1334+ ) state.domids
1335+ ) info in
12791336 (* Notes on error handling: if something fails we log and continue, to
12801337 maximise the amount of state which is correctly synced. If something
12811338 does fail then we may end up permanently out-of-sync until either a
@@ -1292,6 +1349,7 @@ let update_vm ~__context id =
12921349 should not be reset as there maybe a checkpoint is ongoing*)
12931350 Xapi_vm_lifecycle. force_state_reset_keep_current_operations ~__context ~self ~value: power_state;
12941351
1352+ if power_state = `Running then create_guest_metrics_if_needed () ;
12951353 if power_state = `Suspended || power_state = `Halted then begin
12961354 Xapi_network. detach_for_vm ~__context ~host: localhost ~vm: self;
12971355 Storage_access. reset ~__context ~vm: self;
@@ -1439,63 +1497,6 @@ let update_vm ~__context id =
14391497 )
14401498 info
14411499 end ;
1442- (* Helpers to create and update guest metrics when needed *)
1443- let lookup state key =
1444- if List. mem_assoc key state.guest_agent then Some (List. assoc key state.guest_agent) else None in
1445- let list state dir =
1446- let dir = if dir.[0 ] = '/' then String. sub dir 1 (String. length dir - 1 ) else dir in
1447- let results = Listext.List. filter_map (fun (path , value ) ->
1448- if String. startswith dir path then begin
1449- let rest = String. sub path (String. length dir) (String. length path - (String. length dir)) in
1450- match List. filter (fun x -> x <> " " ) (String. split '/' rest) with
1451- | x :: _ -> Some x
1452- | _ -> None
1453- end else None
1454- ) state.guest_agent |> Listext.List. setify in
1455- results in
1456- let create_guest_metrics_if_needed () =
1457- let gm = Db.VM. get_guest_metrics ~__context ~self in
1458- if gm = Ref. null then
1459- Opt. iter
1460- (fun (_ , state ) ->
1461- List. iter
1462- (fun domid ->
1463- try
1464- let new_gm_ref = Xapi_guest_agent. create_and_set_guest_metrics (lookup state) (list state) ~__context ~domid ~uuid: id in
1465- debug " xenopsd event: created guest metrics %s for VM %s" (Ref. string_of new_gm_ref) id
1466- with e ->
1467- error " Caught %s: while creating VM %s guest metrics" (Printexc. to_string e) id
1468- ) state.domids
1469- ) info in
1470- let check_guest_agent () =
1471- Opt. iter
1472- (fun (_ , state ) ->
1473- Opt. iter (fun oldstate ->
1474- let old_ga = oldstate.guest_agent in
1475- let new_ga = state.guest_agent in
1476-
1477- (* Remove memory keys *)
1478- let ignored_keys = [ " data/meminfo_free" ; " data/updated" ; " data/update_cnt" ] in
1479- let remove_ignored ga =
1480- List. fold_left (fun acc k -> List. filter (fun x -> fst x <> k) acc) ga ignored_keys in
1481- let old_ga = remove_ignored old_ga in
1482- let new_ga = remove_ignored new_ga in
1483- if new_ga <> old_ga then begin
1484- debug " Will update VM.allowed_operations because guest_agent has changed." ;
1485- should_update_allowed_operations := true
1486- end else begin
1487- debug " Supressing VM.allowed_operations update because guest_agent data is largely the same"
1488- end
1489- ) previous;
1490- List. iter
1491- (fun domid ->
1492- try
1493- debug " xenopsd event: Updating VM %s domid %d guest_agent" id domid;
1494- Xapi_guest_agent. all (lookup state) (list state) ~__context ~domid ~uuid: id
1495- with e ->
1496- error " Caught %s: while updating VM %s guest_agent" (Printexc. to_string e) id
1497- ) state.domids
1498- ) info in
14991500 let update_pv_drivers_detected () =
15001501 Opt. iter
15011502 (fun (_ , state ) ->
0 commit comments