1 /*
2 * Copyright (c) 2021, 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 "ProcDiskStatsCollector.h"
18
19 #include <android-base/file.h>
20 #include <android-base/strings.h>
21 #include <gmock/gmock.h>
22
23 #include <inttypes.h>
24
25 namespace android {
26 namespace automotive {
27 namespace watchdog {
28
29 using ::android::base::StartsWith;
30 using ::android::base::StringAppendF;
31 using ::android::base::WriteStringToFile;
32
33 namespace {
34
getDiskStatsLine(const DiskStats & stats)35 std::string getDiskStatsLine(const DiskStats& stats) {
36 /**
37 * /proc/diskstats lines doesn't contain tab spaces instead they have whitespace.
38 * Thus write the output in the same format as /proc/diskstats so the tests can reproduce the
39 * on device file format.
40 */
41 std::string buffer;
42 StringAppendF(&buffer, " %d %d %s", stats.major, stats.minor, stats.deviceName.c_str());
43 StringAppendF(&buffer, " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, stats.numReadsCompleted,
44 stats.numReadsMerged, stats.numKibRead * 2, stats.readTimeInMs);
45 StringAppendF(&buffer, " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64,
46 stats.numWritesCompleted, stats.numWritesMerged, stats.numKibWritten * 2,
47 stats.writeTimeInMs);
48 StringAppendF(&buffer, " 0 %" PRIu64 " %" PRIu64 " 0 0 0 0 %" PRIu64 " %" PRIu64 "\n",
49 stats.totalIoTimeInMs, stats.weightedTotalIoTimeInMs, stats.numFlushCompleted,
50 stats.flushTimeInMs);
51 return buffer;
52 }
53
getDiskStatsFile(const ProcDiskStatsCollectorInterface::PerPartitionDiskStats & allStats)54 std::string getDiskStatsFile(
55 const ProcDiskStatsCollectorInterface::PerPartitionDiskStats& allStats) {
56 std::string buffer;
57 for (const auto& stats : allStats) {
58 StringAppendF(&buffer, "%s", getDiskStatsLine(stats).c_str());
59 }
60 return buffer;
61 }
62
aggregateSystemWideDiskStats(const ProcDiskStatsCollectorInterface::PerPartitionDiskStats & perPartitionDiskStats)63 DiskStats aggregateSystemWideDiskStats(
64 const ProcDiskStatsCollectorInterface::PerPartitionDiskStats& perPartitionDiskStats) {
65 DiskStats systemWideStats;
66 for (const auto& stats : perPartitionDiskStats) {
67 if (recordStatsForDevice(stats.deviceName)) {
68 systemWideStats += stats;
69 }
70 }
71 return systemWideStats;
72 }
73
isEquals(const DiskStats & lhs,const DiskStats & rhs)74 bool isEquals(const DiskStats& lhs, const DiskStats& rhs) {
75 auto tieStats = [](const DiskStats& stats) {
76 return std::tie(stats.major, stats.minor, stats.deviceName, stats.numReadsCompleted,
77 stats.numReadsMerged, stats.numKibRead, stats.readTimeInMs,
78 stats.numWritesCompleted, stats.numWritesMerged, stats.numKibWritten,
79 stats.writeTimeInMs, stats.totalIoTimeInMs, stats.weightedTotalIoTimeInMs,
80 stats.numFlushCompleted, stats.flushTimeInMs);
81 };
82 return tieStats(lhs) == tieStats(rhs);
83 }
84
85 } // namespace
86
TEST(ProcDiskStatsCollectorTest,TestValidStatsFile)87 TEST(ProcDiskStatsCollectorTest, TestValidStatsFile) {
88 ProcDiskStatsCollectorInterface::PerPartitionDiskStats latestDiskStats =
89 {{252, 32, "vdc", 120000, 760, 2000, 17190, 15000, 305000, 560000, 190000, 186140,
90 213482, 64709, 4505},
91 {251, 0, "zram0", 21959, 0, 175672, 868, 113635, 0, 909080, 9320, 25940, 10188, 0, 0},
92 {254, 0, "dm-0", 340000, 0, 4000, 104264, 0, 0, 0, 0, 85768, 104264, 0, 0}};
93 TemporaryFile tf;
94 ASSERT_NE(tf.fd, -1);
95 ASSERT_TRUE(WriteStringToFile(getDiskStatsFile(latestDiskStats), tf.path));
96
97 DiskStats expectedDiskStats = aggregateSystemWideDiskStats(latestDiskStats);
98
99 ProcDiskStatsCollector collector(tf.path);
100 collector.init();
101
102 ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
103 ASSERT_RESULT_OK(collector.collect());
104
105 auto actualDiskStats = collector.deltaSystemWideDiskStats();
106
107 ASSERT_TRUE(isEquals(expectedDiskStats, actualDiskStats))
108 << "Expected 1st collection: '" << getDiskStatsLine(expectedDiskStats) << "'\nActual: '"
109 << getDiskStatsLine(actualDiskStats) << "'";
110
111 uint64_t maxUint64 = std::numeric_limits<uint64_t>::max();
112 latestDiskStats = {{252, 32, "vdc", maxUint64, 130000, 100, maxUint64, 30000, 1305000, 1560000,
113 800, 386140, 313482, 164709, 14505},
114 {251, 0, "zram0", 21959, 0, 175672, 868, 113635, 0, 909080, 9320, 25940,
115 10188, 0, 0},
116 {254, 0, "dm-0", 3400, 0, 5000, 1104264, 0, 0, 0, 0, 185768, 1104264, 0, 0}};
117 ASSERT_TRUE(WriteStringToFile(getDiskStatsFile(latestDiskStats), tf.path));
118
119 expectedDiskStats =
120 {0, 0, "", maxUint64, 129240, maxUint64 - 900, maxUint64,
121 15000, 1000000, 1000000, maxUint64 - 189200, 300000, 1100000, 100000,
122 10000};
123
124 ASSERT_RESULT_OK(collector.collect());
125
126 actualDiskStats = collector.deltaSystemWideDiskStats();
127
128 ASSERT_TRUE(isEquals(expectedDiskStats, actualDiskStats))
129 << "Expected 2nd collection: '" << getDiskStatsLine(expectedDiskStats) << "'\nActual: '"
130 << getDiskStatsLine(actualDiskStats) << "'";
131 }
132
TEST(ProcDiskStatsCollectorTest,TestErrorOnInvalidStatsFile)133 TEST(ProcDiskStatsCollectorTest, TestErrorOnInvalidStatsFile) {
134 constexpr char contents[] = "252 0 disk 1200 300 0 CORRUPTED DATA\n";
135 TemporaryFile tf;
136 ASSERT_NE(tf.fd, -1);
137 ASSERT_TRUE(WriteStringToFile(contents, tf.path));
138
139 ProcDiskStatsCollector collector(tf.path);
140 collector.init();
141
142 ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
143 EXPECT_FALSE(collector.collect().ok()) << "No error returned for invalid file";
144 }
145
146 } // namespace watchdog
147 } // namespace automotive
148 } // namespace android
149