1 /*
2 * Copyright (c) 2021-2022 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 "sync_fence.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstdlib>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <securec.h>
24 #include <fcntl.h>
25 #include <sys/poll.h>
26 #include <linux/sync_file.h>
27 #include <sys/ioctl.h>
28 #include "sw_sync.h"
29 #include "hilog/log.h"
30
31 namespace OHOS {
32 using namespace OHOS::HiviewDFX;
33
34 namespace {
35 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD001400, "SyncFence" };
36 constexpr int32_t INVALID_FD = -1;
37 } // namespace
38
39 const sptr<SyncFence> SyncFence::INVALID_FENCE = sptr<SyncFence>(new SyncFence(INVALID_FD));
40 const ns_sec_t SyncFence::INVALID_TIMESTAMP = -1;
41 const ns_sec_t SyncFence::FENCE_PENDING_TIMESTAMP = INT64_MAX;
42
SyncTimeline()43 SyncTimeline::SyncTimeline() noexcept
44 {
45 if (!IsSupportSoftwareSync()) {
46 HiLog::Error(LABEL, "don't support SyncTimeline");
47 return;
48 }
49 int32_t fd = CreateSyncTimeline();
50 if (fd > 0) {
51 timeLineFd_ = fd;
52 }
53 }
54
~SyncTimeline()55 SyncTimeline::~SyncTimeline() noexcept
56 {
57 if (timeLineFd_ > 0) {
58 close(timeLineFd_);
59 timeLineFd_ = -1;
60 isValid_ = false;
61 }
62 }
63
IncreaseSyncPoint(uint32_t step)64 int32_t SyncTimeline::IncreaseSyncPoint(uint32_t step)
65 {
66 if (timeLineFd_ < 0) {
67 return -1;
68 }
69 return IncreaseSyncPointOnTimeline(timeLineFd_, step);
70 }
71
IsValid()72 bool SyncTimeline::IsValid()
73 {
74 if (timeLineFd_ > 0) {
75 if (fcntl(timeLineFd_, F_GETFD, 0) < 0) {
76 return false;
77 } else {
78 return true;
79 }
80 } else {
81 return false;
82 }
83 }
84
GenerateFence(std::string name,uint32_t point)85 int32_t SyncTimeline::GenerateFence(std::string name, uint32_t point)
86 {
87 if (timeLineFd_ < 0) {
88 return -1;
89 }
90 int32_t fd = CreateSyncFence(timeLineFd_, name.c_str(), point);
91 if (fd < 0) {
92 HiLog::Error(LABEL, "Fail to CreateSyncFence");
93 return -1;
94 }
95 return fd;
96 }
97
SyncFence(int32_t fenceFd)98 SyncFence::SyncFence(int32_t fenceFd) : fenceFd_(fenceFd)
99 {
100 }
101
~SyncFence()102 SyncFence::~SyncFence()
103 {
104 }
105
Wait(uint32_t timeout)106 int32_t SyncFence::Wait(uint32_t timeout)
107 {
108 int retCode = -1;
109 if (fenceFd_ < 0) {
110 return retCode;
111 }
112
113 struct pollfd pollfds = {0};
114 pollfds.fd = fenceFd_;
115 pollfds.events = POLLIN;
116
117 do {
118 retCode = poll(&pollfds, 1, timeout);
119 } while (retCode == -1 && (errno == EINTR || errno == EAGAIN));
120
121 if (retCode == 0) {
122 retCode = -1;
123 errno = ETIME;
124 } else if (retCode > 0) {
125 retCode = 0;
126 if (pollfds.revents & (POLLERR | POLLNVAL)) {
127 retCode = -1;
128 errno = EINVAL;
129 }
130 }
131
132 return retCode < 0 ? -errno : 0;
133 }
134
SyncMerge(const char * name,int fd1,int fd2)135 int SyncFence::SyncMerge(const char *name, int fd1, int fd2)
136 {
137 int retCode = -1;
138 struct sync_merge_data syncMergeData = {0};
139 syncMergeData.fd2 = fd2;
140 if (strcpy_s(syncMergeData.name, sizeof(syncMergeData.name), name)) {
141 HiLog::Error(LABEL, "SyncMerge ctrcpy fence name failed.");
142 return retCode;
143 }
144
145 retCode = ioctl(fd1, SYNC_IOC_MERGE, &syncMergeData);
146 if (retCode < 0) {
147 errno = EINVAL;
148 HiLog::Error(LABEL, "Fence merge failed, errno is %{public}d.", errno);
149 return retCode;
150 }
151
152 return syncMergeData.fence;
153 }
154
MergeFence(const std::string & name,const sptr<SyncFence> & fence1,const sptr<SyncFence> & fence2)155 sptr<SyncFence> SyncFence::MergeFence(const std::string &name,
156 const sptr<SyncFence>& fence1, const sptr<SyncFence>& fence2)
157 {
158 int32_t newFenceFd = INVALID_FD;
159 int32_t fenceFd1 = fence1->fenceFd_;
160 int32_t fenceFd2 = fence2->fenceFd_;
161
162 if (fenceFd1 >= 0 && fenceFd2 >= 0) {
163 newFenceFd = SyncFence::SyncMerge(name.c_str(), fenceFd1, fenceFd2);
164 } else if (fenceFd1 >= 0) {
165 newFenceFd = SyncFence::SyncMerge(name.c_str(), fenceFd1, fenceFd1);
166 } else if (fenceFd2 >= 0) {
167 newFenceFd = SyncFence::SyncMerge(name.c_str(), fenceFd2, fenceFd2);
168 } else {
169 return INVALID_FENCE;
170 }
171
172 if (newFenceFd == INVALID_FD) {
173 HiLog::Error(LABEL, "sync_merge(%{public}s) failed, error: %{public}s (%{public}d)",
174 name.c_str(), strerror(errno), errno);
175 return INVALID_FENCE;
176 }
177
178 return sptr<SyncFence>(new SyncFence(newFenceFd));
179 }
180
SyncFileReadTimestamp()181 ns_sec_t SyncFence::SyncFileReadTimestamp()
182 {
183 std::vector<SyncPointInfo> ptInfos = GetFenceInfo();
184 if (ptInfos.empty()) {
185 return FENCE_PENDING_TIMESTAMP;
186 }
187 size_t signalFenceCount = 0;
188 for (const auto &info : ptInfos) {
189 if (info.status == SIGNALED) {
190 signalFenceCount++;
191 }
192 }
193 if (signalFenceCount == ptInfos.size()) {
194 // fence signaled
195 uint64_t timestamp = 0;
196 for (const auto &ptInfo : ptInfos) {
197 if (ptInfo.timestampNs > timestamp) {
198 timestamp = ptInfo.timestampNs;
199 }
200 }
201 return static_cast<ns_sec_t>(timestamp);
202 } else {
203 // fence still active
204 return FENCE_PENDING_TIMESTAMP;
205 }
206 }
207
GetFenceInfo()208 std::vector<SyncPointInfo> SyncFence::GetFenceInfo()
209 {
210 struct sync_file_info arg;
211 struct sync_file_info *infoPtr = nullptr;
212
213 (void)memset_s(&arg, sizeof(struct sync_file_info), 0, sizeof(struct sync_file_info));
214 int32_t ret = ioctl(fenceFd_, SYNC_IOC_FILE_INFO, &arg);
215 if (ret < 0) {
216 HiLog::Debug(LABEL, "GetFenceInfo SYNC_IOC_FILE_INFO ioctl failed, ret: %{public}d", ret);
217 return {};
218 }
219
220 if (arg.num_fences <= 0) {
221 HiLog::Debug(LABEL, "GetFenceInfo arg.num_fences failed, num_fences: %{public}d", arg.num_fences);
222 return {};
223 }
224 // to malloc sync_file_info and the number of 'sync_fence_info' memory
225 size_t syncFileInfoMemSize = sizeof(struct sync_file_info) + sizeof(struct sync_fence_info) * arg.num_fences;
226 infoPtr = (struct sync_file_info *)malloc(syncFileInfoMemSize);
227 if (infoPtr == nullptr) {
228 HiLog::Debug(LABEL, "GetFenceInfo malloc failed oom");
229 return {};
230 }
231 (void)memset_s(infoPtr, syncFileInfoMemSize, 0, syncFileInfoMemSize);
232 infoPtr->num_fences = arg.num_fences;
233 infoPtr->sync_fence_info = static_cast<uint64_t>(uintptr_t(infoPtr + 1));
234
235 ret = ioctl(fenceFd_, SYNC_IOC_FILE_INFO, infoPtr);
236 if (ret < 0) {
237 HiLog::Error(LABEL, "GetTotalFenceInfo SYNC_IOC_FILE_INFO ioctl failed, ret: %{public}d", ret);
238 free(infoPtr);
239 return {};
240 }
241 std::vector<SyncPointInfo> infos;
242 const auto fenceInfos = (struct sync_fence_info *)(uintptr_t)(infoPtr->sync_fence_info);
243 for (uint32_t i = 0; i < infoPtr->num_fences; i++) {
244 infos.push_back(SyncPointInfo { fenceInfos[i].timestamp_ns,
245 static_cast<FenceStatus>(fenceInfos[i].status) } );
246 }
247
248 free(infoPtr);
249 return infos;
250 }
251
Dup() const252 int32_t SyncFence::Dup() const
253 {
254 return ::dup(fenceFd_);
255 }
256
GetStatus()257 FenceStatus SyncFence::GetStatus()
258 {
259 if (fenceFd_ < 0) {
260 return ERROR;
261 }
262 std::vector<SyncPointInfo> ptInfos = GetFenceInfo();
263 if (ptInfos.empty()) {
264 return ERROR;
265 }
266 size_t signalFenceCount = 0;
267 for (const auto &info : ptInfos) {
268 if (info.status == SIGNALED) {
269 signalFenceCount++;
270 }
271 }
272 if (signalFenceCount == ptInfos.size()) {
273 return SIGNALED;
274 } else {
275 return ACTIVE;
276 }
277 }
278
Get() const279 int32_t SyncFence::Get() const
280 {
281 return fenceFd_;
282 }
283
IsValid() const284 bool SyncFence::IsValid() const
285 {
286 return fenceFd_ != -1;
287 }
288
ReadFromMessageParcel(MessageParcel & parcel)289 sptr<SyncFence> SyncFence::ReadFromMessageParcel(MessageParcel &parcel)
290 {
291 int32_t fence = parcel.ReadInt32();
292 if (fence < 0) {
293 return INVALID_FENCE;
294 }
295
296 fence = parcel.ReadFileDescriptor();
297
298 return sptr<SyncFence>(new SyncFence(fence));
299 }
300
WriteToMessageParcel(MessageParcel & parcel)301 void SyncFence::WriteToMessageParcel(MessageParcel &parcel)
302 {
303 int32_t fence = fenceFd_;
304 if (fence >= 0 && fcntl(fence, F_GETFL) == -1 && errno == EBADF) {
305 fence = -1;
306 }
307
308 parcel.WriteInt32(fence);
309
310 if (fence < 0) {
311 HiLog::Debug(LABEL, "WriteToMessageParcel fence is invalid : %{public}d", fence);
312 return;
313 }
314
315 parcel.WriteFileDescriptor(fence);
316 }
317
318 } // namespace OHOS
319