• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 PROC_UTIL_H
17 #define PROC_UTIL_H
18 
19 #include <string>
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 
24 class Schedstat {
25 public:
26     bool ParseSchedstat(const std::string& schedstatPath);
27     std::string ToString() const;
28     void Reset();
29 
30     uint64_t runTime_{0}; // time spent on the cpu (in nanoseconds)
31     uint64_t waitTime_{0}; // time spent waiting on a runqueue (in nanoseconds)
32     uint64_t timeslicesNum_{0}; // of timeslices run on this cpu
33 };
34 
35 enum class ThreadState : char {
36     UNKNOWM = 0,
37     RUNNING = 'R', //Runnig
38     SLEEP = 'S', // Sleep in an interruptible wait
39     DISK_SLEEP = 'D', // Waiting in uninterruptible disk sleep
40     ZOMBIE = 'Z', // Zombie
41     STOPPED = 'T', // Stopped (on a signal) or (before Linux2.6.33) trace stopped
42     TRACING_STOP = 't', // Tracing stop (Linux 2.6.33 onward)
43     DEAD_2_6 = 'X', // Dead (from Linux 2.6.0 oneard)
44     DEAD = 'x', // Dead (Linux 2.6.33 to 3.13 only)
45     WAKEKILL = 'K', // Wakekill (Linux 2.6.33 to 3.13 only)
46     WAKING = 'W', // Wakekill (Linux 2.6.33 to 3.13 only)
47     PARKED = 'P', // Parked (Linux 2.6.33 to 3.13 only)
48     IDLE = 'I', // Idle (Linux 4.14 onward)
49 };
50 
51 class ThreadInfo {
52 public:
53     bool ParserThreadInfo(pid_t tid);
54     std::string ToString() const;
55 
56     ThreadState state_{ThreadState::UNKNOWM};
57     uint64_t utime_{0};
58     uint64_t stime_{0};
59     int64_t cutime_{0};
60     Schedstat schedstat_;
61 };
62 
63 constexpr int TASK_COMM_LEN = 16;
64 
65 struct ProcessInfo {
66     int32_t pid{0}; // field 1
67     char comm[TASK_COMM_LEN + 1]{0}; // field 2
68     ThreadState state{ThreadState::UNKNOWM}; // field 3
69     int32_t ppid{0}; // field 4
70     uint64_t utime{0}; // field 14
71     uint64_t stime{0}; // field 15
72     int64_t cutime{0}; // field 16
73     int64_t cstime{0}; // field 17
74     int64_t priority{0}; // field 18
75     int64_t nice{0}; // field 19
76     int64_t numThreads{0}; // field 20
77     uint64_t starttime{0}; // field 22
78     int32_t rss{0}; // field 24
79     uint64_t signal{0}; // field 31
80     uint64_t blocked{0}; // field 32
81     uint64_t sigignore{0}; // field 33
82     uint64_t sigcatch{0}; // field 34
83 };
84 
85 std::string ThreadStateToString(ThreadState state);
86 bool ParseStat(const std::string& statPath, ProcessInfo& info);
87 bool ParseProcInfo(pid_t pid, ProcessInfo& info);
88 std::string GetFirstNumberSeq(const std::string& cont);
89 bool GetUidAndSigBlk(pid_t pid, long& uid, uint64_t& sigBlk);
90 bool IsSigDumpMask(uint64_t sigBlk);
91 } // namespace Dfx
92 } // namespace OHOS
93 #endif // PROC_UTIL_H
94