]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Cache the presence of machine1 service
authorJán Tomko <jtomko@redhat.com>
Fri, 24 Jun 2016 15:41:09 +0000 (17:41 +0200)
committerJán Tomko <jtomko@redhat.com>
Mon, 6 Mar 2017 14:44:33 +0000 (15:44 +0100)
After the system has been booted, it should not change.

Cache the return value of virSystemdHasMachined.
Allow starting and terminating machines with just one
DBus call, instead of three, reducing the chance of
the call timing out.

Also introduce a small function for resetting the cache
to be used in tests.

src/libvirt_private.syms
src/util/virsystemd.c
src/util/virsystemdpriv.h [new file with mode: 0644]
tests/virsystemdtest.c

index 2349ce0a2914f26aa4cdbf66e8d4ada62c0b9526..b5505072ceea1b27eeb9a5a1f935ec7cad3a80be 100644 (file)
@@ -2567,6 +2567,7 @@ virSystemdCanHybridSleep;
 virSystemdCanSuspend;
 virSystemdCreateMachine;
 virSystemdGetMachineNameByPID;
+virSystemdHasMachinedResetCachedValue;
 virSystemdMakeMachineName;
 virSystemdMakeScopeName;
 virSystemdMakeSliceName;
index 90dd68afaf0499df38246e61873a1a657e81376e..ceb1c1a958a69b698d195ba6dd3c91f4ab422232 100644 (file)
@@ -26,6 +26,9 @@
 # include <sys/un.h>
 #endif
 
+#define __VIR_SYSTEMD_PRIV_H_ALLOW__ 1
+#include "virsystemdpriv.h"
+
 #include "virsystemd.h"
 #include "viratomic.h"
 #include "virbuffer.h"
@@ -182,6 +185,15 @@ virSystemdMakeMachineName(const char *drivername,
     return machinename;
 }
 
+static int virSystemdHasMachinedCachedValue = -1;
+
+/* Reset the cache from tests for testing the underlying dbus calls
+ * as well */
+void virSystemdHasMachinedResetCachedValue(void)
+{
+    virSystemdHasMachinedCachedValue = -1;
+}
+
 /* -2 = machine1 is not supported on this machine
  * -1 = error
  *  0 = machine1 is available
@@ -190,10 +202,22 @@ static int
 virSystemdHasMachined(void)
 {
     int ret;
-    if ((ret = virDBusIsServiceEnabled("org.freedesktop.machine1")) < 0)
+    int val;
+
+    val = virAtomicIntGet(&virSystemdHasMachinedCachedValue);
+    if (val != -1)
+        return val;
+
+    if ((ret = virDBusIsServiceEnabled("org.freedesktop.machine1")) < 0) {
+        if (ret == -2)
+            virAtomicIntSet(&virSystemdHasMachinedCachedValue, -2);
         return ret;
+    }
 
-    return virDBusIsServiceRegistered("org.freedesktop.systemd1");
+    if ((ret = virDBusIsServiceRegistered("org.freedesktop.systemd1")) == -1)
+        return ret;
+    virAtomicIntSet(&virSystemdHasMachinedCachedValue, ret);
+    return ret;
 }
 
 
diff --git a/src/util/virsystemdpriv.h b/src/util/virsystemdpriv.h
new file mode 100644 (file)
index 0000000..4f4058b
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * virsystemdpriv.h: Functions for testing virSystemd APIs
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library 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; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef __VIR_SYSTEMD_PRIV_H_ALLOW__
+# error "virsystemdpriv.h may only be included by virsystemd.c or test suites"
+#endif
+
+#ifndef __VIR_SYSTEMD_PRIV_H__
+# define __VIR_SYSTEMD_PRIV_H__
+
+# include "virsystemd.h"
+
+void virSystemdHasMachinedResetCachedValue(void);
+
+#endif /* __VIR_SYSTEMD_PRIV_H__ */
index 177cccc2f91ef1eadd3b2f0871216e6dfc07b0f2..d1f8e6f608696dd9d77780dddf14647489560cfb 100644 (file)
@@ -27,6 +27,9 @@
 # include <stdlib.h>
 # include <dbus/dbus.h>
 
+# define __VIR_SYSTEMD_PRIV_H_ALLOW__ 1
+# include "virsystemdpriv.h"
+
 # include "virsystemd.h"
 # include "virdbus.h"
 # include "virlog.h"
@@ -522,6 +525,7 @@ mymain(void)
     do {                                                                \
         if (virTestRun(_name, func, NULL) < 0)                          \
             ret = -1;                                                   \
+        virSystemdHasMachinedResetCachedValue();                        \
     } while (0)
 
     DO_TEST("Test create container ", testCreateContainer);