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 
16 #include "sched/frame_interval.h"
17 #include "dfx/log/ffrt_log_api.h"
18 #include "sched/workgroup_internal.h"
19 #include "util/ffrt_facade.h"
20 
21 #define GET_TID() syscall(SYS_gettid)
22 
23 namespace ffrt {
FrameInterval(uint64_t deadline,const QoS & qos)24 FrameInterval::FrameInterval(uint64_t deadline, const QoS& qos) : Interval(deadline, qos), qos(qos)
25 {
26     wg = nullptr;
27     isBegun = false;
28     wg = WorkgroupCreate(deadline);
29     if (wg == nullptr) {
30         FFRT_LOGE("[WorkGroup][Interface] Create WorkGroup Failed");
31     } else {
32         FFRTFacade::GetEUInstance().BindWG(DevType::CPU, this->qos);
33     }
34 }
35 
~FrameInterval()36 FrameInterval::~FrameInterval()
37 {
38     if (wg == nullptr) {
39         FFRT_LOGE("[Error] WorkGroup is nullptr");
40     } else {
41         WorkgroupClear(wg);
42     }
43 }
44 
OnQoSIntervals(IntervalState state)45 void FrameInterval::OnQoSIntervals(IntervalState state)
46 {
47     if (wg == nullptr) {
48         FFRT_LOGE("[Error] Interval's workgroup is null in %s", __func__);
49         return;
50     }
51     if (state == IntervalState::DEADLINE_BEGIN) {
52         WorkgroupStartInterval(wg);
53     } else if (state == IntervalState::DEADLINE_END) {
54         WorkgroupStopInterval(wg);
55     }
56 }
57 
Begin()58 int FrameInterval::Begin()
59 {
60     if (isBegun) {
61         FFRT_LOGD("[Error] Interval is already begun");
62         return -1;
63     }
64     isBegun = true;
65     OnQoSIntervals(ffrt::IntervalState::DEADLINE_BEGIN);
66 
67     return 0;
68 }
69 
End()70 void FrameInterval::End()
71 {
72     if (!isBegun) {
73         FFRT_LOGD("[Error] Interval is already end");
74         return;
75     }
76     isBegun = false;
77     OnQoSIntervals(ffrt::IntervalState::DEADLINE_END);
78 }
79 
Join()80 void FrameInterval::Join()
81 {
82     if (wg == nullptr) {
83         FFRT_LOGD("[Error] Interval has no workgroup");
84         return;
85     }
86     int tid = GET_TID();
87     WorkgroupJoin(wg, tid);
88 }
89 }
90