1 /* 2 * Copyright (c) 2024 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 #ifndef ECMASCRIPT_PGO_PROFILER_PGO_STATE_H 17 #define ECMASCRIPT_PGO_PROFILER_PGO_STATE_H 18 19 #include <atomic> 20 #include <string> 21 22 #include "ecmascript/log_wrapper.h" 23 #include "ecmascript/platform/mutex.h" 24 25 #define PRINT_STATE_CHANGE false 26 27 namespace panda::ecmascript::pgo { 28 class PGOProfiler; 29 class PGOProfilerManager; 30 class PGOState { 31 public: 32 PGOState(); 33 34 enum class State : uint8_t { 35 START, 36 SAVE, 37 STOP, 38 }; 39 40 enum class GCState : uint8_t { 41 RUNNING, 42 WAITING, 43 STOP, 44 }; 45 46 static std::string ToString(State state); 47 static std::string ToString(GCState state); 48 State GetState() const; 49 void SetState(State state); 50 GCState GetGCState() const; 51 void SetGCState(GCState state); 52 bool GCIsWaiting() const; 53 bool GCIsRunning() const; 54 bool GCIsStop() const; 55 void SuspendByGC(); 56 void ResumeByGC(PGOProfiler* profiler); 57 bool StateIsStop() const; 58 bool StateIsStart() const; 59 bool StateIsSave() const; 60 void SetStopAndNotify(); 61 void SetSaveAndNotify(); 62 bool SetStartIfStop(); 63 void WaitDump(); 64 void WaitGC(); 65 void NotifyAllDumpWaiters(); 66 void NotifyAllGCWaiters(); 67 bool TryChangeState(State expected, State desired); 68 void StartDumpBeforeDestroy(); 69 70 private: 71 #if PRINT_STATE_CHANGE 72 template<typename T> PrintStateChange(T expected,T desired)73 void PrintStateChange(T expected, T desired) 74 { 75 LOG_PGO(INFO) << ToString(expected) << " -> " << ToString(desired); 76 } 77 78 template<typename T> PrintStateChange(T original,T expected,T desired)79 void PrintStateChange(T original, T expected, T desired) 80 { 81 LOG_PGO(INFO) << ToString(original) << " -x " << ToString(desired) << ", " << ToString(expected); 82 } 83 #endif 84 85 std::atomic<State> state_ {State::STOP}; 86 std::atomic<GCState> gcState_ {GCState::STOP}; 87 ConditionVariable stateCondition_; 88 ConditionVariable gcCondition_; 89 PGOProfilerManager* manager_ {nullptr}; 90 std::atomic_bool needRedump_ {false}; 91 92 protected: 93 Mutex stateMutex_; 94 }; 95 } // namespace panda::ecmascript::pgo 96 97 #endif // ECMASCRIPT_PGO_PROFILER_PGO_STATE_H