• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cpp/deadline.h"
17 #include "c/deadline.h"
18 #include "internal_inc/osal.h"
19 #include "sched/interval.h"
20 #include "dm/dependence_manager.h"
21 #include "sched/frame_interval.h"
22 #include "dfx/log/ffrt_log_api.h"
23 
24 constexpr uint64_t MAX_US_COUNT = static_cast<uint64_t>(std::chrono::microseconds::max().count());
25 
26 namespace ffrt {
27 class QosIntervalPrivate {
28 public:
29     template <typename... Args>
QosIntervalPrivate(uint64_t deadlineUs,const QoS & qos)30     QosIntervalPrivate(uint64_t deadlineUs, const QoS& qos)
31     {
32         if (qos == qos_user_interactive || qos > qos_max) {
33             it = std::make_unique<FrameInterval>(deadlineUs, qos);
34         } else {
35             it = std::make_unique<DefaultInterval>(deadlineUs, qos);
36         }
37     }
38 
operator ->()39     Interval* operator->()
40     {
41         if (it == nullptr) {
42             FFRT_SYSEVENT_LOGE("Invalid QoS Interval!");
43             return nullptr;
44         }
45         return it.get();
46     }
47 
48 private:
49     std::unique_ptr<Interval> it;
50 };
51 }; // namespace ffrt
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 API_ATTRIBUTE((visibility("default")))
ffrt_interval_create(uint64_t deadline_us,ffrt_qos_t qos)57 ffrt_interval_t ffrt_interval_create(uint64_t deadline_us, ffrt_qos_t qos)
58 {
59     if (qos < static_cast<int>(ffrt_qos_deadline_request)) {
60         FFRT_LOGE("Invalid QoS Interval!");
61         return nullptr;
62     }
63 
64     if (deadline_us > MAX_US_COUNT) {
65         FFRT_LOGW("Deadline exceeds maximum allowed value %llu us. Clamping to %llu us.", deadline_us, MAX_US_COUNT);
66         deadline_us = MAX_US_COUNT;
67     }
68 
69     return new ffrt::QosIntervalPrivate(deadline_us, qos);
70 }
71 
72 API_ATTRIBUTE((visibility("default")))
ffrt_interval_update(ffrt_interval_t it,uint64_t new_deadline_us)73 int ffrt_interval_update(ffrt_interval_t it, uint64_t new_deadline_us)
74 {
75     if (!it) {
76         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
77         return ffrt_error;
78     }
79 
80     if (new_deadline_us > MAX_US_COUNT) {
81         FFRT_LOGW("Deadline exceeds maximum allowed value %llu us. Clamping to %llu us.", new_deadline_us,
82             MAX_US_COUNT);
83         new_deadline_us = MAX_US_COUNT;
84     }
85 
86     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
87 
88     (*_it)->Update(new_deadline_us);
89     return ffrt_success;
90 }
91 
92 API_ATTRIBUTE((visibility("default")))
ffrt_interval_begin(ffrt_interval_t it)93 int ffrt_interval_begin(ffrt_interval_t it)
94 {
95     if (!it) {
96         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
97         return ffrt_error;
98     }
99 
100     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
101 
102     return (*_it)->Begin();
103 }
104 
105 API_ATTRIBUTE((visibility("default")))
ffrt_interval_end(ffrt_interval_t it)106 int ffrt_interval_end(ffrt_interval_t it)
107 {
108     if (!it) {
109         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
110         return ffrt_error;
111     }
112 
113     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
114 
115     (*_it)->End();
116     return ffrt_success;
117 }
118 
119 API_ATTRIBUTE((visibility("default")))
ffrt_interval_destroy(ffrt_interval_t it)120 void ffrt_interval_destroy(ffrt_interval_t it)
121 {
122     if (!it) {
123         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
124         return;
125     }
126 
127     delete static_cast<ffrt::QosIntervalPrivate *>(it);
128 }
129 
130 API_ATTRIBUTE((visibility("default")))
ffrt_interval_join(ffrt_interval_t it)131 int ffrt_interval_join(ffrt_interval_t it)
132 {
133     if (!it) {
134         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
135         return ffrt_error;
136     }
137 
138     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
139 
140     (*_it)->Join();
141     return ffrt_success;
142 }
143 
144 API_ATTRIBUTE((visibility("default")))
ffrt_interval_leave(ffrt_interval_t it)145 int ffrt_interval_leave(ffrt_interval_t it)
146 {
147     if (!it) {
148         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
149         return ffrt_error;
150     }
151 
152     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
153 
154     (*_it)->Leave();
155     return ffrt_success;
156 }
157 #ifdef __cplusplus
158 }
159 #endif
160