1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SRC_TRACE_PROCESSOR_TYPES_TASK_STATE_H_ 18 #define SRC_TRACE_PROCESSOR_TYPES_TASK_STATE_H_ 19 20 #include <stddef.h> 21 #include <array> 22 #include <utility> 23 24 #include "perfetto/base/logging.h" 25 #include "perfetto/ext/base/optional.h" 26 #include "perfetto/ext/base/string_view.h" 27 #include "perfetto/ext/base/string_writer.h" 28 #include "src/trace_processor/types/version_number.h" 29 30 namespace perfetto { 31 namespace trace_processor { 32 namespace ftrace_utils { 33 34 // A strongly typed representation of the TaskState enum given in sched_switch 35 // events. 36 class TaskState { 37 public: 38 using TaskStateStr = std::array<char, 4>; 39 40 // The ordering and values of these fields comes from the kernel in the file 41 // https://android.googlesource.com/kernel/msm.git/+/android-msm-wahoo-4.4-pie-qpr1/include/linux/sched.h#212 42 enum Atom : uint16_t { 43 kRunnable = 0, 44 kInterruptibleSleep = 1, 45 kUninterruptibleSleep = 2, 46 kStopped = 4, 47 kTraced = 8, 48 kExitDead = 16, 49 kExitZombie = 32, 50 kTaskDead = 64, 51 kWakeKill = 128, 52 kWaking = 256, 53 kParked = 512, 54 kNoLoad = 1024, 55 // This was added in kernel v4.9 but is never used. 56 kTaskNew = 2048, 57 58 kValid = 0x8000, 59 }; 60 61 TaskState() = default; 62 explicit TaskState(uint16_t raw_state, 63 base::Optional<VersionNumber> = VersionNumber{4, 4}); 64 explicit TaskState(const char* state_str); 65 66 // Returns if this TaskState has a valid representation. is_valid()67 bool is_valid() const { return state_ & kValid; } 68 69 // Returns the string representation of this (valid) TaskState. This array 70 // is null terminated. |separator| specifies if a separator should be printed 71 // between the atoms (default: \0 meaning no separator). 72 // Note: This function CHECKs that |is_valid()| is true. 73 TaskStateStr ToString(char separator = '\0') const; 74 75 // Returns the raw state this class can be recreated from. raw_state()76 uint16_t raw_state() const { 77 PERFETTO_DCHECK(is_valid()); 78 return state_ & ~kValid; 79 } 80 81 // Returns if this TaskState is runnable. is_runnable()82 bool is_runnable() const { return ((state_ & (max_state_ - 1)) == 0); } 83 84 // Returns whether kernel preemption caused the exit state. is_kernel_preempt()85 bool is_kernel_preempt() const { return state_ & max_state_; } 86 87 private: 88 // One of Atom - based on given raw_state and version. Will have isValid set 89 // if valid. 90 uint16_t state_ = 0; 91 uint16_t max_state_ = 2048; 92 }; 93 94 } // namespace ftrace_utils 95 } // namespace trace_processor 96 } // namespace perfetto 97 98 #endif // SRC_TRACE_PROCESSOR_TYPES_TASK_STATE_H_ 99