sync from openharmony/resourceschedule_ffrt

Signed-off-by: wenxingqi <wenxingqi1@h-partners.com>
This commit is contained in:
wenxingqi 2024-11-21 19:28:59 +08:00
parent c671b6cfed
commit a9d24161ef
7 changed files with 41 additions and 59 deletions

View File

@ -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<CoRoutine*>(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<int>(CoStatus::CO_NOT_FINISH)) {
for (auto name : task->traceTag) {
for (auto& name : task->traceTag) {
FFRT_TRACE_BEGIN(name.c_str());
}
}

View File

@ -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

View File

@ -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<std::shared_mutex> lck(groupCtl[qos].tgMutex);
bool isEmpty = groupCtl[qos].threads.empty();
lck.unlock();
if (!isEmpty) {
SimpleAllocator<WaitUntilEntry>::FreeMem(static_cast<WaitUntilEntry*>(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<const CPUWorker*>(thread)->priority_task ||
reinterpret_cast<const CPUWorker*>(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());

View File

@ -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<void*>(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<SCPUEUTask*>(task)->waitCond_.wait(lck);
@ -235,7 +221,7 @@ int Poller::WaitFdEvent(struct epoll_event* eventsVec, int maxevents, int timeou
m_waitTaskMap[task] = {static_cast<void*>(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<CPUEUTask*, EventVec>& syncTaskEven
return;
}
std::unordered_set<int> timerHandlesToRemove;
std::unordered_set<CPUEUTask*> tasksToWake;
m_mapMutex.lock();
for (auto& taskEventPair : syncTaskEvents) {
CPUEUTask* currTask = taskEventPair.first;
@ -354,28 +338,11 @@ void Poller::WakeSyncTask(std::unordered_map<CPUEUTask*, EventVec>& 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

View File

@ -124,7 +124,7 @@ private:
char* p = reinterpret_cast<char*>(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<T*>(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();

View File

@ -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)

View File

@ -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
}