• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 FFRT_TASK_STATE_HPP
17 #define FFRT_TASK_STATE_HPP
18 
19 #include <array>
20 #include <string_view>
21 #include <functional>
22 #include <mutex>
23 #include <shared_mutex>
24 #include <chrono>
25 
26 namespace ffrt {
27 struct TaskCtx;
28 
29 class TaskState {
30 public:
31     enum State { PENDING, READY, RUNNING, BLOCKED, EXITED, MAX };
32     using Op = typename std::function<bool(TaskCtx*)>;
33 
34     TaskState() = default;
35 
36     TaskState(const TaskState&) = delete;
37     TaskState(TaskState&&) = delete;
38 
39     TaskState& operator=(const TaskState&) = delete;
40     TaskState& operator=(TaskState&&) = delete;
41 
42     bool operator==(State state) const
43     {
44         return this->curState == state;
45     }
46 
47     bool operator!=(State state) const
48     {
49         return this->curState != state;
50     }
51 
operator()52     State operator()() const
53     {
54         return curState;
55     }
56 
CurState()57     State CurState() const
58     {
59         return curState;
60     }
61 
SetCurState(State state)62     void SetCurState(State state)
63     {
64         curState = state;
65     }
66 
PreState()67     State PreState() const
68     {
69         return preState;
70     }
71 
String()72     const char* String() const
73     {
74         return String(curState);
75     }
76 
WaitingTime()77     uint64_t WaitingTime() const
78     {
79 #if defined(TRACE_TASKSTAT_LOG_ENABLE) && (TRACE_TASKSTAT_LOG_ENABLE == 1)
80         return stat.WaitingTime();
81 #else
82         return 0;
83 #endif
84     }
85 
RunningTime()86     uint64_t RunningTime() const
87     {
88 #if defined(TRACE_TASKSTAT_LOG_ENABLE) && (TRACE_TASKSTAT_LOG_ENABLE == 1)
89         return stat.RunningTime();
90 #else
91         return 0;
92 #endif
93     }
94 
RegisterOps(State state,Op && op)95     static void RegisterOps(State state, Op&& op)
96     {
97         ops[static_cast<size_t>(state)] = op;
98     }
99 
100     static int OnTransition(State state, TaskCtx* task, Op&& op = Op());
101 
String(State state)102     static const char* String(State state)
103     {
104         static const char* m[] = {"PENDING", "READY", "RUNNING", "BLOCKED", "EXITED", "MAX"};
105 
106         return m[static_cast<size_t>(state)];
107     }
108 
109 private:
110     class TaskStateStat {
111     public:
112         uint64_t WaitingTime() const;
113         uint64_t RunningTime() const;
114 
115         inline void Count(TaskCtx* task);
116 
117     private:
118         inline void Count(TaskState::State state);
119         inline uint64_t CalcDuration(TaskState::State preState, TaskState::State curState) const;
120 
121         std::array<std::chrono::steady_clock::time_point, static_cast<size_t>(TaskState::State::MAX)> timepoint;
122     };
123 
124 #if defined(TRACE_TASKSTAT_LOG_ENABLE) && (TRACE_TASKSTAT_LOG_ENABLE == 1)
125     TaskStateStat stat;
126 #endif
127 
128     State curState = PENDING;
129     State preState = PENDING;
130 
131     static std::array<Op, static_cast<size_t>(TaskState::MAX)> ops;
132 };
133 } // namespace ffrt
134 
135 #endif
136