blob: 39007e46032c086725a79f95abff140c48f55cbd [file] [log] [blame]
Connor O'Brien57337192018-11-20 12:49:16 -08001
Connor O'Briena178a732019-06-05 18:27:47 -07002#include "timeinstate.h"
3
4#include <sys/sysinfo.h>
5
Connor O'Brien57337192018-11-20 12:49:16 -08006#include <unordered_map>
7#include <vector>
8
9#include <gtest/gtest.h>
10
Connor O'Briena178a732019-06-05 18:27:47 -070011#include <android-base/unique_fd.h>
12#include <bpf/BpfMap.h>
Connor O'Brien57337192018-11-20 12:49:16 -080013#include <cputimeinstate.h>
Connor O'Briena178a732019-06-05 18:27:47 -070014#include <libbpf.h>
Connor O'Brien57337192018-11-20 12:49:16 -080015
16namespace android {
17namespace bpf {
18
Connor O'Briena178a732019-06-05 18:27:47 -070019static constexpr uint64_t NSEC_PER_SEC = 1000000000;
20static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365;
21
Connor O'Brien57337192018-11-20 12:49:16 -080022using std::vector;
23
24TEST(TimeInStateTest, SingleUid) {
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070025 auto times = getUidCpuFreqTimes(0);
26 ASSERT_TRUE(times.has_value());
27 EXPECT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -080028}
29
30TEST(TimeInStateTest, AllUid) {
31 vector<size_t> sizes;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070032 auto map = getUidsCpuFreqTimes();
33 ASSERT_TRUE(map.has_value());
Connor O'Brien57337192018-11-20 12:49:16 -080034
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070035 ASSERT_FALSE(map->empty());
Connor O'Brien57337192018-11-20 12:49:16 -080036
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070037 auto firstEntry = map->begin()->second;
Connor O'Brien57337192018-11-20 12:49:16 -080038 for (const auto &subEntry : firstEntry) sizes.emplace_back(subEntry.size());
39
Connor O'Brienf03b6ae2019-06-05 18:03:12 -070040 for (const auto &vec : *map) {
Connor O'Brien57337192018-11-20 12:49:16 -080041 ASSERT_EQ(vec.second.size(), sizes.size());
42 for (size_t i = 0; i < vec.second.size(); ++i) ASSERT_EQ(vec.second[i].size(), sizes[i]);
43 }
44}
45
Connor O'Briena178a732019-06-05 18:27:47 -070046TEST(TimeInStateTest, SingleAndAllUidConsistent) {
47 auto map = getUidsCpuFreqTimes();
48 ASSERT_TRUE(map.has_value());
49 ASSERT_FALSE(map->empty());
50
51 for (const auto &kv : *map) {
52 uint32_t uid = kv.first;
53 auto times1 = kv.second;
54 auto times2 = getUidCpuFreqTimes(uid);
55 ASSERT_TRUE(times2.has_value());
56
57 ASSERT_EQ(times1.size(), times2->size());
58 for (uint32_t i = 0; i < times1.size(); ++i) {
59 ASSERT_EQ(times1[i].size(), (*times2)[i].size());
60 for (uint32_t j = 0; j < times1[i].size(); ++j) {
61 ASSERT_LE((*times2)[i][j] - times1[i][j], NSEC_PER_SEC);
62 }
63 }
64 }
65}
66
67void TestCheckDelta(uint64_t before, uint64_t after) {
68 // Times should never decrease
69 ASSERT_LE(before, after);
70 // UID can't have run for more than ~1s on each CPU
71 ASSERT_LE(after - before, NSEC_PER_SEC * 2 * get_nprocs_conf());
72}
73
74TEST(TimeInStateTest, AllUidMonotonic) {
75 auto map1 = getUidsCpuFreqTimes();
76 ASSERT_TRUE(map1.has_value());
77 sleep(1);
78 auto map2 = getUidsCpuFreqTimes();
79 ASSERT_TRUE(map2.has_value());
80
81 for (const auto &kv : *map1) {
82 uint32_t uid = kv.first;
83 auto times = kv.second;
84 ASSERT_NE(map2->find(uid), map2->end());
85 for (uint32_t policy = 0; policy < times.size(); ++policy) {
86 for (uint32_t freqIdx = 0; freqIdx < times[policy].size(); ++freqIdx) {
87 auto before = times[policy][freqIdx];
88 auto after = (*map2)[uid][policy][freqIdx];
89 ASSERT_NO_FATAL_FAILURE(TestCheckDelta(before, after));
90 }
91 }
92 }
93}
94
95TEST(TimeInStateTest, AllUidSanityCheck) {
96 auto map = getUidsCpuFreqTimes();
97 ASSERT_TRUE(map.has_value());
98
99 bool foundLargeValue = false;
100 for (const auto &kv : *map) {
101 for (const auto &timeVec : kv.second) {
102 for (const auto &time : timeVec) {
103 ASSERT_LE(time, NSEC_PER_YEAR);
104 if (time > UINT32_MAX) foundLargeValue = true;
105 }
106 }
107 }
108 // UINT32_MAX nanoseconds is less than 5 seconds, so if every part of our pipeline is using
109 // uint64_t as expected, we should have some times higher than that.
110 ASSERT_TRUE(foundLargeValue);
111}
112
Connor O'Brien57337192018-11-20 12:49:16 -0800113TEST(TimeInStateTest, RemoveUid) {
Connor O'Briena178a732019-06-05 18:27:47 -0700114 uint32_t uid = 0;
115 {
116 // Find an unused UID
117 auto times = getUidsCpuFreqTimes();
118 ASSERT_TRUE(times.has_value());
119 ASSERT_FALSE(times->empty());
120 for (const auto &kv : *times) uid = std::max(uid, kv.first);
121 ++uid;
122 }
123 {
124 // Add a map entry for our fake UID by copying a real map entry
125 android::base::unique_fd fd{bpf_obj_get(BPF_FS_PATH "map_time_in_state_uid_times_map")};
126 ASSERT_GE(fd, 0);
127 time_key_t k;
128 ASSERT_FALSE(getFirstMapKey(fd, &k));
Connor O'Brien1a180402019-06-07 16:39:49 -0700129 std::vector<val_t> vals(get_nprocs_conf());
130 ASSERT_FALSE(findMapEntry(fd, &k, vals.data()));
Connor O'Briena178a732019-06-05 18:27:47 -0700131 k.uid = uid;
Connor O'Brien1a180402019-06-07 16:39:49 -0700132 ASSERT_FALSE(writeToMapEntry(fd, &k, vals.data(), BPF_NOEXIST));
Connor O'Briena178a732019-06-05 18:27:47 -0700133 }
134 auto times = getUidCpuFreqTimes(uid);
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700135 ASSERT_TRUE(times.has_value());
136 ASSERT_FALSE(times->empty());
Connor O'Brien57337192018-11-20 12:49:16 -0800137
138 uint64_t sum = 0;
Connor O'Brienf03b6ae2019-06-05 18:03:12 -0700139 for (size_t i = 0; i < times->size(); ++i) {
140 for (auto x : (*times)[i]) sum += x;
Connor O'Brien57337192018-11-20 12:49:16 -0800141 }
142 ASSERT_GT(sum, (uint64_t)0);
143
Connor O'Briena178a732019-06-05 18:27:47 -0700144 ASSERT_TRUE(clearUidCpuFreqTimes(uid));
Connor O'Brien57337192018-11-20 12:49:16 -0800145
Connor O'Briena178a732019-06-05 18:27:47 -0700146 auto allTimes = getUidsCpuFreqTimes();
147 ASSERT_TRUE(allTimes.has_value());
148 ASSERT_FALSE(allTimes->empty());
149 ASSERT_EQ(allTimes->find(uid), allTimes->end());
Connor O'Brien57337192018-11-20 12:49:16 -0800150}
151
152} // namespace bpf
153} // namespace android