1 /* 2 * Copyright (c) 2021 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 BYTRACE_THREAD_STATES_H 17 #define BYTRACE_THREAD_STATES_H 18 19 #include <map> 20 #include "log.h" 21 22 namespace SysTuning { 23 namespace TraceStreamer { 24 enum Direction { NEED_GO = 0, NEED_CONTINUE, NEED_BREAK }; 25 enum Stat : uint32_t { 26 RUNNABLE = 0, 27 INTERRUPTABLESLEEP = 1, 28 UNINTERRUPTIBLESLEEP = 2, 29 STOPPED = 4, 30 TRACED = 8, // the process is being debug 31 EXITDEAD = 16, 32 EXITZOMBIE = 32, 33 TASKDEAD = 64, 34 WAKEKILL = 128, 35 WAKING = 256, 36 PARKED = 512, 37 NOLOAD = 1024, 38 TASKNEW = 2048, 39 VALID = 0X8000, 40 }; 41 class ThreadStateFlag { 42 public: 43 explicit ThreadStateFlag(const std::string& stateStr); ~ThreadStateFlag()44 ~ThreadStateFlag() {} 45 State()46 uint32_t State() const 47 { 48 return state_ & ~VALID; 49 } IsValid()50 bool IsValid() const 51 { 52 return invalid_; 53 } 54 55 private: SetStat(Stat value)56 void SetStat(Stat value) 57 { 58 state_ |= value; 59 } 60 61 void ProcessSate(const std::string& stateStr); 62 Direction SetStatByChar(char ch); 63 64 private: 65 uint32_t state_ = 0; 66 std::map<char, Stat> statMap_ = { 67 {'R', RUNNABLE}, 68 {'S', INTERRUPTABLESLEEP}, 69 {'D', UNINTERRUPTIBLESLEEP}, 70 {'T', STOPPED}, 71 {'t', TRACED}, 72 {'X', EXITDEAD}, 73 {'Z', EXITZOMBIE}, 74 {'x', TASKDEAD}, 75 {'I', TASKDEAD}, 76 {'K', WAKEKILL}, 77 {'P', PARKED}, 78 {'N', NOLOAD}, 79 {'|', VALID}, 80 }; 81 bool invalid_ = false; 82 }; 83 } // namespace TraceStreamer 84 } // namespace SysTuning 85 86 #endif // _BYTRACE_THREAD_STATES_H_ 87