From 3e878cfaa57cf0156845b89c5d905fb0a200f489 Mon Sep 17 00:00:00 2001 From: Zhu Guodong Date: Mon, 3 Jul 2023 15:31:06 +0800 Subject: [PATCH] fix core binding in ohos --- mindspore/core/mindrt/BUILD.gn | 7 ++++++ .../core/mindrt/src/thread/core_affinity.cc | 6 ++--- .../core/mindrt/src/thread/core_affinity.h | 2 +- mindspore/core/mindrt/src/thread/threadlog.h | 25 ++++++++++++++++++- .../core/mindrt/src/thread/threadpool.cc | 5 ++-- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/mindspore/core/mindrt/BUILD.gn b/mindspore/core/mindrt/BUILD.gn index 2bc0e6cd..455aef96 100644 --- a/mindspore/core/mindrt/BUILD.gn +++ b/mindspore/core/mindrt/BUILD.gn @@ -39,6 +39,13 @@ ohos_source_set("mindrt_obj") { "../../core/", ] + defines = [ + "ENABLE_MINDRT", + "MS_COMPILE_OHOS", + ] + + external_deps = [ "hilog:libhilog" ] + remove_configs = [ "//build/config/compiler:no_rtti" ] part_name = "mindspore" diff --git a/mindspore/core/mindrt/src/thread/core_affinity.cc b/mindspore/core/mindrt/src/thread/core_affinity.cc index f329ca05..d8315431 100644 --- a/mindspore/core/mindrt/src/thread/core_affinity.cc +++ b/mindspore/core/mindrt/src/thread/core_affinity.cc @@ -347,12 +347,12 @@ int CoreAffinity::InitBindCoreId(size_t thread_num, BindMode bind_mode) { int CoreAffinity::SetAffinity() { return THREAD_OK; } #elif defined(BIND_CORE) int CoreAffinity::SetAffinity(const pthread_t &thread_id, cpu_set_t *cpu_set) { -#ifdef __ANDROID__ -#if __ANDROID_API__ >= 21 +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) +#if (__ANDROID_API__ >= 21) || defined(MS_COMPILE_OHOS) THREAD_INFO("thread: %d, mask: %lu", pthread_gettid_np(thread_id), cpu_set->__bits[0]); int ret = sched_setaffinity(pthread_gettid_np(thread_id), sizeof(cpu_set_t), cpu_set); if (ret != THREAD_OK) { - THREAD_ERROR("bind thread %d to cpu failed. ERROR %d", pthread_gettid_np(thread_id), ret); + THREAD_ERROR("bind thread %d to cpu failed. ERROR %{public}d", pthread_gettid_np(thread_id), ret); return THREAD_ERROR; } #endif diff --git a/mindspore/core/mindrt/src/thread/core_affinity.h b/mindspore/core/mindrt/src/thread/core_affinity.h index a61b66c7..cd267e71 100644 --- a/mindspore/core/mindrt/src/thread/core_affinity.h +++ b/mindspore/core/mindrt/src/thread/core_affinity.h @@ -23,7 +23,7 @@ #ifdef SERVER_INFERENCE #define BIND_CORE #endif -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) #define BIND_CORE #include #endif diff --git a/mindspore/core/mindrt/src/thread/threadlog.h b/mindspore/core/mindrt/src/thread/threadlog.h index 7ed917f1..5fc182ae 100644 --- a/mindspore/core/mindrt/src/thread/threadlog.h +++ b/mindspore/core/mindrt/src/thread/threadlog.h @@ -32,13 +32,36 @@ namespace mindspore { } #else #define THREAD_DEBUG(content, ...) -#define THREAD_INFO(content, ...) #define THREAD_TEST_TRUE(flag) + #if defined(__ANDROID__) +#define THREAD_INFO(content, ...) #include #define THREAD_ERROR(content, args...) \ { __android_log_print(ANDROID_LOG_ERROR, "MS_LITE", "%s|%d: " #content "\r\n", __func__, __LINE__, ##args); } + +#elif defined(MS_COMPILE_OHOS) // For OHOS, use hilog. + +#include "hilog/log.h" +#define MINDRT_OHOS_LOG_DOMAIN 0x2102 +#define MINDRT_OHOS_LOG_TAG "MS_LITE" + +#ifdef MS_COMPILE_WITH_OHOS_NDK +// When build with OHOS NDK, use public api of hilog module. +#define THREAD_INFO(content, args...) \ + { OH_LOG_Print(LOG_APP, LOG_INFO, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } +#define THREAD_ERROR(content, args...) \ + { OH_LOG_Print(LOG_APP, LOG_ERROR, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } #else +// When build in OHOS repo, use inner api of hilog module. +#define THREAD_INFO(content, args...) \ + { HiLogPrint(LOG_APP, LOG_INFO, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } +#define THREAD_ERROR(content, args...) \ + { HiLogPrint(LOG_APP, LOG_ERROR, MINDRT_OHOS_LOG_DOMAIN, MINDRT_OHOS_LOG_TAG, "%s:%d " #content, __func__, __LINE__, ##args); } +#endif + +#else +#define THREAD_INFO(content, ...) #define THREAD_ERROR(content, ...) #endif #endif diff --git a/mindspore/core/mindrt/src/thread/threadpool.cc b/mindspore/core/mindrt/src/thread/threadpool.cc index ec75bd6e..55392f85 100644 --- a/mindspore/core/mindrt/src/thread/threadpool.cc +++ b/mindspore/core/mindrt/src/thread/threadpool.cc @@ -53,10 +53,11 @@ void Worker::SetAffinity() { #ifdef _WIN32 SetWindowsSelfAffinity(core_id_); #elif defined(BIND_CORE) -#ifdef __ANDROID__ +#if defined(__ANDROID__) || defined(MS_COMPILE_OHOS) + THREAD_INFO("thread: %d, mask: %lu", gettid(), mask_.__bits[0]); int ret = sched_setaffinity(gettid(), sizeof(cpu_set_t), &mask_); if (ret != THREAD_OK) { - THREAD_ERROR("bind thread %d to cpu failed. ERROR %d", gettid(), errno); + THREAD_ERROR("bind thread %d to cpu failed. ERROR %{public}d", gettid(), errno); } return; #else -- 2.34.1