1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "tm/cpu_task.h"
16 #include <securec.h>
17 #include "dfx/trace_record/ffrt_trace_record.h"
18 #include "dm/dependence_manager.h"
19 
20 #include "internal_inc/osal.h"
21 #include "tm/task_factory.h"
22 #include "util/ffrt_facade.h"
23 #include "util/slab.h"
24 
25 namespace {
26 const int TSD_SIZE = 128;
27 }
28 
29 namespace ffrt {
SetQos(const QoS & newQos)30 void CPUEUTask::SetQos(const QoS& newQos)
31 {
32     if (newQos == qos_inherit) {
33         if (!this->IsRoot()) {
34             this->qos = parent->qos;
35         } else {
36             this->qos = QoS();
37         }
38         FFRT_LOGD("Change task %s QoS %d", label.c_str(), this->qos());
39     } else {
40         this->qos = newQos;
41     }
42 }
43 
FreeMem()44 void CPUEUTask::FreeMem()
45 {
46     BboxCheckAndFreeze();
47     FFRTFacade::GetPPInstance().GetPoller(qos).ClearCachedEvents(this);
48 #ifdef FFRT_TASK_LOCAL_ENABLE
49     TaskTsdDeconstruct(this);
50 #endif
51     ffrt::TaskFactory::Free(this);
52 }
53 
Execute()54 void CPUEUTask::Execute()
55 {
56     FFRT_LOGD("Execute task[%lu], name[%s]", gid, label.c_str());
57     FFRTTraceRecord::TaskExecute(&(this->executeTime));
58     UpdateState(TaskState::RUNNING);
59     auto f = reinterpret_cast<ffrt_function_header_t*>(func_storage);
60     auto exp = ffrt::SkipStatus::SUBMITTED;
61     if (likely(__atomic_compare_exchange_n(&skipped, &exp, ffrt::SkipStatus::EXECUTED, 0,
62         __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))) {
63         f->exec(f);
64     }
65     f->destroy(f);
66     FFRT_TASKDONE_MARKER(gid);
67     if (!USE_COROUTINE) {
68         this->UpdateState(ffrt::TaskState::EXITED);
69     } else {
70         this->coRoutine->isTaskDone = true;
71     }
72 }
73 
CPUEUTask(const task_attr_private * attr,CPUEUTask * parent,const uint64_t & id,const QoS & qos)74 CPUEUTask::CPUEUTask(const task_attr_private *attr, CPUEUTask *parent, const uint64_t &id,
75     const QoS &qos)
76     : parent(parent), rank(id), qos(qos)
77 {
78     fq_we.task = this;
79     if (attr && !attr->name_.empty()) {
80         label = attr->name_;
81     } else if (IsRoot()) {
82         label = "root";
83     } else if (parent->IsRoot()) {
84         label = "t" + std::to_string(rank);
85     } else {
86         label = parent->label + "." + std::to_string(rank);
87     }
88     if (!IsRoot()) {
89         FFRT_SUBMIT_MARKER(label, gid);
90     }
91 
92     taskLocal = false;
93     tsd = nullptr;
94     if (attr && attr->taskLocal_) {
95         tsd = (void **)malloc(TSD_SIZE * sizeof(void *));
96         if (unlikely(tsd == nullptr)) {
97             FFRT_LOGE("task local malloc tsd failed");
98             return;
99         }
100         memset_s(tsd, TSD_SIZE * sizeof(void *), 0, TSD_SIZE * sizeof(void *));
101         taskLocal = attr->taskLocal_;
102     }
103     if (attr) {
104         stack_size = std::max(attr->stackSize_, MIN_STACK_SIZE);
105     }
106 }
107 } /* namespace ffrt */