• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ProcDiskStats.h"
18 
19 #include <android-base/file.h>
20 #include <android-base/strings.h>
21 #include <gmock/gmock.h>
22 #include <log/log.h>
23 
24 #include <inttypes.h>
25 
26 namespace android {
27 namespace automotive {
28 namespace watchdog {
29 
30 using ::android::base::StartsWith;
31 using ::android::base::StringAppendF;
32 using ::android::base::WriteStringToFile;
33 
34 namespace {
35 
getDiskStatsLine(const DiskStats & stats)36 std::string getDiskStatsLine(const DiskStats& stats) {
37     /**
38      * /proc/diskstats lines doesn't contain tab spaces instead they have whitespace.
39      * Thus write the output in the same format as /proc/diskstats so the tests can reproduce the
40      * on device file format.
41      */
42     std::string buffer;
43     StringAppendF(&buffer, "   %d       %d %s", stats.major, stats.minor, stats.deviceName.c_str());
44     StringAppendF(&buffer, " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, stats.numReadsCompleted,
45                   stats.numReadsMerged, stats.numKibRead * 2, stats.readTimeInMs);
46     StringAppendF(&buffer, " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64,
47                   stats.numWritesCompleted, stats.numWritesMerged, stats.numKibWritten * 2,
48                   stats.writeTimeInMs);
49     StringAppendF(&buffer, " 0 %" PRIu64 " %" PRIu64 " 0 0 0 0 %" PRIu64 " %" PRIu64 "\n",
50                   stats.totalIoTimeInMs, stats.weightedTotalIoTimeInMs, stats.numFlushCompleted,
51                   stats.flushTimeInMs);
52     return buffer;
53 }
54 
getDiskStatsFile(const IProcDiskStatsInterface::PerPartitionDiskStats & allStats)55 std::string getDiskStatsFile(const IProcDiskStatsInterface::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 IProcDiskStatsInterface::PerPartitionDiskStats & perPartitionDiskStats)63 DiskStats aggregateSystemWideDiskStats(
64         const IProcDiskStatsInterface::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(ProcDiskStatsTest,TestValidStatsFile)87 TEST(ProcDiskStatsTest, TestValidStatsFile) {
88     IProcDiskStatsInterface::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     ProcDiskStats procDiskStats(tf.path);
100     ASSERT_TRUE(procDiskStats.enabled()) << "Temporary file is inaccessible";
101     ASSERT_RESULT_OK(procDiskStats.collect());
102 
103     auto actualDiskStats = procDiskStats.deltaSystemWideDiskStats();
104 
105     ASSERT_TRUE(isEquals(expectedDiskStats, actualDiskStats))
106             << "Expected 1st collection: '" << getDiskStatsLine(expectedDiskStats) << "'\nActual: '"
107             << getDiskStatsLine(actualDiskStats) << "'";
108 
109     uint64_t maxUint64 = std::numeric_limits<uint64_t>::max();
110     latestDiskStats = {{252, 32, "vdc", maxUint64, 130000, 100, maxUint64, 30000, 1305000, 1560000,
111                         800, 386140, 313482, 164709, 14505},
112                        {251, 0, "zram0", 21959, 0, 175672, 868, 113635, 0, 909080, 9320, 25940,
113                         10188, 0, 0},
114                        {254, 0, "dm-0", 3400, 0, 5000, 1104264, 0, 0, 0, 0, 185768, 1104264, 0, 0}};
115     ASSERT_TRUE(WriteStringToFile(getDiskStatsFile(latestDiskStats), tf.path));
116 
117     expectedDiskStats =
118             {0,     0,       "",      maxUint64,          129240, maxUint64 - 900, maxUint64,
119              15000, 1000000, 1000000, maxUint64 - 189200, 300000, 1100000,         100000,
120              10000};
121 
122     ASSERT_RESULT_OK(procDiskStats.collect());
123 
124     actualDiskStats = procDiskStats.deltaSystemWideDiskStats();
125 
126     ASSERT_TRUE(isEquals(expectedDiskStats, actualDiskStats))
127             << "Expected 2nd collection: '" << getDiskStatsLine(expectedDiskStats) << "'\nActual: '"
128             << getDiskStatsLine(actualDiskStats) << "'";
129 }
130 
TEST(ProcDiskStatsTest,TestErrorOnInvalidStatsFile)131 TEST(ProcDiskStatsTest, TestErrorOnInvalidStatsFile) {
132     constexpr char contents[] = "252 0 disk 1200 300 0 CORRUPTED DATA\n";
133     TemporaryFile tf;
134     ASSERT_NE(tf.fd, -1);
135     ASSERT_TRUE(WriteStringToFile(contents, tf.path));
136 
137     ProcDiskStats procDiskStats(tf.path);
138     ASSERT_TRUE(procDiskStats.enabled()) << "Temporary file is inaccessible";
139     EXPECT_FALSE(procDiskStats.collect().ok()) << "No error returned for invalid file";
140 }
141 
142 }  // namespace watchdog
143 }  // namespace automotive
144 }  // namespace android
145