[openrtm-commit:02123] r745 - trunk/OpenRTM-aist-Python/OpenRTM_aist

openrtm @ openrtm.org openrtm @ openrtm.org
2016年 11月 18日 (金) 16:23:23 JST


Author: miyamoto
Date: 2016-11-18 16:23:23 +0900 (Fri, 18 Nov 2016)
New Revision: 745

Modified:
   trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py
   trunk/OpenRTM-aist-Python/OpenRTM_aist/PeriodicExecutionContext.py
Log:
[incompat,->RELENG_1_2] CPU affinity setting has been added.

Modified: trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py	2016-11-18 02:32:05 UTC (rev 744)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py	2016-11-18 07:23:23 UTC (rev 745)
@@ -22,6 +22,7 @@
 import time
 from omniORB import CORBA, PortableServer
 from types import IntType, ListType
+import platform
 
 import OpenRTM_aist
 import RTC
@@ -29,6 +30,7 @@
 import CosNaming
 
 
+
 #------------------------------------------------------------
 # static var
 #------------------------------------------------------------
@@ -1850,14 +1852,10 @@
     
     if not self._config.findNode("manager.cpu_affinity"):
       return
+          
+
     
-    try:
-      import affinity
-    except ImportError:
-      self._rtcout.RTC_DEBUG("not found affinity module")
-      return
     
-    
     affinity_str = self._config.getProperty("manager.cpu_affinity")
     self._rtcout.RTC_DEBUG("CPU affinity property: %s", affinity_str)
 
@@ -1876,11 +1874,36 @@
 
     if cpu_num == 0:
       return
+
+
+
     
-    affinity.set_process_affinity_mask(pid, cpu_num)
-    ret = affinity.get_process_affinity_mask(pid)
-    if ret != cpu_num:
-      self._rtcout.RTC_ERROR("get_process_affinity_mask(): returned error.")
+    if platform.system() == "Windows":
+      import win32process
+      import win32api
+      import win32con
+      flag = win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_SET_INFORMATION
+      h = win32api.OpenProcess(flag, 0, pid)
+      result = win32process.SetProcessAffinityMask(h, cpu_num)
+      result = win32process.GetProcessAffinityMask(h)[0]
+      if result != cpu_num:
+        self._rtcout.RTC_ERROR("GetProcessAffinityMask(): returned error.")
+    else:
+      import ctypes
+      from ctypes.util import find_library
+      pthread = find_library("pthread")
+      if pthread is None:
+        self._rtcout.RTC_ERROR("Not Found pthread Module")
+      pthread = CDLL(pthread)
+      
+      mask = ctypes.c_long()
+      mask.value = cpu_num
+      result = pthread.sched_setaffinity(os.getpid(), ctypes.sizeof(mask), ctypes.byref(mask))
+      mask = ctypes.c_long()
+      result = pthread.sched_getaffinity(os.getpid(), ctypes.sizeof(mask), ctypes.byref(mask))
+      
+      if mask.value != cpu_num:
+        self._rtcout.RTC_ERROR("CPU affinity mask setting failed")
     
 
 

Modified: trunk/OpenRTM-aist-Python/OpenRTM_aist/PeriodicExecutionContext.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/PeriodicExecutionContext.py	2016-11-18 02:32:05 UTC (rev 744)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/PeriodicExecutionContext.py	2016-11-18 07:23:23 UTC (rev 745)
@@ -19,6 +19,7 @@
 import threading
 import time
 import os
+import platform
 from omniORB import CORBA, PortableServer
 
 import OpenRTM_aist
@@ -121,8 +122,22 @@
     self._rtcout.RTC_TRACE("svc()")
     count_ = 0
 
+    if self._cpu > 0:
+      if platform.system() == "Windows":
+        import win32process
+        import win32api
+        import win32con
+        h = win32api.GetCurrentThread()
+        result = win32process.SetThreadAffinityMask(h, self._cpu)
+        
+      else:
+        from ctypes.util import find_library
+        pthread = find_library("pthread")
+        if pthread is None:
+          return
+        pthread = CDLL(pthread)
+        
     
-    
     while self.threadRunning():
       OpenRTM_aist.ExecutionContextBase.invokeWorkerPreDo(self)
       # Thread will stopped when all RTCs are INACTIVE.



More information about the openrtm-commit mailing list