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