• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 
18 #ifndef ANDROID_TIME_CHECK_H
19 #define ANDROID_TIME_CHECK_H
20 
21 #include <utils/KeyedVector.h>
22 #include <utils/Thread.h>
23 
24 
25 namespace android {
26 
27 // A class monitoring execution time for a code block (scoped variable) and causing an assert
28 // if it exceeds a certain time
29 
30 class TimeCheck {
31 public:
32 
33     // The default timeout is chosen to be less than system server watchdog timeout
34     static constexpr uint32_t kDefaultTimeOutMs = 5000;
35 
36             TimeCheck(const char *tag, uint32_t timeoutMs = kDefaultTimeOutMs);
37             ~TimeCheck();
38 
39 private:
40 
41     class TimeCheckThread : public Thread {
42     public:
43 
TimeCheckThread()44                             TimeCheckThread() {}
45         virtual             ~TimeCheckThread() override;
46 
47                 nsecs_t     startMonitoring(const char *tag, uint32_t timeoutMs);
48                 void        stopMonitoring(nsecs_t endTimeNs);
49 
50     private:
51 
52                 // RefBase
onFirstRef()53         virtual void        onFirstRef() override { run("TimeCheckThread", PRIORITY_URGENT_AUDIO); }
54 
55                 // Thread
56         virtual bool        threadLoop() override;
57 
58                 Condition           mCond;
59                 Mutex               mMutex;
60                 // using the end time in ns as key is OK given the risk is low that two entries
61                 // are added in such a way that <add time> + <timeout> are the same for both.
62                 KeyedVector< nsecs_t, const char*>  mMonitorRequests;
63     };
64 
65     static sp<TimeCheckThread> getTimeCheckThread();
66 
67     const           nsecs_t mEndTimeNs;
68 };
69 
70 }; // namespace android
71 
72 #endif  // ANDROID_TIME_CHECK_H
73