[libvirt] [PATCH] openvz: determine kb/pages only once

Guido Günther agx at sigxcpu.org
Tue May 8 20:12:37 UTC 2012


to safe some syscalls (as suggested by Eric Blake)
---
I've moved the code into an extra module since there's some more code to
come that should be shared between openvz_conf.c and openvz_driver.c
Cheers,
 -- Guido

 src/Makefile.am            |    3 ++-
 src/openvz/openvz_conf.c   |   12 ++++-------
 src/openvz/openvz_driver.c |   19 +++++------------
 src/openvz/openvz_util.c   |   51 ++++++++++++++++++++++++++++++++++++++++++++
 src/openvz/openvz_util.h   |   28 ++++++++++++++++++++++++
 5 files changed, 90 insertions(+), 23 deletions(-)
 create mode 100644 src/openvz/openvz_util.c
 create mode 100644 src/openvz/openvz_util.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 0dadc29..2ecd188 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -353,7 +353,8 @@ PHYP_DRIVER_SOURCES =						\
 
 OPENVZ_DRIVER_SOURCES =						\
 		openvz/openvz_conf.c openvz/openvz_conf.h	\
-		openvz/openvz_driver.c openvz/openvz_driver.h
+		openvz/openvz_driver.c openvz/openvz_driver.h   \
+		openvz/openvz_util.c openvz/openvz_util.h
 
 VMWARE_DRIVER_SOURCES =						\
 		vmware/vmware_driver.c vmware/vmware_driver.h	\
diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c
index f691ae7..fedd6a8 100644
--- a/src/openvz/openvz_conf.c
+++ b/src/openvz/openvz_conf.c
@@ -45,6 +45,7 @@
 
 #include "virterror_internal.h"
 #include "openvz_conf.h"
+#include "openvz_util.h"
 #include "uuid.h"
 #include "buf.h"
 #include "memory.h"
@@ -499,16 +500,11 @@ openvzReadMemConf(virDomainDefPtr def, int veid)
     char *temp = NULL;
     unsigned long long barrier, limit;
     const char *param;
-    unsigned long kb_per_pages;
+    long kb_per_pages;
 
-    kb_per_pages = sysconf(_SC_PAGESIZE);
-    if (kb_per_pages > 0) {
-        kb_per_pages /= 1024;
-    } else {
-        openvzError(VIR_ERR_INTERNAL_ERROR,
-                    _("Can't determine page size"));
+    kb_per_pages = openvzKBPerPages();
+    if (kb_per_pages < 0)
         goto error;
-    }
 
     /* Memory allocation guarantee */
     param = "VMGUARPAGES";
diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c
index 2661d60..45ab262 100644
--- a/src/openvz/openvz_driver.c
+++ b/src/openvz/openvz_driver.c
@@ -48,6 +48,7 @@
 #include "virterror_internal.h"
 #include "datatypes.h"
 #include "openvz_driver.h"
+#include "openvz_util.h"
 #include "buf.h"
 #include "util.h"
 #include "openvz_conf.h"
@@ -1735,14 +1736,9 @@ openvzDomainGetMemoryParameters(virDomainPtr domain,
 
     virCheckFlags(0, -1);
 
-    kb_per_pages = sysconf(_SC_PAGESIZE);
-    if (kb_per_pages > 0) {
-        kb_per_pages /= 1024;
-    } else {
-        openvzError(VIR_ERR_INTERNAL_ERROR,
-                    _("Can't determine page size"));
+    kb_per_pages = openvzKBPerPages();
+    if (kb_per_pages < 0)
         goto cleanup;
-    }
 
     if (*nparams == 0) {
         *nparams = OPENVZ_NB_MEM_PARAM;
@@ -1806,14 +1802,9 @@ openvzDomainSetMemoryParameters(virDomainPtr domain,
     int i, result = -1;
     long kb_per_pages;
 
-    kb_per_pages = sysconf(_SC_PAGESIZE);
-    if (kb_per_pages > 0) {
-        kb_per_pages /= 1024;
-    } else {
-        openvzError(VIR_ERR_INTERNAL_ERROR,
-                    _("Can't determine page size"));
+    kb_per_pages = openvzKBPerPages();
+    if (kb_per_pages < 0)
         goto cleanup;
-    }
 
     virCheckFlags(0, -1);
     if (virTypedParameterArrayValidate(params, nparams,
diff --git a/src/openvz/openvz_util.c b/src/openvz/openvz_util.c
new file mode 100644
index 0000000..ecb7a42
--- /dev/null
+++ b/src/openvz/openvz_util.c
@@ -0,0 +1,51 @@
+/*
+ * openvz_driver.c: core driver methods for managing OpenVZ VEs
+ *
+ * Copyright (C) Guido Günther
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ */
+
+#include <config.h>
+
+#include <unistd.h>
+
+#include "internal.h"
+
+#include "virterror_internal.h"
+
+#include "openvz_conf.h"
+#include "openvz_util.h"
+
+
+long
+openvzKBPerPages(void)
+{
+    static long kb_per_pages = 0;
+
+    if (kb_per_pages == 0) {
+        kb_per_pages = sysconf(_SC_PAGESIZE);
+        if (kb_per_pages > 0) {
+            kb_per_pages /= 1024;
+        } else {
+            openvzError(VIR_ERR_INTERNAL_ERROR,
+                        _("Can't determine page size"));
+            kb_per_pages = 0;
+            return -1;
+        }
+    }
+    return kb_per_pages;
+}
diff --git a/src/openvz/openvz_util.h b/src/openvz/openvz_util.h
new file mode 100644
index 0000000..a0d9bbb
--- /dev/null
+++ b/src/openvz/openvz_util.h
@@ -0,0 +1,28 @@
+/*
+ * openvz_driver.h: common util functions for managing openvz VPEs
+ *
+ * Copyright (C) 2012 Guido Günther
+ *
+ * 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, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ *
+ */
+
+
+#ifndef OPENVZ_UTIL_H
+# define OPENVZ_UTIL_H
+
+long openvzKBPerPages(void);
+
+#endif
-- 
1.7.10




More information about the libvir-list mailing list