• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class 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     PARKED = 64,
34     TASKDEAD = 128,
35     WAKEKILL = 256,
36     WAKING = 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_ & ~static_cast<uint32_t>(Stat::VALID);
49     }
IsInvalid()50     bool IsInvalid() const
51     {
52         return invalid_;
53     }
54 
55 private:
SetStat(Stat value)56     void SetStat(Stat value)
57     {
58         state_ |= static_cast<uint32_t>(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', Stat::RUNNABLE},
68         {'S', Stat::INTERRUPTABLESLEEP},
69         {'D', Stat::UNINTERRUPTIBLESLEEP},
70         {'T', Stat::STOPPED},
71         {'t', Stat::TRACED},
72         {'X', Stat::EXITDEAD},
73         {'Z', Stat::EXITZOMBIE},
74         {'P', Stat::PARKED},
75         {'I', Stat::TASKDEAD},
76         {'|', Stat::VALID},
77     };
78     bool invalid_ = false;
79 };
80 } // namespace TraceStreamer
81 } // namespace SysTuning
82 
83 #endif // _BYTRACE_THREAD_STATES_H_
84