Replace the affinity library with psutil
authorHrvoje Ribicic <riba@google.com>
Tue, 17 Jun 2014 16:10:50 +0000 (16:10 +0000)
committerHrvoje Ribicic <riba@google.com>
Wed, 18 Jun 2014 12:02:18 +0000 (12:02 +0000)
This patch replaces the old and lacking affinity library with the
psutil library, fixing issue 845.

Signed-off-by: Hrvoje Ribicic <riba@google.com>
Reviewed-by: Klaus Aehlig <aehlig@google.com>

configure.ac
devel/build_chroot
lib/hypervisor/hv_kvm/__init__.py
src/Ganeti/Constants.hs

index e576e06..41e1144 100644 (file)
@@ -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
index 37a367f..da37f95 100755 (executable)
@@ -439,7 +439,7 @@ in_chroot -- \
                python-mock fping qemu-utils
 
 in_chroot -- \
-  easy_install affinity
+  easy_install psutil
 
 in_chroot -- \
   easy_install jsonpointer \
index 0f33d8d..61d6187 100644 (file)
@@ -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):
index a77b275..832c86c 100644 (file)
@@ -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