From a9d24161efd8aac415b9d73ec4eead82c3f4b70c Mon Sep 17 00:00:00 2001 From: wenxingqi Date: Thu, 21 Nov 2024 19:28:59 +0800 Subject: [PATCH] sync from openharmony/resourceschedule_ffrt Signed-off-by: wenxingqi --- src/eu/co_routine.cpp | 6 ++--- src/eu/co_routine.h | 8 +++++- src/eu/scpuworker_manager.cpp | 16 +++++------- src/sync/poller.cpp | 41 +++--------------------------- src/util/slab.h | 8 +++--- test/ut/testcase/ut_cgroup_qos.cpp | 14 +++++++--- test/ut/testcase/ut_dump.cpp | 7 ++++- 7 files changed, 41 insertions(+), 59 deletions(-) diff --git a/src/eu/co_routine.cpp b/src/eu/co_routine.cpp index 85a02d7..6a44791 100644 --- a/src/eu/co_routine.cpp +++ b/src/eu/co_routine.cpp @@ -161,7 +161,7 @@ void UpdateWorkerTsdValueToThread(void** taskTsd) FFRT_UNLIKELY_COND_DO_ABORT((threadVal && taskVal && (threadVal != taskVal)), "FFRT abort: mismatch key=[%u]", key); FFRT_UNLIKELY_COND_DO_ABORT((threadVal && !taskVal), - "FFRT abort: unexpected: thread exists but task not exist, key=[%u]", key); + "FFRT abort: unexpected: thread exists but task not exists, key=[%u]", key); } taskTsd[key] = nullptr; } @@ -234,7 +234,7 @@ static inline void CoStartEntry(void* arg) CoRoutine* co = reinterpret_cast(arg); #ifdef ASAN_MODE /* thread to co finish first */ - __sanitizer_finish_switch_fiber(co->asanFakeStack, (void **)&co->asanFiberAddr, &co->asanFiberSize); + __sanitizer_finish_switch_fiber(co->asanFakeStack, (const void **)&co->asanFiberAddr, &co->asanFiberSize); #endif ffrt::CPUEUTask* task = co->task; bool isNormalTask = false; @@ -390,7 +390,7 @@ static inline int CoCreat(ffrt::CPUEUTask* task) static inline void CoSwitchInTransaction(ffrt::CPUEUTask* task) { if (task->coRoutine->status == static_cast(CoStatus::CO_NOT_FINISH)) { - for (auto name : task->traceTag) { + for (auto& name : task->traceTag) { FFRT_TRACE_BEGIN(name.c_str()); } } diff --git a/src/eu/co_routine.h b/src/eu/co_routine.h index e5dbf2d..b6b5dec 100644 --- a/src/eu/co_routine.h +++ b/src/eu/co_routine.h @@ -33,6 +33,12 @@ constexpr size_t STACK_MAGIC = 0x7BCDABCDABCDABCD; #define FFRT_STACK_SIZE (1 << 20) #endif +#ifdef ASAN_MODE +extern "C" void __sanitizer_start_switch_fiber(void **fake_stack_save, const void *bottom, size_t size); +extern "C" void __sanitizer_finish_switch_fiber(void *fake_stack_save, const void **bottom_old, size_t *size_old); +extern "C" void __asan_handle_no_return(); +#endif + namespace ffrt { class CPUEUTask; struct WaitEntry; @@ -80,7 +86,7 @@ struct CoRoutine { CoRoutineEnv* thEnv; ffrt::CPUEUTask* task; #ifdef ASAN_MODE - void *asanFakeStack = nullptr; // not finished, need further verification + void *asanFakeStack = nullptr; // not finished, need further verification const void *asanFiberAddr = nullptr; size_t asanFiberSize = 0; #endif diff --git a/src/eu/scpuworker_manager.cpp b/src/eu/scpuworker_manager.cpp index 3c13017..54e5f08 100644 --- a/src/eu/scpuworker_manager.cpp +++ b/src/eu/scpuworker_manager.cpp @@ -32,7 +32,7 @@ namespace { /* SUPPORT_WORKER_DESTRUCT indicates that the idle thread destruction function is supported. - * The stack canary is saved or destored during coroutine switch-out and switch-in, + * The stack canary is saved or restored during coroutine switch-out and switch-in, * currently, only the stack canary used by the ohos compiler stack protection is global * and is not affected by worker destruction. */ @@ -110,7 +110,7 @@ void SCPUWorkerManager::WorkerRetiredSimplified(WorkerThread* thread) worker = nullptr; } - //qos has no worker, start delay worker to monitor task + // qos has no worker, start delay worker to monitor task if (isEmptyQosThreads) { std::shared_mutex& exitMtx = GetExitMtx(); exitMtx.lock_shared(); @@ -188,7 +188,7 @@ void SCPUWorkerManager::AddDelayedTask(int qos) std::unique_lock lck(groupCtl[qos].tgMutex); bool isEmpty = groupCtl[qos].threads.empty(); lck.unlock(); - + if (!isEmpty) { SimpleAllocator::FreeMem(static_cast(we)); FFRT_LOGW("qos[%d] has worker, no need add delayed task", qos); @@ -223,10 +223,8 @@ WorkerAction SCPUWorkerManager::WorkerIdleAction(const WorkerThread* thread) #ifdef FFRT_WORKERS_DYNAMIC_SCALING BlockawareEnterSleeping(); #endif - if (ctl.cv.wait_for(lk, std::chrono::seconds(waiting_seconds), [this, thread] { - bool taskExistence = GetTaskCount(thread->GetQos()) || - reinterpret_cast(thread)->priority_task || - reinterpret_cast(thread)->localFifo.GetLength(); + if (ctl.cv.wait_for(lk, std::chrono::seconds(waiting_seconds), [this, thread] { + bool taskExistence = GetTaskCount(thread->GetQos()); bool needPoll = !FFRTFacade::GetPPInstance().GetPoller(thread->GetQos()).DetermineEmptyMap() && (polling_[thread->GetQos()] == 0); return tearDown || taskExistence || needPoll; @@ -286,12 +284,12 @@ WorkerAction SCPUWorkerManager::WorkerIdleActionSimplified(const WorkerThread* t monitor->WakeupDeepSleep(thread->GetQos()); return WorkerAction::RETRY; #else - monitor->TimeoutCount(thread->GetQos()); + monitor->TimeoutCount(thread->GetQos()); return WorkerAction::RETIRE; #endif } } - + void SCPUWorkerManager::WorkerPrepare(WorkerThread* thread) { WorkerJoinTg(thread->GetQos(), thread->Id()); diff --git a/src/sync/poller.cpp b/src/sync/poller.cpp index aaa2e85..a914fd2 100644 --- a/src/sync/poller.cpp +++ b/src/sync/poller.cpp @@ -90,20 +90,6 @@ int Poller::DelFdEvent(int fd) noexcept return -1; } - for (auto it = m_cachedTaskEvents.begin(); it != m_cachedTaskEvents.end();) { - auto& events = it->second; - events.erase(std::remove_if(events.begin(), events.end(), - [fd](const epoll_event& event) { - return event.data.fd == fd; - }), events.end()); - - if (events.empty()) { - it = m_cachedTaskEvents.erase(it); - } else { - ++it; - } - } - m_delCntMap[fd]++; WakeUp(); return 0; @@ -207,7 +193,7 @@ int Poller::WaitFdEvent(struct epoll_event* eventsVec, int maxevents, int timeou m_waitTaskMap[task] = {static_cast(eventsVec), maxevents, &nfds, currTime}; if (timeout > -1) { FFRT_LOGD("poller meet timeout={%d}", timeout); - m_waitTaskMap[task].timerHandle = RegisterTimer(timeout, nullptr, nullptr); + RegisterTimer(timeout, nullptr, nullptr); } m_mapMutex.unlock(); reinterpret_cast(task)->waitCond_.wait(lck); @@ -235,7 +221,7 @@ int Poller::WaitFdEvent(struct epoll_event* eventsVec, int maxevents, int timeou m_waitTaskMap[task] = {static_cast(eventsVec), maxevents, &nfds, currTime}; if (timeout > -1) { FFRT_LOGD("poller meet timeout={%d}", timeout); - m_waitTaskMap[task].timerHandle = RegisterTimer(timeout, nullptr, nullptr); + RegisterTimer(timeout, nullptr, nullptr); } m_mapMutex.unlock(); // The ownership of the task belongs to m_waitTaskMap, and the task cannot be accessed any more. @@ -342,8 +328,6 @@ void Poller::WakeSyncTask(std::unordered_map& syncTaskEven return; } - std::unordered_set timerHandlesToRemove; - std::unordered_set tasksToWake; m_mapMutex.lock(); for (auto& taskEventPair : syncTaskEvents) { CPUEUTask* currTask = taskEventPair.first; @@ -354,28 +338,11 @@ void Poller::WakeSyncTask(std::unordered_map& syncTaskEven } CopyEventsInfoToConsumer(iter->second, taskEventPair.second); - auto timerHandle = iter->second.timerHandle; - if (timerHandle > -1) { - timerHandlesToRemove.insert(timerHandle); - } - tasksToWake.insert(currTask); m_waitTaskMap.erase(iter); + + WakeTask(currTask); } m_mapMutex.unlock(); - if (timerHandlesToRemove.size() > 0) { - std::lock_guard lock(timerMutex_); - for (auto cur = timerMap_.begin(); cur != timerMap_.end(); cur++) { - if (timerHandlesToRemove.find(cur->second.handle) != timerHandlesToRemove.end()) { - timerMap_.erase(cur); - break; - } - } - timerEmpty_.store(timerMap_.empty()); - } - - for (auto task : tasksToWake) { - WakeTask(task); - } } uint64_t Poller::GetTaskWaitTime(CPUEUTask* task) noexcept diff --git a/src/util/slab.h b/src/util/slab.h index de8329c..aa2f50c 100644 --- a/src/util/slab.h +++ b/src/util/slab.h @@ -124,7 +124,7 @@ private: char* p = reinterpret_cast(std::calloc(1, MmapSz)); if (p == nullptr) { FFRT_LOGE("calloc failed"); - std::terminate; + std::terminate(); } count = MmapSz / TSize; primaryCache.reserve(count); @@ -143,12 +143,12 @@ private: t = reinterpret_cast(std::calloc(1, TSize)); if (t == nullptr) { FFRT_LOGE("calloc failed"); - std::terminate; + std::terminate(); } #ifdef FFRT_BBOX_ENABLE - secondaryCache.insert(t); + secondaryCache.insert(t); #endif - lock.unlock(); + lock.unlock(); return t; } init(); diff --git a/test/ut/testcase/ut_cgroup_qos.cpp b/test/ut/testcase/ut_cgroup_qos.cpp index 7bfd673..e4142c7 100644 --- a/test/ut/testcase/ut_cgroup_qos.cpp +++ b/test/ut/testcase/ut_cgroup_qos.cpp @@ -238,10 +238,15 @@ HWTEST_F(QosInterfaceTest, FFRTAuthGetTest, TestSize.Level1) unsigned int uid = 3039; unsigned int uaFlag = 0x1fff; unsigned int status = 3; - - FFRTAuthEnable(uid, uaFlag, status); + int ret = 0; + ret = FFRTAuthEnable(uid, uaFlag, status); FFRTAuthGet(uid, &uaFlag, &status); - EXPECT_EQ(status, 3); + + if (ret < 0) { + EXPECT_EQ(status, 0); + } else { + EXPECT_EQ(status, 3); + } } HWTEST_F(QosInterfaceTest, FFRTQosApplyTest, TestSize.Level1) @@ -263,7 +268,8 @@ HWTEST_F(QosInterfaceTest, FFRTQosApplyForOtherTest, TestSize.Level1) HWTEST_F(QosInterfaceTest, FFRTQosLeaveTest, TestSize.Level1) { - FFRTQosLeave(); + int ret = FFRTQosLeave(); + EXPECT_EQ(ret, 0); } HWTEST_F(QosInterfaceTest, FFRTQosLeaveForOtherTest, TestSize.Level1) diff --git a/test/ut/testcase/ut_dump.cpp b/test/ut/testcase/ut_dump.cpp index f0b0b33..c7b2d46 100644 --- a/test/ut/testcase/ut_dump.cpp +++ b/test/ut/testcase/ut_dump.cpp @@ -51,5 +51,10 @@ protected: HWTEST_F(DumpTest, dump_succ, TestSize.Level1) { char dumpinfo[1024 * 512] = {0}; - ffrt_dump(ffrt_dump_cmd_t::DUMP_INFO_ALL, dumpinfo, 1024 * 512); + int ret = ffrt_dump(ffrt_dump_cmd_t::DUMP_INFO_ALL, dumpinfo, 1024 * 512); +#ifdef FFRT_CO_BACKTRACE_OH_ENABLE + EXPECT_NE(ret, -1); +#else + EXPECT_EQ(ret, -1); +#endif } \ No newline at end of file