[openrtm-commit:02557] r2978 - trunk/OpenRTM-aist/src/lib/coil/posix/coil

openrtm @ openrtm.org openrtm @ openrtm.org
2017年 4月 5日 (水) 17:46:15 JST


Author: n-ando
Date: 2017-04-05 17:46:15 +0900 (Wed, 05 Apr 2017)
New Revision: 2978

Added:
   trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.cpp
   trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.h
Modified:
   trunk/OpenRTM-aist/src/lib/coil/posix/coil/Makefile.am
Log:
[incompat,newfunc] CPU affinity functionality in coil implemented.

Added: trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.cpp
===================================================================
--- trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.cpp	                        (rev 0)
+++ trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.cpp	2017-04-05 08:46:15 UTC (rev 2978)
@@ -0,0 +1,127 @@
+// -*- C++ -*-
+/*!
+ * @file Affinity.cpp
+ * @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$
+ *
+ */
+
+#include <unistd.h>
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#include <pthread.h>
+#endif // _GNU_SOURCE
+#include <coil/stringutil.h>
+#include <coil/Affinity.h>
+
+namespace coil
+{
+  bool getProcCpuAffinity(CpuMask& cpu_mask)
+  {
+    pid_t pid(getpid());
+    cpu_set_t cpu_set;
+    CPU_ZERO(&cpu_set);
+
+    int result = sched_getaffinity(pid, sizeof(cpu_set_t), &cpu_set);
+    if (result != 0) { return false; }
+
+    for (size_t i(0); i < CPU_SETSIZE; ++i)
+      {
+        if (CPU_ISSET(i, &cpu_set))
+          {
+            cpu_mask.push_back((unsigned int)i);
+          }
+      }
+    return true;
+  }
+
+  bool setProcCpuAffinity(std::vector<unsigned int> mask)
+  {
+    pid_t pid(getpid());
+    cpu_set_t cpu_set;
+    CPU_ZERO(&cpu_set);
+
+    for (size_t i(0); i < mask.size(); ++i)
+      {
+        CPU_SET(i, &cpu_set);
+      }
+
+    int result = sched_setaffinity(pid, sizeof(cpu_set_t), &cpu_set);
+    if (result != 0) { return false; }
+    return true;
+  }
+
+  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)
+  {
+    pthread_t tid(pthread_self());
+    cpu_set_t cpu_set;
+    CPU_ZERO(&cpu_set);
+
+    int result = pthread_getaffinity_np(tid, sizeof(cpu_set_t), &cpu_set);
+    if (result != 0) { return false; }
+
+    for (size_t i(0); i < CPU_SETSIZE; ++i)
+      {
+        if (CPU_ISSET(i, &cpu_set))
+          {
+            cpu_mask.push_back((unsigned int)i);
+          }
+      }
+    return true;
+  }
+
+  bool setThreadCpuAffinity(std::vector<unsigned int> mask)
+  {
+    pthread_t tid(pthread_self());
+    cpu_set_t cpu_set;
+    CPU_ZERO(&cpu_set);
+
+    for (size_t i(0); i < mask.size(); ++i)
+      {
+        CPU_SET(i, &cpu_set);
+      }
+
+    int result = pthread_setaffinity_np(tid, sizeof(cpu_set_t), &cpu_set);
+    if (result != 0) { return false; }
+    return true;
+  }
+
+  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


Property changes on: trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.cpp
___________________________________________________________________
Added: svn:executable
   + *

Added: trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.h
===================================================================
--- trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.h	                        (rev 0)
+++ trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.h	2017-04-05 08:46:15 UTC (rev 2978)
@@ -0,0 +1,121 @@
+// -*- 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 <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


Property changes on: trunk/OpenRTM-aist/src/lib/coil/posix/coil/Affinity.h
___________________________________________________________________
Added: svn:executable
   + *

Modified: trunk/OpenRTM-aist/src/lib/coil/posix/coil/Makefile.am
===================================================================
--- trunk/OpenRTM-aist/src/lib/coil/posix/coil/Makefile.am	2017-03-24 08:15:24 UTC (rev 2977)
+++ trunk/OpenRTM-aist/src/lib/coil/posix/coil/Makefile.am	2017-04-05 08:46:15 UTC (rev 2978)
@@ -10,6 +10,7 @@
 
 # posix API dependent sources
 COIL_PLATFORM_SRC =    \
+	Affinity.cpp   \
 	Condition.cpp  \
 	DynamicLib.cpp \
 	Mutex.cpp      \



openrtm-commit メーリングリストの案内