• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
139     if (memset_s(&syncMergeData, sizeof(struct sync_merge_data), 0, sizeof(struct sync_merge_data)) != EOK) {
140         HiLog::Error(LABEL, "SyncMerge memset sync merge data failed.");
141         return retCode;
142     }
143     syncMergeData.fd2 = fd2;
144     if (strcpy_s(syncMergeData.name, sizeof(syncMergeData.name), name)) {
145         HiLog::Error(LABEL, "SyncMerge ctrcpy fence name failed.");
146         return retCode;
147     }
148 
149     retCode = ioctl(fd1, SYNC_IOC_MERGE, &syncMergeData);
150     if (retCode < 0) {
151         errno = EINVAL;
152         HiLog::Error(LABEL, "Fence merge failed, errno is %{public}d.", errno);
153         return retCode;
154     }
155 
156     return syncMergeData.fence;
157 }
158 
MergeFence(const std::string & name,const sptr<SyncFence> & fence1,const sptr<SyncFence> & fence2)159 sptr<SyncFence> SyncFence::MergeFence(const std::string &name,
160                                       const sptr<SyncFence>& fence1, const sptr<SyncFence>& fence2)
161 {
162     int32_t newFenceFd = INVALID_FD;
163     int32_t fenceFd1 = fence1->fenceFd_;
164     int32_t fenceFd2 = fence2->fenceFd_;
165 
166     if (fenceFd1 >= 0 && fenceFd2 >= 0) {
167         newFenceFd = SyncFence::SyncMerge(name.c_str(), fenceFd1, fenceFd2);
168     } else if (fenceFd1 >= 0) {
169         newFenceFd = SyncFence::SyncMerge(name.c_str(), fenceFd1, fenceFd1);
170     } else if (fenceFd2 >= 0) {
171         newFenceFd = SyncFence::SyncMerge(name.c_str(), fenceFd2, fenceFd2);
172     } else {
173         return INVALID_FENCE;
174     }
175 
176     if (newFenceFd == INVALID_FD) {
177         HiLog::Error(LABEL, "sync_merge(%{public}s) failed, error: %{public}s (%{public}d)",
178                      name.c_str(), strerror(errno), errno);
179         return INVALID_FENCE;
180     }
181 
182     return sptr<SyncFence>(new SyncFence(newFenceFd));
183 }
184 
SyncFileReadTimestamp()185 ns_sec_t SyncFence::SyncFileReadTimestamp()
186 {
187     std::vector<SyncPointInfo> ptInfos = GetFenceInfo();
188     if (ptInfos.empty()) {
189         return FENCE_PENDING_TIMESTAMP;
190     }
191     size_t signalFenceCount = 0;
192     for (const auto &info : ptInfos) {
193         if (info.status == SIGNALED) {
194             signalFenceCount++;
195         }
196     }
197     if (signalFenceCount == ptInfos.size()) {
198         // fence signaled
199         uint64_t timestamp = 0;
200         for (const auto &ptInfo : ptInfos) {
201             if (ptInfo.timestampNs > timestamp) {
202                 timestamp = ptInfo.timestampNs;
203             }
204         }
205         return static_cast<ns_sec_t>(timestamp);
206     } else {
207         // fence still active
208         return FENCE_PENDING_TIMESTAMP;
209     }
210 }
211 
GetFenceInfo()212 std::vector<SyncPointInfo> SyncFence::GetFenceInfo()
213 {
214     struct sync_file_info arg;
215     struct sync_file_info *infoPtr = nullptr;
216 
217     (void)memset_s(&arg, sizeof(struct sync_file_info), 0, sizeof(struct sync_file_info));
218     int32_t ret = ioctl(fenceFd_, SYNC_IOC_FILE_INFO, &arg);
219     if (ret < 0) {
220         HiLog::Debug(LABEL, "GetFenceInfo SYNC_IOC_FILE_INFO ioctl failed, ret: %{public}d", ret);
221         return {};
222     }
223 
224     if (arg.num_fences <= 0) {
225         HiLog::Debug(LABEL, "GetFenceInfo arg.num_fences failed, num_fences: %{public}d", arg.num_fences);
226         return {};
227     }
228     // to malloc sync_file_info and the number of 'sync_fence_info' memory
229     size_t syncFileInfoMemSize = sizeof(struct sync_file_info) + sizeof(struct sync_fence_info) * arg.num_fences;
230     infoPtr = (struct sync_file_info *)malloc(syncFileInfoMemSize);
231     if (infoPtr == nullptr) {
232         HiLog::Debug(LABEL, "GetFenceInfo malloc failed oom");
233         return {};
234     }
235     (void)memset_s(infoPtr, syncFileInfoMemSize, 0, syncFileInfoMemSize);
236     infoPtr->num_fences = arg.num_fences;
237     infoPtr->sync_fence_info = static_cast<uint64_t>(uintptr_t(infoPtr + 1));
238 
239     ret = ioctl(fenceFd_, SYNC_IOC_FILE_INFO, infoPtr);
240     if (ret < 0) {
241         HiLog::Error(LABEL, "GetTotalFenceInfo SYNC_IOC_FILE_INFO ioctl failed, ret: %{public}d", ret);
242         free(infoPtr);
243         return {};
244     }
245     std::vector<SyncPointInfo> infos;
246     const auto fenceInfos = (struct sync_fence_info *)(uintptr_t)(infoPtr->sync_fence_info);
247     for (uint32_t i = 0; i < infoPtr->num_fences; i++) {
248         infos.push_back(SyncPointInfo { fenceInfos[i].timestamp_ns,
249             static_cast<FenceStatus>(fenceInfos[i].status) } );
250     }
251 
252     free(infoPtr);
253     return infos;
254 }
255 
Dup() const256 int32_t SyncFence::Dup() const
257 {
258     return ::dup(fenceFd_);
259 }
260 
GetStatus()261 FenceStatus SyncFence::GetStatus()
262 {
263     if (fenceFd_ < 0) {
264         return ERROR;
265     }
266     std::vector<SyncPointInfo> ptInfos = GetFenceInfo();
267     if (ptInfos.empty()) {
268         return ERROR;
269     }
270     size_t signalFenceCount = 0;
271     for (const auto &info : ptInfos) {
272         if (info.status == SIGNALED) {
273             signalFenceCount++;
274         }
275     }
276     if (signalFenceCount == ptInfos.size()) {
277         return SIGNALED;
278     } else {
279         return ACTIVE;
280     }
281 }
282 
Get() const283 int32_t SyncFence::Get() const
284 {
285     return fenceFd_;
286 }
287 
IsValid() const288 bool SyncFence::IsValid() const
289 {
290     return fenceFd_ != -1;
291 }
292 
ReadFromMessageParcel(MessageParcel & parcel)293 sptr<SyncFence> SyncFence::ReadFromMessageParcel(MessageParcel &parcel)
294 {
295     int32_t fence = parcel.ReadInt32();
296     if (fence < 0) {
297         return INVALID_FENCE;
298     }
299 
300     fence = parcel.ReadFileDescriptor();
301 
302     return sptr<SyncFence>(new SyncFence(fence));
303 }
304 
WriteToMessageParcel(MessageParcel & parcel)305 void SyncFence::WriteToMessageParcel(MessageParcel &parcel)
306 {
307     int32_t fence = fenceFd_;
308     if (fence >= 0 && fcntl(fence, F_GETFL) == -1 && errno == EBADF) {
309         fence = -1;
310     }
311 
312     parcel.WriteInt32(fence);
313 
314     if (fence < 0) {
315         HiLog::Debug(LABEL, "WriteToMessageParcel fence is invalid : %{public}d", fence);
316         return;
317     }
318 
319     parcel.WriteFileDescriptor(fence);
320 }
321 
322 } // namespace OHOS
323