1From 3e878cfaa57cf0156845b89c5d905fb0a200f489 Mon Sep 17 00:00:00 2001 2From: Zhu Guodong <zhuguodong0001@163.com> 3Date: Mon, 3 Jul 2023 15:31:06 +0800 4Subject: [PATCH] fix core binding in ohos 5 6--- 7 mindspore/core/mindrt/BUILD.gn | 7 ++++++ 8 .../core/mindrt/src/thread/core_affinity.cc | 6 ++--- 9 .../core/mindrt/src/thread/core_affinity.h | 2 +- 10 mindspore/core/mindrt/src/thread/threadlog.h | 25 ++++++++++++++++++- 11 .../core/mindrt/src/thread/threadpool.cc | 5 ++-- 12 5 files changed, 38 insertions(+), 7 deletions(-) 13 14diff --git a/mindspore/core/mindrt/BUILD.gn b/mindspore/core/mindrt/BUILD.gn 15index 2bc0e6cd..455aef96 100644 16--- a/mindspore/core/mindrt/BUILD.gn 17+++ b/mindspore/core/mindrt/BUILD.gn 18@@ -39,6 +39,13 @@ ohos_source_set("mindrt_obj") { 19 "../../core/", 20 ] 21 22+ defines = [ 23+ "ENABLE_MINDRT", 24+ "MS_COMPILE_OHOS", 25+ ] 26+ 27+ external_deps = [ "hilog:libhilog" ] 28+ 29 remove_configs = [ "//build/config/compiler:no_rtti" ] 30 31 part_name = "mindspore" 32diff --git a/mindspore/core/mindrt/src/thread/core_affinity.cc b/mindspore/core/mindrt/src/thread/core_affinity.cc 33index f329ca05..d8315431 100644 34--- a/mindspore/core/mindrt/src/thread/core_affinity.cc 35+++ b/mindspore/core/mindrt/src/thread/core_affinity.cc 36@@ -347,12 +347,12 @@ int CoreAffinity::InitBindCoreId(size_t thread_num, BindMode bind_mode) { 37 int CoreAffinity::SetAffinity() { return THREAD_OK; } 38 #elif defined(BIND_CORE) 39 int CoreAffinity::SetAffinity(const pthread_t &thread_id, cpu_set_t *cpu_set) { 40-#ifdef __ANDROID__ 41-#if __ANDROID_API__ >= 21 42+#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) 43+#if (__ANDROID_API__ >= 21) || defined(MS_COMPILE_OHOS) 44 THREAD_INFO("thread: %d, mask: %lu", pthread_gettid_np(thread_id), cpu_set->__bits[0]); 45 int ret = sched_setaffinity(pthread_gettid_np(thread_id), sizeof(cpu_set_t), cpu_set); 46 if (ret != THREAD_OK) { 47- THREAD_ERROR("bind thread %d to cpu failed. ERROR %d", pthread_gettid_np(thread_id), ret); 48+ THREAD_ERROR("bind thread %d to cpu failed. ERROR %{public}d", pthread_gettid_np(thread_id), ret); 49 return THREAD_ERROR; 50 } 51 #endif 52diff --git a/mindspore/core/mindrt/src/thread/core_affinity.h b/mindspore/core/mindrt/src/thread/core_affinity.h 53index a61b66c7..cd267e71 100644 54--- a/mindspore/core/mindrt/src/thread/core_affinity.h 55+++ b/mindspore/core/mindrt/src/thread/core_affinity.h 56@@ -23,7 +23,7 @@ 57 #ifdef SERVER_INFERENCE 58 #define BIND_CORE 59 #endif 60-#ifdef __ANDROID__ 61+#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) 62 #define BIND_CORE 63 #include <sched.h> 64 #endif 65diff --git a/mindspore/core/mindrt/src/thread/threadlog.h b/mindspore/core/mindrt/src/thread/threadlog.h 66index 7ed917f1..5fc182ae 100644 67--- a/mindspore/core/mindrt/src/thread/threadlog.h 68+++ b/mindspore/core/mindrt/src/thread/threadlog.h 69@@ -32,13 +32,36 @@ namespace mindspore { 70 } 71 #else 72 #define THREAD_DEBUG(content, ...) 73-#define THREAD_INFO(content, ...) 74 #define THREAD_TEST_TRUE(flag) 75+ 76 #if defined(__ANDROID__) 77+#define THREAD_INFO(content, ...) 78 #include <android/log.h> 79 #define THREAD_ERROR(content, args...) \ 80 { __android_log_print(ANDROID_LOG_ERROR, "MS_LITE", "%s|%d: " #content "\r\n", __func__, __LINE__, ##args); } 81+ 82+#elif defined(MS_COMPILE_OHOS) // For OHOS, use hilog. 83+ 84+#include "hilog/log.h" 85+#define MINDRT_OHOS_LOG_DOMAIN 0x2102 86+#define MINDRT_OHOS_LOG_TAG "MS_LITE" 87+ 88+#ifdef MS_COMPILE_WITH_OHOS_NDK 89+// When build with OHOS NDK, use public api of hilog module. 90+#define THREAD_INFO(content, args...) \ 91+ { OH_LOG_Print(LOG_APP, LOG_INFO, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } 92+#define THREAD_ERROR(content, args...) \ 93+ { OH_LOG_Print(LOG_APP, LOG_ERROR, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } 94 #else 95+// When build in OHOS repo, use inner api of hilog module. 96+#define THREAD_INFO(content, args...) \ 97+ { HiLogPrint(LOG_APP, LOG_INFO, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } 98+#define THREAD_ERROR(content, args...) \ 99+ { HiLogPrint(LOG_APP, LOG_ERROR, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } 100+#endif 101+ 102+#else 103+#define THREAD_INFO(content, ...) 104 #define THREAD_ERROR(content, ...) 105 #endif 106 #endif 107diff --git a/mindspore/core/mindrt/src/thread/threadpool.cc b/mindspore/core/mindrt/src/thread/threadpool.cc 108index ec75bd6e..55392f85 100644 109--- a/mindspore/core/mindrt/src/thread/threadpool.cc 110+++ b/mindspore/core/mindrt/src/thread/threadpool.cc 111@@ -53,10 +53,11 @@ void Worker::SetAffinity() { 112 #ifdef _WIN32 113 SetWindowsSelfAffinity(core_id_); 114 #elif defined(BIND_CORE) 115-#ifdef __ANDROID__ 116+#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) 117+ THREAD_INFO("thread: %d, mask: %lu", gettid(), mask_.__bits[0]); 118 int ret = sched_setaffinity(gettid(), sizeof(cpu_set_t), &mask_); 119 if (ret != THREAD_OK) { 120- THREAD_ERROR("bind thread %d to cpu failed. ERROR %d", gettid(), errno); 121+ THREAD_ERROR("bind thread %d to cpu failed. ERROR %{public}d", gettid(), errno); 122 } 123 return; 124 #else 125-- 1262.34.1 127 128