[openrtm-commit:03096] r3180 - in branches/RELENG_1_2/OpenRTM-aist/src/lib: coil/win32/coil rtm
openrtm @ openrtm.org
openrtm @ openrtm.org
2018年 1月 22日 (月) 15:05:31 JST
Author: miyamoto
Date: 2018-01-22 15:05:31 +0900 (Mon, 22 Jan 2018)
New Revision: 3180
Added:
branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.cpp
branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.h
Modified:
branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Makefile.am
branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp
branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h
Log:
[incompat,->RELENG_1_2] refs #3711
Added: branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.cpp
===================================================================
--- branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.cpp (rev 0)
+++ branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.cpp 2018-01-22 06:05:31 UTC (rev 3180)
@@ -0,0 +1,121 @@
+// -*- C++ -*-
+/*!
+ * @file Affinity.cpp
+ * @brief Processor affinity operation class
+ * @date $Date$
+ * @author Nobuhiko Miyamoto <n-miyamoto at aist.go.jp>
+ *
+ * Copyright (C) 2018
+ * Nobuhiko Miyamoto
+ * National Institute of
+ * Advanced Industrial Science and Technology (AIST), Japan
+ * All rights reserved.
+ *
+ * $Id$
+ *
+ */
+
+
+#include <windows.h>
+#include <coil/stringutil.h>
+#include <coil/Affinity.h>
+
+namespace coil
+{
+ DWORD listToCUPNUM(CpuMask &cpu_mask)
+ {
+ DWORD cpu_num = 0;
+ for(CpuMask::iterator itr = cpu_mask.begin(); itr != cpu_mask.end(); ++itr) {
+ DWORD_PTR p = 0x01 << (*itr);
+ cpu_num += p;
+ }
+ return cpu_num;
+ }
+ bool getProcCpuAffinity(CpuMask& cpu_mask)
+ {
+ DWORD cpu_num = listToCUPNUM(cpu_mask);
+ DWORD processMask, systemMask = 0;
+ HANDLE h = GetCurrentProcess();
+ BOOL success = GetProcessAffinityMask(h, &processMask, &systemMask);
+ if (success)
+ {
+ for (int i = 0; i < 32; i++)
+ {
+ if (processMask & (0x00000001 << i))
+ {
+ cpu_mask.push_back(i);
+ }
+ }
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ bool setProcCpuAffinity(std::vector<unsigned int> mask)
+ {
+ DWORD cpu_num = listToCUPNUM(mask);
+ HANDLE h = GetCurrentProcess();
+ BOOL success = SetProcessAffinityMask(h, cpu_num);
+ if (success)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ bool setProcCpuAffinity(std::string cpu_mask)
+ {
+ coil::vstring tmp = coil::split(cpu_mask, ",", true);
+ CpuMask mask;
+ for (size_t i(0); i < tmp.size(); ++i)
+ {
+ int num;
+ if (coil::stringTo(num, tmp[i].c_str()))
+ {
+ mask.push_back(num);
+ }
+ }
+ return setProcCpuAffinity(mask);
+ }
+
+ bool getThreadCpuAffinity(CpuMask& cpu_mask)
+ {
+ return true;
+ }
+
+ bool setThreadCpuAffinity(std::vector<unsigned int> mask)
+ {
+ DWORD cpu_num = listToCUPNUM(mask);
+ HANDLE h = GetCurrentThread();
+ BOOL success = SetThreadAffinityMask(h, cpu_num);
+ if (success)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ bool setThreadCpuAffinity(std::string cpu_mask)
+ {
+ coil::vstring tmp = coil::split(cpu_mask, ",", true);
+ CpuMask mask;
+ for (size_t i(0); i < tmp.size(); ++i)
+ {
+ int num;
+ if (coil::stringTo(num, tmp[i].c_str()))
+ {
+ mask.push_back(num);
+ }
+ }
+ return setThreadCpuAffinity(mask);
+ }
+}; // namespace coil
Added: branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.h
===================================================================
--- branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.h (rev 0)
+++ branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Affinity.h 2018-01-22 06:05:31 UTC (rev 3180)
@@ -0,0 +1,123 @@
+// -*- C++ -*-
+/*!
+ * @file Affinity.h
+ * @brief Processor affinity operation class
+ * @date $Date$
+ * @author Noriaki Ando <n-ando at aist.go.jp>
+ *
+ * Copyright (C) 2016
+ * Noriaki Ando
+ * National Institute of
+ * Advanced Industrial Science and Technology (AIST), Japan
+ * All rights reserved.
+ *
+ * $Id$
+ *
+ */
+
+#ifndef COIL_AFFINITY_H
+#define COIL_AFFINITY_H
+
+#include <string>
+#include <vector>
+#include <coil/Singleton.h>
+#include <coil/TimeValue.h>
+#include <coil/Mutex.h>
+#include <coil/Guard.h>
+
+
+namespace coil
+{
+ typedef std::vector<unsigned int> CpuMask;
+ /*!
+ * @if jp
+ * @brief プロセスのCPU affinityを取得する
+ * @prop cpu_mask 現在設定されている affinity が CPU IDの
+ * std::vector<unsigned int> で返される。
+ * @return True: 成功、False: 失敗
+ * @else
+ * @brief Getting current process CPU affinity
+ * @prop cpu_mask Current CPU affinity mask is returned as CPU ID in
+ * std::vector<unsigned int>.
+ * @return True: success, False: fail
+ * @endif
+ */
+ bool getProcCpuAffinity(CpuMask& cpu_mask);
+
+ /*!
+ * @if jp
+ * @brief プロセスのCPU affinityを設定する
+ * @prop cpu_mask 設定する CPU affinity を CPU ID の
+ * std::vector<unsigned int> リストで与える
+ * @return True: 成功、False: 失敗
+ * @else
+ * @brief Setting process CPU affinity
+ * @prop cpu_mask CPU affinity mask to be set is given with CPU ID in
+ * std::vector<unsigned int> list.
+ * @return True: success, False: fail
+ * @endif
+ */
+ bool setProcCpuAffinity(const CpuMask cpu_mask);
+
+ /*!
+ * @if jp
+ * @brief プロセスのCPU affinityを文字列で設定する
+ * @prop cpu_mask 設定する CPU affinity を CPU ID のカンマ区切り文字
+ * 列のリストで与える
+ * @return True: 成功、False: 失敗
+ * @else
+ * @brief Setting process CPU affinity with string
+ * @prop cpu_mask CPU affinity mask to be set is given with comma
+ * separated CPU ID string.
+ * @return True: success, False: fail
+ * @endif
+ */
+ bool setProcCpuAffinity(std::string cpu_mask);
+
+ /*!
+ * @if jp
+ * @brief スレッドのCPU affinityを取得する
+ * @prop cpu_mask 現在設定されている affinity が CPU IDの
+ * std::vector<unsigned int> で返される。
+ * @return True: 成功、False: 失敗
+ * @else
+ * @brief Getting current process CPU affinity
+ * @prop cpu_mask Current CPU affinity mask is returned as CPU ID in
+ * std::vector<unsigned int>.
+ * @return True: success, False: fail
+ * @endif
+ */
+ bool getThreadCpuAffinity(CpuMask& cpu_mask);
+
+ /*!
+ * @if jp
+ * @brief スレッドのCPU affinityを設定する
+ * @prop cpu_mask 設定する CPU affinity を CPU ID の
+ * std::vector<unsigned int> リストで与える
+ * @return True: 成功、False: 失敗
+ * @else
+ * @brief Setting process CPU affinity
+ * @prop cpu_mask CPU affinity mask to be set is given with CPU ID in
+ * std::vector<unsigned int> list.
+ * @return True: success, False: fail
+ * @endif
+ */
+ bool setThreadCpuAffinity(const CpuMask cpu_mask);
+
+ /*!
+ * @if jp
+ * @brief スレッドのCPU affinityを文字列で設定する
+ * @prop cpu_mask 設定する CPU affinity を CPU ID のカンマ区切り文字
+ * 列のリストで与える
+ * @return True: 成功、False: 失敗
+ * @else
+ * @brief Setting process CPU affinity with string
+ * @prop cpu_mask CPU affinity mask to be set is given with comma
+ * separated CPU ID string.
+ * @return True: success, False: fail
+ * @endif
+ */
+ bool setThreadCpuAffinity(std::string mask);
+
+}; // namespace coil
+#endif // COIL_AFFINITY_H
Modified: branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Makefile.am
===================================================================
--- branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Makefile.am 2018-01-22 04:05:38 UTC (rev 3179)
+++ branches/RELENG_1_2/OpenRTM-aist/src/lib/coil/win32/coil/Makefile.am 2018-01-22 06:05:31 UTC (rev 3180)
@@ -16,7 +16,8 @@
Task.cpp \
Time.cpp \
UUID.cpp \
- SharedMemory.cpp
+ SharedMemory.cpp \
+ Affinity.cpp
# Win32 API dependent headers
COIL_PLATFORM_H = \
Modified: branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp
===================================================================
--- branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp 2018-01-22 04:05:38 UTC (rev 3179)
+++ branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.cpp 2018-01-22 06:05:31 UTC (rev 3180)
@@ -21,6 +21,7 @@
#ifdef RTM_OS_LINUX
#define _GNU_SOURCE
#include <pthread.h>
+#include <algorithm>
#endif // RTM_OS_LINUX
#include <coil/Time.h>
#include <coil/TimeValue.h>
@@ -123,39 +124,32 @@
RTC_TRACE(("svc()"));
int count(0);
-#ifdef RTM_OS_LINUX
- pthread_t tid(pthread_self());
- cpu_set_t cpu_set;
- CPU_ZERO(&cpu_set);
+ bool result = coil::setThreadCpuAffinity(m_cpu);
- for (size_t i(0); i < m_cpu.size(); ++i)
+ if (!result)
{
- RTC_DEBUG(("CPU affinity mask set to %d", m_cpu[i]));
- CPU_SET(m_cpu[i], &cpu_set);
- }
-
- int result = pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpu_set);
- if (result != 0)
- {
- RTC_ERROR(("pthread_getaffinity_np():"
+ RTC_ERROR(("setThreadCpuAffinity():"
"CPU affinity mask setting failed"));
- }
- CPU_ZERO(&cpu_set);
- tid = pthread_self();
- result = pthread_getaffinity_np(tid, sizeof(cpu_set_t), &cpu_set);
- if (result != 0)
- {
- RTC_ERROR(("pthread_getaffinity_np(): returned error."));
- }
- for (size_t j(0); j < CPU_SETSIZE; ++j)
- {
- if (CPU_ISSET(j, &cpu_set))
- {
- RTC_DEBUG(("Current CPU affinity mask is %d.", j));
- }
- }
-#endif // RTM_OS_LINUX
+ };
+
+ coil::CpuMask ret_cpu;
+ result = coil::getThreadCpuAffinity(ret_cpu);
+
+
+#ifdef RTM_OS_LINUX
+ std::sort(ret_cpu.begin(), ret_cpu.end());
+ std::sort(m_cpu.begin(), m_cpu.end());
+ if (result && ret_cpu.size() == ret_cpu.size() && std::equal(ret_cpu.cbegin(), ret_cpu.cend(), m_cpu.cbegin()))
+ {
+
+ }
+ else
+ {
+ RTC_ERROR(("pthread_getaffinity_np(): returned error."));
+ }
+#endif
+
do
{
ExecutionContextBase::invokeWorkerPreDo();
Modified: branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h
===================================================================
--- branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h 2018-01-22 04:05:38 UTC (rev 3179)
+++ branches/RELENG_1_2/OpenRTM-aist/src/lib/rtm/PeriodicExecutionContext.h 2018-01-22 06:05:31 UTC (rev 3180)
@@ -25,6 +25,7 @@
#include <coil/Task.h>
#include <coil/Mutex.h>
#include <coil/Condition.h>
+#include <coil/Affinity.h>
#include <rtm/ExecutionContextBase.h>
@@ -716,7 +717,7 @@
/*!
* @brief CPU affinity mask list
*/
- std::vector<int> m_cpu;
+ coil::CpuMask m_cpu;
}; // class PeriodicExecutionContext
}; // namespace RTC
More information about the openrtm-commit
mailing list