From 1686b198dcf9b695a864f82d18fbc9b2e76f54df Mon Sep 17 00:00:00 2001
From: tangbinzy <tangbin_yewu@cmss.chinamobile.com>
Date: Thu, 16 Mar 2023 02:17:30 +0000
Subject: [PATCH] qemu: Return perf status that affect next boot  for shutoff
 domains While we set up perf events for a shutoff domain and check the
 settings, All of perf events are reported as 'disabled', unless we add
 --config, This is redundant for a shutoff domain.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

 # virsh domstate $GUEST
shut off

 # virsh perf --domain $GUEST
cmt            : disabled
mbmt           : disabled
mbml           : disabled
......

 # virsh perf --domain $GUEST --enable mbmt
mbmt           : enabled

 # virsh perf --domain $GUEST
cmt            : disabled
mbmt           : disabled
mbml           : disabled
......

Use virDomainObjGetOneDefState instead of virDomainObjGetOneDef to fix
the issue. After patch, The perf event status of a shutoff domain is
reported correctly:

 # virsh domstate $GUEST
shut off

 # virsh perf --domain $GUEST
cmt            : disabled
mbmt           : disabled
mbml           : disabled
......

 # virsh perf --domain $GUEST --enable mbmt
mbmt           : enabled

 # virsh perf --domain $GUEST
cmt            : disabled
mbmt           : enabled
mbml           : disabled
......

Signed-off-by: Lin Ma <lma@suse.de>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>

Signed-off-by: tangbin <tangbin_yewu@cmss.chinamobile.com>
(cherry-pick from 10841b6cb612fed1a4cb9be05c4f5e150008cac7)
---
 src/qemu/qemu_driver.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 32b3ef3cf1..ed621262aa 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9952,6 +9952,7 @@ qemuDomainGetPerfEvents(virDomainPtr dom,
     int npar = 0;
     size_t i;
     int ret = -1;
+    bool live = false;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
                   VIR_DOMAIN_AFFECT_CONFIG |
@@ -9966,7 +9967,7 @@ qemuDomainGetPerfEvents(virDomainPtr dom,
     if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_QUERY) < 0)
         goto cleanup;
 
-    if (!(def = virDomainObjGetOneDef(vm, flags)))
+    if (!(def = virDomainObjGetOneDefState(vm, flags, &live)))
         goto endjob;
 
     priv = vm->privateData;
@@ -9974,7 +9975,7 @@ qemuDomainGetPerfEvents(virDomainPtr dom,
     for (i = 0; i < VIR_PERF_EVENT_LAST; i++) {
         bool perf_enabled;
 
-        if (flags & VIR_DOMAIN_AFFECT_CONFIG)
+        if ((flags & VIR_DOMAIN_AFFECT_CONFIG) || !live)
             perf_enabled = def->perf.events[i] == VIR_TRISTATE_BOOL_YES;
         else
             perf_enabled = virPerfEventIsEnabled(priv->perf, i);
-- 
2.27.0

