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 "UidIoStatsCollector.h"
18
19 #include <android-base/file.h>
20 #include <android-base/stringprintf.h>
21 #include <gmock/gmock.h>
22
23 #include <unordered_map>
24
25 namespace android {
26 namespace automotive {
27 namespace watchdog {
28
29 using ::android::base::StringAppendF;
30 using ::android::base::WriteStringToFile;
31 using ::testing::UnorderedElementsAreArray;
32
33 namespace {
34
toString(std::unordered_map<uid_t,UidIoStats> uidIoStatsByUid)35 std::string toString(std::unordered_map<uid_t, UidIoStats> uidIoStatsByUid) {
36 std::string buffer;
37 for (const auto& [uid, stats] : uidIoStatsByUid) {
38 StringAppendF(&buffer, "{%d: %s}\n", uid, stats.toString().c_str());
39 }
40 return buffer;
41 }
42
43 } // namespace
44
TEST(UidIoStatsCollectorTest,TestValidStatFile)45 TEST(UidIoStatsCollectorTest, TestValidStatFile) {
46 // Format: uid fgRdChar fgWrChar fgRdBytes fgWrBytes bgRdChar bgWrChar bgRdBytes bgWrBytes
47 // fgFsync bgFsync
48 constexpr char firstSnapshot[] = "1001234 5000 1000 3000 500 0 0 0 0 20 0\n"
49 "1005678 500 100 30 50 300 400 100 200 45 60\n"
50 "1009 0 0 0 0 40000 50000 20000 30000 0 300\n"
51 "1001000 4000 3000 2000 1000 400 300 200 100 50 10\n";
52 std::unordered_map<uid_t, UidIoStats> expectedFirstUsage =
53 {{1001234,
54 UidIoStats{/*fgRdBytes=*/3'000, /*bgRdBytes=*/0, /*fgWrBytes=*/500,
55 /*bgWrBytes=*/0, /*fgFsync=*/20, /*bgFsync=*/0}},
56 {1005678,
57 UidIoStats{/*fgRdBytes=*/30, /*bgRdBytes=*/100, /*fgWrBytes=*/50, /*bgWrBytes=*/200,
58 /*fgFsync=*/45, /*bgFsync=*/60}},
59 {1009,
60 UidIoStats{/*fgRdBytes=*/0, /*bgRdBytes=*/20'000, /*fgWrBytes=*/0,
61 /*bgWrBytes=*/30'000,
62 /*fgFsync=*/0, /*bgFsync=*/300}},
63 {1001000,
64 UidIoStats{/*fgRdBytes=*/2'000, /*bgRdBytes=*/200, /*fgWrBytes=*/1'000,
65 /*bgWrBytes=*/100, /*fgFsync=*/50, /*bgFsync=*/10}}};
66 TemporaryFile tf;
67 ASSERT_NE(tf.fd, -1);
68 ASSERT_TRUE(WriteStringToFile(firstSnapshot, tf.path));
69
70 UidIoStatsCollector collector(tf.path);
71 collector.init();
72
73 ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
74 ASSERT_RESULT_OK(collector.collect());
75
76 const auto& actualFirstUsage = collector.deltaStats();
77 EXPECT_THAT(actualFirstUsage, UnorderedElementsAreArray(expectedFirstUsage))
78 << "Expected: " << toString(expectedFirstUsage)
79 << "Actual: " << toString(actualFirstUsage);
80
81 constexpr char secondSnapshot[] = "1001234 10000 2000 7000 950 0 0 0 0 45 0\n"
82 "1005678 600 100 40 50 1000 1000 1000 600 50 70\n"
83 "1003456 300 500 200 300 0 0 0 0 50 0\n"
84 "1001000 400 300 200 100 40 30 20 10 5 1\n";
85 std::unordered_map<uid_t, UidIoStats> expectedSecondUsage =
86 {{1001234,
87 UidIoStats{/*fgRdBytes=*/4'000, /*bgRdBytes=*/0,
88 /*fgWrBytes=*/450, /*bgWrBytes=*/0, /*fgFsync=*/25,
89 /*bgFsync=*/0}},
90 {1005678,
91 UidIoStats{/*fgRdBytes=*/10, /*bgRdBytes=*/900, /*fgWrBytes=*/0, /*bgWrBytes=*/400,
92 /*fgFsync=*/5, /*bgFsync=*/10}},
93 {1003456,
94 UidIoStats{/*fgRdBytes=*/200, /*bgRdBytes=*/0, /*fgWrBytes=*/300, /*bgWrBytes=*/0,
95 /*fgFsync=*/50, /*bgFsync=*/0}}};
96 ASSERT_TRUE(WriteStringToFile(secondSnapshot, tf.path));
97 ASSERT_RESULT_OK(collector.collect());
98
99 const auto& actualSecondUsage = collector.deltaStats();
100 EXPECT_THAT(actualSecondUsage, UnorderedElementsAreArray(expectedSecondUsage))
101 << "Expected: " << toString(expectedSecondUsage)
102 << "Actual: " << toString(actualSecondUsage);
103 }
104
TEST(UidIoStatsCollectorTest,TestErrorOnInvalidStatFile)105 TEST(UidIoStatsCollectorTest, TestErrorOnInvalidStatFile) {
106 // Format: uid fgRdChar fgWrChar fgRdBytes fgWrBytes bgRdChar bgWrChar bgRdBytes bgWrBytes
107 // fgFsync bgFsync
108 constexpr char contents[] = "1001234 5000 1000 3000 500 0 0 0 0 20 0\n"
109 "1005678 500 100 30 50 300 400 100 200 45 60\n"
110 "1009012 0 0 0 0 40000 50000 20000 30000 0 300\n"
111 "1001000 4000 3000 2000 1000 CORRUPTED DATA\n";
112 TemporaryFile tf;
113 ASSERT_NE(tf.fd, -1);
114 ASSERT_TRUE(WriteStringToFile(contents, tf.path));
115
116 UidIoStatsCollector collector(tf.path);
117 collector.init();
118
119 ASSERT_TRUE(collector.enabled()) << "Temporary file is inaccessible";
120 EXPECT_FALSE(collector.collect().ok()) << "No error returned for invalid file";
121 }
122
123 } // namespace watchdog
124 } // namespace automotive
125 } // namespace android
126