• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #include <ui/FenceTime.h>
18 
19 #define LOG_TAG "FenceTime"
20 
21 #include <cutils/compiler.h>  // For CC_[UN]LIKELY
22 #include <utils/Log.h>
23 #include <inttypes.h>
24 #include <stdlib.h>
25 
26 #include <memory>
27 
28 namespace android {
29 
30 // ============================================================================
31 // FenceTime
32 // ============================================================================
33 
34 const auto FenceTime::NO_FENCE = std::make_shared<FenceTime>(Fence::NO_FENCE);
35 
FenceTime(const sp<Fence> & fence)36 FenceTime::FenceTime(const sp<Fence>& fence)
37   : mState(((fence.get() != nullptr) && fence->isValid()) ?
38             State::VALID : State::INVALID),
39     mFence(fence),
40     mSignalTime(mState == State::INVALID ?
41             Fence::SIGNAL_TIME_INVALID : Fence::SIGNAL_TIME_PENDING) {
42 }
43 
FenceTime(sp<Fence> && fence)44 FenceTime::FenceTime(sp<Fence>&& fence)
45   : mState(((fence.get() != nullptr) && fence->isValid()) ?
46             State::VALID : State::INVALID),
47     mFence(std::move(fence)),
48     mSignalTime(mState == State::INVALID ?
49             Fence::SIGNAL_TIME_INVALID : Fence::SIGNAL_TIME_PENDING) {
50 }
51 
FenceTime(nsecs_t signalTime)52 FenceTime::FenceTime(nsecs_t signalTime)
53   : mState(Fence::isValidTimestamp(signalTime) ? State::VALID : State::INVALID),
54     mFence(nullptr),
55     mSignalTime(signalTime) {
56     if (CC_UNLIKELY(mSignalTime == Fence::SIGNAL_TIME_PENDING)) {
57         ALOGE("Pending signal time not allowed after signal.");
58         mSignalTime = Fence::SIGNAL_TIME_INVALID;
59     }
60 }
61 
applyTrustedSnapshot(const Snapshot & src)62 void FenceTime::applyTrustedSnapshot(const Snapshot& src) {
63     if (CC_UNLIKELY(src.state != Snapshot::State::SIGNAL_TIME)) {
64         // Applying Snapshot::State::FENCE, could change the valid state of the
65         // FenceTime, which is not allowed. Callers should create a new
66         // FenceTime from the snapshot instead.
67         ALOGE("applyTrustedSnapshot: Unexpected fence.");
68         return;
69     }
70 
71     if (src.state == Snapshot::State::EMPTY) {
72         return;
73     }
74 
75     nsecs_t signalTime = mSignalTime.load(std::memory_order_relaxed);
76     if (signalTime != Fence::SIGNAL_TIME_PENDING) {
77         // We should always get the same signalTime here that we did in
78         // getSignalTime(). This check races with getSignalTime(), but it is
79         // only a sanity check so that's okay.
80         if (CC_UNLIKELY(signalTime != src.signalTime)) {
81             ALOGE("FenceTime::applyTrustedSnapshot: signalTime mismatch. "
82                     "(%" PRId64 " (old) != %" PRId64 " (new))",
83                     signalTime, src.signalTime);
84         }
85         return;
86     }
87 
88     std::lock_guard<std::mutex> lock(mMutex);
89     mFence.clear();
90     mSignalTime.store(src.signalTime, std::memory_order_relaxed);
91 }
92 
isValid() const93 bool FenceTime::isValid() const {
94     // We store the valid state in the constructors and return it here.
95     // This lets release code remember the valid state even after the
96     // underlying fence is destroyed.
97     return mState != State::INVALID;
98 }
99 
wait(int timeout)100 status_t FenceTime::wait(int timeout) {
101     // See if we already have a cached value we can return.
102     nsecs_t signalTime = mSignalTime.load(std::memory_order_relaxed);
103     if (signalTime != Fence::SIGNAL_TIME_PENDING) {
104         return NO_ERROR;
105     }
106 
107     // Hold a reference to the fence on the stack in case the class'
108     // reference is removed by another thread. This prevents the
109     // fence from being destroyed until the end of this method, where
110     // we conveniently do not have the lock held.
111     sp<Fence> fence;
112     {
113         // With the lock acquired this time, see if we have the cached
114         // value or if we need to poll the fence.
115         std::lock_guard<std::mutex> lock(mMutex);
116         if (!mFence.get()) {
117             // Another thread set the signal time just before we added the
118             // reference to mFence.
119             return NO_ERROR;
120         }
121         fence = mFence;
122     }
123 
124     // Make the system call without the lock held.
125     return fence->wait(timeout);
126 }
127 
getSignalTime()128 nsecs_t FenceTime::getSignalTime() {
129     // See if we already have a cached value we can return.
130     nsecs_t signalTime = mSignalTime.load(std::memory_order_relaxed);
131     if (signalTime != Fence::SIGNAL_TIME_PENDING) {
132         return signalTime;
133     }
134 
135     // Hold a reference to the fence on the stack in case the class'
136     // reference is removed by another thread. This prevents the
137     // fence from being destroyed until the end of this method, where
138     // we conveniently do not have the lock held.
139     sp<Fence> fence;
140     {
141         // With the lock acquired this time, see if we have the cached
142         // value or if we need to poll the fence.
143         std::lock_guard<std::mutex> lock(mMutex);
144         if (!mFence.get()) {
145             // Another thread set the signal time just before we added the
146             // reference to mFence.
147             return mSignalTime.load(std::memory_order_relaxed);
148         }
149         fence = mFence;
150     }
151 
152     // Make the system call without the lock held.
153     signalTime = fence->getSignalTime();
154 
155     // Allow tests to override SIGNAL_TIME_INVALID behavior, since tests
156     // use invalid underlying Fences without real file descriptors.
157     if (CC_UNLIKELY(mState == State::FORCED_VALID_FOR_TEST)) {
158         if (signalTime == Fence::SIGNAL_TIME_INVALID) {
159             signalTime = Fence::SIGNAL_TIME_PENDING;
160         }
161     }
162 
163     // Make the signal time visible to everyone if it is no longer pending
164     // and remove the class' reference to the fence.
165     if (signalTime != Fence::SIGNAL_TIME_PENDING) {
166         std::lock_guard<std::mutex> lock(mMutex);
167         mFence.clear();
168         mSignalTime.store(signalTime, std::memory_order_relaxed);
169     }
170 
171     return signalTime;
172 }
173 
getCachedSignalTime() const174 nsecs_t FenceTime::getCachedSignalTime() const {
175     // memory_order_acquire since we don't have a lock fallback path
176     // that will do an acquire.
177     return mSignalTime.load(std::memory_order_acquire);
178 }
179 
getSnapshot() const180 FenceTime::Snapshot FenceTime::getSnapshot() const {
181     // Quick check without the lock.
182     nsecs_t signalTime = mSignalTime.load(std::memory_order_relaxed);
183     if (signalTime != Fence::SIGNAL_TIME_PENDING) {
184         return Snapshot(signalTime);
185     }
186 
187     // Do the full check with the lock.
188     std::lock_guard<std::mutex> lock(mMutex);
189     signalTime = mSignalTime.load(std::memory_order_relaxed);
190     if (signalTime != Fence::SIGNAL_TIME_PENDING) {
191         return Snapshot(signalTime);
192     }
193     return Snapshot(mFence);
194 }
195 
196 // For tests only. If forceValidForTest is true, then getSignalTime will
197 // never return SIGNAL_TIME_INVALID and isValid will always return true.
FenceTime(const sp<Fence> & fence,bool forceValidForTest)198 FenceTime::FenceTime(const sp<Fence>& fence, bool forceValidForTest)
199   : mState(forceValidForTest ?
200             State::FORCED_VALID_FOR_TEST : State::INVALID),
201     mFence(fence),
202     mSignalTime(mState == State::INVALID ?
203             Fence::SIGNAL_TIME_INVALID : Fence::SIGNAL_TIME_PENDING) {
204 }
205 
signalForTest(nsecs_t signalTime)206 void FenceTime::signalForTest(nsecs_t signalTime) {
207     // To be realistic, this should really set a hidden value that
208     // gets picked up in the next call to getSignalTime, but this should
209     // be good enough.
210     std::lock_guard<std::mutex> lock(mMutex);
211     mFence.clear();
212     mSignalTime.store(signalTime, std::memory_order_relaxed);
213 }
214 
215 // ============================================================================
216 // FenceTime::Snapshot
217 // ============================================================================
Snapshot(const sp<Fence> & srcFence)218 FenceTime::Snapshot::Snapshot(const sp<Fence>& srcFence)
219     : state(State::FENCE), fence(srcFence) {
220 }
221 
Snapshot(nsecs_t srcSignalTime)222 FenceTime::Snapshot::Snapshot(nsecs_t srcSignalTime)
223     : state(State::SIGNAL_TIME), signalTime(srcSignalTime) {
224 }
225 
getFlattenedSize() const226 size_t FenceTime::Snapshot::getFlattenedSize() const {
227     constexpr size_t min = sizeof(state);
228     switch (state) {
229         case State::EMPTY:
230             return min;
231         case State::FENCE:
232             return min + fence->getFlattenedSize();
233         case State::SIGNAL_TIME:
234             return min + sizeof(signalTime);
235     }
236     return 0;
237 }
238 
getFdCount() const239 size_t FenceTime::Snapshot::getFdCount() const {
240     return state == State::FENCE ? fence->getFdCount() : 0u;
241 }
242 
flatten(void * & buffer,size_t & size,int * & fds,size_t & count) const243 status_t FenceTime::Snapshot::flatten(
244         void*& buffer, size_t& size, int*& fds, size_t& count) const {
245     if (size < getFlattenedSize()) {
246         return NO_MEMORY;
247     }
248 
249     FlattenableUtils::write(buffer, size, state);
250     switch (state) {
251         case State::EMPTY:
252             return NO_ERROR;
253         case State::FENCE:
254             return fence->flatten(buffer, size, fds, count);
255         case State::SIGNAL_TIME:
256             FlattenableUtils::write(buffer, size, signalTime);
257             return NO_ERROR;
258     }
259 
260     return NO_ERROR;
261 }
262 
unflatten(void const * & buffer,size_t & size,int const * & fds,size_t & count)263 status_t FenceTime::Snapshot::unflatten(
264         void const*& buffer, size_t& size, int const*& fds, size_t& count) {
265     if (size < sizeof(state)) {
266         return NO_MEMORY;
267     }
268 
269     FlattenableUtils::read(buffer, size, state);
270     switch (state) {
271         case State::EMPTY:
272             return NO_ERROR;
273         case State::FENCE:
274             fence = new Fence;
275             return fence->unflatten(buffer, size, fds, count);
276         case State::SIGNAL_TIME:
277             if (size < sizeof(signalTime)) {
278                 return NO_MEMORY;
279             }
280             FlattenableUtils::read(buffer, size, signalTime);
281             return NO_ERROR;
282     }
283 
284     return NO_ERROR;
285 }
286 
287 // ============================================================================
288 // FenceTimeline
289 // ============================================================================
push(const std::shared_ptr<FenceTime> & fence)290 void FenceTimeline::push(const std::shared_ptr<FenceTime>& fence) {
291     std::lock_guard<std::mutex> lock(mMutex);
292     while (mQueue.size() >= MAX_ENTRIES) {
293         // This is a sanity check to make sure the queue doesn't grow unbounded.
294         // MAX_ENTRIES should be big enough not to trigger this path.
295         // In case this path is taken though, users of FenceTime must make sure
296         // not to rely solely on FenceTimeline to get the final timestamp and
297         // should eventually call Fence::getSignalTime on their own.
298         std::shared_ptr<FenceTime> front = mQueue.front().lock();
299         if (front) {
300             // Make a last ditch effort to get the signalTime here since
301             // we are removing it from the timeline.
302             front->getSignalTime();
303         }
304         mQueue.pop();
305     }
306     mQueue.push(fence);
307 }
308 
updateSignalTimes()309 void FenceTimeline::updateSignalTimes() {
310     std::lock_guard<std::mutex> lock(mMutex);
311     while (!mQueue.empty()) {
312         std::shared_ptr<FenceTime> fence = mQueue.front().lock();
313         if (!fence) {
314             // The shared_ptr no longer exists and no one cares about the
315             // timestamp anymore.
316             mQueue.pop();
317             continue;
318         } else if (fence->getSignalTime() != Fence::SIGNAL_TIME_PENDING) {
319             // The fence has signaled and we've removed the sp<Fence> ref.
320             mQueue.pop();
321             continue;
322         } else {
323             // The fence didn't signal yet. Break since the later ones
324             // shouldn't have signaled either.
325             break;
326         }
327     }
328 }
329 
330 // ============================================================================
331 // FenceToFenceTimeMap
332 // ============================================================================
createFenceTimeForTest(const sp<Fence> & fence)333 std::shared_ptr<FenceTime> FenceToFenceTimeMap::createFenceTimeForTest(
334         const sp<Fence>& fence) {
335     std::lock_guard<std::mutex> lock(mMutex);
336     // Always garbage collecting isn't efficient, but this is only for testing.
337     garbageCollectLocked();
338     std::shared_ptr<FenceTime> fenceTime(new FenceTime(fence, true));
339     mMap[fence.get()].push_back(fenceTime);
340     return fenceTime;
341 }
342 
signalAllForTest(const sp<Fence> & fence,nsecs_t signalTime)343 void FenceToFenceTimeMap::signalAllForTest(
344         const sp<Fence>& fence, nsecs_t signalTime) {
345     bool signaled = false;
346 
347     std::lock_guard<std::mutex> lock(mMutex);
348     auto it = mMap.find(fence.get());
349     if (it != mMap.end()) {
350         for (auto& weakFenceTime : it->second) {
351             std::shared_ptr<FenceTime> fenceTime = weakFenceTime.lock();
352             if (!fenceTime) {
353                 continue;
354             }
355             ALOGE_IF(!fenceTime->isValid(),
356                     "signalAllForTest: Signaling invalid fence.");
357             fenceTime->signalForTest(signalTime);
358             signaled = true;
359         }
360     }
361 
362     ALOGE_IF(!signaled, "signalAllForTest: Nothing to signal.");
363 }
364 
garbageCollectLocked()365 void FenceToFenceTimeMap::garbageCollectLocked() {
366     for (auto& it : mMap) {
367         // Erase all expired weak pointers from the vector.
368         auto& vect = it.second;
369         vect.erase(
370                 std::remove_if(vect.begin(), vect.end(),
371                         [](const std::weak_ptr<FenceTime>& ft) {
372                             return ft.expired();
373                         }),
374                 vect.end());
375 
376         // Also erase the map entry if the vector is now empty.
377         if (vect.empty()) {
378             mMap.erase(it.first);
379         }
380     }
381 }
382 
383 } // namespace android
384