From 6f179e6616e2f77e1df8eb6dc5d4da82639dc7ea Mon Sep 17 00:00:00 2001 From: Connor O'Brien Date: Tue, 18 Feb 2020 15:54:27 -0800 Subject: libtimeinstate: skip tests on non-supporting devices Devices that can't support BPF time in state tracking (e.g. upgrading devices without kernel support for perf event selinux hooks) currently fail libtimeinstate_test without this failure indicating a bug or incorrect behavior. To make test failures meaningful, skip the tests on such devices instead of failing. isTrackingUidTimesSupported() is now exercised by the SetUp() function, making the IsTrackingSupported test redundant, so remove that test. Test: run libtimeinstate_test on device without time_in_state.o and confirm all tests are skipped Change-Id: I11bc5cc6b2e32f56c40852846fddd6790d6643b3 Signed-off-by: Connor O'Brien --- libs/cputimeinstate/testtimeinstate.cpp | 58 +++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 25 deletions(-) (limited to 'libs') diff --git a/libs/cputimeinstate/testtimeinstate.cpp b/libs/cputimeinstate/testtimeinstate.cpp index 2112b10ebc..1513ecafc8 100644 --- a/libs/cputimeinstate/testtimeinstate.cpp +++ b/libs/cputimeinstate/testtimeinstate.cpp @@ -27,6 +27,7 @@ #include +#include #include #include #include @@ -40,24 +41,31 @@ static constexpr uint64_t NSEC_PER_YEAR = NSEC_PER_SEC * 60 * 60 * 24 * 365; using std::vector; -TEST(TimeInStateTest, IsTrackingSupported) { - isTrackingUidTimesSupported(); - SUCCEED(); -} +class TimeInStateTest : public testing::Test { + protected: + TimeInStateTest() {}; + + void SetUp() { + if (!isTrackingUidTimesSupported() || + !android::base::GetBoolProperty("sys.init.perf_lsm_hooks", false)) { + GTEST_SKIP(); + } + } +}; -TEST(TimeInStateTest, TotalTimeInState) { +TEST_F(TimeInStateTest, TotalTimeInState) { auto times = getTotalCpuFreqTimes(); ASSERT_TRUE(times.has_value()); EXPECT_FALSE(times->empty()); } -TEST(TimeInStateTest, SingleUidTimeInState) { +TEST_F(TimeInStateTest, SingleUidTimeInState) { auto times = getUidCpuFreqTimes(0); ASSERT_TRUE(times.has_value()); EXPECT_FALSE(times->empty()); } -TEST(TimeInStateTest, SingleUidConcurrentTimes) { +TEST_F(TimeInStateTest, SingleUidConcurrentTimes) { auto concurrentTimes = getUidConcurrentTimes(0); ASSERT_TRUE(concurrentTimes.has_value()); ASSERT_FALSE(concurrentTimes->active.empty()); @@ -117,7 +125,7 @@ static void TestUidTimesConsistent(const std::vector> &tim EXPECT_EQ(activeSum, policySum); } -TEST(TimeInStateTest, SingleUidTimesConsistent) { +TEST_F(TimeInStateTest, SingleUidTimesConsistent) { auto times = getUidCpuFreqTimes(0); ASSERT_TRUE(times.has_value()); @@ -127,7 +135,7 @@ TEST(TimeInStateTest, SingleUidTimesConsistent) { ASSERT_NO_FATAL_FAILURE(TestUidTimesConsistent(*times, *concurrentTimes)); } -TEST(TimeInStateTest, AllUidTimeInState) { +TEST_F(TimeInStateTest, AllUidTimeInState) { uint64_t zero = 0; auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)}; for (const auto &map : maps) { @@ -163,7 +171,7 @@ void TestCheckUpdate(const std::vector> &before, ASSERT_LE(sumAfter - sumBefore, NSEC_PER_SEC); } -TEST(TimeInStateTest, AllUidUpdatedTimeInState) { +TEST_F(TimeInStateTest, AllUidUpdatedTimeInState) { uint64_t lastUpdate = 0; auto map1 = getUidsUpdatedCpuFreqTimes(&lastUpdate); ASSERT_TRUE(map1.has_value()); @@ -197,7 +205,7 @@ TEST(TimeInStateTest, AllUidUpdatedTimeInState) { } } -TEST(TimeInStateTest, TotalAndAllUidTimeInStateConsistent) { +TEST_F(TimeInStateTest, TotalAndAllUidTimeInStateConsistent) { auto allUid = getUidsCpuFreqTimes(); auto total = getTotalCpuFreqTimes(); @@ -222,7 +230,7 @@ TEST(TimeInStateTest, TotalAndAllUidTimeInStateConsistent) { } } -TEST(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) { +TEST_F(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) { uint64_t zero = 0; auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)}; for (const auto &map : maps) { @@ -246,7 +254,7 @@ TEST(TimeInStateTest, SingleAndAllUidTimeInStateConsistent) { } } -TEST(TimeInStateTest, AllUidConcurrentTimes) { +TEST_F(TimeInStateTest, AllUidConcurrentTimes) { uint64_t zero = 0; auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)}; for (const auto &map : maps) { @@ -264,7 +272,7 @@ TEST(TimeInStateTest, AllUidConcurrentTimes) { } } -TEST(TimeInStateTest, AllUidUpdatedConcurrentTimes) { +TEST_F(TimeInStateTest, AllUidUpdatedConcurrentTimes) { uint64_t lastUpdate = 0; auto map1 = getUidsUpdatedConcurrentTimes(&lastUpdate); ASSERT_TRUE(map1.has_value()); @@ -299,7 +307,7 @@ TEST(TimeInStateTest, AllUidUpdatedConcurrentTimes) { } } -TEST(TimeInStateTest, SingleAndAllUidConcurrentTimesConsistent) { +TEST_F(TimeInStateTest, SingleAndAllUidConcurrentTimesConsistent) { uint64_t zero = 0; auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)}; for (const auto &map : maps) { @@ -328,7 +336,7 @@ void TestCheckDelta(uint64_t before, uint64_t after) { ASSERT_LE(after - before, NSEC_PER_SEC * 2 * get_nprocs_conf()); } -TEST(TimeInStateTest, TotalTimeInStateMonotonic) { +TEST_F(TimeInStateTest, TotalTimeInStateMonotonic) { auto before = getTotalCpuFreqTimes(); ASSERT_TRUE(before.has_value()); sleep(1); @@ -344,7 +352,7 @@ TEST(TimeInStateTest, TotalTimeInStateMonotonic) { } } -TEST(TimeInStateTest, AllUidTimeInStateMonotonic) { +TEST_F(TimeInStateTest, AllUidTimeInStateMonotonic) { auto map1 = getUidsCpuFreqTimes(); ASSERT_TRUE(map1.has_value()); sleep(1); @@ -365,7 +373,7 @@ TEST(TimeInStateTest, AllUidTimeInStateMonotonic) { } } -TEST(TimeInStateTest, AllUidConcurrentTimesMonotonic) { +TEST_F(TimeInStateTest, AllUidConcurrentTimesMonotonic) { auto map1 = getUidsConcurrentTimes(); ASSERT_TRUE(map1.has_value()); ASSERT_FALSE(map1->empty()); @@ -393,7 +401,7 @@ TEST(TimeInStateTest, AllUidConcurrentTimesMonotonic) { } } -TEST(TimeInStateTest, AllUidTimeInStateSanityCheck) { +TEST_F(TimeInStateTest, AllUidTimeInStateSanityCheck) { uint64_t zero = 0; auto maps = {getUidsCpuFreqTimes(), getUidsUpdatedCpuFreqTimes(&zero)}; for (const auto &map : maps) { @@ -414,7 +422,7 @@ TEST(TimeInStateTest, AllUidTimeInStateSanityCheck) { } } -TEST(TimeInStateTest, AllUidConcurrentTimesSanityCheck) { +TEST_F(TimeInStateTest, AllUidConcurrentTimesSanityCheck) { uint64_t zero = 0; auto maps = {getUidsConcurrentTimes(), getUidsUpdatedConcurrentTimes(&zero)}; for (const auto &concurrentMap : maps) { @@ -441,7 +449,7 @@ TEST(TimeInStateTest, AllUidConcurrentTimesSanityCheck) { } } -TEST(TimeInStateTest, AllUidConcurrentTimesFailsOnInvalidBucket) { +TEST_F(TimeInStateTest, AllUidConcurrentTimesFailsOnInvalidBucket) { uint32_t uid = 0; { // Find an unused UID @@ -463,7 +471,7 @@ TEST(TimeInStateTest, AllUidConcurrentTimesFailsOnInvalidBucket) { ASSERT_FALSE(deleteMapEntry(fd, &key)); } -TEST(TimeInStateTest, AllUidTimesConsistent) { +TEST_F(TimeInStateTest, AllUidTimesConsistent) { auto tisMap = getUidsCpuFreqTimes(); ASSERT_TRUE(tisMap.has_value()); @@ -481,7 +489,7 @@ TEST(TimeInStateTest, AllUidTimesConsistent) { } } -TEST(TimeInStateTest, RemoveUid) { +TEST_F(TimeInStateTest, RemoveUid) { uint32_t uid = 0; { // Find an unused UID @@ -547,7 +555,7 @@ TEST(TimeInStateTest, RemoveUid) { ASSERT_EQ(allConcurrentTimes->find(uid), allConcurrentTimes->end()); } -TEST(TimeInStateTest, GetCpuFreqs) { +TEST_F(TimeInStateTest, GetCpuFreqs) { auto freqs = getCpuFreqs(); ASSERT_TRUE(freqs.has_value()); @@ -583,7 +591,7 @@ void *testThread(void *) { return nullptr; } -TEST(TimeInStateTest, GetAggregatedTaskCpuFreqTimes) { +TEST_F(TimeInStateTest, GetAggregatedTaskCpuFreqTimes) { uint64_t startTimeNs = timeNanos(); sem_init(&pingsem, 0, 1); -- cgit v1.2.3-59-g8ed1b From aeb3a986e2e7231ef5983c13c3a09f9741f6ecf2 Mon Sep 17 00:00:00 2001 From: Connor O'Brien Date: Wed, 19 Feb 2020 20:13:14 -0800 Subject: libtimeinstate: add tests to presubmit Run libtimeinstate_test in presubmit for libtimeinstate changes. Test: atest --test-mapping frameworks/native/libs/cputimeinstate; confirm that libtimeinstate_test runs & passes Bug: 138317993 Change-Id: Ide638de1062c3b5b7fc7c4e969f3030e4264a846 Signed-off-by: Connor O'Brien --- libs/cputimeinstate/Android.bp | 1 + libs/cputimeinstate/TEST_MAPPING | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 libs/cputimeinstate/TEST_MAPPING (limited to 'libs') diff --git a/libs/cputimeinstate/Android.bp b/libs/cputimeinstate/Android.bp index 73f9d4d153..4f63194f03 100644 --- a/libs/cputimeinstate/Android.bp +++ b/libs/cputimeinstate/Android.bp @@ -45,4 +45,5 @@ cc_test { "-Wextra", ], require_root: true, + test_suites: ["general-tests"], } diff --git a/libs/cputimeinstate/TEST_MAPPING b/libs/cputimeinstate/TEST_MAPPING new file mode 100644 index 0000000000..4781520755 --- /dev/null +++ b/libs/cputimeinstate/TEST_MAPPING @@ -0,0 +1,7 @@ +{ + "presubmit": [ + { + "name": "libtimeinstate_test" + } + ] +} -- cgit v1.2.3-59-g8ed1b