This patch replaces the old and lacking affinity library with the
psutil library, fixing issue 845.

Signed-off-by: Hrvoje Ribicic <[email protected]>
---
 configure.ac                      |  2 +-
 devel/build_chroot                |  2 +-
 lib/hypervisor/hv_kvm/__init__.py | 41 +++++++++++----------------------------
 src/Ganeti/Constants.hs           |  8 --------
 4 files changed, 13 insertions(+), 40 deletions(-)

diff --git a/configure.ac b/configure.ac
index e576e06..41e1144 100644
--- a/configure.ac
+++ b/configure.ac
@@ -823,7 +823,7 @@ AC_PYTHON_MODULE(pycurl, t)
 AC_PYTHON_MODULE(bitarray, t)
 AC_PYTHON_MODULE(ipaddr, t)
 AC_PYTHON_MODULE(mock)
-AC_PYTHON_MODULE(affinity)
+AC_PYTHON_MODULE(psutil)
 AC_PYTHON_MODULE(paramiko)
 
 # Development-only Python modules
diff --git a/devel/build_chroot b/devel/build_chroot
index 37a367f..da37f95 100755
--- a/devel/build_chroot
+++ b/devel/build_chroot
@@ -439,7 +439,7 @@ in_chroot -- \
                python-mock fping qemu-utils
 
 in_chroot -- \
-  easy_install affinity
+  easy_install psutil
 
 in_chroot -- \
   easy_install jsonpointer \
diff --git a/lib/hypervisor/hv_kvm/__init__.py 
b/lib/hypervisor/hv_kvm/__init__.py
index 0f33d8d..61d6187 100644
--- a/lib/hypervisor/hv_kvm/__init__.py
+++ b/lib/hypervisor/hv_kvm/__init__.py
@@ -35,9 +35,9 @@ import shutil
 import urllib2
 from bitarray import bitarray
 try:
-  import affinity   # pylint: disable=F0401
+  import psutil   # pylint: disable=F0401
 except ImportError:
-  affinity = None
+  psutil = None
 try:
   import fdsend   # pylint: disable=F0401
 except ImportError:
@@ -710,31 +710,6 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     """
     hv_base.ConfigureNIC([pathutils.KVM_IFUP, tap], instance, seq, nic, tap)
 
-  @staticmethod
-  def _VerifyAffinityPackage():
-    if affinity is None:
-      raise errors.HypervisorError("affinity Python package not"
-                                   " found; cannot use CPU pinning under KVM")
-
-  @staticmethod
-  def _BuildAffinityCpuMask(cpu_list):
-    """Create a CPU mask suitable for sched_setaffinity from a list of
-    CPUs.
-
-    See man taskset for more info on sched_setaffinity masks.
-    For example: [ 0, 2, 5, 6 ] will return 101 (0x65, 0..01100101).
-
-    @type cpu_list: list of int
-    @param cpu_list: list of physical CPU numbers to map to vCPUs in order
-    @rtype: int
-    @return: a bit mask of CPU affinities
-
-    """
-    if cpu_list == constants.CPU_PINNING_OFF:
-      return constants.CPU_PINNING_ALL_KVM
-    else:
-      return sum(2 ** cpu for cpu in cpu_list)
-
   @classmethod
   def _SetProcessAffinity(cls, process_id, cpus):
     """Sets the affinity of a process to the given CPUs.
@@ -744,9 +719,15 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     @param cpus: The list of CPUs the process ID may use.
 
     """
-    cls._VerifyAffinityPackage()
-    affinity.set_process_affinity_mask(process_id,
-                                       cls._BuildAffinityCpuMask(cpus))
+    if psutil is None:
+      raise errors.HypervisorError("psutil Python package not"
+                                   " found; cannot use CPU pinning under KVM")
+
+    target_process = psutil.Process(process_id)
+    if cpus == constants.CPU_PINNING_OFF:
+      target_process.set_cpu_affinity(range(psutil.NUM_CPUS))
+    else:
+      target_process.set_cpu_affinity(cpus)
 
   @classmethod
   def _AssignCpuAffinity(cls, cpu_mask, process_id, thread_dict):
diff --git a/src/Ganeti/Constants.hs b/src/Ganeti/Constants.hs
index a77b275..832c86c 100644
--- a/src/Ganeti/Constants.hs
+++ b/src/Ganeti/Constants.hs
@@ -286,14 +286,6 @@ cpuPinningOff = [cpuPinningAllVal]
 cpuPinningAllXen :: String
 cpuPinningAllXen = "0-63"
 
--- | A KVM-specific implementation detail - the following value is
--- used to set CPU affinity to all processors (--0 through --31), per
--- taskset man page.
---
--- FIXME: This only works for machines with up to 32 CPU cores
-cpuPinningAllKvm :: Int
-cpuPinningAllKvm = 0xFFFFFFFF
-
 -- * Image and wipe
 
 ddCmd :: String
-- 
2.0.0.526.g5318336

Reply via email to