David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "StatsLogProcessor.h" |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 16 | #include "StatsService.h" |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 17 | #include "config/ConfigKey.h" |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 18 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 19 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 20 | #include "guardrail/StatsdStats.h" |
| 21 | #include "logd/LogEvent.h" |
| 22 | #include "packages/UidMap.h" |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 23 | #include "storage/StorageManager.h" |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 24 | #include "statslog_statsdtest.h" |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 25 | |
| 26 | #include <gmock/gmock.h> |
| 27 | #include <gtest/gtest.h> |
| 28 | |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 29 | #include "tests/statsd_test_util.h" |
| 30 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | |
| 33 | using namespace android; |
| 34 | using namespace testing; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 35 | using ::ndk::SharedRefBase; |
| 36 | using std::shared_ptr; |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 37 | |
| 38 | namespace android { |
| 39 | namespace os { |
| 40 | namespace statsd { |
| 41 | |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 42 | using android::util::ProtoOutputStream; |
| 43 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 44 | #ifdef __ANDROID__ |
| 45 | |
| 46 | /** |
| 47 | * Mock MetricsManager (ByteSize() is called). |
| 48 | */ |
| 49 | class MockMetricsManager : public MetricsManager { |
| 50 | public: |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 51 | MockMetricsManager() |
| 52 | : MetricsManager(ConfigKey(1, 12345), StatsdConfig(), 1000, 1000, new UidMap(), |
| 53 | new StatsPullerManager(), |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 54 | new AlarmMonitor(10, |
| 55 | [](const shared_ptr<IStatsCompanionService>&, int64_t) {}, |
| 56 | [](const shared_ptr<IStatsCompanionService>&) {}), |
| 57 | new AlarmMonitor(10, |
| 58 | [](const shared_ptr<IStatsCompanionService>&, int64_t) {}, |
| 59 | [](const shared_ptr<IStatsCompanionService>&) {})) { |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | MOCK_METHOD0(byteSize, size_t()); |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 63 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 64 | MOCK_METHOD1(dropData, void(const int64_t dropTimeNs)); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | TEST(StatsLogProcessorTest, TestRateLimitByteSize) { |
| 68 | sp<UidMap> m = new UidMap(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 69 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 70 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 71 | sp<AlarmMonitor> periodicAlarmMonitor; |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 72 | // Construct the processor with a dummy sendBroadcast function that does nothing. |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 73 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, 0, |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 74 | [](const ConfigKey& key) { return true; }, |
| 75 | [](const int&, const vector<int64_t>&) {return true;}); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 76 | |
| 77 | MockMetricsManager mockMetricsManager; |
| 78 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 79 | ConfigKey key(100, 12345); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 80 | // Expect only the first flush to trigger a check for byte size since the last two are |
| 81 | // rate-limited. |
| 82 | EXPECT_CALL(mockMetricsManager, byteSize()).Times(1); |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 83 | p.flushIfNecessaryLocked(key, mockMetricsManager); |
| 84 | p.flushIfNecessaryLocked(key, mockMetricsManager); |
| 85 | p.flushIfNecessaryLocked(key, mockMetricsManager); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(StatsLogProcessorTest, TestRateLimitBroadcast) { |
| 89 | sp<UidMap> m = new UidMap(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 90 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 91 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 92 | sp<AlarmMonitor> subscriberAlarmMonitor; |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 93 | int broadcastCount = 0; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 94 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 95 | [&broadcastCount](const ConfigKey& key) { |
| 96 | broadcastCount++; |
| 97 | return true; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 98 | }, |
| 99 | [](const int&, const vector<int64_t>&) {return true;}); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 100 | |
| 101 | MockMetricsManager mockMetricsManager; |
| 102 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 103 | ConfigKey key(100, 12345); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 104 | EXPECT_CALL(mockMetricsManager, byteSize()) |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 105 | .Times(1) |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 106 | .WillRepeatedly(::testing::Return(int( |
| 107 | StatsdStats::kMaxMetricsBytesPerConfig * .95))); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 108 | |
| 109 | // Expect only one broadcast despite always returning a size that should trigger broadcast. |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 110 | p.flushIfNecessaryLocked(key, mockMetricsManager); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 111 | EXPECT_EQ(1, broadcastCount); |
| 112 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 113 | // b/73089712 |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 114 | // This next call to flush should not trigger a broadcast. |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 115 | // p.mLastByteSizeTimes.clear(); // Force another check for byte size. |
| 116 | // p.flushIfNecessaryLocked(2, key, mockMetricsManager); |
| 117 | // EXPECT_EQ(1, broadcastCount); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge) { |
| 121 | sp<UidMap> m = new UidMap(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 122 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 123 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 124 | sp<AlarmMonitor> subscriberAlarmMonitor; |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 125 | int broadcastCount = 0; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 126 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 127 | [&broadcastCount](const ConfigKey& key) { |
| 128 | broadcastCount++; |
| 129 | return true; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 130 | }, |
| 131 | [](const int&, const vector<int64_t>&) {return true;}); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 132 | |
| 133 | MockMetricsManager mockMetricsManager; |
| 134 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 135 | ConfigKey key(100, 12345); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 136 | EXPECT_CALL(mockMetricsManager, byteSize()) |
| 137 | .Times(1) |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 138 | .WillRepeatedly(::testing::Return(int(StatsdStats::kMaxMetricsBytesPerConfig * 1.2))); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 139 | |
Yao Chen | 06dba5d | 2018-01-26 13:38:16 -0800 | [diff] [blame] | 140 | EXPECT_CALL(mockMetricsManager, dropData(_)).Times(1); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 141 | |
| 142 | // Expect to call the onDumpReport and skip the broadcast. |
Tej Singh | 480392f | 2019-10-23 15:53:46 -0700 | [diff] [blame] | 143 | p.flushIfNecessaryLocked(key, mockMetricsManager); |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 144 | EXPECT_EQ(0, broadcastCount); |
| 145 | } |
| 146 | |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 147 | StatsdConfig MakeConfig(bool includeMetric) { |
| 148 | StatsdConfig config; |
| 149 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 150 | |
| 151 | if (includeMetric) { |
| 152 | auto appCrashMatcher = CreateProcessCrashAtomMatcher(); |
| 153 | *config.add_atom_matcher() = appCrashMatcher; |
| 154 | auto countMetric = config.add_count_metric(); |
| 155 | countMetric->set_id(StringToId("AppCrashes")); |
| 156 | countMetric->set_what(appCrashMatcher.id()); |
| 157 | countMetric->set_bucket(FIVE_MINUTES); |
| 158 | } |
| 159 | return config; |
| 160 | } |
| 161 | |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 162 | TEST(StatsLogProcessorTest, TestUidMapHasSnapshot) { |
| 163 | // Setup simple config key corresponding to empty config. |
| 164 | sp<UidMap> m = new UidMap(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 165 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 166 | m->updateMap(1, {1, 2}, {1, 2}, {String16("v1"), String16("v2")}, |
| 167 | {String16("p1"), String16("p2")}, {String16(""), String16("")}); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 168 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 169 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 170 | int broadcastCount = 0; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 171 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 172 | [&broadcastCount](const ConfigKey& key) { |
| 173 | broadcastCount++; |
| 174 | return true; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 175 | }, |
| 176 | [](const int&, const vector<int64_t>&) {return true;}); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 177 | ConfigKey key(3, 4); |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 178 | StatsdConfig config = MakeConfig(true); |
Yangster-mac | c04feba | 2018-04-02 14:37:33 -0700 | [diff] [blame] | 179 | p.OnConfigUpdated(0, key, config); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 180 | |
| 181 | // Expect to get no metrics, but snapshot specified above in uidmap. |
| 182 | vector<uint8_t> bytes; |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 183 | p.onDumpReport(key, 1, false, true, ADB_DUMP, FAST, &bytes); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 184 | |
| 185 | ConfigMetricsReportList output; |
| 186 | output.ParseFromArray(bytes.data(), bytes.size()); |
| 187 | EXPECT_TRUE(output.reports_size() > 0); |
| 188 | auto uidmap = output.reports(0).uid_map(); |
| 189 | EXPECT_TRUE(uidmap.snapshots_size() > 0); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 190 | ASSERT_EQ(2, uidmap.snapshots(0).package_info_size()); |
David Chen | 35045cb | 2018-03-23 22:21:47 -0700 | [diff] [blame] | 191 | } |
| 192 | |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 193 | TEST(StatsLogProcessorTest, TestEmptyConfigHasNoUidMap) { |
| 194 | // Setup simple config key corresponding to empty config. |
| 195 | sp<UidMap> m = new UidMap(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 196 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
dwchen | 730403e | 2018-10-29 11:41:56 -0700 | [diff] [blame] | 197 | m->updateMap(1, {1, 2}, {1, 2}, {String16("v1"), String16("v2")}, |
| 198 | {String16("p1"), String16("p2")}, {String16(""), String16("")}); |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 199 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 200 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 201 | int broadcastCount = 0; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 202 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 203 | [&broadcastCount](const ConfigKey& key) { |
| 204 | broadcastCount++; |
| 205 | return true; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 206 | }, |
| 207 | [](const int&, const vector<int64_t>&) {return true;}); |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 208 | ConfigKey key(3, 4); |
| 209 | StatsdConfig config = MakeConfig(false); |
| 210 | p.OnConfigUpdated(0, key, config); |
| 211 | |
| 212 | // Expect to get no metrics, but snapshot specified above in uidmap. |
| 213 | vector<uint8_t> bytes; |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 214 | p.onDumpReport(key, 1, false, true, ADB_DUMP, FAST, &bytes); |
David Chen | 9e6dbbd | 2018-05-07 17:52:29 -0700 | [diff] [blame] | 215 | |
| 216 | ConfigMetricsReportList output; |
| 217 | output.ParseFromArray(bytes.data(), bytes.size()); |
| 218 | EXPECT_TRUE(output.reports_size() > 0); |
| 219 | EXPECT_FALSE(output.reports(0).has_uid_map()); |
| 220 | } |
| 221 | |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 222 | TEST(StatsLogProcessorTest, TestReportIncludesSubConfig) { |
| 223 | // Setup simple config key corresponding to empty config. |
| 224 | sp<UidMap> m = new UidMap(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 225 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 226 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 227 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 228 | int broadcastCount = 0; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 229 | StatsLogProcessor p(m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 230 | [&broadcastCount](const ConfigKey& key) { |
| 231 | broadcastCount++; |
| 232 | return true; |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 233 | }, |
| 234 | [](const int&, const vector<int64_t>&) {return true;}); |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 235 | ConfigKey key(3, 4); |
| 236 | StatsdConfig config; |
| 237 | auto annotation = config.add_annotation(); |
| 238 | annotation->set_field_int64(1); |
| 239 | annotation->set_field_int32(2); |
| 240 | config.add_allowed_log_source("AID_ROOT"); |
| 241 | p.OnConfigUpdated(1, key, config); |
| 242 | |
| 243 | // Expect to get no metrics, but snapshot specified above in uidmap. |
| 244 | vector<uint8_t> bytes; |
Olivier Gaillard | 6c75ecd | 2019-02-20 09:57:33 +0000 | [diff] [blame] | 245 | p.onDumpReport(key, 1, false, true, ADB_DUMP, FAST, &bytes); |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 246 | |
| 247 | ConfigMetricsReportList output; |
| 248 | output.ParseFromArray(bytes.data(), bytes.size()); |
| 249 | EXPECT_TRUE(output.reports_size() > 0); |
| 250 | auto report = output.reports(0); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 251 | ASSERT_EQ(1, report.annotation_size()); |
David Chen | faa1af5 | 2018-03-30 15:14:04 -0700 | [diff] [blame] | 252 | EXPECT_EQ(1, report.annotation(0).field_int64()); |
| 253 | EXPECT_EQ(2, report.annotation(0).field_int32()); |
| 254 | } |
| 255 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 256 | TEST(StatsLogProcessorTest, TestOnDumpReportEraseData) { |
| 257 | // Setup a simple config. |
| 258 | StatsdConfig config; |
| 259 | config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 260 | auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher(); |
| 261 | *config.add_atom_matcher() = wakelockAcquireMatcher; |
| 262 | |
| 263 | auto countMetric = config.add_count_metric(); |
| 264 | countMetric->set_id(123456); |
| 265 | countMetric->set_what(wakelockAcquireMatcher.id()); |
| 266 | countMetric->set_bucket(FIVE_MINUTES); |
| 267 | |
| 268 | ConfigKey cfgKey; |
| 269 | sp<StatsLogProcessor> processor = CreateStatsLogProcessor(1, 1, config, cfgKey); |
| 270 | |
| 271 | std::vector<int> attributionUids = {111}; |
| 272 | std::vector<string> attributionTags = {"App1"}; |
| 273 | std::unique_ptr<LogEvent> event = |
| 274 | CreateAcquireWakelockEvent(2 /*timestamp*/, attributionUids, attributionTags, "wl1"); |
| 275 | processor->OnLogEvent(event.get()); |
| 276 | |
| 277 | vector<uint8_t> bytes; |
| 278 | ConfigMetricsReportList output; |
| 279 | |
| 280 | // Dump report WITHOUT erasing data. |
| 281 | processor->onDumpReport(cfgKey, 3, true, false /* Do NOT erase data. */, ADB_DUMP, FAST, |
| 282 | &bytes); |
| 283 | output.ParseFromArray(bytes.data(), bytes.size()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 284 | ASSERT_EQ(output.reports_size(), 1); |
| 285 | ASSERT_EQ(output.reports(0).metrics_size(), 1); |
| 286 | ASSERT_EQ(output.reports(0).metrics(0).count_metrics().data_size(), 1); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 287 | |
| 288 | // Dump report WITH erasing data. There should be data since we didn't previously erase it. |
| 289 | processor->onDumpReport(cfgKey, 4, true, true /* DO erase data. */, ADB_DUMP, FAST, &bytes); |
| 290 | output.ParseFromArray(bytes.data(), bytes.size()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 291 | ASSERT_EQ(output.reports_size(), 1); |
| 292 | ASSERT_EQ(output.reports(0).metrics_size(), 1); |
| 293 | ASSERT_EQ(output.reports(0).metrics(0).count_metrics().data_size(), 1); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 294 | |
| 295 | // Dump report again. There should be no data since we erased it. |
| 296 | processor->onDumpReport(cfgKey, 5, true, true /* DO erase data. */, ADB_DUMP, FAST, &bytes); |
| 297 | output.ParseFromArray(bytes.data(), bytes.size()); |
| 298 | // We don't care whether statsd has a report, as long as it has no count metrics in it. |
| 299 | bool noData = output.reports_size() == 0 || output.reports(0).metrics_size() == 0 || |
| 300 | output.reports(0).metrics(0).count_metrics().data_size() == 0; |
| 301 | EXPECT_TRUE(noData); |
| 302 | } |
| 303 | |
Tej Singh | 3eb9ced | 2020-04-20 22:04:38 -0700 | [diff] [blame] | 304 | TEST(StatsLogProcessorTest, TestPullUidProviderSetOnConfigUpdate) { |
| 305 | // Setup simple config key corresponding to empty config. |
| 306 | sp<UidMap> m = new UidMap(); |
| 307 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| 308 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 309 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 310 | StatsLogProcessor p( |
| 311 | m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, 0, |
| 312 | [](const ConfigKey& key) { return true; }, |
| 313 | [](const int&, const vector<int64_t>&) { return true; }); |
| 314 | ConfigKey key(3, 4); |
| 315 | StatsdConfig config = MakeConfig(false); |
| 316 | p.OnConfigUpdated(0, key, config); |
| 317 | EXPECT_NE(pullerManager->mPullUidProviders.find(key), pullerManager->mPullUidProviders.end()); |
| 318 | |
| 319 | config.add_default_pull_packages("AID_STATSD"); |
| 320 | p.OnConfigUpdated(5, key, config); |
| 321 | EXPECT_NE(pullerManager->mPullUidProviders.find(key), pullerManager->mPullUidProviders.end()); |
| 322 | |
| 323 | p.OnConfigRemoved(key); |
| 324 | EXPECT_EQ(pullerManager->mPullUidProviders.find(key), pullerManager->mPullUidProviders.end()); |
| 325 | } |
| 326 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 327 | TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead) { |
| 328 | int uid = 1111; |
| 329 | |
| 330 | // Setup a simple config, no activation |
| 331 | StatsdConfig config1; |
| 332 | int64_t cfgId1 = 12341; |
| 333 | config1.set_id(cfgId1); |
| 334 | config1.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 335 | auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher(); |
| 336 | *config1.add_atom_matcher() = wakelockAcquireMatcher; |
| 337 | |
| 338 | long metricId1 = 1234561; |
| 339 | long metricId2 = 1234562; |
| 340 | auto countMetric1 = config1.add_count_metric(); |
| 341 | countMetric1->set_id(metricId1); |
| 342 | countMetric1->set_what(wakelockAcquireMatcher.id()); |
| 343 | countMetric1->set_bucket(FIVE_MINUTES); |
| 344 | |
| 345 | auto countMetric2 = config1.add_count_metric(); |
| 346 | countMetric2->set_id(metricId2); |
| 347 | countMetric2->set_what(wakelockAcquireMatcher.id()); |
| 348 | countMetric2->set_bucket(FIVE_MINUTES); |
| 349 | |
| 350 | ConfigKey cfgKey1(uid, cfgId1); |
| 351 | |
| 352 | // Add another config, with two metrics, one with activation |
| 353 | StatsdConfig config2; |
| 354 | int64_t cfgId2 = 12342; |
| 355 | config2.set_id(cfgId2); |
| 356 | config2.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 357 | *config2.add_atom_matcher() = wakelockAcquireMatcher; |
| 358 | |
| 359 | long metricId3 = 1234561; |
| 360 | long metricId4 = 1234562; |
| 361 | |
| 362 | auto countMetric3 = config2.add_count_metric(); |
| 363 | countMetric3->set_id(metricId3); |
| 364 | countMetric3->set_what(wakelockAcquireMatcher.id()); |
| 365 | countMetric3->set_bucket(FIVE_MINUTES); |
| 366 | |
| 367 | auto countMetric4 = config2.add_count_metric(); |
| 368 | countMetric4->set_id(metricId4); |
| 369 | countMetric4->set_what(wakelockAcquireMatcher.id()); |
| 370 | countMetric4->set_bucket(FIVE_MINUTES); |
| 371 | |
| 372 | auto metric3Activation = config2.add_metric_activation(); |
| 373 | metric3Activation->set_metric_id(metricId3); |
| 374 | metric3Activation->set_activation_type(ACTIVATE_IMMEDIATELY); |
| 375 | auto metric3ActivationTrigger = metric3Activation->add_event_activation(); |
| 376 | metric3ActivationTrigger->set_atom_matcher_id(wakelockAcquireMatcher.id()); |
| 377 | metric3ActivationTrigger->set_ttl_seconds(100); |
| 378 | |
| 379 | ConfigKey cfgKey2(uid, cfgId2); |
| 380 | |
| 381 | // Add another config, with two metrics, both with activations |
| 382 | StatsdConfig config3; |
| 383 | int64_t cfgId3 = 12343; |
| 384 | config3.set_id(cfgId3); |
| 385 | config3.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 386 | *config3.add_atom_matcher() = wakelockAcquireMatcher; |
| 387 | |
| 388 | long metricId5 = 1234565; |
| 389 | long metricId6 = 1234566; |
| 390 | auto countMetric5 = config3.add_count_metric(); |
| 391 | countMetric5->set_id(metricId5); |
| 392 | countMetric5->set_what(wakelockAcquireMatcher.id()); |
| 393 | countMetric5->set_bucket(FIVE_MINUTES); |
| 394 | |
| 395 | auto countMetric6 = config3.add_count_metric(); |
| 396 | countMetric6->set_id(metricId6); |
| 397 | countMetric6->set_what(wakelockAcquireMatcher.id()); |
| 398 | countMetric6->set_bucket(FIVE_MINUTES); |
| 399 | |
| 400 | auto metric5Activation = config3.add_metric_activation(); |
| 401 | metric5Activation->set_metric_id(metricId5); |
| 402 | metric5Activation->set_activation_type(ACTIVATE_IMMEDIATELY); |
| 403 | auto metric5ActivationTrigger = metric5Activation->add_event_activation(); |
| 404 | metric5ActivationTrigger->set_atom_matcher_id(wakelockAcquireMatcher.id()); |
| 405 | metric5ActivationTrigger->set_ttl_seconds(100); |
| 406 | |
| 407 | auto metric6Activation = config3.add_metric_activation(); |
| 408 | metric6Activation->set_metric_id(metricId6); |
| 409 | metric6Activation->set_activation_type(ACTIVATE_IMMEDIATELY); |
| 410 | auto metric6ActivationTrigger = metric6Activation->add_event_activation(); |
| 411 | metric6ActivationTrigger->set_atom_matcher_id(wakelockAcquireMatcher.id()); |
| 412 | metric6ActivationTrigger->set_ttl_seconds(200); |
| 413 | |
| 414 | ConfigKey cfgKey3(uid, cfgId3); |
| 415 | |
| 416 | sp<UidMap> m = new UidMap(); |
| 417 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
| 418 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 419 | sp<AlarmMonitor> subscriberAlarmMonitor; |
| 420 | vector<int64_t> activeConfigsBroadcast; |
| 421 | |
| 422 | long timeBase1 = 1; |
| 423 | int broadcastCount = 0; |
| 424 | StatsLogProcessor processor( |
| 425 | m, pullerManager, anomalyAlarmMonitor, subscriberAlarmMonitor, timeBase1, |
| 426 | [](const ConfigKey& key) { return true; }, |
| 427 | [&uid, &broadcastCount, &activeConfigsBroadcast](const int& broadcastUid, |
| 428 | const vector<int64_t>& activeConfigs) { |
| 429 | broadcastCount++; |
| 430 | EXPECT_EQ(broadcastUid, uid); |
| 431 | activeConfigsBroadcast.clear(); |
| 432 | activeConfigsBroadcast.insert(activeConfigsBroadcast.end(), activeConfigs.begin(), |
| 433 | activeConfigs.end()); |
| 434 | return true; |
| 435 | }); |
| 436 | |
| 437 | processor.OnConfigUpdated(1, cfgKey1, config1); |
| 438 | processor.OnConfigUpdated(2, cfgKey2, config2); |
| 439 | processor.OnConfigUpdated(3, cfgKey3, config3); |
| 440 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 441 | ASSERT_EQ(3, processor.mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 442 | |
| 443 | // Expect the first config and both metrics in it to be active. |
| 444 | auto it = processor.mMetricsManagers.find(cfgKey1); |
| 445 | EXPECT_TRUE(it != processor.mMetricsManagers.end()); |
| 446 | auto& metricsManager1 = it->second; |
| 447 | EXPECT_TRUE(metricsManager1->isActive()); |
| 448 | |
| 449 | auto metricIt = metricsManager1->mAllMetricProducers.begin(); |
| 450 | for (; metricIt != metricsManager1->mAllMetricProducers.end(); metricIt++) { |
| 451 | if ((*metricIt)->getMetricId() == metricId1) { |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | EXPECT_TRUE(metricIt != metricsManager1->mAllMetricProducers.end()); |
| 456 | auto& metricProducer1 = *metricIt; |
| 457 | EXPECT_TRUE(metricProducer1->isActive()); |
| 458 | |
| 459 | metricIt = metricsManager1->mAllMetricProducers.begin(); |
| 460 | for (; metricIt != metricsManager1->mAllMetricProducers.end(); metricIt++) { |
| 461 | if ((*metricIt)->getMetricId() == metricId2) { |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | EXPECT_TRUE(metricIt != metricsManager1->mAllMetricProducers.end()); |
| 466 | auto& metricProducer2 = *metricIt; |
| 467 | EXPECT_TRUE(metricProducer2->isActive()); |
| 468 | |
| 469 | // Expect config 2 to be active. Metric 3 shouldn't be active, metric 4 should be active. |
| 470 | it = processor.mMetricsManagers.find(cfgKey2); |
| 471 | EXPECT_TRUE(it != processor.mMetricsManagers.end()); |
| 472 | auto& metricsManager2 = it->second; |
| 473 | EXPECT_TRUE(metricsManager2->isActive()); |
| 474 | |
| 475 | metricIt = metricsManager2->mAllMetricProducers.begin(); |
| 476 | for (; metricIt != metricsManager2->mAllMetricProducers.end(); metricIt++) { |
| 477 | if ((*metricIt)->getMetricId() == metricId3) { |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | EXPECT_TRUE(metricIt != metricsManager2->mAllMetricProducers.end()); |
| 482 | auto& metricProducer3 = *metricIt; |
| 483 | EXPECT_FALSE(metricProducer3->isActive()); |
| 484 | |
| 485 | metricIt = metricsManager2->mAllMetricProducers.begin(); |
| 486 | for (; metricIt != metricsManager2->mAllMetricProducers.end(); metricIt++) { |
| 487 | if ((*metricIt)->getMetricId() == metricId4) { |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | EXPECT_TRUE(metricIt != metricsManager2->mAllMetricProducers.end()); |
| 492 | auto& metricProducer4 = *metricIt; |
| 493 | EXPECT_TRUE(metricProducer4->isActive()); |
| 494 | |
| 495 | // Expect the third config and both metrics in it to be inactive. |
| 496 | it = processor.mMetricsManagers.find(cfgKey3); |
| 497 | EXPECT_TRUE(it != processor.mMetricsManagers.end()); |
| 498 | auto& metricsManager3 = it->second; |
| 499 | EXPECT_FALSE(metricsManager3->isActive()); |
| 500 | |
| 501 | metricIt = metricsManager3->mAllMetricProducers.begin(); |
| 502 | for (; metricIt != metricsManager2->mAllMetricProducers.end(); metricIt++) { |
| 503 | if ((*metricIt)->getMetricId() == metricId5) { |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | EXPECT_TRUE(metricIt != metricsManager3->mAllMetricProducers.end()); |
| 508 | auto& metricProducer5 = *metricIt; |
| 509 | EXPECT_FALSE(metricProducer5->isActive()); |
| 510 | |
| 511 | metricIt = metricsManager3->mAllMetricProducers.begin(); |
| 512 | for (; metricIt != metricsManager3->mAllMetricProducers.end(); metricIt++) { |
| 513 | if ((*metricIt)->getMetricId() == metricId6) { |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | EXPECT_TRUE(metricIt != metricsManager3->mAllMetricProducers.end()); |
| 518 | auto& metricProducer6 = *metricIt; |
| 519 | EXPECT_FALSE(metricProducer6->isActive()); |
| 520 | |
| 521 | // No broadcast for active configs should have happened yet. |
| 522 | EXPECT_EQ(broadcastCount, 0); |
| 523 | |
| 524 | // Activate all 3 metrics that were not active. |
| 525 | std::vector<int> attributionUids = {111}; |
| 526 | std::vector<string> attributionTags = {"App1"}; |
| 527 | std::unique_ptr<LogEvent> event = |
| 528 | CreateAcquireWakelockEvent(timeBase1 + 100, attributionUids, attributionTags, "wl1"); |
| 529 | processor.OnLogEvent(event.get()); |
| 530 | |
| 531 | // Assert that all 3 configs are active. |
| 532 | EXPECT_TRUE(metricsManager1->isActive()); |
| 533 | EXPECT_TRUE(metricsManager2->isActive()); |
| 534 | EXPECT_TRUE(metricsManager3->isActive()); |
| 535 | |
| 536 | // A broadcast should have happened, and all 3 configs should be active in the broadcast. |
| 537 | EXPECT_EQ(broadcastCount, 1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 538 | ASSERT_EQ(activeConfigsBroadcast.size(), 3); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 539 | EXPECT_TRUE(std::find(activeConfigsBroadcast.begin(), activeConfigsBroadcast.end(), cfgId1) != |
| 540 | activeConfigsBroadcast.end()); |
| 541 | EXPECT_TRUE(std::find(activeConfigsBroadcast.begin(), activeConfigsBroadcast.end(), cfgId2) != |
| 542 | activeConfigsBroadcast.end()); |
| 543 | EXPECT_TRUE(std::find(activeConfigsBroadcast.begin(), activeConfigsBroadcast.end(), cfgId3) != |
| 544 | activeConfigsBroadcast.end()); |
| 545 | |
| 546 | // When we shut down, metrics 3 & 5 have 100ns remaining, metric 6 has 100s + 100ns. |
| 547 | int64_t shutDownTime = timeBase1 + 100 * NS_PER_SEC; |
| 548 | processor.SaveActiveConfigsToDisk(shutDownTime); |
| 549 | const int64_t ttl3 = event->GetElapsedTimestampNs() + |
| 550 | metric3ActivationTrigger->ttl_seconds() * NS_PER_SEC - shutDownTime; |
| 551 | const int64_t ttl5 = event->GetElapsedTimestampNs() + |
| 552 | metric5ActivationTrigger->ttl_seconds() * NS_PER_SEC - shutDownTime; |
| 553 | const int64_t ttl6 = event->GetElapsedTimestampNs() + |
| 554 | metric6ActivationTrigger->ttl_seconds() * NS_PER_SEC - shutDownTime; |
| 555 | |
| 556 | // Create a second StatsLogProcessor and push the same 3 configs. |
| 557 | long timeBase2 = 1000; |
| 558 | sp<StatsLogProcessor> processor2 = |
| 559 | CreateStatsLogProcessor(timeBase2, timeBase2, config1, cfgKey1); |
| 560 | processor2->OnConfigUpdated(timeBase2, cfgKey2, config2); |
| 561 | processor2->OnConfigUpdated(timeBase2, cfgKey3, config3); |
| 562 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 563 | ASSERT_EQ(3, processor2->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 564 | |
| 565 | // First config and both metrics are active. |
| 566 | it = processor2->mMetricsManagers.find(cfgKey1); |
| 567 | EXPECT_TRUE(it != processor2->mMetricsManagers.end()); |
| 568 | auto& metricsManager1001 = it->second; |
| 569 | EXPECT_TRUE(metricsManager1001->isActive()); |
| 570 | |
| 571 | metricIt = metricsManager1001->mAllMetricProducers.begin(); |
| 572 | for (; metricIt != metricsManager1001->mAllMetricProducers.end(); metricIt++) { |
| 573 | if ((*metricIt)->getMetricId() == metricId1) { |
| 574 | break; |
| 575 | } |
| 576 | } |
| 577 | EXPECT_TRUE(metricIt != metricsManager1001->mAllMetricProducers.end()); |
| 578 | auto& metricProducer1001 = *metricIt; |
| 579 | EXPECT_TRUE(metricProducer1001->isActive()); |
| 580 | |
| 581 | metricIt = metricsManager1001->mAllMetricProducers.begin(); |
| 582 | for (; metricIt != metricsManager1001->mAllMetricProducers.end(); metricIt++) { |
| 583 | if ((*metricIt)->getMetricId() == metricId2) { |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | EXPECT_TRUE(metricIt != metricsManager1001->mAllMetricProducers.end()); |
| 588 | auto& metricProducer1002 = *metricIt; |
| 589 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 590 | |
| 591 | // Second config is active. Metric 3 is inactive, metric 4 is active. |
| 592 | it = processor2->mMetricsManagers.find(cfgKey2); |
| 593 | EXPECT_TRUE(it != processor2->mMetricsManagers.end()); |
| 594 | auto& metricsManager1002 = it->second; |
| 595 | EXPECT_TRUE(metricsManager1002->isActive()); |
| 596 | |
| 597 | metricIt = metricsManager1002->mAllMetricProducers.begin(); |
| 598 | for (; metricIt != metricsManager1002->mAllMetricProducers.end(); metricIt++) { |
| 599 | if ((*metricIt)->getMetricId() == metricId3) { |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | EXPECT_TRUE(metricIt != metricsManager1002->mAllMetricProducers.end()); |
| 604 | auto& metricProducer1003 = *metricIt; |
| 605 | EXPECT_FALSE(metricProducer1003->isActive()); |
| 606 | |
| 607 | metricIt = metricsManager1002->mAllMetricProducers.begin(); |
| 608 | for (; metricIt != metricsManager1002->mAllMetricProducers.end(); metricIt++) { |
| 609 | if ((*metricIt)->getMetricId() == metricId4) { |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | EXPECT_TRUE(metricIt != metricsManager1002->mAllMetricProducers.end()); |
| 614 | auto& metricProducer1004 = *metricIt; |
| 615 | EXPECT_TRUE(metricProducer1004->isActive()); |
| 616 | |
| 617 | // Config 3 is inactive. both metrics are inactive. |
| 618 | it = processor2->mMetricsManagers.find(cfgKey3); |
| 619 | EXPECT_TRUE(it != processor2->mMetricsManagers.end()); |
| 620 | auto& metricsManager1003 = it->second; |
| 621 | EXPECT_FALSE(metricsManager1003->isActive()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 622 | ASSERT_EQ(2, metricsManager1003->mAllMetricProducers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 623 | |
| 624 | metricIt = metricsManager1003->mAllMetricProducers.begin(); |
| 625 | for (; metricIt != metricsManager1002->mAllMetricProducers.end(); metricIt++) { |
| 626 | if ((*metricIt)->getMetricId() == metricId5) { |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | EXPECT_TRUE(metricIt != metricsManager1003->mAllMetricProducers.end()); |
| 631 | auto& metricProducer1005 = *metricIt; |
| 632 | EXPECT_FALSE(metricProducer1005->isActive()); |
| 633 | |
| 634 | metricIt = metricsManager1003->mAllMetricProducers.begin(); |
| 635 | for (; metricIt != metricsManager1003->mAllMetricProducers.end(); metricIt++) { |
| 636 | if ((*metricIt)->getMetricId() == metricId6) { |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | EXPECT_TRUE(metricIt != metricsManager1003->mAllMetricProducers.end()); |
| 641 | auto& metricProducer1006 = *metricIt; |
| 642 | EXPECT_FALSE(metricProducer1006->isActive()); |
| 643 | |
| 644 | // Assert that all 3 metrics with activation are inactive and that the ttls were properly set. |
| 645 | EXPECT_FALSE(metricProducer1003->isActive()); |
| 646 | const auto& activation1003 = metricProducer1003->mEventActivationMap.begin()->second; |
| 647 | EXPECT_EQ(100 * NS_PER_SEC, activation1003->ttl_ns); |
| 648 | EXPECT_EQ(0, activation1003->start_ns); |
| 649 | EXPECT_FALSE(metricProducer1005->isActive()); |
| 650 | const auto& activation1005 = metricProducer1005->mEventActivationMap.begin()->second; |
| 651 | EXPECT_EQ(100 * NS_PER_SEC, activation1005->ttl_ns); |
| 652 | EXPECT_EQ(0, activation1005->start_ns); |
| 653 | EXPECT_FALSE(metricProducer1006->isActive()); |
| 654 | const auto& activation1006 = metricProducer1006->mEventActivationMap.begin()->second; |
| 655 | EXPECT_EQ(200 * NS_PER_SEC, activation1006->ttl_ns); |
| 656 | EXPECT_EQ(0, activation1006->start_ns); |
| 657 | |
| 658 | processor2->LoadActiveConfigsFromDisk(); |
| 659 | |
| 660 | // After loading activations from disk, assert that all 3 metrics are active. |
| 661 | EXPECT_TRUE(metricProducer1003->isActive()); |
| 662 | EXPECT_EQ(timeBase2 + ttl3 - activation1003->ttl_ns, activation1003->start_ns); |
| 663 | EXPECT_TRUE(metricProducer1005->isActive()); |
| 664 | EXPECT_EQ(timeBase2 + ttl5 - activation1005->ttl_ns, activation1005->start_ns); |
| 665 | EXPECT_TRUE(metricProducer1006->isActive()); |
| 666 | EXPECT_EQ(timeBase2 + ttl6 - activation1006->ttl_ns, activation1003->start_ns); |
| 667 | |
| 668 | // Make sure no more broadcasts have happened. |
| 669 | EXPECT_EQ(broadcastCount, 1); |
| 670 | } |
| 671 | |
| 672 | TEST(StatsLogProcessorTest, TestActivationOnBoot) { |
| 673 | int uid = 1111; |
| 674 | |
| 675 | StatsdConfig config1; |
| 676 | config1.set_id(12341); |
| 677 | config1.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 678 | auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher(); |
| 679 | *config1.add_atom_matcher() = wakelockAcquireMatcher; |
| 680 | |
| 681 | long metricId1 = 1234561; |
| 682 | long metricId2 = 1234562; |
| 683 | auto countMetric1 = config1.add_count_metric(); |
| 684 | countMetric1->set_id(metricId1); |
| 685 | countMetric1->set_what(wakelockAcquireMatcher.id()); |
| 686 | countMetric1->set_bucket(FIVE_MINUTES); |
| 687 | |
| 688 | auto countMetric2 = config1.add_count_metric(); |
| 689 | countMetric2->set_id(metricId2); |
| 690 | countMetric2->set_what(wakelockAcquireMatcher.id()); |
| 691 | countMetric2->set_bucket(FIVE_MINUTES); |
| 692 | |
| 693 | auto metric1Activation = config1.add_metric_activation(); |
| 694 | metric1Activation->set_metric_id(metricId1); |
| 695 | metric1Activation->set_activation_type(ACTIVATE_ON_BOOT); |
| 696 | auto metric1ActivationTrigger = metric1Activation->add_event_activation(); |
| 697 | metric1ActivationTrigger->set_atom_matcher_id(wakelockAcquireMatcher.id()); |
| 698 | metric1ActivationTrigger->set_ttl_seconds(100); |
| 699 | |
| 700 | ConfigKey cfgKey1(uid, 12341); |
| 701 | long timeBase1 = 1; |
| 702 | sp<StatsLogProcessor> processor = |
| 703 | CreateStatsLogProcessor(timeBase1, timeBase1, config1, cfgKey1); |
| 704 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 705 | ASSERT_EQ(1, processor->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 706 | auto it = processor->mMetricsManagers.find(cfgKey1); |
| 707 | EXPECT_TRUE(it != processor->mMetricsManagers.end()); |
| 708 | auto& metricsManager1 = it->second; |
| 709 | EXPECT_TRUE(metricsManager1->isActive()); |
| 710 | |
| 711 | auto metricIt = metricsManager1->mAllMetricProducers.begin(); |
| 712 | for (; metricIt != metricsManager1->mAllMetricProducers.end(); metricIt++) { |
| 713 | if ((*metricIt)->getMetricId() == metricId1) { |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | EXPECT_TRUE(metricIt != metricsManager1->mAllMetricProducers.end()); |
| 718 | auto& metricProducer1 = *metricIt; |
| 719 | EXPECT_FALSE(metricProducer1->isActive()); |
| 720 | |
| 721 | metricIt = metricsManager1->mAllMetricProducers.begin(); |
| 722 | for (; metricIt != metricsManager1->mAllMetricProducers.end(); metricIt++) { |
| 723 | if ((*metricIt)->getMetricId() == metricId2) { |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | EXPECT_TRUE(metricIt != metricsManager1->mAllMetricProducers.end()); |
| 728 | auto& metricProducer2 = *metricIt; |
| 729 | EXPECT_TRUE(metricProducer2->isActive()); |
| 730 | |
| 731 | const auto& activation1 = metricProducer1->mEventActivationMap.begin()->second; |
| 732 | EXPECT_EQ(100 * NS_PER_SEC, activation1->ttl_ns); |
| 733 | EXPECT_EQ(0, activation1->start_ns); |
| 734 | EXPECT_EQ(kNotActive, activation1->state); |
| 735 | |
| 736 | std::vector<int> attributionUids = {111}; |
| 737 | std::vector<string> attributionTags = {"App1"}; |
| 738 | std::unique_ptr<LogEvent> event = |
| 739 | CreateAcquireWakelockEvent(timeBase1 + 100, attributionUids, attributionTags, "wl1"); |
| 740 | processor->OnLogEvent(event.get()); |
| 741 | |
| 742 | EXPECT_FALSE(metricProducer1->isActive()); |
| 743 | EXPECT_EQ(0, activation1->start_ns); |
| 744 | EXPECT_EQ(kActiveOnBoot, activation1->state); |
| 745 | |
| 746 | int64_t shutDownTime = timeBase1 + 100 * NS_PER_SEC; |
| 747 | processor->SaveActiveConfigsToDisk(shutDownTime); |
| 748 | EXPECT_FALSE(metricProducer1->isActive()); |
| 749 | const int64_t ttl1 = metric1ActivationTrigger->ttl_seconds() * NS_PER_SEC; |
| 750 | |
| 751 | long timeBase2 = 1000; |
| 752 | sp<StatsLogProcessor> processor2 = |
| 753 | CreateStatsLogProcessor(timeBase2, timeBase2, config1, cfgKey1); |
| 754 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 755 | ASSERT_EQ(1, processor2->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 756 | it = processor2->mMetricsManagers.find(cfgKey1); |
| 757 | EXPECT_TRUE(it != processor2->mMetricsManagers.end()); |
| 758 | auto& metricsManager1001 = it->second; |
| 759 | EXPECT_TRUE(metricsManager1001->isActive()); |
| 760 | |
| 761 | metricIt = metricsManager1001->mAllMetricProducers.begin(); |
| 762 | for (; metricIt != metricsManager1001->mAllMetricProducers.end(); metricIt++) { |
| 763 | if ((*metricIt)->getMetricId() == metricId1) { |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | EXPECT_TRUE(metricIt != metricsManager1001->mAllMetricProducers.end()); |
| 768 | auto& metricProducer1001 = *metricIt; |
| 769 | EXPECT_FALSE(metricProducer1001->isActive()); |
| 770 | |
| 771 | metricIt = metricsManager1001->mAllMetricProducers.begin(); |
| 772 | for (; metricIt != metricsManager1001->mAllMetricProducers.end(); metricIt++) { |
| 773 | if ((*metricIt)->getMetricId() == metricId2) { |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | EXPECT_TRUE(metricIt != metricsManager1001->mAllMetricProducers.end()); |
| 778 | auto& metricProducer1002 = *metricIt; |
| 779 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 780 | |
| 781 | const auto& activation1001 = metricProducer1001->mEventActivationMap.begin()->second; |
| 782 | EXPECT_EQ(100 * NS_PER_SEC, activation1001->ttl_ns); |
| 783 | EXPECT_EQ(0, activation1001->start_ns); |
| 784 | EXPECT_EQ(kNotActive, activation1001->state); |
| 785 | |
| 786 | processor2->LoadActiveConfigsFromDisk(); |
| 787 | |
| 788 | EXPECT_TRUE(metricProducer1001->isActive()); |
| 789 | EXPECT_EQ(timeBase2 + ttl1 - activation1001->ttl_ns, activation1001->start_ns); |
| 790 | EXPECT_EQ(kActive, activation1001->state); |
| 791 | } |
| 792 | |
| 793 | TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivations) { |
| 794 | int uid = 1111; |
| 795 | |
| 796 | // Create config with 2 metrics: |
| 797 | // Metric 1: Activate on boot with 2 activations |
| 798 | // Metric 2: Always active |
| 799 | StatsdConfig config1; |
| 800 | config1.set_id(12341); |
| 801 | config1.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 802 | auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher(); |
| 803 | auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher(); |
| 804 | *config1.add_atom_matcher() = wakelockAcquireMatcher; |
| 805 | *config1.add_atom_matcher() = screenOnMatcher; |
| 806 | |
| 807 | long metricId1 = 1234561; |
| 808 | long metricId2 = 1234562; |
| 809 | |
| 810 | auto countMetric1 = config1.add_count_metric(); |
| 811 | countMetric1->set_id(metricId1); |
| 812 | countMetric1->set_what(wakelockAcquireMatcher.id()); |
| 813 | countMetric1->set_bucket(FIVE_MINUTES); |
| 814 | |
| 815 | auto countMetric2 = config1.add_count_metric(); |
| 816 | countMetric2->set_id(metricId2); |
| 817 | countMetric2->set_what(wakelockAcquireMatcher.id()); |
| 818 | countMetric2->set_bucket(FIVE_MINUTES); |
| 819 | |
| 820 | auto metric1Activation = config1.add_metric_activation(); |
| 821 | metric1Activation->set_metric_id(metricId1); |
| 822 | metric1Activation->set_activation_type(ACTIVATE_ON_BOOT); |
| 823 | auto metric1ActivationTrigger1 = metric1Activation->add_event_activation(); |
| 824 | metric1ActivationTrigger1->set_atom_matcher_id(wakelockAcquireMatcher.id()); |
| 825 | metric1ActivationTrigger1->set_ttl_seconds(100); |
| 826 | auto metric1ActivationTrigger2 = metric1Activation->add_event_activation(); |
| 827 | metric1ActivationTrigger2->set_atom_matcher_id(screenOnMatcher.id()); |
| 828 | metric1ActivationTrigger2->set_ttl_seconds(200); |
| 829 | |
| 830 | ConfigKey cfgKey1(uid, 12341); |
| 831 | long timeBase1 = 1; |
| 832 | sp<StatsLogProcessor> processor = |
| 833 | CreateStatsLogProcessor(timeBase1, timeBase1, config1, cfgKey1); |
| 834 | |
| 835 | // Metric 1 is not active. |
| 836 | // Metric 2 is active. |
| 837 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 838 | ASSERT_EQ(1, processor->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 839 | auto it = processor->mMetricsManagers.find(cfgKey1); |
| 840 | EXPECT_TRUE(it != processor->mMetricsManagers.end()); |
| 841 | auto& metricsManager1 = it->second; |
| 842 | EXPECT_TRUE(metricsManager1->isActive()); |
| 843 | |
| 844 | auto metricIt = metricsManager1->mAllMetricProducers.begin(); |
| 845 | for (; metricIt != metricsManager1->mAllMetricProducers.end(); metricIt++) { |
| 846 | if ((*metricIt)->getMetricId() == metricId1) { |
| 847 | break; |
| 848 | } |
| 849 | } |
| 850 | EXPECT_TRUE(metricIt != metricsManager1->mAllMetricProducers.end()); |
| 851 | auto& metricProducer1 = *metricIt; |
| 852 | EXPECT_FALSE(metricProducer1->isActive()); |
| 853 | |
| 854 | metricIt = metricsManager1->mAllMetricProducers.begin(); |
| 855 | for (; metricIt != metricsManager1->mAllMetricProducers.end(); metricIt++) { |
| 856 | if ((*metricIt)->getMetricId() == metricId2) { |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | EXPECT_TRUE(metricIt != metricsManager1->mAllMetricProducers.end()); |
| 861 | auto& metricProducer2 = *metricIt; |
| 862 | EXPECT_TRUE(metricProducer2->isActive()); |
| 863 | |
| 864 | int i = 0; |
| 865 | for (; i < metricsManager1->mAllAtomMatchers.size(); i++) { |
| 866 | if (metricsManager1->mAllAtomMatchers[i]->getId() == |
| 867 | metric1ActivationTrigger1->atom_matcher_id()) { |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | const auto& activation1 = metricProducer1->mEventActivationMap.at(i); |
| 872 | EXPECT_EQ(100 * NS_PER_SEC, activation1->ttl_ns); |
| 873 | EXPECT_EQ(0, activation1->start_ns); |
| 874 | EXPECT_EQ(kNotActive, activation1->state); |
| 875 | |
| 876 | i = 0; |
| 877 | for (; i < metricsManager1->mAllAtomMatchers.size(); i++) { |
| 878 | if (metricsManager1->mAllAtomMatchers[i]->getId() == |
| 879 | metric1ActivationTrigger2->atom_matcher_id()) { |
| 880 | break; |
| 881 | } |
| 882 | } |
| 883 | const auto& activation2 = metricProducer1->mEventActivationMap.at(i); |
| 884 | EXPECT_EQ(200 * NS_PER_SEC, activation2->ttl_ns); |
| 885 | EXPECT_EQ(0, activation2->start_ns); |
| 886 | EXPECT_EQ(kNotActive, activation2->state); |
| 887 | // }}}------------------------------------------------------------------------------ |
| 888 | |
| 889 | // Trigger Activation 1 for Metric 1 |
| 890 | std::vector<int> attributionUids = {111}; |
| 891 | std::vector<string> attributionTags = {"App1"}; |
| 892 | std::unique_ptr<LogEvent> event = |
| 893 | CreateAcquireWakelockEvent(timeBase1 + 100, attributionUids, attributionTags, "wl1"); |
| 894 | processor->OnLogEvent(event.get()); |
| 895 | |
| 896 | // Metric 1 is not active; Activation 1 set to kActiveOnBoot |
| 897 | // Metric 2 is active. |
| 898 | // {{{--------------------------------------------------------------------------- |
| 899 | EXPECT_FALSE(metricProducer1->isActive()); |
| 900 | EXPECT_EQ(0, activation1->start_ns); |
| 901 | EXPECT_EQ(kActiveOnBoot, activation1->state); |
| 902 | EXPECT_EQ(0, activation2->start_ns); |
| 903 | EXPECT_EQ(kNotActive, activation2->state); |
| 904 | |
| 905 | EXPECT_TRUE(metricProducer2->isActive()); |
| 906 | // }}}----------------------------------------------------------------------------- |
| 907 | |
| 908 | // Simulate shutdown by saving state to disk |
| 909 | int64_t shutDownTime = timeBase1 + 100 * NS_PER_SEC; |
| 910 | processor->SaveActiveConfigsToDisk(shutDownTime); |
| 911 | EXPECT_FALSE(metricProducer1->isActive()); |
| 912 | int64_t ttl1 = metric1ActivationTrigger1->ttl_seconds() * NS_PER_SEC; |
| 913 | |
| 914 | // Simulate device restarted state by creating new instance of StatsLogProcessor with the |
| 915 | // same config. |
| 916 | long timeBase2 = 1000; |
| 917 | sp<StatsLogProcessor> processor2 = |
| 918 | CreateStatsLogProcessor(timeBase2, timeBase2, config1, cfgKey1); |
| 919 | |
| 920 | // Metric 1 is not active. |
| 921 | // Metric 2 is active. |
| 922 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 923 | ASSERT_EQ(1, processor2->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 924 | it = processor2->mMetricsManagers.find(cfgKey1); |
| 925 | EXPECT_TRUE(it != processor2->mMetricsManagers.end()); |
| 926 | auto& metricsManager1001 = it->second; |
| 927 | EXPECT_TRUE(metricsManager1001->isActive()); |
| 928 | |
| 929 | metricIt = metricsManager1001->mAllMetricProducers.begin(); |
| 930 | for (; metricIt != metricsManager1001->mAllMetricProducers.end(); metricIt++) { |
| 931 | if ((*metricIt)->getMetricId() == metricId1) { |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | EXPECT_TRUE(metricIt != metricsManager1001->mAllMetricProducers.end()); |
| 936 | auto& metricProducer1001 = *metricIt; |
| 937 | EXPECT_FALSE(metricProducer1001->isActive()); |
| 938 | |
| 939 | metricIt = metricsManager1001->mAllMetricProducers.begin(); |
| 940 | for (; metricIt != metricsManager1001->mAllMetricProducers.end(); metricIt++) { |
| 941 | if ((*metricIt)->getMetricId() == metricId2) { |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | EXPECT_TRUE(metricIt != metricsManager1001->mAllMetricProducers.end()); |
| 946 | auto& metricProducer1002 = *metricIt; |
| 947 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 948 | |
| 949 | i = 0; |
| 950 | for (; i < metricsManager1001->mAllAtomMatchers.size(); i++) { |
| 951 | if (metricsManager1001->mAllAtomMatchers[i]->getId() == |
| 952 | metric1ActivationTrigger1->atom_matcher_id()) { |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | const auto& activation1001_1 = metricProducer1001->mEventActivationMap.at(i); |
| 957 | EXPECT_EQ(100 * NS_PER_SEC, activation1001_1->ttl_ns); |
| 958 | EXPECT_EQ(0, activation1001_1->start_ns); |
| 959 | EXPECT_EQ(kNotActive, activation1001_1->state); |
| 960 | |
| 961 | i = 0; |
| 962 | for (; i < metricsManager1001->mAllAtomMatchers.size(); i++) { |
| 963 | if (metricsManager1001->mAllAtomMatchers[i]->getId() == |
| 964 | metric1ActivationTrigger2->atom_matcher_id()) { |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | const auto& activation1001_2 = metricProducer1001->mEventActivationMap.at(i); |
| 970 | EXPECT_EQ(200 * NS_PER_SEC, activation1001_2->ttl_ns); |
| 971 | EXPECT_EQ(0, activation1001_2->start_ns); |
| 972 | EXPECT_EQ(kNotActive, activation1001_2->state); |
| 973 | // }}}----------------------------------------------------------------------------------- |
| 974 | |
| 975 | // Load saved state from disk. |
| 976 | processor2->LoadActiveConfigsFromDisk(); |
| 977 | |
| 978 | // Metric 1 active; Activation 1 is active, Activation 2 is not active |
| 979 | // Metric 2 is active. |
| 980 | // {{{--------------------------------------------------------------------------- |
| 981 | EXPECT_TRUE(metricProducer1001->isActive()); |
| 982 | EXPECT_EQ(timeBase2 + ttl1 - activation1001_1->ttl_ns, activation1001_1->start_ns); |
| 983 | EXPECT_EQ(kActive, activation1001_1->state); |
| 984 | EXPECT_EQ(0, activation1001_2->start_ns); |
| 985 | EXPECT_EQ(kNotActive, activation1001_2->state); |
| 986 | |
| 987 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 988 | // }}}-------------------------------------------------------------------------------- |
| 989 | |
| 990 | // Trigger Activation 2 for Metric 1. |
| 991 | auto screenOnEvent = |
| 992 | CreateScreenStateChangedEvent(timeBase2 + 200, android::view::DISPLAY_STATE_ON); |
| 993 | processor2->OnLogEvent(screenOnEvent.get()); |
| 994 | |
| 995 | // Metric 1 active; Activation 1 is active, Activation 2 is set to kActiveOnBoot |
| 996 | // Metric 2 is active. |
| 997 | // {{{--------------------------------------------------------------------------- |
| 998 | EXPECT_TRUE(metricProducer1001->isActive()); |
| 999 | EXPECT_EQ(timeBase2 + ttl1 - activation1001_1->ttl_ns, activation1001_1->start_ns); |
| 1000 | EXPECT_EQ(kActive, activation1001_1->state); |
| 1001 | EXPECT_EQ(0, activation1001_2->start_ns); |
| 1002 | EXPECT_EQ(kActiveOnBoot, activation1001_2->state); |
| 1003 | |
| 1004 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 1005 | // }}}--------------------------------------------------------------------------- |
| 1006 | |
| 1007 | // Simulate shutdown by saving state to disk |
| 1008 | shutDownTime = timeBase2 + 50 * NS_PER_SEC; |
| 1009 | processor2->SaveActiveConfigsToDisk(shutDownTime); |
| 1010 | EXPECT_TRUE(metricProducer1001->isActive()); |
| 1011 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 1012 | ttl1 = timeBase2 + metric1ActivationTrigger1->ttl_seconds() * NS_PER_SEC - shutDownTime; |
| 1013 | int64_t ttl2 = metric1ActivationTrigger2->ttl_seconds() * NS_PER_SEC; |
| 1014 | |
| 1015 | // Simulate device restarted state by creating new instance of StatsLogProcessor with the |
| 1016 | // same config. |
| 1017 | long timeBase3 = timeBase2 + 120 * NS_PER_SEC; |
| 1018 | sp<StatsLogProcessor> processor3 = |
| 1019 | CreateStatsLogProcessor(timeBase3, timeBase3, config1, cfgKey1); |
| 1020 | |
| 1021 | // Metric 1 is not active. |
| 1022 | // Metric 2 is active. |
| 1023 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1024 | ASSERT_EQ(1, processor3->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1025 | it = processor3->mMetricsManagers.find(cfgKey1); |
| 1026 | EXPECT_TRUE(it != processor3->mMetricsManagers.end()); |
| 1027 | auto& metricsManagerTimeBase3 = it->second; |
| 1028 | EXPECT_TRUE(metricsManagerTimeBase3->isActive()); |
| 1029 | |
| 1030 | metricIt = metricsManagerTimeBase3->mAllMetricProducers.begin(); |
| 1031 | for (; metricIt != metricsManagerTimeBase3->mAllMetricProducers.end(); metricIt++) { |
| 1032 | if ((*metricIt)->getMetricId() == metricId1) { |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | EXPECT_TRUE(metricIt != metricsManagerTimeBase3->mAllMetricProducers.end()); |
| 1037 | auto& metricProducerTimeBase3_1 = *metricIt; |
| 1038 | EXPECT_FALSE(metricProducerTimeBase3_1->isActive()); |
| 1039 | |
| 1040 | metricIt = metricsManagerTimeBase3->mAllMetricProducers.begin(); |
| 1041 | for (; metricIt != metricsManagerTimeBase3->mAllMetricProducers.end(); metricIt++) { |
| 1042 | if ((*metricIt)->getMetricId() == metricId2) { |
| 1043 | break; |
| 1044 | } |
| 1045 | } |
| 1046 | EXPECT_TRUE(metricIt != metricsManagerTimeBase3->mAllMetricProducers.end()); |
| 1047 | auto& metricProducerTimeBase3_2 = *metricIt; |
| 1048 | EXPECT_TRUE(metricProducerTimeBase3_2->isActive()); |
| 1049 | |
| 1050 | i = 0; |
| 1051 | for (; i < metricsManagerTimeBase3->mAllAtomMatchers.size(); i++) { |
| 1052 | if (metricsManagerTimeBase3->mAllAtomMatchers[i]->getId() == |
| 1053 | metric1ActivationTrigger1->atom_matcher_id()) { |
| 1054 | break; |
| 1055 | } |
| 1056 | } |
| 1057 | const auto& activationTimeBase3_1 = metricProducerTimeBase3_1->mEventActivationMap.at(i); |
| 1058 | EXPECT_EQ(100 * NS_PER_SEC, activationTimeBase3_1->ttl_ns); |
| 1059 | EXPECT_EQ(0, activationTimeBase3_1->start_ns); |
| 1060 | EXPECT_EQ(kNotActive, activationTimeBase3_1->state); |
| 1061 | |
| 1062 | i = 0; |
| 1063 | for (; i < metricsManagerTimeBase3->mAllAtomMatchers.size(); i++) { |
| 1064 | if (metricsManagerTimeBase3->mAllAtomMatchers[i]->getId() == |
| 1065 | metric1ActivationTrigger2->atom_matcher_id()) { |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | const auto& activationTimeBase3_2 = metricProducerTimeBase3_1->mEventActivationMap.at(i); |
| 1071 | EXPECT_EQ(200 * NS_PER_SEC, activationTimeBase3_2->ttl_ns); |
| 1072 | EXPECT_EQ(0, activationTimeBase3_2->start_ns); |
| 1073 | EXPECT_EQ(kNotActive, activationTimeBase3_2->state); |
| 1074 | |
| 1075 | EXPECT_TRUE(metricProducerTimeBase3_2->isActive()); |
| 1076 | // }}}---------------------------------------------------------------------------------- |
| 1077 | |
| 1078 | // Load saved state from disk. |
| 1079 | processor3->LoadActiveConfigsFromDisk(); |
| 1080 | |
| 1081 | // Metric 1 active: Activation 1 is active, Activation 2 is active |
| 1082 | // Metric 2 is active. |
| 1083 | // {{{--------------------------------------------------------------------------- |
| 1084 | EXPECT_TRUE(metricProducerTimeBase3_1->isActive()); |
| 1085 | EXPECT_EQ(timeBase3 + ttl1 - activationTimeBase3_1->ttl_ns, activationTimeBase3_1->start_ns); |
| 1086 | EXPECT_EQ(kActive, activationTimeBase3_1->state); |
| 1087 | EXPECT_EQ(timeBase3 + ttl2 - activationTimeBase3_2->ttl_ns, activationTimeBase3_2->start_ns); |
| 1088 | EXPECT_EQ(kActive, activationTimeBase3_2->state); |
| 1089 | |
| 1090 | EXPECT_TRUE(metricProducerTimeBase3_2->isActive()); |
| 1091 | // }}}------------------------------------------------------------------------------- |
| 1092 | |
| 1093 | // Trigger Activation 2 for Metric 1 again. |
| 1094 | screenOnEvent = CreateScreenStateChangedEvent(timeBase3 + 100 * NS_PER_SEC, |
| 1095 | android::view::DISPLAY_STATE_ON); |
| 1096 | processor3->OnLogEvent(screenOnEvent.get()); |
| 1097 | |
| 1098 | // Metric 1 active; Activation 1 is not active, Activation 2 is set to active |
| 1099 | // Metric 2 is active. |
| 1100 | // {{{--------------------------------------------------------------------------- |
| 1101 | EXPECT_TRUE(metricProducerTimeBase3_1->isActive()); |
| 1102 | EXPECT_EQ(kNotActive, activationTimeBase3_1->state); |
| 1103 | EXPECT_EQ(timeBase3 + ttl2 - activationTimeBase3_2->ttl_ns, activationTimeBase3_2->start_ns); |
| 1104 | EXPECT_EQ(kActive, activationTimeBase3_2->state); |
| 1105 | |
| 1106 | EXPECT_TRUE(metricProducerTimeBase3_2->isActive()); |
| 1107 | // }}}--------------------------------------------------------------------------- |
| 1108 | |
| 1109 | // Simulate shutdown by saving state to disk. |
| 1110 | shutDownTime = timeBase3 + 500 * NS_PER_SEC; |
| 1111 | processor3->SaveActiveConfigsToDisk(shutDownTime); |
| 1112 | EXPECT_TRUE(metricProducer1001->isActive()); |
| 1113 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 1114 | ttl1 = timeBase3 + ttl1 - shutDownTime; |
| 1115 | ttl2 = timeBase3 + metric1ActivationTrigger2->ttl_seconds() * NS_PER_SEC - shutDownTime; |
| 1116 | |
| 1117 | // Simulate device restarted state by creating new instance of StatsLogProcessor with the |
| 1118 | // same config. |
| 1119 | long timeBase4 = timeBase3 + 600 * NS_PER_SEC; |
| 1120 | sp<StatsLogProcessor> processor4 = |
| 1121 | CreateStatsLogProcessor(timeBase4, timeBase4, config1, cfgKey1); |
| 1122 | |
| 1123 | // Metric 1 is not active. |
| 1124 | // Metric 2 is active. |
| 1125 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1126 | ASSERT_EQ(1, processor4->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1127 | it = processor4->mMetricsManagers.find(cfgKey1); |
| 1128 | EXPECT_TRUE(it != processor4->mMetricsManagers.end()); |
| 1129 | auto& metricsManagerTimeBase4 = it->second; |
| 1130 | EXPECT_TRUE(metricsManagerTimeBase4->isActive()); |
| 1131 | |
| 1132 | metricIt = metricsManagerTimeBase4->mAllMetricProducers.begin(); |
| 1133 | for (; metricIt != metricsManagerTimeBase4->mAllMetricProducers.end(); metricIt++) { |
| 1134 | if ((*metricIt)->getMetricId() == metricId1) { |
| 1135 | break; |
| 1136 | } |
| 1137 | } |
| 1138 | EXPECT_TRUE(metricIt != metricsManagerTimeBase4->mAllMetricProducers.end()); |
| 1139 | auto& metricProducerTimeBase4_1 = *metricIt; |
| 1140 | EXPECT_FALSE(metricProducerTimeBase4_1->isActive()); |
| 1141 | |
| 1142 | metricIt = metricsManagerTimeBase4->mAllMetricProducers.begin(); |
| 1143 | for (; metricIt != metricsManagerTimeBase4->mAllMetricProducers.end(); metricIt++) { |
| 1144 | if ((*metricIt)->getMetricId() == metricId2) { |
| 1145 | break; |
| 1146 | } |
| 1147 | } |
| 1148 | EXPECT_TRUE(metricIt != metricsManagerTimeBase4->mAllMetricProducers.end()); |
| 1149 | auto& metricProducerTimeBase4_2 = *metricIt; |
| 1150 | EXPECT_TRUE(metricProducerTimeBase4_2->isActive()); |
| 1151 | |
| 1152 | i = 0; |
| 1153 | for (; i < metricsManagerTimeBase4->mAllAtomMatchers.size(); i++) { |
| 1154 | if (metricsManagerTimeBase4->mAllAtomMatchers[i]->getId() == |
| 1155 | metric1ActivationTrigger1->atom_matcher_id()) { |
| 1156 | break; |
| 1157 | } |
| 1158 | } |
| 1159 | const auto& activationTimeBase4_1 = metricProducerTimeBase4_1->mEventActivationMap.at(i); |
| 1160 | EXPECT_EQ(100 * NS_PER_SEC, activationTimeBase4_1->ttl_ns); |
| 1161 | EXPECT_EQ(0, activationTimeBase4_1->start_ns); |
| 1162 | EXPECT_EQ(kNotActive, activationTimeBase4_1->state); |
| 1163 | |
| 1164 | i = 0; |
| 1165 | for (; i < metricsManagerTimeBase4->mAllAtomMatchers.size(); i++) { |
| 1166 | if (metricsManagerTimeBase4->mAllAtomMatchers[i]->getId() == |
| 1167 | metric1ActivationTrigger2->atom_matcher_id()) { |
| 1168 | break; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | const auto& activationTimeBase4_2 = metricProducerTimeBase4_1->mEventActivationMap.at(i); |
| 1173 | EXPECT_EQ(200 * NS_PER_SEC, activationTimeBase4_2->ttl_ns); |
| 1174 | EXPECT_EQ(0, activationTimeBase4_2->start_ns); |
| 1175 | EXPECT_EQ(kNotActive, activationTimeBase4_2->state); |
| 1176 | |
| 1177 | EXPECT_TRUE(metricProducerTimeBase4_2->isActive()); |
| 1178 | // }}}---------------------------------------------------------------------------------- |
| 1179 | |
| 1180 | // Load saved state from disk. |
| 1181 | processor4->LoadActiveConfigsFromDisk(); |
| 1182 | |
| 1183 | // Metric 1 active: Activation 1 is not active, Activation 2 is not active |
| 1184 | // Metric 2 is active. |
| 1185 | // {{{--------------------------------------------------------------------------- |
| 1186 | EXPECT_FALSE(metricProducerTimeBase4_1->isActive()); |
| 1187 | EXPECT_EQ(kNotActive, activationTimeBase4_1->state); |
| 1188 | EXPECT_EQ(kNotActive, activationTimeBase4_2->state); |
| 1189 | |
| 1190 | EXPECT_TRUE(metricProducerTimeBase4_2->isActive()); |
| 1191 | // }}}------------------------------------------------------------------------------- |
| 1192 | } |
| 1193 | |
| 1194 | TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivationsDifferentActivationTypes) { |
| 1195 | int uid = 1111; |
| 1196 | |
| 1197 | // Create config with 2 metrics: |
| 1198 | // Metric 1: Activate on boot with 2 activations |
| 1199 | // Metric 2: Always active |
| 1200 | StatsdConfig config1; |
| 1201 | config1.set_id(12341); |
| 1202 | config1.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 1203 | auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher(); |
| 1204 | auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher(); |
| 1205 | *config1.add_atom_matcher() = wakelockAcquireMatcher; |
| 1206 | *config1.add_atom_matcher() = screenOnMatcher; |
| 1207 | |
| 1208 | long metricId1 = 1234561; |
| 1209 | long metricId2 = 1234562; |
| 1210 | |
| 1211 | auto countMetric1 = config1.add_count_metric(); |
| 1212 | countMetric1->set_id(metricId1); |
| 1213 | countMetric1->set_what(wakelockAcquireMatcher.id()); |
| 1214 | countMetric1->set_bucket(FIVE_MINUTES); |
| 1215 | |
| 1216 | auto countMetric2 = config1.add_count_metric(); |
| 1217 | countMetric2->set_id(metricId2); |
| 1218 | countMetric2->set_what(wakelockAcquireMatcher.id()); |
| 1219 | countMetric2->set_bucket(FIVE_MINUTES); |
| 1220 | |
| 1221 | auto metric1Activation = config1.add_metric_activation(); |
| 1222 | metric1Activation->set_metric_id(metricId1); |
| 1223 | metric1Activation->set_activation_type(ACTIVATE_ON_BOOT); |
| 1224 | auto metric1ActivationTrigger1 = metric1Activation->add_event_activation(); |
| 1225 | metric1ActivationTrigger1->set_atom_matcher_id(wakelockAcquireMatcher.id()); |
| 1226 | metric1ActivationTrigger1->set_ttl_seconds(100); |
| 1227 | auto metric1ActivationTrigger2 = metric1Activation->add_event_activation(); |
| 1228 | metric1ActivationTrigger2->set_atom_matcher_id(screenOnMatcher.id()); |
| 1229 | metric1ActivationTrigger2->set_ttl_seconds(200); |
| 1230 | metric1ActivationTrigger2->set_activation_type(ACTIVATE_IMMEDIATELY); |
| 1231 | |
| 1232 | ConfigKey cfgKey1(uid, 12341); |
| 1233 | long timeBase1 = 1; |
| 1234 | sp<StatsLogProcessor> processor1 = |
| 1235 | CreateStatsLogProcessor(timeBase1, timeBase1, config1, cfgKey1); |
| 1236 | |
| 1237 | // Metric 1 is not active. |
| 1238 | // Metric 2 is active. |
| 1239 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1240 | ASSERT_EQ(1, processor1->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1241 | auto it = processor1->mMetricsManagers.find(cfgKey1); |
| 1242 | EXPECT_TRUE(it != processor1->mMetricsManagers.end()); |
| 1243 | auto& metricsManager1 = it->second; |
| 1244 | EXPECT_TRUE(metricsManager1->isActive()); |
| 1245 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1246 | ASSERT_EQ(metricsManager1->mAllMetricProducers.size(), 2); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1247 | // We assume that the index of a MetricProducer within the mAllMetricProducers |
| 1248 | // array follows the order in which metrics are added to the config. |
| 1249 | auto& metricProducer1_1 = metricsManager1->mAllMetricProducers[0]; |
| 1250 | EXPECT_EQ(metricProducer1_1->getMetricId(), metricId1); |
| 1251 | EXPECT_FALSE(metricProducer1_1->isActive()); // inactive due to associated MetricActivation |
| 1252 | |
| 1253 | auto& metricProducer1_2 = metricsManager1->mAllMetricProducers[1]; |
| 1254 | EXPECT_EQ(metricProducer1_2->getMetricId(), metricId2); |
| 1255 | EXPECT_TRUE(metricProducer1_2->isActive()); |
| 1256 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1257 | ASSERT_EQ(metricProducer1_1->mEventActivationMap.size(), 2); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1258 | // The key in mEventActivationMap is the index of the associated atom matcher. We assume |
| 1259 | // that matchers are indexed in the order that they are added to the config. |
| 1260 | const auto& activation1_1_1 = metricProducer1_1->mEventActivationMap.at(0); |
| 1261 | EXPECT_EQ(100 * NS_PER_SEC, activation1_1_1->ttl_ns); |
| 1262 | EXPECT_EQ(0, activation1_1_1->start_ns); |
| 1263 | EXPECT_EQ(kNotActive, activation1_1_1->state); |
| 1264 | EXPECT_EQ(ACTIVATE_ON_BOOT, activation1_1_1->activationType); |
| 1265 | |
| 1266 | const auto& activation1_1_2 = metricProducer1_1->mEventActivationMap.at(1); |
| 1267 | EXPECT_EQ(200 * NS_PER_SEC, activation1_1_2->ttl_ns); |
| 1268 | EXPECT_EQ(0, activation1_1_2->start_ns); |
| 1269 | EXPECT_EQ(kNotActive, activation1_1_2->state); |
| 1270 | EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation1_1_2->activationType); |
| 1271 | // }}}------------------------------------------------------------------------------ |
| 1272 | |
| 1273 | // Trigger Activation 1 for Metric 1 |
| 1274 | std::vector<int> attributionUids = {111}; |
| 1275 | std::vector<string> attributionTags = {"App1"}; |
| 1276 | std::unique_ptr<LogEvent> event = |
| 1277 | CreateAcquireWakelockEvent(timeBase1 + 100, attributionUids, attributionTags, "wl1"); |
| 1278 | processor1->OnLogEvent(event.get()); |
| 1279 | |
| 1280 | // Metric 1 is not active; Activation 1 set to kActiveOnBoot |
| 1281 | // Metric 2 is active. |
| 1282 | // {{{--------------------------------------------------------------------------- |
| 1283 | EXPECT_FALSE(metricProducer1_1->isActive()); |
| 1284 | EXPECT_EQ(0, activation1_1_1->start_ns); |
| 1285 | EXPECT_EQ(kActiveOnBoot, activation1_1_1->state); |
| 1286 | EXPECT_EQ(0, activation1_1_2->start_ns); |
| 1287 | EXPECT_EQ(kNotActive, activation1_1_2->state); |
| 1288 | |
| 1289 | EXPECT_TRUE(metricProducer1_2->isActive()); |
| 1290 | // }}}----------------------------------------------------------------------------- |
| 1291 | |
| 1292 | // Simulate shutdown by saving state to disk |
| 1293 | int64_t shutDownTime = timeBase1 + 100 * NS_PER_SEC; |
| 1294 | processor1->SaveActiveConfigsToDisk(shutDownTime); |
| 1295 | EXPECT_FALSE(metricProducer1_1->isActive()); |
| 1296 | |
| 1297 | // Simulate device restarted state by creating new instance of StatsLogProcessor with the |
| 1298 | // same config. |
| 1299 | long timeBase2 = 1000; |
| 1300 | sp<StatsLogProcessor> processor2 = |
| 1301 | CreateStatsLogProcessor(timeBase2, timeBase2, config1, cfgKey1); |
| 1302 | |
| 1303 | // Metric 1 is not active. |
| 1304 | // Metric 2 is active. |
| 1305 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1306 | ASSERT_EQ(1, processor2->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1307 | it = processor2->mMetricsManagers.find(cfgKey1); |
| 1308 | EXPECT_TRUE(it != processor2->mMetricsManagers.end()); |
| 1309 | auto& metricsManager2 = it->second; |
| 1310 | EXPECT_TRUE(metricsManager2->isActive()); |
| 1311 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1312 | ASSERT_EQ(metricsManager2->mAllMetricProducers.size(), 2); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1313 | // We assume that the index of a MetricProducer within the mAllMetricProducers |
| 1314 | // array follows the order in which metrics are added to the config. |
| 1315 | auto& metricProducer2_1 = metricsManager2->mAllMetricProducers[0]; |
| 1316 | EXPECT_EQ(metricProducer2_1->getMetricId(), metricId1); |
| 1317 | EXPECT_FALSE(metricProducer2_1->isActive()); |
| 1318 | |
| 1319 | auto& metricProducer2_2 = metricsManager2->mAllMetricProducers[1]; |
| 1320 | EXPECT_EQ(metricProducer2_2->getMetricId(), metricId2); |
| 1321 | EXPECT_TRUE(metricProducer2_2->isActive()); |
| 1322 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1323 | ASSERT_EQ(metricProducer2_1->mEventActivationMap.size(), 2); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1324 | // The key in mEventActivationMap is the index of the associated atom matcher. We assume |
| 1325 | // that matchers are indexed in the order that they are added to the config. |
| 1326 | const auto& activation2_1_1 = metricProducer2_1->mEventActivationMap.at(0); |
| 1327 | EXPECT_EQ(100 * NS_PER_SEC, activation2_1_1->ttl_ns); |
| 1328 | EXPECT_EQ(0, activation2_1_1->start_ns); |
| 1329 | EXPECT_EQ(kNotActive, activation2_1_1->state); |
| 1330 | EXPECT_EQ(ACTIVATE_ON_BOOT, activation2_1_1->activationType); |
| 1331 | |
| 1332 | const auto& activation2_1_2 = metricProducer2_1->mEventActivationMap.at(1); |
| 1333 | EXPECT_EQ(200 * NS_PER_SEC, activation2_1_2->ttl_ns); |
| 1334 | EXPECT_EQ(0, activation2_1_2->start_ns); |
| 1335 | EXPECT_EQ(kNotActive, activation2_1_2->state); |
| 1336 | EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation2_1_2->activationType); |
| 1337 | // }}}----------------------------------------------------------------------------------- |
| 1338 | |
| 1339 | // Load saved state from disk. |
| 1340 | processor2->LoadActiveConfigsFromDisk(); |
| 1341 | |
| 1342 | // Metric 1 active; Activation 1 is active, Activation 2 is not active |
| 1343 | // Metric 2 is active. |
| 1344 | // {{{--------------------------------------------------------------------------- |
| 1345 | EXPECT_TRUE(metricProducer2_1->isActive()); |
| 1346 | int64_t ttl1 = metric1ActivationTrigger1->ttl_seconds() * NS_PER_SEC; |
| 1347 | EXPECT_EQ(timeBase2 + ttl1 - activation2_1_1->ttl_ns, activation2_1_1->start_ns); |
| 1348 | EXPECT_EQ(kActive, activation2_1_1->state); |
| 1349 | EXPECT_EQ(0, activation2_1_2->start_ns); |
| 1350 | EXPECT_EQ(kNotActive, activation2_1_2->state); |
| 1351 | |
| 1352 | EXPECT_TRUE(metricProducer2_2->isActive()); |
| 1353 | // }}}-------------------------------------------------------------------------------- |
| 1354 | |
| 1355 | // Trigger Activation 2 for Metric 1. |
| 1356 | auto screenOnEvent = |
| 1357 | CreateScreenStateChangedEvent(timeBase2 + 200, android::view::DISPLAY_STATE_ON); |
| 1358 | processor2->OnLogEvent(screenOnEvent.get()); |
| 1359 | |
| 1360 | // Metric 1 active; Activation 1 is active, Activation 2 is active |
| 1361 | // Metric 2 is active. |
| 1362 | // {{{--------------------------------------------------------------------------- |
| 1363 | EXPECT_TRUE(metricProducer2_1->isActive()); |
| 1364 | EXPECT_EQ(timeBase2 + ttl1 - activation2_1_1->ttl_ns, activation2_1_1->start_ns); |
| 1365 | EXPECT_EQ(kActive, activation2_1_1->state); |
| 1366 | EXPECT_EQ(screenOnEvent->GetElapsedTimestampNs(), activation2_1_2->start_ns); |
| 1367 | EXPECT_EQ(kActive, activation2_1_2->state); |
| 1368 | |
| 1369 | EXPECT_TRUE(metricProducer2_2->isActive()); |
| 1370 | // }}}--------------------------------------------------------------------------- |
| 1371 | |
| 1372 | // Simulate shutdown by saving state to disk |
| 1373 | shutDownTime = timeBase2 + 50 * NS_PER_SEC; |
| 1374 | processor2->SaveActiveConfigsToDisk(shutDownTime); |
| 1375 | EXPECT_TRUE(metricProducer2_1->isActive()); |
| 1376 | EXPECT_TRUE(metricProducer2_2->isActive()); |
| 1377 | ttl1 -= shutDownTime - timeBase2; |
| 1378 | int64_t ttl2 = metric1ActivationTrigger2->ttl_seconds() * NS_PER_SEC - |
| 1379 | (shutDownTime - screenOnEvent->GetElapsedTimestampNs()); |
| 1380 | |
| 1381 | // Simulate device restarted state by creating new instance of StatsLogProcessor with the |
| 1382 | // same config. |
| 1383 | long timeBase3 = timeBase2 + 120 * NS_PER_SEC; |
| 1384 | sp<StatsLogProcessor> processor3 = |
| 1385 | CreateStatsLogProcessor(timeBase3, timeBase3, config1, cfgKey1); |
| 1386 | |
| 1387 | // Metric 1 is not active. |
| 1388 | // Metric 2 is active. |
| 1389 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1390 | ASSERT_EQ(1, processor3->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1391 | it = processor3->mMetricsManagers.find(cfgKey1); |
| 1392 | EXPECT_TRUE(it != processor3->mMetricsManagers.end()); |
| 1393 | auto& metricsManager3 = it->second; |
| 1394 | EXPECT_TRUE(metricsManager3->isActive()); |
| 1395 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1396 | ASSERT_EQ(metricsManager3->mAllMetricProducers.size(), 2); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1397 | // We assume that the index of a MetricProducer within the mAllMetricProducers |
| 1398 | // array follows the order in which metrics are added to the config. |
| 1399 | auto& metricProducer3_1 = metricsManager3->mAllMetricProducers[0]; |
| 1400 | EXPECT_EQ(metricProducer3_1->getMetricId(), metricId1); |
| 1401 | EXPECT_FALSE(metricProducer3_1->isActive()); |
| 1402 | |
| 1403 | auto& metricProducer3_2 = metricsManager3->mAllMetricProducers[1]; |
| 1404 | EXPECT_EQ(metricProducer3_2->getMetricId(), metricId2); |
| 1405 | EXPECT_TRUE(metricProducer3_2->isActive()); |
| 1406 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1407 | ASSERT_EQ(metricProducer3_1->mEventActivationMap.size(), 2); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1408 | // The key in mEventActivationMap is the index of the associated atom matcher. We assume |
| 1409 | // that matchers are indexed in the order that they are added to the config. |
| 1410 | const auto& activation3_1_1 = metricProducer3_1->mEventActivationMap.at(0); |
| 1411 | EXPECT_EQ(100 * NS_PER_SEC, activation3_1_1->ttl_ns); |
| 1412 | EXPECT_EQ(0, activation3_1_1->start_ns); |
| 1413 | EXPECT_EQ(kNotActive, activation3_1_1->state); |
| 1414 | EXPECT_EQ(ACTIVATE_ON_BOOT, activation3_1_1->activationType); |
| 1415 | |
| 1416 | const auto& activation3_1_2 = metricProducer3_1->mEventActivationMap.at(1); |
| 1417 | EXPECT_EQ(200 * NS_PER_SEC, activation3_1_2->ttl_ns); |
| 1418 | EXPECT_EQ(0, activation3_1_2->start_ns); |
| 1419 | EXPECT_EQ(kNotActive, activation3_1_2->state); |
| 1420 | EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation3_1_2->activationType); |
| 1421 | // }}}---------------------------------------------------------------------------------- |
| 1422 | |
| 1423 | // Load saved state from disk. |
| 1424 | processor3->LoadActiveConfigsFromDisk(); |
| 1425 | |
| 1426 | // Metric 1 active: Activation 1 is active, Activation 2 is active |
| 1427 | // Metric 2 is active. |
| 1428 | // {{{--------------------------------------------------------------------------- |
| 1429 | EXPECT_TRUE(metricProducer3_1->isActive()); |
| 1430 | EXPECT_EQ(timeBase3 + ttl1 - activation3_1_1->ttl_ns, activation3_1_1->start_ns); |
| 1431 | EXPECT_EQ(kActive, activation3_1_1->state); |
| 1432 | EXPECT_EQ(timeBase3 + ttl2 - activation3_1_2->ttl_ns, activation3_1_2->start_ns); |
| 1433 | EXPECT_EQ(kActive, activation3_1_2->state); |
| 1434 | |
| 1435 | EXPECT_TRUE(metricProducer3_2->isActive()); |
| 1436 | // }}}------------------------------------------------------------------------------- |
| 1437 | |
| 1438 | // Trigger Activation 2 for Metric 1 again. |
| 1439 | screenOnEvent = CreateScreenStateChangedEvent(timeBase3 + 100 * NS_PER_SEC, |
| 1440 | android::view::DISPLAY_STATE_ON); |
| 1441 | processor3->OnLogEvent(screenOnEvent.get()); |
| 1442 | |
| 1443 | // Metric 1 active; Activation 1 is inactive (above screenOnEvent causes ttl1 to expire), |
| 1444 | // Activation 2 is set to active |
| 1445 | // Metric 2 is active. |
| 1446 | // {{{--------------------------------------------------------------------------- |
| 1447 | EXPECT_TRUE(metricProducer3_1->isActive()); |
| 1448 | EXPECT_EQ(kNotActive, activation3_1_1->state); |
| 1449 | EXPECT_EQ(screenOnEvent->GetElapsedTimestampNs(), activation3_1_2->start_ns); |
| 1450 | EXPECT_EQ(kActive, activation3_1_2->state); |
| 1451 | |
| 1452 | EXPECT_TRUE(metricProducer3_2->isActive()); |
| 1453 | // }}}--------------------------------------------------------------------------- |
| 1454 | } |
| 1455 | |
| 1456 | TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart) { |
| 1457 | int uid = 9876; |
| 1458 | long configId = 12341; |
| 1459 | |
| 1460 | // Create config with 3 metrics: |
| 1461 | // Metric 1: Activate on 2 activations, 1 on boot, 1 immediate. |
| 1462 | // Metric 2: Activate on 2 activations, 1 on boot, 1 immediate. |
| 1463 | // Metric 3: Always active |
| 1464 | StatsdConfig config1; |
| 1465 | config1.set_id(configId); |
| 1466 | config1.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root. |
| 1467 | auto wakelockAcquireMatcher = CreateAcquireWakelockAtomMatcher(); |
| 1468 | auto screenOnMatcher = CreateScreenTurnedOnAtomMatcher(); |
| 1469 | auto jobStartMatcher = CreateStartScheduledJobAtomMatcher(); |
| 1470 | auto jobFinishMatcher = CreateFinishScheduledJobAtomMatcher(); |
| 1471 | *config1.add_atom_matcher() = wakelockAcquireMatcher; |
| 1472 | *config1.add_atom_matcher() = screenOnMatcher; |
| 1473 | *config1.add_atom_matcher() = jobStartMatcher; |
| 1474 | *config1.add_atom_matcher() = jobFinishMatcher; |
| 1475 | |
| 1476 | long metricId1 = 1234561; |
| 1477 | long metricId2 = 1234562; |
| 1478 | long metricId3 = 1234563; |
| 1479 | |
| 1480 | auto countMetric1 = config1.add_count_metric(); |
| 1481 | countMetric1->set_id(metricId1); |
| 1482 | countMetric1->set_what(wakelockAcquireMatcher.id()); |
| 1483 | countMetric1->set_bucket(FIVE_MINUTES); |
| 1484 | |
| 1485 | auto countMetric2 = config1.add_count_metric(); |
| 1486 | countMetric2->set_id(metricId2); |
| 1487 | countMetric2->set_what(wakelockAcquireMatcher.id()); |
| 1488 | countMetric2->set_bucket(FIVE_MINUTES); |
| 1489 | |
| 1490 | auto countMetric3 = config1.add_count_metric(); |
| 1491 | countMetric3->set_id(metricId3); |
| 1492 | countMetric3->set_what(wakelockAcquireMatcher.id()); |
| 1493 | countMetric3->set_bucket(FIVE_MINUTES); |
| 1494 | |
| 1495 | // Metric 1 activates on boot for wakelock acquire, immediately for screen on. |
| 1496 | auto metric1Activation = config1.add_metric_activation(); |
| 1497 | metric1Activation->set_metric_id(metricId1); |
| 1498 | auto metric1ActivationTrigger1 = metric1Activation->add_event_activation(); |
| 1499 | metric1ActivationTrigger1->set_atom_matcher_id(wakelockAcquireMatcher.id()); |
| 1500 | metric1ActivationTrigger1->set_ttl_seconds(100); |
| 1501 | metric1ActivationTrigger1->set_activation_type(ACTIVATE_ON_BOOT); |
| 1502 | auto metric1ActivationTrigger2 = metric1Activation->add_event_activation(); |
| 1503 | metric1ActivationTrigger2->set_atom_matcher_id(screenOnMatcher.id()); |
| 1504 | metric1ActivationTrigger2->set_ttl_seconds(200); |
| 1505 | metric1ActivationTrigger2->set_activation_type(ACTIVATE_IMMEDIATELY); |
| 1506 | |
| 1507 | // Metric 2 activates on boot for scheduled job start, immediately for scheduled job finish. |
| 1508 | auto metric2Activation = config1.add_metric_activation(); |
| 1509 | metric2Activation->set_metric_id(metricId2); |
| 1510 | auto metric2ActivationTrigger1 = metric2Activation->add_event_activation(); |
| 1511 | metric2ActivationTrigger1->set_atom_matcher_id(jobStartMatcher.id()); |
| 1512 | metric2ActivationTrigger1->set_ttl_seconds(100); |
| 1513 | metric2ActivationTrigger1->set_activation_type(ACTIVATE_ON_BOOT); |
| 1514 | auto metric2ActivationTrigger2 = metric2Activation->add_event_activation(); |
| 1515 | metric2ActivationTrigger2->set_atom_matcher_id(jobFinishMatcher.id()); |
| 1516 | metric2ActivationTrigger2->set_ttl_seconds(200); |
| 1517 | metric2ActivationTrigger2->set_activation_type(ACTIVATE_IMMEDIATELY); |
| 1518 | |
| 1519 | // Send the config. |
| 1520 | shared_ptr<StatsService> service = SharedRefBase::make<StatsService>(nullptr, nullptr); |
| 1521 | string serialized = config1.SerializeAsString(); |
| 1522 | service->addConfigurationChecked(uid, configId, {serialized.begin(), serialized.end()}); |
| 1523 | |
| 1524 | // Make sure the config is stored on disk. Otherwise, we will not reset on system server death. |
| 1525 | StatsdConfig tmpConfig; |
| 1526 | ConfigKey cfgKey1(uid, configId); |
| 1527 | EXPECT_TRUE(StorageManager::readConfigFromDisk(cfgKey1, &tmpConfig)); |
| 1528 | |
| 1529 | // Metric 1 is not active. |
| 1530 | // Metric 2 is not active. |
| 1531 | // Metric 3 is active. |
| 1532 | // {{{--------------------------------------------------------------------------- |
| 1533 | sp<StatsLogProcessor> processor = service->mProcessor; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1534 | ASSERT_EQ(1, processor->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1535 | auto it = processor->mMetricsManagers.find(cfgKey1); |
| 1536 | EXPECT_TRUE(it != processor->mMetricsManagers.end()); |
| 1537 | auto& metricsManager1 = it->second; |
| 1538 | EXPECT_TRUE(metricsManager1->isActive()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1539 | ASSERT_EQ(3, metricsManager1->mAllMetricProducers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | auto& metricProducer1 = metricsManager1->mAllMetricProducers[0]; |
| 1542 | EXPECT_EQ(metricId1, metricProducer1->getMetricId()); |
| 1543 | EXPECT_FALSE(metricProducer1->isActive()); |
| 1544 | |
| 1545 | auto& metricProducer2 = metricsManager1->mAllMetricProducers[1]; |
| 1546 | EXPECT_EQ(metricId2, metricProducer2->getMetricId()); |
| 1547 | EXPECT_FALSE(metricProducer2->isActive()); |
| 1548 | |
| 1549 | auto& metricProducer3 = metricsManager1->mAllMetricProducers[2]; |
| 1550 | EXPECT_EQ(metricId3, metricProducer3->getMetricId()); |
| 1551 | EXPECT_TRUE(metricProducer3->isActive()); |
| 1552 | |
| 1553 | // Check event activations. |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1554 | ASSERT_EQ(metricsManager1->mAllAtomMatchers.size(), 4); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1555 | EXPECT_EQ(metricsManager1->mAllAtomMatchers[0]->getId(), |
| 1556 | metric1ActivationTrigger1->atom_matcher_id()); |
| 1557 | const auto& activation1 = metricProducer1->mEventActivationMap.at(0); |
| 1558 | EXPECT_EQ(100 * NS_PER_SEC, activation1->ttl_ns); |
| 1559 | EXPECT_EQ(0, activation1->start_ns); |
| 1560 | EXPECT_EQ(kNotActive, activation1->state); |
| 1561 | EXPECT_EQ(ACTIVATE_ON_BOOT, activation1->activationType); |
| 1562 | |
| 1563 | EXPECT_EQ(metricsManager1->mAllAtomMatchers[1]->getId(), |
| 1564 | metric1ActivationTrigger2->atom_matcher_id()); |
| 1565 | const auto& activation2 = metricProducer1->mEventActivationMap.at(1); |
| 1566 | EXPECT_EQ(200 * NS_PER_SEC, activation2->ttl_ns); |
| 1567 | EXPECT_EQ(0, activation2->start_ns); |
| 1568 | EXPECT_EQ(kNotActive, activation2->state); |
| 1569 | EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation2->activationType); |
| 1570 | |
| 1571 | EXPECT_EQ(metricsManager1->mAllAtomMatchers[2]->getId(), |
| 1572 | metric2ActivationTrigger1->atom_matcher_id()); |
| 1573 | const auto& activation3 = metricProducer2->mEventActivationMap.at(2); |
| 1574 | EXPECT_EQ(100 * NS_PER_SEC, activation3->ttl_ns); |
| 1575 | EXPECT_EQ(0, activation3->start_ns); |
| 1576 | EXPECT_EQ(kNotActive, activation3->state); |
| 1577 | EXPECT_EQ(ACTIVATE_ON_BOOT, activation3->activationType); |
| 1578 | |
| 1579 | EXPECT_EQ(metricsManager1->mAllAtomMatchers[3]->getId(), |
| 1580 | metric2ActivationTrigger2->atom_matcher_id()); |
| 1581 | const auto& activation4 = metricProducer2->mEventActivationMap.at(3); |
| 1582 | EXPECT_EQ(200 * NS_PER_SEC, activation4->ttl_ns); |
| 1583 | EXPECT_EQ(0, activation4->start_ns); |
| 1584 | EXPECT_EQ(kNotActive, activation4->state); |
| 1585 | EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation4->activationType); |
| 1586 | // }}}------------------------------------------------------------------------------ |
| 1587 | |
| 1588 | // Trigger Activation 1 for Metric 1. Should activate on boot. |
| 1589 | // Trigger Activation 4 for Metric 2. Should activate immediately. |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 1590 | int64_t configAddedTimeNs = metricsManager1->mLastReportTimeNs; |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1591 | std::vector<int> attributionUids = {111}; |
| 1592 | std::vector<string> attributionTags = {"App1"}; |
| 1593 | std::unique_ptr<LogEvent> event1 = CreateAcquireWakelockEvent( |
| 1594 | 1 + configAddedTimeNs, attributionUids, attributionTags, "wl1"); |
| 1595 | processor->OnLogEvent(event1.get()); |
| 1596 | |
| 1597 | std::unique_ptr<LogEvent> event2 = CreateFinishScheduledJobEvent( |
| 1598 | 2 + configAddedTimeNs, attributionUids, attributionTags, "finish1"); |
| 1599 | processor->OnLogEvent(event2.get()); |
| 1600 | |
| 1601 | // Metric 1 is not active; Activation 1 set to kActiveOnBoot |
| 1602 | // Metric 2 is active. Activation 4 set to kActive |
| 1603 | // Metric 3 is active. |
| 1604 | // {{{--------------------------------------------------------------------------- |
| 1605 | EXPECT_FALSE(metricProducer1->isActive()); |
| 1606 | EXPECT_EQ(0, activation1->start_ns); |
| 1607 | EXPECT_EQ(kActiveOnBoot, activation1->state); |
| 1608 | EXPECT_EQ(0, activation2->start_ns); |
| 1609 | EXPECT_EQ(kNotActive, activation2->state); |
| 1610 | |
| 1611 | EXPECT_TRUE(metricProducer2->isActive()); |
| 1612 | EXPECT_EQ(0, activation3->start_ns); |
| 1613 | EXPECT_EQ(kNotActive, activation3->state); |
| 1614 | EXPECT_EQ(2 + configAddedTimeNs, activation4->start_ns); |
| 1615 | EXPECT_EQ(kActive, activation4->state); |
| 1616 | |
| 1617 | EXPECT_TRUE(metricProducer3->isActive()); |
| 1618 | // }}}----------------------------------------------------------------------------- |
| 1619 | |
| 1620 | // Can't fake time with StatsService. |
| 1621 | // Lets get a time close to the system server death time and make sure it's sane. |
| 1622 | int64_t approximateSystemServerDeath = getElapsedRealtimeNs(); |
| 1623 | EXPECT_TRUE(approximateSystemServerDeath > 2 + configAddedTimeNs); |
| 1624 | EXPECT_TRUE(approximateSystemServerDeath < NS_PER_SEC + configAddedTimeNs); |
| 1625 | |
| 1626 | // System server dies. |
| 1627 | service->statsCompanionServiceDiedImpl(); |
| 1628 | |
| 1629 | // We should have a new metrics manager. Lets get it and ensure activation status is restored. |
| 1630 | // {{{--------------------------------------------------------------------------- |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1631 | ASSERT_EQ(1, processor->mMetricsManagers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1632 | it = processor->mMetricsManagers.find(cfgKey1); |
| 1633 | EXPECT_TRUE(it != processor->mMetricsManagers.end()); |
| 1634 | auto& metricsManager2 = it->second; |
| 1635 | EXPECT_TRUE(metricsManager2->isActive()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1636 | ASSERT_EQ(3, metricsManager2->mAllMetricProducers.size()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1637 | |
| 1638 | auto& metricProducer1001 = metricsManager2->mAllMetricProducers[0]; |
| 1639 | EXPECT_EQ(metricId1, metricProducer1001->getMetricId()); |
| 1640 | EXPECT_FALSE(metricProducer1001->isActive()); |
| 1641 | |
| 1642 | auto& metricProducer1002 = metricsManager2->mAllMetricProducers[1]; |
| 1643 | EXPECT_EQ(metricId2, metricProducer1002->getMetricId()); |
| 1644 | EXPECT_TRUE(metricProducer1002->isActive()); |
| 1645 | |
| 1646 | auto& metricProducer1003 = metricsManager2->mAllMetricProducers[2]; |
| 1647 | EXPECT_EQ(metricId3, metricProducer1003->getMetricId()); |
| 1648 | EXPECT_TRUE(metricProducer1003->isActive()); |
| 1649 | |
| 1650 | // Check event activations. |
| 1651 | // Activation 1 is kActiveOnBoot. |
| 1652 | // Activation 2 and 3 are not active. |
| 1653 | // Activation 4 is active. |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1654 | ASSERT_EQ(metricsManager2->mAllAtomMatchers.size(), 4); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 1655 | EXPECT_EQ(metricsManager2->mAllAtomMatchers[0]->getId(), |
| 1656 | metric1ActivationTrigger1->atom_matcher_id()); |
| 1657 | const auto& activation1001 = metricProducer1001->mEventActivationMap.at(0); |
| 1658 | EXPECT_EQ(100 * NS_PER_SEC, activation1001->ttl_ns); |
| 1659 | EXPECT_EQ(0, activation1001->start_ns); |
| 1660 | EXPECT_EQ(kActiveOnBoot, activation1001->state); |
| 1661 | EXPECT_EQ(ACTIVATE_ON_BOOT, activation1001->activationType); |
| 1662 | |
| 1663 | EXPECT_EQ(metricsManager2->mAllAtomMatchers[1]->getId(), |
| 1664 | metric1ActivationTrigger2->atom_matcher_id()); |
| 1665 | const auto& activation1002 = metricProducer1001->mEventActivationMap.at(1); |
| 1666 | EXPECT_EQ(200 * NS_PER_SEC, activation1002->ttl_ns); |
| 1667 | EXPECT_EQ(0, activation1002->start_ns); |
| 1668 | EXPECT_EQ(kNotActive, activation1002->state); |
| 1669 | EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation1002->activationType); |
| 1670 | |
| 1671 | EXPECT_EQ(metricsManager2->mAllAtomMatchers[2]->getId(), |
| 1672 | metric2ActivationTrigger1->atom_matcher_id()); |
| 1673 | const auto& activation1003 = metricProducer1002->mEventActivationMap.at(2); |
| 1674 | EXPECT_EQ(100 * NS_PER_SEC, activation1003->ttl_ns); |
| 1675 | EXPECT_EQ(0, activation1003->start_ns); |
| 1676 | EXPECT_EQ(kNotActive, activation1003->state); |
| 1677 | EXPECT_EQ(ACTIVATE_ON_BOOT, activation1003->activationType); |
| 1678 | |
| 1679 | EXPECT_EQ(metricsManager2->mAllAtomMatchers[3]->getId(), |
| 1680 | metric2ActivationTrigger2->atom_matcher_id()); |
| 1681 | const auto& activation1004 = metricProducer1002->mEventActivationMap.at(3); |
| 1682 | EXPECT_EQ(200 * NS_PER_SEC, activation1004->ttl_ns); |
| 1683 | EXPECT_EQ(2 + configAddedTimeNs, activation1004->start_ns); |
| 1684 | EXPECT_EQ(kActive, activation1004->state); |
| 1685 | EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation1004->activationType); |
| 1686 | // }}}------------------------------------------------------------------------------ |
| 1687 | |
| 1688 | // Clear the data stored on disk as a result of the system server death. |
| 1689 | vector<uint8_t> buffer; |
| 1690 | processor->onDumpReport(cfgKey1, configAddedTimeNs + NS_PER_SEC, false, true, ADB_DUMP, FAST, |
| 1691 | &buffer); |
| 1692 | } |
Tej Singh | f53d445 | 2019-05-09 18:17:59 -0700 | [diff] [blame] | 1693 | |
Muhammad Qureshi | b635b3a | 2020-04-22 20:57:41 -0700 | [diff] [blame] | 1694 | TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogHostUid) { |
| 1695 | int hostUid = 20; |
| 1696 | int isolatedUid = 30; |
| 1697 | uint64_t eventTimeNs = 12355; |
| 1698 | int atomId = 89; |
| 1699 | int field1 = 90; |
| 1700 | int field2 = 28; |
| 1701 | sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid}); |
| 1702 | ConfigKey cfgKey; |
| 1703 | StatsdConfig config = MakeConfig(false); |
| 1704 | sp<StatsLogProcessor> processor = |
| 1705 | CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap); |
| 1706 | |
| 1707 | shared_ptr<LogEvent> logEvent = makeUidLogEvent(atomId, eventTimeNs, hostUid, field1, field2); |
| 1708 | |
| 1709 | processor->OnLogEvent(logEvent.get()); |
| 1710 | |
| 1711 | const vector<FieldValue>* actualFieldValues = &logEvent->getValues(); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1712 | ASSERT_EQ(3, actualFieldValues->size()); |
Muhammad Qureshi | b635b3a | 2020-04-22 20:57:41 -0700 | [diff] [blame] | 1713 | EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value); |
| 1714 | EXPECT_EQ(field1, actualFieldValues->at(1).mValue.int_value); |
| 1715 | EXPECT_EQ(field2, actualFieldValues->at(2).mValue.int_value); |
| 1716 | } |
| 1717 | |
| 1718 | TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogIsolatedUid) { |
| 1719 | int hostUid = 20; |
| 1720 | int isolatedUid = 30; |
| 1721 | uint64_t eventTimeNs = 12355; |
| 1722 | int atomId = 89; |
| 1723 | int field1 = 90; |
| 1724 | int field2 = 28; |
| 1725 | sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid}); |
| 1726 | ConfigKey cfgKey; |
| 1727 | StatsdConfig config = MakeConfig(false); |
| 1728 | sp<StatsLogProcessor> processor = |
| 1729 | CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap); |
| 1730 | |
| 1731 | shared_ptr<LogEvent> logEvent = |
| 1732 | makeUidLogEvent(atomId, eventTimeNs, isolatedUid, field1, field2); |
| 1733 | |
| 1734 | processor->OnLogEvent(logEvent.get()); |
| 1735 | |
| 1736 | const vector<FieldValue>* actualFieldValues = &logEvent->getValues(); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1737 | ASSERT_EQ(3, actualFieldValues->size()); |
Muhammad Qureshi | b635b3a | 2020-04-22 20:57:41 -0700 | [diff] [blame] | 1738 | EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value); |
| 1739 | EXPECT_EQ(field1, actualFieldValues->at(1).mValue.int_value); |
| 1740 | EXPECT_EQ(field2, actualFieldValues->at(2).mValue.int_value); |
| 1741 | } |
| 1742 | |
| 1743 | TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogHostUidAttributionChain) { |
| 1744 | int hostUid = 20; |
| 1745 | int isolatedUid = 30; |
| 1746 | uint64_t eventTimeNs = 12355; |
| 1747 | int atomId = 89; |
| 1748 | int field1 = 90; |
| 1749 | int field2 = 28; |
| 1750 | sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid}); |
| 1751 | ConfigKey cfgKey; |
| 1752 | StatsdConfig config = MakeConfig(false); |
| 1753 | sp<StatsLogProcessor> processor = |
| 1754 | CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap); |
| 1755 | |
| 1756 | shared_ptr<LogEvent> logEvent = makeAttributionLogEvent(atomId, eventTimeNs, {hostUid, 200}, |
| 1757 | {"tag1", "tag2"}, field1, field2); |
| 1758 | |
| 1759 | processor->OnLogEvent(logEvent.get()); |
| 1760 | |
| 1761 | const vector<FieldValue>* actualFieldValues = &logEvent->getValues(); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1762 | ASSERT_EQ(6, actualFieldValues->size()); |
Muhammad Qureshi | b635b3a | 2020-04-22 20:57:41 -0700 | [diff] [blame] | 1763 | EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value); |
| 1764 | EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value); |
| 1765 | EXPECT_EQ(200, actualFieldValues->at(2).mValue.int_value); |
| 1766 | EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value); |
| 1767 | EXPECT_EQ(field1, actualFieldValues->at(4).mValue.int_value); |
| 1768 | EXPECT_EQ(field2, actualFieldValues->at(5).mValue.int_value); |
| 1769 | } |
| 1770 | |
| 1771 | TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogIsolatedUidAttributionChain) { |
| 1772 | int hostUid = 20; |
| 1773 | int isolatedUid = 30; |
| 1774 | uint64_t eventTimeNs = 12355; |
| 1775 | int atomId = 89; |
| 1776 | int field1 = 90; |
| 1777 | int field2 = 28; |
| 1778 | sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid}); |
| 1779 | ConfigKey cfgKey; |
| 1780 | StatsdConfig config = MakeConfig(false); |
| 1781 | sp<StatsLogProcessor> processor = |
| 1782 | CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap); |
| 1783 | |
| 1784 | shared_ptr<LogEvent> logEvent = makeAttributionLogEvent(atomId, eventTimeNs, {isolatedUid, 200}, |
| 1785 | {"tag1", "tag2"}, field1, field2); |
| 1786 | |
| 1787 | processor->OnLogEvent(logEvent.get()); |
| 1788 | |
| 1789 | const vector<FieldValue>* actualFieldValues = &logEvent->getValues(); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 1790 | ASSERT_EQ(6, actualFieldValues->size()); |
Muhammad Qureshi | b635b3a | 2020-04-22 20:57:41 -0700 | [diff] [blame] | 1791 | EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value); |
| 1792 | EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value); |
| 1793 | EXPECT_EQ(200, actualFieldValues->at(2).mValue.int_value); |
| 1794 | EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value); |
| 1795 | EXPECT_EQ(field1, actualFieldValues->at(4).mValue.int_value); |
| 1796 | EXPECT_EQ(field2, actualFieldValues->at(5).mValue.int_value); |
| 1797 | } |
| 1798 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 1799 | #else |
| 1800 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 1801 | #endif |
| 1802 | |
| 1803 | } // namespace statsd |
| 1804 | } // namespace os |
Yao Chen | 288c600 | 2017-12-12 13:43:18 -0800 | [diff] [blame] | 1805 | } // namespace android |