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 class CPUEUTask; 28 29 class TaskState { 30 public: 31 enum State { PENDING, READY, RUNNING, BLOCKED, EXITED, MAX }; 32 using Op = typename std::function<bool(CPUEUTask*)>; 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 62 #ifdef FFRT_IO_TASK_SCHEDULER SetCurState(State state)63 void SetCurState(State state) 64 { 65 curState = state; 66 } 67 #endif 68 PreState()69 State PreState() const 70 { 71 return preState; 72 } 73 String()74 const char* String() const 75 { 76 return String(curState); 77 } 78 WaitingTime()79 uint64_t WaitingTime() const 80 { 81 #if defined(TRACE_TASKSTAT_LOG_ENABLE) && (TRACE_TASKSTAT_LOG_ENABLE == 1) 82 return stat.WaitingTime(); 83 #else 84 return 0; 85 #endif 86 } 87 RunningTime()88 uint64_t RunningTime() const 89 { 90 #if defined(TRACE_TASKSTAT_LOG_ENABLE) && (TRACE_TASKSTAT_LOG_ENABLE == 1) 91 return stat.RunningTime(); 92 #else 93 return 0; 94 #endif 95 } 96 RegisterOps(State state,Op && op)97 static void RegisterOps(State state, Op&& op) 98 { 99 ops[static_cast<size_t>(state)] = op; 100 } 101 102 static int OnTransition(State state, CPUEUTask* task, Op&& op = Op()); 103 String(State state)104 static const char* String(State state) 105 { 106 static const char* m[] = {"PENDING", "READY", "RUNNING", "BLOCKED", "EXITED", "MAX"}; 107 108 return m[static_cast<size_t>(state)]; 109 } 110 111 private: 112 class TaskStateStat { 113 public: 114 uint64_t WaitingTime() const; 115 uint64_t RunningTime() const; 116 117 inline void Count(CPUEUTask* task); 118 119 private: 120 inline void Count(TaskState::State state); 121 inline uint64_t CalcDuration(TaskState::State pre, TaskState::State cur) const; 122 123 std::array<std::chrono::steady_clock::time_point, static_cast<size_t>(TaskState::State::MAX)> timepoint; 124 }; 125 126 #if defined(TRACE_TASKSTAT_LOG_ENABLE) && (TRACE_TASKSTAT_LOG_ENABLE == 1) 127 TaskStateStat stat; 128 #endif 129 130 State curState = PENDING; 131 State preState = PENDING; 132 133 static std::array<Op, static_cast<size_t>(TaskState::MAX)> ops; 134 }; 135 } // namespace ffrt 136 137 #endif 138