Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [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 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 16 | #include <private/android_filesystem_config.h> |
| 17 | #include <stdio.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 19 | #include <set> |
| 20 | #include <unordered_map> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 24 | #include "metrics/metrics_test_helper.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 25 | #include "src/condition/ConditionTracker.h" |
| 26 | #include "src/matchers/LogMatchingTracker.h" |
| 27 | #include "src/metrics/CountMetricProducer.h" |
Yangster | 1d4d686 | 2017-10-31 12:58:51 -0700 | [diff] [blame] | 28 | #include "src/metrics/GaugeMetricProducer.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 29 | #include "src/metrics/MetricProducer.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 30 | #include "src/metrics/ValueMetricProducer.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 31 | #include "src/metrics/metrics_manager_util.h" |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 32 | #include "statsd_test_util.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 33 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 34 | using namespace testing; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 35 | using android::sp; |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 36 | using android::os::statsd::Predicate; |
| 37 | using std::map; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 38 | using std::set; |
| 39 | using std::unordered_map; |
| 40 | using std::vector; |
| 41 | |
| 42 | #ifdef __ANDROID__ |
| 43 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 44 | namespace android { |
| 45 | namespace os { |
| 46 | namespace statsd { |
| 47 | |
| 48 | namespace { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 49 | const ConfigKey kConfigKey(0, 12345); |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 50 | const long kAlertId = 3; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 51 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 52 | const long timeBaseSec = 1000; |
| 53 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 54 | StatsdConfig buildGoodConfig() { |
| 55 | StatsdConfig config; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 56 | config.set_id(12345); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 57 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 58 | AtomMatcher* eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 59 | eventMatcher->set_id(StringToId("SCREEN_IS_ON")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 60 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 61 | SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 62 | simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/); |
| 63 | simpleAtomMatcher->add_field_value_matcher()->set_field( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 64 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 65 | simpleAtomMatcher->mutable_field_value_matcher(0)->set_eq_int( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 66 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 67 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 68 | eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 69 | eventMatcher->set_id(StringToId("SCREEN_IS_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 70 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 71 | simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 72 | simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/); |
| 73 | simpleAtomMatcher->add_field_value_matcher()->set_field( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 74 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 75 | simpleAtomMatcher->mutable_field_value_matcher(0)->set_eq_int( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 76 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 77 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 78 | eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 79 | eventMatcher->set_id(StringToId("SCREEN_ON_OR_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 80 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 81 | AtomMatcher_Combination* combination = eventMatcher->mutable_combination(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 82 | combination->set_operation(LogicalOperation::OR); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 83 | combination->add_matcher(StringToId("SCREEN_IS_ON")); |
| 84 | combination->add_matcher(StringToId("SCREEN_IS_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 85 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 86 | CountMetric* metric = config.add_count_metric(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 87 | metric->set_id(3); |
| 88 | metric->set_what(StringToId("SCREEN_IS_ON")); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 89 | metric->set_bucket(ONE_MINUTE); |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 90 | metric->mutable_dimensions_in_what()->set_field(2 /*SCREEN_STATE_CHANGE*/); |
| 91 | metric->mutable_dimensions_in_what()->add_child()->set_field(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 92 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 93 | config.add_no_report_metric(3); |
| 94 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 95 | auto alert = config.add_alert(); |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 96 | alert->set_id(kAlertId); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 97 | alert->set_metric_id(3); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 98 | alert->set_num_buckets(10); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 99 | alert->set_refractory_period_secs(100); |
| 100 | alert->set_trigger_if_sum_gt(100); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 101 | return config; |
| 102 | } |
| 103 | |
| 104 | StatsdConfig buildCircleMatchers() { |
| 105 | StatsdConfig config; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 106 | config.set_id(12345); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 107 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 108 | AtomMatcher* eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 109 | eventMatcher->set_id(StringToId("SCREEN_IS_ON")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 110 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 111 | SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 112 | simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/); |
| 113 | simpleAtomMatcher->add_field_value_matcher()->set_field( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 114 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 115 | simpleAtomMatcher->mutable_field_value_matcher(0)->set_eq_int( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 116 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 117 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 118 | eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 119 | eventMatcher->set_id(StringToId("SCREEN_ON_OR_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 120 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 121 | AtomMatcher_Combination* combination = eventMatcher->mutable_combination(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 122 | combination->set_operation(LogicalOperation::OR); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 123 | combination->add_matcher(StringToId("SCREEN_IS_ON")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 124 | // Circle dependency |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 125 | combination->add_matcher(StringToId("SCREEN_ON_OR_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 126 | |
| 127 | return config; |
| 128 | } |
| 129 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 130 | StatsdConfig buildAlertWithUnknownMetric() { |
| 131 | StatsdConfig config; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 132 | config.set_id(12345); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 133 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 134 | AtomMatcher* eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 135 | eventMatcher->set_id(StringToId("SCREEN_IS_ON")); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 136 | |
| 137 | CountMetric* metric = config.add_count_metric(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 138 | metric->set_id(3); |
| 139 | metric->set_what(StringToId("SCREEN_IS_ON")); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 140 | metric->set_bucket(ONE_MINUTE); |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 141 | metric->mutable_dimensions_in_what()->set_field(2 /*SCREEN_STATE_CHANGE*/); |
| 142 | metric->mutable_dimensions_in_what()->add_child()->set_field(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 143 | |
| 144 | auto alert = config.add_alert(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 145 | alert->set_id(3); |
| 146 | alert->set_metric_id(2); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 147 | alert->set_num_buckets(10); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 148 | alert->set_refractory_period_secs(100); |
| 149 | alert->set_trigger_if_sum_gt(100); |
| 150 | return config; |
| 151 | } |
| 152 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 153 | StatsdConfig buildMissingMatchers() { |
| 154 | StatsdConfig config; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 155 | config.set_id(12345); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 156 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 157 | AtomMatcher* eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 158 | eventMatcher->set_id(StringToId("SCREEN_IS_ON")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 159 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 160 | SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 161 | simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/); |
| 162 | simpleAtomMatcher->add_field_value_matcher()->set_field( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 163 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 164 | simpleAtomMatcher->mutable_field_value_matcher(0)->set_eq_int( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 165 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 166 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 167 | eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 168 | eventMatcher->set_id(StringToId("SCREEN_ON_OR_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 169 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 170 | AtomMatcher_Combination* combination = eventMatcher->mutable_combination(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 171 | combination->set_operation(LogicalOperation::OR); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 172 | combination->add_matcher(StringToId("SCREEN_IS_ON")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 173 | // undefined matcher |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 174 | combination->add_matcher(StringToId("ABC")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 175 | |
| 176 | return config; |
| 177 | } |
| 178 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 179 | StatsdConfig buildMissingPredicate() { |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 180 | StatsdConfig config; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 181 | config.set_id(12345); |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 182 | |
| 183 | CountMetric* metric = config.add_count_metric(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 184 | metric->set_id(3); |
| 185 | metric->set_what(StringToId("SCREEN_EVENT")); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 186 | metric->set_bucket(ONE_MINUTE); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 187 | metric->set_condition(StringToId("SOME_CONDITION")); |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 188 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 189 | AtomMatcher* eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 190 | eventMatcher->set_id(StringToId("SCREEN_EVENT")); |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 191 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 192 | SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 193 | simpleAtomMatcher->set_atom_id(2); |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 194 | |
| 195 | return config; |
| 196 | } |
| 197 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 198 | StatsdConfig buildDimensionMetricsWithMultiTags() { |
| 199 | StatsdConfig config; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 200 | config.set_id(12345); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 201 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 202 | AtomMatcher* eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 203 | eventMatcher->set_id(StringToId("BATTERY_VERY_LOW")); |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 204 | SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 205 | simpleAtomMatcher->set_atom_id(2); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 206 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 207 | eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 208 | eventMatcher->set_id(StringToId("BATTERY_VERY_VERY_LOW")); |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 209 | simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 210 | simpleAtomMatcher->set_atom_id(3); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 211 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 212 | eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 213 | eventMatcher->set_id(StringToId("BATTERY_LOW")); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 214 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 215 | AtomMatcher_Combination* combination = eventMatcher->mutable_combination(); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 216 | combination->set_operation(LogicalOperation::OR); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 217 | combination->add_matcher(StringToId("BATTERY_VERY_LOW")); |
| 218 | combination->add_matcher(StringToId("BATTERY_VERY_VERY_LOW")); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 219 | |
| 220 | // Count process state changes, slice by uid, while SCREEN_IS_OFF |
| 221 | CountMetric* metric = config.add_count_metric(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 222 | metric->set_id(3); |
| 223 | metric->set_what(StringToId("BATTERY_LOW")); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 224 | metric->set_bucket(ONE_MINUTE); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 225 | // This case is interesting. We want to dimension across two atoms. |
Yangster-mac | 468ff04 | 2018-01-17 12:26:34 -0800 | [diff] [blame] | 226 | metric->mutable_dimensions_in_what()->add_child()->set_field(1); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 227 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 228 | auto alert = config.add_alert(); |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 229 | alert->set_id(kAlertId); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 230 | alert->set_metric_id(3); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 231 | alert->set_num_buckets(10); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 232 | alert->set_refractory_period_secs(100); |
| 233 | alert->set_trigger_if_sum_gt(100); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 234 | return config; |
| 235 | } |
| 236 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 237 | StatsdConfig buildCirclePredicates() { |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 238 | StatsdConfig config; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 239 | config.set_id(12345); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 240 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 241 | AtomMatcher* eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 242 | eventMatcher->set_id(StringToId("SCREEN_IS_ON")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 243 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 244 | SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 245 | simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/); |
| 246 | simpleAtomMatcher->add_field_value_matcher()->set_field( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 247 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 248 | simpleAtomMatcher->mutable_field_value_matcher(0)->set_eq_int( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 249 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 250 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 251 | eventMatcher = config.add_atom_matcher(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 252 | eventMatcher->set_id(StringToId("SCREEN_IS_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 253 | |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 254 | simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 255 | simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/); |
| 256 | simpleAtomMatcher->add_field_value_matcher()->set_field( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 257 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 258 | simpleAtomMatcher->mutable_field_value_matcher(0)->set_eq_int( |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 259 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 260 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 261 | auto condition = config.add_predicate(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 262 | condition->set_id(StringToId("SCREEN_IS_ON")); |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 263 | SimplePredicate* simplePredicate = condition->mutable_simple_predicate(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 264 | simplePredicate->set_start(StringToId("SCREEN_IS_ON")); |
| 265 | simplePredicate->set_stop(StringToId("SCREEN_IS_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 266 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 267 | condition = config.add_predicate(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 268 | condition->set_id(StringToId("SCREEN_IS_EITHER_ON_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 269 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 270 | Predicate_Combination* combination = condition->mutable_combination(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 271 | combination->set_operation(LogicalOperation::OR); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 272 | combination->add_predicate(StringToId("SCREEN_IS_ON")); |
| 273 | combination->add_predicate(StringToId("SCREEN_IS_EITHER_ON_OFF")); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 274 | |
| 275 | return config; |
| 276 | } |
| 277 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 278 | bool isSubset(const set<int32_t>& set1, const set<int32_t>& set2) { |
| 279 | return std::includes(set2.begin(), set2.end(), set1.begin(), set1.end()); |
| 280 | } |
| 281 | } // anonymous namespace |
| 282 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 283 | TEST(MetricsManagerTest, TestGoodConfig) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 284 | UidMap uidMap; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 285 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 286 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 287 | sp<AlarmMonitor> periodicAlarmMonitor; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 288 | StatsdConfig config = buildGoodConfig(); |
| 289 | set<int> allTagIds; |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 290 | vector<sp<LogMatchingTracker>> allAtomMatchers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 291 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 292 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 293 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 294 | std::vector<sp<AlarmTracker>> allAlarmTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 295 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 296 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 297 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 298 | unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap; |
| 299 | unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap; |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 300 | unordered_map<int64_t, int> alertTrackerMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 301 | vector<int> metricsWithActivation; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 302 | std::set<int64_t> noReportMetricIds; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 303 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 304 | EXPECT_TRUE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, |
| 305 | periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, |
| 306 | allAtomMatchers, allConditionTrackers, allMetricProducers, |
| 307 | allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 308 | trackerToMetricMap, trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 309 | activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 310 | alertTrackerMap, metricsWithActivation, |
| 311 | noReportMetricIds)); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 312 | EXPECT_EQ(1u, allMetricProducers.size()); |
| 313 | EXPECT_EQ(1u, allAnomalyTrackers.size()); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 314 | EXPECT_EQ(1u, noReportMetricIds.size()); |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 315 | EXPECT_EQ(1u, alertTrackerMap.size()); |
| 316 | EXPECT_NE(alertTrackerMap.find(kAlertId), alertTrackerMap.end()); |
| 317 | EXPECT_EQ(alertTrackerMap.find(kAlertId)->second, 0); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 320 | TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 321 | UidMap uidMap; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 322 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 323 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 324 | sp<AlarmMonitor> periodicAlarmMonitor; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 325 | StatsdConfig config = buildDimensionMetricsWithMultiTags(); |
| 326 | set<int> allTagIds; |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 327 | vector<sp<LogMatchingTracker>> allAtomMatchers; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 328 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 329 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 330 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 331 | std::vector<sp<AlarmTracker>> allAlarmTrackers; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 332 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 333 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 334 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 335 | unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap; |
| 336 | unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap; |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 337 | unordered_map<int64_t, int> alertTrackerMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 338 | vector<int> metricsWithActivation; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 339 | std::set<int64_t> noReportMetricIds; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 340 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 341 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, |
| 342 | periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, |
| 343 | allAtomMatchers, allConditionTrackers, allMetricProducers, |
| 344 | allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 345 | trackerToMetricMap, trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 346 | activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 347 | alertTrackerMap, metricsWithActivation, |
| 348 | noReportMetricIds)); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 351 | TEST(MetricsManagerTest, TestCircleLogMatcherDependency) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 352 | UidMap uidMap; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 353 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 354 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 355 | sp<AlarmMonitor> periodicAlarmMonitor; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 356 | StatsdConfig config = buildCircleMatchers(); |
| 357 | set<int> allTagIds; |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 358 | vector<sp<LogMatchingTracker>> allAtomMatchers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 359 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 360 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 361 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 362 | std::vector<sp<AlarmTracker>> allAlarmTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 363 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 364 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 365 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 366 | unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap; |
| 367 | unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap; |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 368 | unordered_map<int64_t, int> alertTrackerMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 369 | vector<int> metricsWithActivation; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 370 | std::set<int64_t> noReportMetricIds; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 371 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 372 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, |
| 373 | periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, |
| 374 | allAtomMatchers, allConditionTrackers, allMetricProducers, |
| 375 | allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 376 | trackerToMetricMap, trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 377 | activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 378 | alertTrackerMap, metricsWithActivation, |
| 379 | noReportMetricIds)); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | TEST(MetricsManagerTest, TestMissingMatchers) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 383 | UidMap uidMap; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 384 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 385 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 386 | sp<AlarmMonitor> periodicAlarmMonitor; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 387 | StatsdConfig config = buildMissingMatchers(); |
| 388 | set<int> allTagIds; |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 389 | vector<sp<LogMatchingTracker>> allAtomMatchers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 390 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 391 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 392 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 393 | std::vector<sp<AlarmTracker>> allAlarmTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 394 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 395 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 396 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 397 | unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap; |
| 398 | unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap; |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 399 | unordered_map<int64_t, int> alertTrackerMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 400 | vector<int> metricsWithActivation; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 401 | std::set<int64_t> noReportMetricIds; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 402 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, |
| 403 | periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, |
| 404 | allAtomMatchers, allConditionTrackers, allMetricProducers, |
| 405 | allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 406 | trackerToMetricMap, trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 407 | activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 408 | alertTrackerMap, metricsWithActivation, |
| 409 | noReportMetricIds)); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 412 | TEST(MetricsManagerTest, TestMissingPredicate) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 413 | UidMap uidMap; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 414 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 415 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 416 | sp<AlarmMonitor> periodicAlarmMonitor; |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 417 | StatsdConfig config = buildMissingPredicate(); |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 418 | set<int> allTagIds; |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 419 | vector<sp<LogMatchingTracker>> allAtomMatchers; |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 420 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 421 | vector<sp<MetricProducer>> allMetricProducers; |
| 422 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 423 | std::vector<sp<AlarmTracker>> allAlarmTrackers; |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 424 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 425 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 426 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 427 | unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap; |
| 428 | unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap; |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 429 | unordered_map<int64_t, int> alertTrackerMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 430 | vector<int> metricsWithActivation; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 431 | std::set<int64_t> noReportMetricIds; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 432 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, |
| 433 | periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, |
| 434 | allAtomMatchers, allConditionTrackers, allMetricProducers, |
| 435 | allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 436 | trackerToMetricMap, trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 437 | activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 438 | alertTrackerMap, metricsWithActivation, noReportMetricIds)); |
Yao Chen | 10535b9 | 2017-11-27 11:31:55 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 441 | TEST(MetricsManagerTest, TestCirclePredicateDependency) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 442 | UidMap uidMap; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 443 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 444 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 445 | sp<AlarmMonitor> periodicAlarmMonitor; |
Stefan Lafon | 12d01fa | 2017-12-04 20:56:09 -0800 | [diff] [blame] | 446 | StatsdConfig config = buildCirclePredicates(); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 447 | set<int> allTagIds; |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 448 | vector<sp<LogMatchingTracker>> allAtomMatchers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 449 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 450 | vector<sp<MetricProducer>> allMetricProducers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 451 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 452 | std::vector<sp<AlarmTracker>> allAlarmTrackers; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 453 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 454 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 455 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 456 | unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap; |
| 457 | unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap; |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 458 | unordered_map<int64_t, int> alertTrackerMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 459 | vector<int> metricsWithActivation; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 460 | std::set<int64_t> noReportMetricIds; |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 461 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 462 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, |
| 463 | periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, |
| 464 | allAtomMatchers, allConditionTrackers, allMetricProducers, |
| 465 | allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 466 | trackerToMetricMap, trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 467 | activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 468 | alertTrackerMap, metricsWithActivation, |
| 469 | noReportMetricIds)); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | TEST(MetricsManagerTest, testAlertWithUnknownMetric) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 473 | UidMap uidMap; |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 474 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 475 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 476 | sp<AlarmMonitor> periodicAlarmMonitor; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 477 | StatsdConfig config = buildAlertWithUnknownMetric(); |
| 478 | set<int> allTagIds; |
Stefan Lafon | b8c9aa8 | 2017-12-03 14:27:25 -0800 | [diff] [blame] | 479 | vector<sp<LogMatchingTracker>> allAtomMatchers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 480 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 481 | vector<sp<MetricProducer>> allMetricProducers; |
| 482 | std::vector<sp<AnomalyTracker>> allAnomalyTrackers; |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 483 | std::vector<sp<AlarmTracker>> allAlarmTrackers; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 484 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 485 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 486 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 487 | unordered_map<int, std::vector<int>> activationAtomTrackerToMetricMap; |
| 488 | unordered_map<int, std::vector<int>> deactivationAtomTrackerToMetricMap; |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 489 | unordered_map<int64_t, int> alertTrackerMap; |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 490 | vector<int> metricsWithActivation; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 491 | std::set<int64_t> noReportMetricIds; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 492 | |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 493 | EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, pullerManager, anomalyAlarmMonitor, |
| 494 | periodicAlarmMonitor, timeBaseSec, timeBaseSec, allTagIds, |
| 495 | allAtomMatchers, allConditionTrackers, allMetricProducers, |
| 496 | allAnomalyTrackers, allAlarmTrackers, conditionToMetricMap, |
Yangster-mac | 849dfdc2 | 2018-10-12 15:41:45 -0700 | [diff] [blame] | 497 | trackerToMetricMap, trackerToConditionMap, |
Muhammad Qureshi | 3a5ebf5 | 2019-03-28 12:38:21 -0700 | [diff] [blame] | 498 | activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap, |
Jeffrey Huang | 38d7026 | 2020-03-19 17:28:59 -0700 | [diff] [blame] | 499 | alertTrackerMap, metricsWithActivation, |
| 500 | noReportMetricIds)); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 503 | TEST(MetricsManagerTest, TestLogSources) { |
| 504 | string app1 = "app1"; |
| 505 | set<int32_t> app1Uids = {1111, 11111}; |
| 506 | string app2 = "app2"; |
| 507 | set<int32_t> app2Uids = {2222}; |
| 508 | string app3 = "app3"; |
| 509 | set<int32_t> app3Uids = {3333, 1111}; |
| 510 | |
| 511 | map<string, set<int32_t>> pkgToUids; |
| 512 | pkgToUids[app1] = app1Uids; |
| 513 | pkgToUids[app2] = app2Uids; |
| 514 | pkgToUids[app3] = app3Uids; |
| 515 | |
| 516 | int32_t atom1 = 10; |
| 517 | int32_t atom2 = 20; |
| 518 | int32_t atom3 = 30; |
| 519 | sp<MockUidMap> uidMap = new StrictMock<MockUidMap>(); |
| 520 | EXPECT_CALL(*uidMap, getAppUid(_)) |
| 521 | .Times(4) |
| 522 | .WillRepeatedly(Invoke([&pkgToUids](const string& pkg) { |
| 523 | const auto& it = pkgToUids.find(pkg); |
| 524 | if (it != pkgToUids.end()) { |
| 525 | return it->second; |
| 526 | } |
| 527 | return set<int32_t>(); |
| 528 | })); |
| 529 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 530 | EXPECT_CALL(*pullerManager, RegisterPullUidProvider(kConfigKey, _)).Times(1); |
Tej Singh | 3eb9ced | 2020-04-20 22:04:38 -0700 | [diff] [blame] | 531 | EXPECT_CALL(*pullerManager, UnregisterPullUidProvider(kConfigKey, _)).Times(1); |
Tej Singh | 3be093b | 2020-03-04 20:08:38 -0800 | [diff] [blame] | 532 | |
| 533 | sp<AlarmMonitor> anomalyAlarmMonitor; |
| 534 | sp<AlarmMonitor> periodicAlarmMonitor; |
| 535 | |
| 536 | StatsdConfig config = buildGoodConfig(); |
| 537 | config.add_allowed_log_source("AID_SYSTEM"); |
| 538 | config.add_allowed_log_source(app1); |
| 539 | config.add_default_pull_packages("AID_SYSTEM"); |
| 540 | config.add_default_pull_packages("AID_ROOT"); |
| 541 | |
| 542 | const set<int32_t> defaultPullUids = {AID_SYSTEM, AID_ROOT}; |
| 543 | |
| 544 | PullAtomPackages* pullAtomPackages = config.add_pull_atom_packages(); |
| 545 | pullAtomPackages->set_atom_id(atom1); |
| 546 | pullAtomPackages->add_packages(app1); |
| 547 | pullAtomPackages->add_packages(app3); |
| 548 | |
| 549 | pullAtomPackages = config.add_pull_atom_packages(); |
| 550 | pullAtomPackages->set_atom_id(atom2); |
| 551 | pullAtomPackages->add_packages(app2); |
| 552 | pullAtomPackages->add_packages("AID_STATSD"); |
| 553 | |
| 554 | MetricsManager metricsManager(kConfigKey, config, timeBaseSec, timeBaseSec, uidMap, |
| 555 | pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor); |
| 556 | |
| 557 | EXPECT_TRUE(metricsManager.isConfigValid()); |
| 558 | |
| 559 | ASSERT_EQ(metricsManager.mAllowedUid.size(), 1); |
| 560 | EXPECT_EQ(metricsManager.mAllowedUid[0], AID_SYSTEM); |
| 561 | |
| 562 | ASSERT_EQ(metricsManager.mAllowedPkg.size(), 1); |
| 563 | EXPECT_EQ(metricsManager.mAllowedPkg[0], app1); |
| 564 | |
| 565 | ASSERT_EQ(metricsManager.mAllowedLogSources.size(), 3); |
| 566 | EXPECT_TRUE(isSubset({AID_SYSTEM}, metricsManager.mAllowedLogSources)); |
| 567 | EXPECT_TRUE(isSubset(app1Uids, metricsManager.mAllowedLogSources)); |
| 568 | |
| 569 | ASSERT_EQ(metricsManager.mDefaultPullUids.size(), 2); |
| 570 | EXPECT_TRUE(isSubset(defaultPullUids, metricsManager.mDefaultPullUids)); |
| 571 | ; |
| 572 | |
| 573 | vector<int32_t> atom1Uids = metricsManager.getPullAtomUids(atom1); |
| 574 | ASSERT_EQ(atom1Uids.size(), 5); |
| 575 | set<int32_t> expectedAtom1Uids; |
| 576 | expectedAtom1Uids.insert(defaultPullUids.begin(), defaultPullUids.end()); |
| 577 | expectedAtom1Uids.insert(app1Uids.begin(), app1Uids.end()); |
| 578 | expectedAtom1Uids.insert(app3Uids.begin(), app3Uids.end()); |
| 579 | EXPECT_TRUE(isSubset(expectedAtom1Uids, set<int32_t>(atom1Uids.begin(), atom1Uids.end()))); |
| 580 | |
| 581 | vector<int32_t> atom2Uids = metricsManager.getPullAtomUids(atom2); |
| 582 | ASSERT_EQ(atom2Uids.size(), 4); |
| 583 | set<int32_t> expectedAtom2Uids; |
| 584 | expectedAtom1Uids.insert(defaultPullUids.begin(), defaultPullUids.end()); |
| 585 | expectedAtom1Uids.insert(app2Uids.begin(), app2Uids.end()); |
| 586 | expectedAtom1Uids.insert(AID_STATSD); |
| 587 | EXPECT_TRUE(isSubset(expectedAtom2Uids, set<int32_t>(atom2Uids.begin(), atom2Uids.end()))); |
| 588 | |
| 589 | vector<int32_t> atom3Uids = metricsManager.getPullAtomUids(atom3); |
| 590 | ASSERT_EQ(atom3Uids.size(), 2); |
| 591 | EXPECT_TRUE(isSubset(defaultPullUids, set<int32_t>(atom3Uids.begin(), atom3Uids.end()))); |
| 592 | } |
| 593 | |
| 594 | } // namespace statsd |
| 595 | } // namespace os |
| 596 | } // namespace android |
| 597 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 598 | #else |
| 599 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 600 | #endif |