Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 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 <inttypes.h> |
| 22 | |
| 23 | #include <string> |
| 24 | |
| 25 | #include "gmock/gmock.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace automotive { |
| 29 | namespace watchdog { |
| 30 | |
| 31 | using android::base::StringPrintf; |
| 32 | using android::base::WriteStringToFile; |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | std::string toString(const ProcStatInfo& info) { |
| 37 | const auto& cpuStats = info.cpuStats; |
| 38 | return StringPrintf("Cpu Stats:\nUserTime: %" PRIu64 " NiceTime: %" PRIu64 " SysTime: %" PRIu64 |
| 39 | " IdleTime: %" PRIu64 " IoWaitTime: %" PRIu64 " IrqTime: %" PRIu64 |
| 40 | " SoftIrqTime: %" PRIu64 " StealTime: %" PRIu64 " GuestTime: %" PRIu64 |
| 41 | " GuestNiceTime: %" PRIu64 "\nNumber of running processes: %" PRIu32 |
| 42 | "\nNumber of blocked processes: %" PRIu32, |
| 43 | cpuStats.userTime, cpuStats.niceTime, cpuStats.sysTime, cpuStats.idleTime, |
| 44 | cpuStats.ioWaitTime, cpuStats.irqTime, cpuStats.softIrqTime, |
| 45 | cpuStats.stealTime, cpuStats.guestTime, cpuStats.guestNiceTime, |
| 46 | info.runnableProcessesCnt, info.ioBlockedProcessesCnt); |
| 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | TEST(ProcStatTest, TestValidStatFile) { |
| 52 | constexpr char firstSnapshot[] = |
| 53 | "cpu 6200 5700 1700 3100 1100 5200 3900 0 0 0\n" |
| 54 | "cpu0 2400 2900 600 690 340 4300 2100 0 0 0\n" |
| 55 | "cpu1 1900 2380 510 760 51 370 1500 0 0 0\n" |
| 56 | "cpu2 900 400 400 1000 600 400 160 0 0 0\n" |
| 57 | "cpu3 1000 20 190 650 109 130 140 0 0 0\n" |
| 58 | "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 " |
| 59 | "0 0\n" |
| 60 | // Skipped most of the intr line as it is not important for testing the ProcStat parsing |
| 61 | // logic. |
| 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.runnableProcessesCnt = 17; |
| 82 | expectedFirstDelta.ioBlockedProcessesCnt = 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 | |
| 91 | const auto& actualFirstDelta = procStat.collect(); |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 92 | EXPECT_RESULT_OK(actualFirstDelta); |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 93 | EXPECT_EQ(expectedFirstDelta, *actualFirstDelta) |
| 94 | << "First snapshot doesnt't match.\nExpected:\n" |
| 95 | << toString(expectedFirstDelta) << "\nActual:\n" |
| 96 | << toString(*actualFirstDelta); |
| 97 | |
| 98 | constexpr char secondSnapshot[] = |
| 99 | "cpu 16200 8700 2000 4100 2200 6200 5900 0 0 0\n" |
| 100 | "cpu0 4400 3400 700 890 800 4500 3100 0 0 0\n" |
| 101 | "cpu1 5900 3380 610 960 100 670 2000 0 0 0\n" |
| 102 | "cpu2 2900 1000 450 1400 800 600 460 0 0 0\n" |
| 103 | "cpu3 3000 920 240 850 500 430 340 0 0 0\n" |
| 104 | "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 " |
| 105 | "0 0\n" |
| 106 | "ctxt 579020168\n" |
| 107 | "btime 1579718450\n" |
| 108 | "processes 113804\n" |
| 109 | "procs_running 10\n" |
| 110 | "procs_blocked 2\n" |
| 111 | "softirq 33275060 934664 11958403 5111 516325 200333 0 341482 10651335 0 8667407\n"; |
| 112 | ProcStatInfo expectedSecondDelta; |
| 113 | expectedSecondDelta.cpuStats = { |
| 114 | .userTime = 10000, |
| 115 | .niceTime = 3000, |
| 116 | .sysTime = 300, |
| 117 | .idleTime = 1000, |
| 118 | .ioWaitTime = 1100, |
| 119 | .irqTime = 1000, |
| 120 | .softIrqTime = 2000, |
| 121 | .stealTime = 0, |
| 122 | .guestTime = 0, |
| 123 | .guestNiceTime = 0, |
| 124 | }; |
| 125 | expectedSecondDelta.runnableProcessesCnt = 10; |
| 126 | expectedSecondDelta.ioBlockedProcessesCnt = 2; |
| 127 | |
| 128 | ASSERT_TRUE(WriteStringToFile(secondSnapshot, tf.path)); |
| 129 | const auto& actualSecondDelta = procStat.collect(); |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 130 | EXPECT_RESULT_OK(actualSecondDelta); |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 131 | EXPECT_EQ(expectedSecondDelta, *actualSecondDelta) |
| 132 | << "Second snapshot doesnt't match.\nExpected:\n" |
| 133 | << toString(expectedSecondDelta) << "\nActual:\n" |
| 134 | << toString(*actualSecondDelta); |
| 135 | } |
| 136 | |
| 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"; |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 158 | EXPECT_FALSE(procStat.collect().ok()) << "No error returned for corrupted file"; |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 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"; |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 181 | EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing cpu line"; |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 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"; |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 204 | EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing procs_running line"; |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 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"; |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 227 | EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to missing procs_blocked line"; |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 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"; |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 252 | EXPECT_FALSE(procStat.collect().ok()) << "No error returned due to unknown procs line"; |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | TEST(ProcStatTest, TestProcStatContentsFromDevice) { |
| 256 | ProcStat procStat; |
| 257 | ASSERT_TRUE(procStat.enabled()) << kProcStatPath << " file is inaccessible"; |
| 258 | |
| 259 | const auto& info = procStat.collect(); |
Bernie Innocenti | 266cba6 | 2020-02-19 18:09:36 +0900 | [diff] [blame] | 260 | ASSERT_RESULT_OK(info); |
Lakshman Annadorai | f9b47c2 | 2020-02-10 16:45:18 -0800 | [diff] [blame] | 261 | |
| 262 | // The below checks should pass because the /proc/stats file should have the CPU time spent |
| 263 | // since bootup and there should be at least one running process. |
| 264 | EXPECT_GT(info->totalCpuTime(), 0); |
| 265 | EXPECT_GT(info->totalProcessesCnt(), 0); |
| 266 | } |
| 267 | |
| 268 | } // namespace watchdog |
| 269 | } // namespace automotive |
| 270 | } // namespace android |