Home | History | Annotate | Download | only in power-libperfmgr
      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 #ifndef INTERACTIONHANDLER_H
     18 #define INTERACTIONHANDLER_H
     19 
     20 #include <condition_variable>
     21 #include <mutex>
     22 #include <thread>
     23 
     24 #include <perfmgr/HintManager.h>
     25 
     26 using ::android::perfmgr::HintManager;
     27 
     28 enum interaction_state {
     29     INTERACTION_STATE_UNINITIALIZED,
     30     INTERACTION_STATE_IDLE,
     31     INTERACTION_STATE_INTERACTION,
     32     INTERACTION_STATE_WAITING,
     33 };
     34 
     35 struct InteractionHandler {
     36     InteractionHandler(std::shared_ptr<HintManager> const & hint_manager);
     37     ~InteractionHandler();
     38     bool Init();
     39     void Exit();
     40     void Acquire(int32_t duration);
     41 
     42  private:
     43     void Release();
     44     void WaitForIdle(int32_t wait_ms, int32_t timeout_ms);
     45     void AbortWaitLocked();
     46     void Routine();
     47 
     48     void PerfLock();
     49     void PerfRel();
     50 
     51     long long CalcTimespecDiffMs(struct timespec start, struct timespec end);
     52 
     53     enum interaction_state mState;
     54 
     55     int mIdleFd;
     56     int mEventFd;
     57 
     58     int32_t mWaitMs;
     59     int32_t mMinDurationMs;
     60     int32_t mMaxDurationMs;
     61     int32_t mDurationMs;
     62 
     63     struct timespec mLastTimespec;
     64 
     65     std::unique_ptr<std::thread> mThread;
     66     std::mutex mLock;
     67     std::condition_variable mCond;
     68     std::shared_ptr<HintManager> mHintManager;
     69 };
     70 
     71 #endif //INTERACTIONHANDLER_H
     72