Yangster-mac | 2087716 | 2017-12-22 17:19:39 -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 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 15 | #include "statsd_test_util.h" |
| 16 | |
| 17 | namespace android { |
| 18 | namespace os { |
| 19 | namespace statsd { |
| 20 | |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 21 | AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId) { |
| 22 | AtomMatcher atom_matcher; |
| 23 | atom_matcher.set_id(StringToId(name)); |
| 24 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 25 | simple_atom_matcher->set_atom_id(atomId); |
| 26 | return atom_matcher; |
| 27 | } |
| 28 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 29 | AtomMatcher CreateScreenBrightnessChangedAtomMatcher() { |
| 30 | AtomMatcher atom_matcher; |
| 31 | atom_matcher.set_id(StringToId("ScreenBrightnessChanged")); |
| 32 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 33 | simple_atom_matcher->set_atom_id(android::util::SCREEN_BRIGHTNESS_CHANGED); |
| 34 | return atom_matcher; |
| 35 | } |
| 36 | |
| 37 | AtomMatcher CreateUidProcessStateChangedAtomMatcher() { |
| 38 | AtomMatcher atom_matcher; |
| 39 | atom_matcher.set_id(StringToId("UidProcessStateChanged")); |
| 40 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 41 | simple_atom_matcher->set_atom_id(android::util::UID_PROCESS_STATE_CHANGED); |
| 42 | return atom_matcher; |
| 43 | } |
| 44 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 45 | AtomMatcher CreateWakelockStateChangedAtomMatcher(const string& name, |
| 46 | WakelockStateChanged::State state) { |
| 47 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 48 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 49 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 50 | simple_atom_matcher->set_atom_id(android::util::WAKELOCK_STATE_CHANGED); |
| 51 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 52 | field_value_matcher->set_field(4); // State field. |
| 53 | field_value_matcher->set_eq_int(state); |
| 54 | return atom_matcher; |
| 55 | } |
| 56 | |
| 57 | AtomMatcher CreateAcquireWakelockAtomMatcher() { |
| 58 | return CreateWakelockStateChangedAtomMatcher("AcquireWakelock", WakelockStateChanged::ACQUIRE); |
| 59 | } |
| 60 | |
| 61 | AtomMatcher CreateReleaseWakelockAtomMatcher() { |
| 62 | return CreateWakelockStateChangedAtomMatcher("ReleaseWakelock", WakelockStateChanged::RELEASE); |
| 63 | } |
| 64 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 65 | AtomMatcher CreateBatterySaverModeStateChangedAtomMatcher( |
| 66 | const string& name, BatterySaverModeStateChanged::State state) { |
| 67 | AtomMatcher atom_matcher; |
| 68 | atom_matcher.set_id(StringToId(name)); |
| 69 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 70 | simple_atom_matcher->set_atom_id(android::util::BATTERY_SAVER_MODE_STATE_CHANGED); |
| 71 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 72 | field_value_matcher->set_field(1); // State field. |
| 73 | field_value_matcher->set_eq_int(state); |
| 74 | return atom_matcher; |
| 75 | } |
| 76 | |
| 77 | AtomMatcher CreateBatterySaverModeStartAtomMatcher() { |
| 78 | return CreateBatterySaverModeStateChangedAtomMatcher( |
| 79 | "BatterySaverModeStart", BatterySaverModeStateChanged::ON); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | AtomMatcher CreateBatterySaverModeStopAtomMatcher() { |
| 84 | return CreateBatterySaverModeStateChangedAtomMatcher( |
| 85 | "BatterySaverModeStop", BatterySaverModeStateChanged::OFF); |
| 86 | } |
| 87 | |
| 88 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 89 | AtomMatcher CreateScreenStateChangedAtomMatcher( |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 90 | const string& name, android::view::DisplayStateEnum state) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 91 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 92 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 93 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 94 | simple_atom_matcher->set_atom_id(android::util::SCREEN_STATE_CHANGED); |
| 95 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 96 | field_value_matcher->set_field(1); // State field. |
| 97 | field_value_matcher->set_eq_int(state); |
| 98 | return atom_matcher; |
| 99 | } |
| 100 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 101 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 102 | AtomMatcher CreateScreenTurnedOnAtomMatcher() { |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 103 | return CreateScreenStateChangedAtomMatcher("ScreenTurnedOn", |
| 104 | android::view::DisplayStateEnum::DISPLAY_STATE_ON); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | AtomMatcher CreateScreenTurnedOffAtomMatcher() { |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 108 | return CreateScreenStateChangedAtomMatcher("ScreenTurnedOff", |
| 109 | ::android::view::DisplayStateEnum::DISPLAY_STATE_OFF); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | AtomMatcher CreateSyncStateChangedAtomMatcher( |
| 113 | const string& name, SyncStateChanged::State state) { |
| 114 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 115 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 116 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 117 | simple_atom_matcher->set_atom_id(android::util::SYNC_STATE_CHANGED); |
| 118 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 119 | field_value_matcher->set_field(3); // State field. |
| 120 | field_value_matcher->set_eq_int(state); |
| 121 | return atom_matcher; |
| 122 | } |
| 123 | |
| 124 | AtomMatcher CreateSyncStartAtomMatcher() { |
| 125 | return CreateSyncStateChangedAtomMatcher("SyncStart", SyncStateChanged::ON); |
| 126 | } |
| 127 | |
| 128 | AtomMatcher CreateSyncEndAtomMatcher() { |
| 129 | return CreateSyncStateChangedAtomMatcher("SyncEnd", SyncStateChanged::OFF); |
| 130 | } |
| 131 | |
| 132 | AtomMatcher CreateActivityForegroundStateChangedAtomMatcher( |
| 133 | const string& name, ActivityForegroundStateChanged::Activity activity) { |
| 134 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 135 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 136 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 137 | simple_atom_matcher->set_atom_id(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED); |
| 138 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 139 | field_value_matcher->set_field(4); // Activity field. |
| 140 | field_value_matcher->set_eq_int(activity); |
| 141 | return atom_matcher; |
| 142 | } |
| 143 | |
| 144 | AtomMatcher CreateMoveToBackgroundAtomMatcher() { |
| 145 | return CreateActivityForegroundStateChangedAtomMatcher( |
| 146 | "MoveToBackground", ActivityForegroundStateChanged::MOVE_TO_BACKGROUND); |
| 147 | } |
| 148 | |
| 149 | AtomMatcher CreateMoveToForegroundAtomMatcher() { |
| 150 | return CreateActivityForegroundStateChangedAtomMatcher( |
| 151 | "MoveToForeground", ActivityForegroundStateChanged::MOVE_TO_FOREGROUND); |
| 152 | } |
| 153 | |
| 154 | AtomMatcher CreateProcessLifeCycleStateChangedAtomMatcher( |
| 155 | const string& name, ProcessLifeCycleStateChanged::Event event) { |
| 156 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 157 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 158 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 159 | simple_atom_matcher->set_atom_id(android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
| 160 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 161 | field_value_matcher->set_field(3); // Process state field. |
| 162 | field_value_matcher->set_eq_int(event); |
| 163 | return atom_matcher; |
| 164 | } |
| 165 | |
| 166 | AtomMatcher CreateProcessCrashAtomMatcher() { |
| 167 | return CreateProcessLifeCycleStateChangedAtomMatcher( |
| 168 | "ProcessCrashed", ProcessLifeCycleStateChanged::PROCESS_CRASHED); |
| 169 | } |
| 170 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 171 | Predicate CreateBatterySaverModePredicate() { |
| 172 | Predicate predicate; |
| 173 | predicate.set_id(StringToId("BatterySaverIsOn")); |
| 174 | predicate.mutable_simple_predicate()->set_start(StringToId("BatterySaverModeStart")); |
| 175 | predicate.mutable_simple_predicate()->set_stop(StringToId("BatterySaverModeStop")); |
| 176 | return predicate; |
| 177 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 178 | |
| 179 | Predicate CreateScreenIsOnPredicate() { |
| 180 | Predicate predicate; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 181 | predicate.set_id(StringToId("ScreenIsOn")); |
| 182 | predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOn")); |
| 183 | predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOff")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 184 | return predicate; |
| 185 | } |
| 186 | |
| 187 | Predicate CreateScreenIsOffPredicate() { |
| 188 | Predicate predicate; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 189 | predicate.set_id(1111123); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 190 | predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOff")); |
| 191 | predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOn")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 192 | return predicate; |
| 193 | } |
| 194 | |
| 195 | Predicate CreateHoldingWakelockPredicate() { |
| 196 | Predicate predicate; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 197 | predicate.set_id(StringToId("HoldingWakelock")); |
| 198 | predicate.mutable_simple_predicate()->set_start(StringToId("AcquireWakelock")); |
| 199 | predicate.mutable_simple_predicate()->set_stop(StringToId("ReleaseWakelock")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 200 | return predicate; |
| 201 | } |
| 202 | |
| 203 | Predicate CreateIsSyncingPredicate() { |
| 204 | Predicate predicate; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 205 | predicate.set_id(33333333333333); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 206 | predicate.mutable_simple_predicate()->set_start(StringToId("SyncStart")); |
| 207 | predicate.mutable_simple_predicate()->set_stop(StringToId("SyncEnd")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 208 | return predicate; |
| 209 | } |
| 210 | |
| 211 | Predicate CreateIsInBackgroundPredicate() { |
| 212 | Predicate predicate; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 213 | predicate.set_id(StringToId("IsInBackground")); |
| 214 | predicate.mutable_simple_predicate()->set_start(StringToId("MoveToBackground")); |
| 215 | predicate.mutable_simple_predicate()->set_stop(StringToId("MoveToForeground")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 216 | return predicate; |
| 217 | } |
| 218 | |
| 219 | void addPredicateToPredicateCombination(const Predicate& predicate, |
| 220 | Predicate* combinationPredicate) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 221 | combinationPredicate->mutable_combination()->add_predicate(predicate.id()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | FieldMatcher CreateAttributionUidDimensions(const int atomId, |
| 225 | const std::vector<Position>& positions) { |
| 226 | FieldMatcher dimensions; |
| 227 | dimensions.set_field(atomId); |
| 228 | for (const auto position : positions) { |
| 229 | auto child = dimensions.add_child(); |
| 230 | child->set_field(1); |
| 231 | child->set_position(position); |
| 232 | child->add_child()->set_field(1); |
| 233 | } |
| 234 | return dimensions; |
| 235 | } |
| 236 | |
| 237 | FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId, |
| 238 | const std::vector<Position>& positions) { |
| 239 | FieldMatcher dimensions; |
| 240 | dimensions.set_field(atomId); |
| 241 | for (const auto position : positions) { |
| 242 | auto child = dimensions.add_child(); |
| 243 | child->set_field(1); |
| 244 | child->set_position(position); |
| 245 | child->add_child()->set_field(1); |
| 246 | child->add_child()->set_field(2); |
| 247 | } |
| 248 | return dimensions; |
| 249 | } |
| 250 | |
| 251 | FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields) { |
| 252 | FieldMatcher dimensions; |
| 253 | dimensions.set_field(atomId); |
| 254 | for (const int field : fields) { |
| 255 | dimensions.add_child()->set_field(field); |
| 256 | } |
| 257 | return dimensions; |
| 258 | } |
| 259 | |
| 260 | std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 261 | const android::view::DisplayStateEnum state, uint64_t timestampNs) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 262 | auto event = std::make_unique<LogEvent>(android::util::SCREEN_STATE_CHANGED, timestampNs); |
| 263 | EXPECT_TRUE(event->write(state)); |
| 264 | event->init(); |
| 265 | return event; |
| 266 | } |
| 267 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 268 | std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs) { |
| 269 | auto event = std::make_unique<LogEvent>( |
| 270 | android::util::BATTERY_SAVER_MODE_STATE_CHANGED, timestampNs); |
| 271 | EXPECT_TRUE(event->write(BatterySaverModeStateChanged::ON)); |
| 272 | event->init(); |
| 273 | return event; |
| 274 | } |
| 275 | |
| 276 | std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs) { |
| 277 | auto event = std::make_unique<LogEvent>( |
| 278 | android::util::BATTERY_SAVER_MODE_STATE_CHANGED, timestampNs); |
| 279 | EXPECT_TRUE(event->write(BatterySaverModeStateChanged::OFF)); |
| 280 | event->init(); |
| 281 | return event; |
| 282 | } |
| 283 | |
| 284 | std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent( |
| 285 | int level, uint64_t timestampNs) { |
| 286 | auto event = std::make_unique<LogEvent>(android::util::SCREEN_BRIGHTNESS_CHANGED, timestampNs); |
| 287 | EXPECT_TRUE(event->write(level)); |
| 288 | event->init(); |
| 289 | return event; |
| 290 | |
| 291 | } |
| 292 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 293 | std::unique_ptr<LogEvent> CreateWakelockStateChangedEvent( |
| 294 | const std::vector<AttributionNode>& attributions, const string& wakelockName, |
| 295 | const WakelockStateChanged::State state, uint64_t timestampNs) { |
| 296 | auto event = std::make_unique<LogEvent>(android::util::WAKELOCK_STATE_CHANGED, timestampNs); |
| 297 | event->write(attributions); |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 298 | event->write(android::os::WakeLockLevelEnum::PARTIAL_WAKE_LOCK); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 299 | event->write(wakelockName); |
| 300 | event->write(state); |
| 301 | event->init(); |
| 302 | return event; |
| 303 | } |
| 304 | |
| 305 | std::unique_ptr<LogEvent> CreateAcquireWakelockEvent( |
| 306 | const std::vector<AttributionNode>& attributions, |
| 307 | const string& wakelockName, uint64_t timestampNs) { |
| 308 | return CreateWakelockStateChangedEvent( |
| 309 | attributions, wakelockName, WakelockStateChanged::ACQUIRE, timestampNs); |
| 310 | } |
| 311 | |
| 312 | std::unique_ptr<LogEvent> CreateReleaseWakelockEvent( |
| 313 | const std::vector<AttributionNode>& attributions, |
| 314 | const string& wakelockName, uint64_t timestampNs) { |
| 315 | return CreateWakelockStateChangedEvent( |
| 316 | attributions, wakelockName, WakelockStateChanged::RELEASE, timestampNs); |
| 317 | } |
| 318 | |
| 319 | std::unique_ptr<LogEvent> CreateActivityForegroundStateChangedEvent( |
| 320 | const int uid, const ActivityForegroundStateChanged::Activity activity, uint64_t timestampNs) { |
| 321 | auto event = std::make_unique<LogEvent>( |
| 322 | android::util::ACTIVITY_FOREGROUND_STATE_CHANGED, timestampNs); |
| 323 | event->write(uid); |
| 324 | event->write("pkg_name"); |
| 325 | event->write("class_name"); |
| 326 | event->write(activity); |
| 327 | event->init(); |
| 328 | return event; |
| 329 | } |
| 330 | |
| 331 | std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(const int uid, uint64_t timestampNs) { |
| 332 | return CreateActivityForegroundStateChangedEvent( |
| 333 | uid, ActivityForegroundStateChanged::MOVE_TO_BACKGROUND, timestampNs); |
| 334 | } |
| 335 | |
| 336 | std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(const int uid, uint64_t timestampNs) { |
| 337 | return CreateActivityForegroundStateChangedEvent( |
| 338 | uid, ActivityForegroundStateChanged::MOVE_TO_FOREGROUND, timestampNs); |
| 339 | } |
| 340 | |
| 341 | std::unique_ptr<LogEvent> CreateSyncStateChangedEvent( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 342 | const std::vector<AttributionNode>& attributions, |
| 343 | const string& name, const SyncStateChanged::State state, uint64_t timestampNs) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 344 | auto event = std::make_unique<LogEvent>(android::util::SYNC_STATE_CHANGED, timestampNs); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 345 | event->write(attributions); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 346 | event->write(name); |
| 347 | event->write(state); |
| 348 | event->init(); |
| 349 | return event; |
| 350 | } |
| 351 | |
| 352 | std::unique_ptr<LogEvent> CreateSyncStartEvent( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 353 | const std::vector<AttributionNode>& attributions, const string& name, uint64_t timestampNs){ |
| 354 | return CreateSyncStateChangedEvent(attributions, name, SyncStateChanged::ON, timestampNs); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | std::unique_ptr<LogEvent> CreateSyncEndEvent( |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 358 | const std::vector<AttributionNode>& attributions, const string& name, uint64_t timestampNs) { |
| 359 | return CreateSyncStateChangedEvent(attributions, name, SyncStateChanged::OFF, timestampNs); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | std::unique_ptr<LogEvent> CreateProcessLifeCycleStateChangedEvent( |
| 363 | const int uid, const ProcessLifeCycleStateChanged::Event event, uint64_t timestampNs) { |
| 364 | auto logEvent = std::make_unique<LogEvent>( |
| 365 | android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED, timestampNs); |
| 366 | logEvent->write(uid); |
| 367 | logEvent->write(""); |
| 368 | logEvent->write(event); |
| 369 | logEvent->init(); |
| 370 | return logEvent; |
| 371 | } |
| 372 | |
| 373 | std::unique_ptr<LogEvent> CreateAppCrashEvent(const int uid, uint64_t timestampNs) { |
| 374 | return CreateProcessLifeCycleStateChangedEvent( |
| 375 | uid, ProcessLifeCycleStateChanged::PROCESS_CRASHED, timestampNs); |
| 376 | } |
| 377 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 378 | std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent( |
| 379 | int isolatedUid, int hostUid, bool is_create, uint64_t timestampNs) { |
| 380 | auto logEvent = std::make_unique<LogEvent>( |
| 381 | android::util::ISOLATED_UID_CHANGED, timestampNs); |
| 382 | logEvent->write(hostUid); |
| 383 | logEvent->write(isolatedUid); |
| 384 | logEvent->write(is_create); |
| 385 | logEvent->init(); |
| 386 | return logEvent; |
| 387 | } |
| 388 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 389 | sp<StatsLogProcessor> CreateStatsLogProcessor(const long timeBaseSec, const StatsdConfig& config, |
| 390 | const ConfigKey& key) { |
| 391 | sp<UidMap> uidMap = new UidMap(); |
| 392 | sp<AnomalyMonitor> anomalyMonitor = new AnomalyMonitor(10); // 10 seconds |
| 393 | sp<StatsLogProcessor> processor = new StatsLogProcessor( |
| 394 | uidMap, anomalyMonitor, timeBaseSec, [](const ConfigKey&){}); |
| 395 | processor->OnConfigUpdated(key, config); |
| 396 | return processor; |
| 397 | } |
| 398 | |
| 399 | AttributionNode CreateAttribution(const int& uid, const string& tag) { |
| 400 | AttributionNode attribution; |
| 401 | attribution.set_uid(uid); |
| 402 | attribution.set_tag(tag); |
| 403 | return attribution; |
| 404 | } |
| 405 | |
| 406 | void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events) { |
| 407 | std::sort(events->begin(), events->end(), |
| 408 | [](const std::unique_ptr<LogEvent>& a, const std::unique_ptr<LogEvent>& b) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 409 | return a->GetElapsedTimestampNs() < b->GetElapsedTimestampNs(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 410 | }); |
| 411 | } |
| 412 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 413 | int64_t StringToId(const string& str) { |
| 414 | return static_cast<int64_t>(std::hash<std::string>()(str)); |
| 415 | } |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 416 | |
| 417 | void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid) { |
| 418 | EXPECT_EQ(value.field(), atomId); |
| 419 | EXPECT_EQ(value.value_tuple().dimensions_value_size(), 1); |
| 420 | // Attribution field. |
| 421 | EXPECT_EQ(value.value_tuple().dimensions_value(0).field(), 1); |
| 422 | // Uid only. |
| 423 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 424 | .value_tuple().dimensions_value_size(), 1); |
| 425 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 426 | .value_tuple().dimensions_value(0).field(), 1); |
| 427 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 428 | .value_tuple().dimensions_value(0).value_int(), uid); |
| 429 | } |
| 430 | |
| 431 | void ValidateUidDimension(const DimensionsValue& value, int atomId, int uid) { |
| 432 | EXPECT_EQ(value.field(), atomId); |
| 433 | EXPECT_EQ(value.value_tuple().dimensions_value_size(), 1); |
| 434 | // Attribution field. |
| 435 | EXPECT_EQ(value.value_tuple().dimensions_value(0).field(), 1); |
| 436 | // Uid only. |
| 437 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 438 | .value_tuple().dimensions_value_size(), 1); |
| 439 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 440 | .value_tuple().dimensions_value(0).field(), 1); |
| 441 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 442 | .value_tuple().dimensions_value(0).value_int(), uid); |
| 443 | } |
| 444 | |
| 445 | void ValidateAttributionUidAndTagDimension( |
| 446 | const DimensionsValue& value, int atomId, int uid, const std::string& tag) { |
| 447 | EXPECT_EQ(value.field(), atomId); |
| 448 | EXPECT_EQ(value.value_tuple().dimensions_value_size(), 1); |
| 449 | // Attribution field. |
| 450 | EXPECT_EQ(value.value_tuple().dimensions_value(0).field(), 1); |
| 451 | // Uid only. |
| 452 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 453 | .value_tuple().dimensions_value_size(), 2); |
| 454 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 455 | .value_tuple().dimensions_value(0).field(), 1); |
| 456 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 457 | .value_tuple().dimensions_value(0).value_int(), uid); |
| 458 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 459 | .value_tuple().dimensions_value(1).field(), 2); |
| 460 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 461 | .value_tuple().dimensions_value(1).value_str(), tag); |
| 462 | } |
| 463 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 464 | bool EqualsTo(const DimensionsValue& s1, const DimensionsValue& s2) { |
| 465 | if (s1.field() != s2.field()) { |
| 466 | return false; |
| 467 | } |
| 468 | if (s1.value_case() != s2.value_case()) { |
| 469 | return false; |
| 470 | } |
| 471 | switch (s1.value_case()) { |
| 472 | case DimensionsValue::ValueCase::kValueStr: |
| 473 | return (s1.value_str() == s2.value_str()); |
| 474 | case DimensionsValue::ValueCase::kValueInt: |
| 475 | return s1.value_int() == s2.value_int(); |
| 476 | case DimensionsValue::ValueCase::kValueLong: |
| 477 | return s1.value_long() == s2.value_long(); |
| 478 | case DimensionsValue::ValueCase::kValueBool: |
| 479 | return s1.value_bool() == s2.value_bool(); |
| 480 | case DimensionsValue::ValueCase::kValueFloat: |
| 481 | return s1.value_float() == s2.value_float(); |
| 482 | case DimensionsValue::ValueCase::kValueTuple: { |
| 483 | if (s1.value_tuple().dimensions_value_size() != |
| 484 | s2.value_tuple().dimensions_value_size()) { |
| 485 | return false; |
| 486 | } |
| 487 | bool allMatched = true; |
| 488 | for (int i = 0; allMatched && i < s1.value_tuple().dimensions_value_size(); ++i) { |
| 489 | allMatched &= EqualsTo(s1.value_tuple().dimensions_value(i), |
| 490 | s2.value_tuple().dimensions_value(i)); |
| 491 | } |
| 492 | return allMatched; |
| 493 | } |
| 494 | case DimensionsValue::ValueCase::VALUE_NOT_SET: |
| 495 | default: |
| 496 | return true; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2) { |
| 501 | if (s1.field() != s2.field()) { |
| 502 | return s1.field() < s2.field(); |
| 503 | } |
| 504 | if (s1.value_case() != s2.value_case()) { |
| 505 | return s1.value_case() < s2.value_case(); |
| 506 | } |
| 507 | switch (s1.value_case()) { |
| 508 | case DimensionsValue::ValueCase::kValueStr: |
| 509 | return s1.value_str() < s2.value_str(); |
| 510 | case DimensionsValue::ValueCase::kValueInt: |
| 511 | return s1.value_int() < s2.value_int(); |
| 512 | case DimensionsValue::ValueCase::kValueLong: |
| 513 | return s1.value_long() < s2.value_long(); |
| 514 | case DimensionsValue::ValueCase::kValueBool: |
| 515 | return (int)s1.value_bool() < (int)s2.value_bool(); |
| 516 | case DimensionsValue::ValueCase::kValueFloat: |
| 517 | return s1.value_float() < s2.value_float(); |
| 518 | case DimensionsValue::ValueCase::kValueTuple: { |
| 519 | if (s1.value_tuple().dimensions_value_size() != |
| 520 | s2.value_tuple().dimensions_value_size()) { |
| 521 | return s1.value_tuple().dimensions_value_size() < |
| 522 | s2.value_tuple().dimensions_value_size(); |
| 523 | } |
| 524 | for (int i = 0; i < s1.value_tuple().dimensions_value_size(); ++i) { |
| 525 | if (EqualsTo(s1.value_tuple().dimensions_value(i), |
| 526 | s2.value_tuple().dimensions_value(i))) { |
| 527 | continue; |
| 528 | } else { |
| 529 | return LessThan(s1.value_tuple().dimensions_value(i), |
| 530 | s2.value_tuple().dimensions_value(i)); |
| 531 | } |
| 532 | } |
| 533 | return false; |
| 534 | } |
| 535 | case DimensionsValue::ValueCase::VALUE_NOT_SET: |
| 536 | default: |
| 537 | return false; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2) { |
| 542 | if (LessThan(s1.dimInWhat, s2.dimInWhat)) { |
| 543 | return true; |
| 544 | } else if (LessThan(s2.dimInWhat, s1.dimInWhat)) { |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | return LessThan(s1.dimInCondition, s2.dimInCondition); |
| 549 | } |
| 550 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 551 | } // namespace statsd |
| 552 | } // namespace os |
| 553 | } // namespace android |