• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020, 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 CPP_WATCHDOG_SERVER_SRC_PROCSTAT_H_
18 #define CPP_WATCHDOG_SERVER_SRC_PROCSTAT_H_
19 
20 #include <android-base/result.h>
21 #include <stdint.h>
22 #include <utils/Mutex.h>
23 #include <utils/RefBase.h>
24 
25 namespace android {
26 namespace automotive {
27 namespace watchdog {
28 
29 constexpr const char* kProcStatPath = "/proc/stat";
30 
31 struct CpuStats {
32     uint64_t userTime = 0;       // Time spent in user mode.
33     uint64_t niceTime = 0;       // Time spent in user mode with low priority (nice).
34     uint64_t sysTime = 0;        // Time spent in system mode.
35     uint64_t idleTime = 0;       // Time spent in the idle task.
36     uint64_t ioWaitTime = 0;     // Time spent on context switching/waiting due to I/O operations.
37     uint64_t irqTime = 0;        // Time servicing interrupts.
38     uint64_t softIrqTime = 0;    // Time servicing soft interrupts.
39     uint64_t stealTime = 0;      // Stolen time (Time spent in other OS in a virtualized env).
40     uint64_t guestTime = 0;      // Time spent running a virtual CPU for guest OS.
41     uint64_t guestNiceTime = 0;  // Time spent running a niced virtual CPU for guest OS.
42 
43     CpuStats& operator-=(const CpuStats& rhs) {
44         userTime -= rhs.userTime;
45         niceTime -= rhs.niceTime;
46         sysTime -= rhs.sysTime;
47         idleTime -= rhs.idleTime;
48         ioWaitTime -= rhs.ioWaitTime;
49         irqTime -= rhs.irqTime;
50         softIrqTime -= rhs.softIrqTime;
51         stealTime -= rhs.stealTime;
52         guestTime -= rhs.guestTime;
53         guestNiceTime -= rhs.guestNiceTime;
54         return *this;
55     }
56 };
57 
58 class ProcStatInfo {
59 public:
ProcStatInfo()60     ProcStatInfo() : cpuStats({}), runnableProcessesCnt(0), ioBlockedProcessesCnt(0) {}
ProcStatInfo(CpuStats stats,uint32_t runnableCnt,uint32_t ioBlockedCnt)61     ProcStatInfo(CpuStats stats, uint32_t runnableCnt, uint32_t ioBlockedCnt) :
62           cpuStats(stats), runnableProcessesCnt(runnableCnt), ioBlockedProcessesCnt(ioBlockedCnt) {}
63     CpuStats cpuStats;
64     uint32_t runnableProcessesCnt;
65     uint32_t ioBlockedProcessesCnt;
66 
totalCpuTime()67     uint64_t totalCpuTime() const {
68         return cpuStats.userTime + cpuStats.niceTime + cpuStats.sysTime + cpuStats.idleTime +
69                 cpuStats.ioWaitTime + cpuStats.irqTime + cpuStats.softIrqTime + cpuStats.stealTime +
70                 cpuStats.guestTime + cpuStats.guestNiceTime;
71     }
totalProcessesCnt()72     uint32_t totalProcessesCnt() const { return runnableProcessesCnt + ioBlockedProcessesCnt; }
73     bool operator==(const ProcStatInfo& info) const {
74         return memcmp(&cpuStats, &info.cpuStats, sizeof(cpuStats)) == 0 &&
75                 runnableProcessesCnt == info.runnableProcessesCnt &&
76                 ioBlockedProcessesCnt == info.ioBlockedProcessesCnt;
77     }
78     ProcStatInfo& operator-=(const ProcStatInfo& rhs) {
79         cpuStats -= rhs.cpuStats;
80         // Don't diff *ProcessesCnt as they are real-time values unlike |cpuStats|, which are
81         // aggregated values since system startup.
82         return *this;
83     }
84 };
85 
86 // Collector/parser for `/proc/stat` file.
87 class ProcStat : public RefBase {
88 public:
89     explicit ProcStat(const std::string& path = kProcStatPath) :
90           mLatestStats({}),
91           kEnabled(!access(path.c_str(), R_OK)),
92           kPath(path) {}
93 
~ProcStat()94     virtual ~ProcStat() {}
95 
96     // Collects proc stat delta since the last collection.
97     virtual android::base::Result<void> collect();
98 
99     // Returns true when the proc stat file is accessible. Otherwise, returns false.
100     // Called by WatchdogPerfService and tests.
enabled()101     virtual bool enabled() { return kEnabled; }
102 
filePath()103     virtual std::string filePath() { return kProcStatPath; }
104 
105     // Returns the latest stats.
latestStats()106     virtual const ProcStatInfo latestStats() const {
107         Mutex::Autolock lock(mMutex);
108         return mLatestStats;
109     }
110 
111     // Returns the delta of stats from the latest collection.
deltaStats()112     virtual const ProcStatInfo deltaStats() const {
113         Mutex::Autolock lock(mMutex);
114         return mDeltaStats;
115     }
116 
117 private:
118     // Reads the contents of |kPath|.
119     android::base::Result<ProcStatInfo> getProcStatLocked() const;
120 
121     // Makes sure only one collection is running at any given time.
122     mutable Mutex mMutex;
123 
124     // Latest dump of CPU stats from the file at |kPath|.
125     ProcStatInfo mLatestStats GUARDED_BY(mMutex);
126 
127     // Delta of CPU stats from the latest collection.
128     ProcStatInfo mDeltaStats GUARDED_BY(mMutex);
129 
130     // True if |kPath| is accessible.
131     const bool kEnabled;
132 
133     // Path to proc stat file. Default path is |kProcStatPath|.
134     const std::string kPath;
135 };
136 
137 }  // namespace watchdog
138 }  // namespace automotive
139 }  // namespace android
140 
141 #endif  //  CPP_WATCHDOG_SERVER_SRC_PROCSTAT_H_
142