[openrtm-commit:02127] r748 - trunk/OpenRTM-aist-Python/OpenRTM_aist
openrtm @ openrtm.org
openrtm @ openrtm.org
2016年 11月 21日 (月) 19:13:14 JST
Author: miyamoto
Date: 2016-11-21 19:13:14 +0900 (Mon, 21 Nov 2016)
New Revision: 748
Added:
trunk/OpenRTM-aist-Python/OpenRTM_aist/CPUAffinity.py
Modified:
trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py
trunk/OpenRTM-aist-Python/OpenRTM_aist/PeriodicExecutionContext.py
trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py
Log:
[incompat,->RELENG_1_2] CPU affinity setting has been added. #3712
Added: trunk/OpenRTM-aist-Python/OpenRTM_aist/CPUAffinity.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/CPUAffinity.py (rev 0)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/CPUAffinity.py 2016-11-21 10:13:14 UTC (rev 748)
@@ -0,0 +1,163 @@
+#!/usr/bin/env python
+# -*- coding: euc-jp -*-
+
+##
+# @file CPUAffinity.py
+# @brief
+# @date $Date$
+# @author Nobuhiko Miyamoto
+#
+
+import sys
+import os
+import platform
+import ctypes
+
+
+##
+# @if jp
+# @brief CPUの番号リストを変換
+#
+# @param cpu_num_list CPUの番号リスト
+# @return 数値
+#
+# @else
+# @brief
+#
+# @param cpu_num_list
+# @return
+#
+# @endif
+#
+def listToCUPNUM(cpu_num_list):
+ cpu_num = 0
+ try:
+ for num in cpu_num_list:
+ p = 0x01 << (int(num))
+ cpu_num += p
+ except ValueError:
+ pass
+ return cpu_num
+
+
+##
+# @if jp
+# @brief プロセスのCPUアフィニティを設定
+#
+# @param cpu_num_list CPUの番号リスト
+# @return 成功でTrue、失敗でFalse
+#
+# @else
+# @brief
+#
+# @param cpu_num_list
+# @return
+#
+# @endif
+#
+def setProcessAffinity(cpu_num_list):
+ cpu_num = listToCUPNUM(cpu_num_list)
+ if cpu_num == 0:
+ return False
+ pid = os.getpid()
+
+ if platform.system() == "Windows":
+
+ PROCESS_QUERY_INFORMATION = 0x0400
+ PROCESS_SET_INFORMATION = 0x0200
+
+
+
+ flag = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION
+ h = ctypes.windll.kernel32.OpenProcess(flag, 0, pid)
+
+
+ result = ctypes.windll.kernel32.SetProcessAffinityMask(ctypes.windll.kernel32.GetCurrentProcess(), cpu_num)
+ processMask = ctypes.c_long()
+ systemMask = ctypes.c_long()
+ result = ctypes.windll.kernel32.GetProcessAffinityMask(ctypes.windll.kernel32.GetCurrentProcess(),ctypes.byref(processMask),ctypes.byref(systemMask))
+
+ if processMask.value != cpu_num:
+ return False
+ else:
+ return True
+
+ else:
+ from ctypes.util import find_library
+ pthread = find_library("pthread")
+ if pthread is None:
+ return False
+ pthread = ctypes.CDLL(pthread)
+
+ mask = ctypes.c_long()
+ mask.value = cpu_num
+ result = pthread.sched_setaffinity(pid, ctypes.sizeof(mask), ctypes.byref(mask))
+ if result != 0:
+ return False
+ mask = ctypes.c_long()
+ result = pthread.sched_getaffinity(pid, ctypes.sizeof(mask), ctypes.byref(mask))
+ if mask.value != cpu_num:
+ return False
+ else:
+ return True
+
+
+##
+# @if jp
+# @brief スレッドのCPUアフィニティを設定
+#
+# @param cpu_num_list CPUの番号リスト
+# @return 成功でTrue、失敗でFalse
+#
+# @else
+# @brief
+#
+# @param cpu_num_list
+# @return
+#
+# @endif
+#
+def setThreadAffinity(cpu_num_list):
+ cpu_num = listToCUPNUM(cpu_num_list)
+ if cpu_num == 0:
+ return False
+
+
+ if platform.system() == "Windows":
+ PROCESS_QUERY_INFORMATION = 0x0400
+ PROCESS_SET_INFORMATION = 0x0200
+
+
+
+ h = ctypes.windll.kernel32.GetCurrentThread()
+
+ result = ctypes.windll.kernel32.SetThreadAffinityMask(h, cpu_num)
+ result = ctypes.windll.kernel32.SetThreadAffinityMask(h, cpu_num)
+
+ if result != cpu_num:
+ return False
+
+ return True
+
+ else:
+ from ctypes.util import find_library
+ pthread = find_library("pthread")
+ if pthread is None:
+ return False
+ pthread = ctypes.CDLL(pthread)
+ libc = find_library("libc")
+ libc = ctypes.CDLL(libc)
+
+ mask = ctypes.c_long()
+ mask.value = cpu_num
+ tid = libc.syscall(186)
+ result = pthread.sched_setaffinity(tid, ctypes.sizeof(mask), ctypes.byref(mask))
+ if result != 0:
+ return False
+ mask = ctypes.c_long()
+ result = pthread.sched_getaffinity(tid, ctypes.sizeof(mask), ctypes.byref(mask))
+ if mask.value != cpu_num:
+ return False
+ else:
+ return True
+
Modified: trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py 2016-11-21 05:57:54 UTC (rev 747)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/Manager.py 2016-11-21 10:13:14 UTC (rev 748)
@@ -1861,50 +1861,27 @@
tmp = affinity_str.split(",")
- pid = os.getpid()
- cpu_num = 0
+
+ cpu_num = []
for num in tmp:
try:
- p = 0x01 << (int(num)-1)
- cpu_num += p
+ cpu_num.append(int(num))
+ self._rtcout.RTC_DEBUG("CPU affinity mask set to %d", int(num))
except:
pass
- self._rtcout.RTC_DEBUG("CPU affinity mask set to %d", cpu_num)
+
- if cpu_num == 0:
+ if len(cpu_num) == 0:
return
-
+ ret = OpenRTM_aist.setProcessAffinity(cpu_num)
- 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")
+ if ret == False:
+ 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-21 05:57:54 UTC (rev 747)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/PeriodicExecutionContext.py 2016-11-21 10:13:14 UTC (rev 748)
@@ -75,7 +75,7 @@
self._rtcout.RTC_DEBUG("Actual rate: %d [sec], %d [usec]",
(self._profile.getPeriod().sec(), self._profile.getPeriod().usec()))
- self._cpu = 0
+ self._cpu = []
return
@@ -122,21 +122,10 @@
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)
-
+ if len(self._cpu) > 0:
+ ret = OpenRTM_aist.setThreadAffinity(self._cpu)
+ if ret == False:
+ self._rtcout.RTC_ERROR("CPU affinity mask setting failed")
while self.threadRunning():
OpenRTM_aist.ExecutionContextBase.invokeWorkerPreDo(self)
@@ -768,11 +757,10 @@
self._rtcout.RTC_DEBUG("CPU affinity property: %s",affinity_str)
tmp = affinity_str.split(",")
- self._cpu = 0
+ self._cpu = []
for num in tmp:
try:
- p = 0x01 << (int(num)-1)
- self._cpu += p
+ self._cpu.append(int(num))
self._rtcout.RTC_DEBUG("CPU affinity int value: %d added.",int(num))
except ValueError:
pass
Modified: trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py 2016-11-21 05:57:54 UTC (rev 747)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py 2016-11-21 10:13:14 UTC (rev 748)
@@ -113,4 +113,5 @@
from NumberingPolicy import *
from NodeNumberingPolicy import *
from NamingServiceNumberingPolicy import *
+from CPUAffinity import *
More information about the openrtm-commit
mailing list