1 /*
2 * Copyright 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 #include "ProcStat.h"
18
19 #include <android-base/file.h>
20 #include <android-base/stringprintf.h>
21 #include <gmock/gmock.h>
22 #include <inttypes.h>
23
24 #include <string>
25
26 namespace android {
27 namespace automotive {
28 namespace watchdog {
29
30 using ::android::base::StringPrintf;
31 using ::android::base::WriteStringToFile;
32
33 namespace {
34
toString(const ProcStatInfo & info)35 std::string toString(const ProcStatInfo& info) {
36 const auto& cpuStats = info.cpuStats;
37 return StringPrintf("Cpu Stats:\nUserTime: %" PRIu64 " NiceTime: %" PRIu64 " SysTime: %" PRIu64
38 " IdleTime: %" PRIu64 " IoWaitTime: %" PRIu64 " IrqTime: %" PRIu64
39 " SoftIrqTime: %" PRIu64 " StealTime: %" PRIu64 " GuestTime: %" PRIu64
40 " GuestNiceTime: %" PRIu64 "\nNumber of running processes: %" PRIu32
41 "\nNumber of blocked processes: %" PRIu32,
42 cpuStats.userTime, cpuStats.niceTime, cpuStats.sysTime, cpuStats.idleTime,
43 cpuStats.ioWaitTime, cpuStats.irqTime, cpuStats.softIrqTime,
44 cpuStats.stealTime, cpuStats.guestTime, cpuStats.guestNiceTime,
45 info.runnableProcessCount, info.ioBlockedProcessCount);
46 }
47
48 } // namespace
49
TEST(ProcStatTest,TestValidStatFile)50 TEST(ProcStatTest, TestValidStatFile) {
51 constexpr char firstSnapshot[] =
52 "cpu 6200 5700 1700 3100 1100 5200 3900 0 0 0\n"
53 "cpu0 2400 2900 600 690 340 4300 2100 0 0 0\n"
54 "cpu1 1900 2380 510 760 51 370 1500 0 0 0\n"
55 "cpu2 900 400 400 1000 600 400 160 0 0 0\n"
56 "cpu3 1000 20 190 650 109 130 140 0 0 0\n"
57 "intr 694351583 0 0 0 297062868 0 5922464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
58 "0 0\n"
59 /* Skipped most of the intr line as it is not important for testing the ProcStat parsing
60 * logic.
61 */
62 "ctxt 579020168\n"
63 "btime 1579718450\n"
64 "processes 113804\n"
65 "procs_running 17\n"
66 "procs_blocked 5\n"
67 "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n";
68 ProcStatInfo expectedFirstDelta;
69 expectedFirstDelta.cpuStats = {
70 .userTime = 6200,
71 .niceTime = 5700,
72 .sysTime = 1700,
73 .idleTime = 3100,
74 .ioWaitTime = 1100,
75 .irqTime = 5200,
76 .softIrqTime = 3900,
77 .stealTime = 0,
78 .guestTime = 0,
79 .guestNiceTime = 0,
80 };
81 expectedFirstDelta.runnableProcessCount = 17;
82 expectedFirstDelta.ioBlockedProcessCount = 5;
83
84 TemporaryFile tf;
85 ASSERT_NE(tf.fd, -1);
86 ASSERT_TRUE(WriteStringToFile(firstSnapshot, tf.path));
87
88 ProcStat procStat(tf.path);
89 ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
90 ASSERT_RESULT_OK(procStat.collect());
91
92 const auto& actualFirstDelta = procStat.deltaStats();
93 EXPECT_EQ(expectedFirstDelta, actualFirstDelta) << "First snapshot doesn't match.\nExpected:\n"
94 << toString(expectedFirstDelta) << "\nActual:\n"
95 << toString(actualFirstDelta);
96
97 constexpr char secondSnapshot[] =
98 "cpu 16200 8700 2000 4100 2200 6200 5900 0 0 0\n"
99 "cpu0 4400 3400 700 890 800 4500 3100 0 0 0\n"
100 "cpu1 5900 3380 610 960 100 670 2000 0 0 0\n"
101 "cpu2 2900 1000 450 1400 800 600 460 0 0 0\n"
102 "cpu3 3000 920 240 850 500 430 340 0 0 0\n"
103 "intr 694351583 0 0 0 297062868 0 5922464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
104 "0 0\n"
105 "ctxt 579020168\n"
106 "btime 1579718450\n"
107 "processes 113804\n"
108 "procs_running 10\n"
109 "procs_blocked 2\n"
110 "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n";
111 ProcStatInfo expectedSecondDelta;
112 expectedSecondDelta.cpuStats = {
113 .userTime = 10000,
114 .niceTime = 3000,
115 .sysTime = 300,
116 .idleTime = 1000,
117 .ioWaitTime = 1100,
118 .irqTime = 1000,
119 .softIrqTime = 2000,
120 .stealTime = 0,
121 .guestTime = 0,
122 .guestNiceTime = 0,
123 };
124 expectedSecondDelta.runnableProcessCount = 10;
125 expectedSecondDelta.ioBlockedProcessCount = 2;
126
127 ASSERT_TRUE(WriteStringToFile(secondSnapshot, tf.path));
128 ASSERT_RESULT_OK(procStat.collect());
129
130 const auto& actualSecondDelta = procStat.deltaStats();
131 EXPECT_EQ(expectedSecondDelta, actualSecondDelta)
132 << "Second snapshot doesnt't match.\nExpected:\n"
133 << toString(expectedSecondDelta) << "\nActual:\n"
134 << toString(actualSecondDelta);
135 }
136
TEST(ProcStatTest,TestErrorOnCorruptedStatFile)137 TEST(ProcStatTest, TestErrorOnCorruptedStatFile) {
138 constexpr char contents[] =
139 "cpu 6200 5700 1700 3100 CORRUPTED DATA\n"
140 "cpu0 2400 2900 600 690 340 4300 2100 0 0 0\n"
141 "cpu1 1900 2380 510 760 51 370 1500 0 0 0\n"
142 "cpu2 900 400 400 1000 600 400 160 0 0 0\n"
143 "cpu3 1000 20 190 650 109 130 140 0 0 0\n"
144 "intr 694351583 0 0 0 297062868 0 5922464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
145 "0 0\n"
146 "ctxt 579020168\n"
147 "btime 1579718450\n"
148 "processes 113804\n"
149 "procs_running 17\n"
150 "procs_blocked 5\n"
151 "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n";
152 TemporaryFile tf;
153 ASSERT_NE(tf.fd, -1);
154 ASSERT_TRUE(WriteStringToFile(contents, tf.path));
155
156 ProcStat procStat(tf.path);
157 ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
158 EXPECT_FALSE(procStat.collect().ok()) << "No error returned for corrupted file";
159 }
160
TEST(ProcStatTest,TestErrorOnMissingCpuLine)161 TEST(ProcStatTest, TestErrorOnMissingCpuLine) {
162 constexpr char contents[] =
163 "cpu0 2400 2900 600 690 340 4300 2100 0 0 0\n"
164 "cpu1 1900 2380 510 760 51 370 1500 0 0 0\n"
165 "cpu2 900 400 400 1000 600 400 160 0 0 0\n"
166 "cpu3 1000 20 190 650 109 130 140 0 0 0\n"
167 "intr 694351583 0 0 0 297062868 0 5922464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
168 "0 0\n"
169 "ctxt 579020168\n"
170 "btime 1579718450\n"
171 "processes 113804\n"
172 "procs_running 17\n"
173 "procs_blocked 5\n"
174 "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n";
175 TemporaryFile tf;
176 ASSERT_NE(tf.fd, -1);
177 ASSERT_TRUE(WriteStringToFile(contents, tf.path));
178
179 ProcStat procStat(tf.path);
180 ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
181 EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing cpu line";
182 }
183
TEST(ProcStatTest,TestErrorOnMissingProcsRunningLine)184 TEST(ProcStatTest, TestErrorOnMissingProcsRunningLine) {
185 constexpr char contents[] =
186 "cpu 16200 8700 2000 4100 1250 6200 5900 0 0 0\n"
187 "cpu0 2400 2900 600 690 340 4300 2100 0 0 0\n"
188 "cpu1 1900 2380 510 760 51 370 1500 0 0 0\n"
189 "cpu2 900 400 400 1000 600 400 160 0 0 0\n"
190 "cpu3 1000 20 190 650 109 130 140 0 0 0\n"
191 "intr 694351583 0 0 0 297062868 0 5922464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
192 "0 0\n"
193 "ctxt 579020168\n"
194 "btime 1579718450\n"
195 "processes 113804\n"
196 "procs_blocked 5\n"
197 "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n";
198 TemporaryFile tf;
199 ASSERT_NE(tf.fd, -1);
200 ASSERT_TRUE(WriteStringToFile(contents, tf.path));
201
202 ProcStat procStat(tf.path);
203 ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
204 EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing procs_running line";
205 }
206
TEST(ProcStatTest,TestErrorOnMissingProcsBlockedLine)207 TEST(ProcStatTest, TestErrorOnMissingProcsBlockedLine) {
208 constexpr char contents[] =
209 "cpu 16200 8700 2000 4100 1250 6200 5900 0 0 0\n"
210 "cpu0 2400 2900 600 690 340 4300 2100 0 0 0\n"
211 "cpu1 1900 2380 510 760 51 370 1500 0 0 0\n"
212 "cpu2 900 400 400 1000 600 400 160 0 0 0\n"
213 "cpu3 1000 20 190 650 109 130 140 0 0 0\n"
214 "intr 694351583 0 0 0 297062868 0 5922464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
215 "0 0\n"
216 "ctxt 579020168\n"
217 "btime 1579718450\n"
218 "processes 113804\n"
219 "procs_running 17\n"
220 "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n";
221 TemporaryFile tf;
222 ASSERT_NE(tf.fd, -1);
223 ASSERT_TRUE(WriteStringToFile(contents, tf.path));
224
225 ProcStat procStat(tf.path);
226 ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
227 EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing procs_blocked line";
228 }
229
TEST(ProcStatTest,TestErrorOnUnknownProcsLine)230 TEST(ProcStatTest, TestErrorOnUnknownProcsLine) {
231 constexpr char contents[] =
232 "cpu 16200 8700 2000 4100 1250 6200 5900 0 0 0\n"
233 "cpu0 2400 2900 600 690 340 4300 2100 0 0 0\n"
234 "cpu1 1900 2380 510 760 51 370 1500 0 0 0\n"
235 "cpu2 900 400 400 1000 600 400 160 0 0 0\n"
236 "cpu3 1000 20 190 650 109 130 140 0 0 0\n"
237 "intr 694351583 0 0 0 297062868 0 5922464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "
238 "0 0\n"
239 "ctxt 579020168\n"
240 "btime 1579718450\n"
241 "processes 113804\n"
242 "procs_running 17\n"
243 "procs_blocked 5\n"
244 "procs_sleeping 15\n"
245 "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n";
246 TemporaryFile tf;
247 ASSERT_NE(tf.fd, -1);
248 ASSERT_TRUE(WriteStringToFile(contents, tf.path));
249
250 ProcStat procStat(tf.path);
251 ASSERT_TRUE(procStat.enabled()) << "Temporary file is inaccessible";
252 EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to unknown procs line";
253 }
254
TEST(ProcStatTest,TestProcStatContentsFromDevice)255 TEST(ProcStatTest, TestProcStatContentsFromDevice) {
256 ProcStat procStat;
257 ASSERT_TRUE(procStat.enabled()) << kProcStatPath << " file is inaccessible";
258 ASSERT_RESULT_OK(procStat.collect());
259
260 const auto& info = procStat.deltaStats();
261 /* The below checks should pass because the /proc/stats file should have the CPU time spent
262 * since bootup and there should be at least one running process.
263 */
264 EXPECT_GT(info.totalCpuTime(), 0);
265 EXPECT_GT(info.totalProcessCount(), 0);
266 }
267
268 } // namespace watchdog
269 } // namespace automotive
270 } // namespace android
271