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 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 17 | #include <aidl/android/util/StatsEventParcel.h> |
Ruchir Rastogi | dfd63d4 | 2020-02-20 17:54:13 -0800 | [diff] [blame] | 18 | #include "stats_event.h" |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 19 | |
| 20 | using aidl::android::util::StatsEventParcel; |
| 21 | using std::shared_ptr; |
| 22 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | namespace os { |
| 25 | namespace statsd { |
| 26 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 27 | StatsLogReport outputStreamToProto(ProtoOutputStream* proto) { |
| 28 | vector<uint8_t> bytes; |
| 29 | bytes.resize(proto->size()); |
| 30 | size_t pos = 0; |
| 31 | sp<ProtoReader> reader = proto->data(); |
| 32 | |
| 33 | while (reader->readBuffer() != NULL) { |
| 34 | size_t toRead = reader->currentToRead(); |
| 35 | std::memcpy(&((bytes)[pos]), reader->readBuffer(), toRead); |
| 36 | pos += toRead; |
| 37 | reader->move(toRead); |
| 38 | } |
| 39 | |
| 40 | StatsLogReport report; |
| 41 | report.ParseFromArray(bytes.data(), bytes.size()); |
| 42 | return report; |
| 43 | } |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 44 | |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 45 | AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId) { |
| 46 | AtomMatcher atom_matcher; |
| 47 | atom_matcher.set_id(StringToId(name)); |
| 48 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 49 | simple_atom_matcher->set_atom_id(atomId); |
| 50 | return atom_matcher; |
| 51 | } |
| 52 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 53 | AtomMatcher CreateTemperatureAtomMatcher() { |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 54 | return CreateSimpleAtomMatcher("TemperatureMatcher", util::TEMPERATURE); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 57 | AtomMatcher CreateScheduledJobStateChangedAtomMatcher(const string& name, |
| 58 | ScheduledJobStateChanged::State state) { |
| 59 | AtomMatcher atom_matcher; |
| 60 | atom_matcher.set_id(StringToId(name)); |
| 61 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 62 | simple_atom_matcher->set_atom_id(util::SCHEDULED_JOB_STATE_CHANGED); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 63 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 64 | field_value_matcher->set_field(3); // State field. |
| 65 | field_value_matcher->set_eq_int(state); |
| 66 | return atom_matcher; |
| 67 | } |
| 68 | |
| 69 | AtomMatcher CreateStartScheduledJobAtomMatcher() { |
| 70 | return CreateScheduledJobStateChangedAtomMatcher("ScheduledJobStart", |
| 71 | ScheduledJobStateChanged::STARTED); |
| 72 | } |
| 73 | |
| 74 | AtomMatcher CreateFinishScheduledJobAtomMatcher() { |
| 75 | return CreateScheduledJobStateChangedAtomMatcher("ScheduledJobFinish", |
| 76 | ScheduledJobStateChanged::FINISHED); |
| 77 | } |
| 78 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 79 | AtomMatcher CreateScreenBrightnessChangedAtomMatcher() { |
| 80 | AtomMatcher atom_matcher; |
| 81 | atom_matcher.set_id(StringToId("ScreenBrightnessChanged")); |
| 82 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 83 | simple_atom_matcher->set_atom_id(util::SCREEN_BRIGHTNESS_CHANGED); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 84 | return atom_matcher; |
| 85 | } |
| 86 | |
| 87 | AtomMatcher CreateUidProcessStateChangedAtomMatcher() { |
| 88 | AtomMatcher atom_matcher; |
| 89 | atom_matcher.set_id(StringToId("UidProcessStateChanged")); |
| 90 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 91 | simple_atom_matcher->set_atom_id(util::UID_PROCESS_STATE_CHANGED); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 92 | return atom_matcher; |
| 93 | } |
| 94 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 95 | AtomMatcher CreateWakelockStateChangedAtomMatcher(const string& name, |
| 96 | WakelockStateChanged::State state) { |
| 97 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 98 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 99 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 100 | simple_atom_matcher->set_atom_id(util::WAKELOCK_STATE_CHANGED); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 101 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 102 | field_value_matcher->set_field(4); // State field. |
| 103 | field_value_matcher->set_eq_int(state); |
| 104 | return atom_matcher; |
| 105 | } |
| 106 | |
| 107 | AtomMatcher CreateAcquireWakelockAtomMatcher() { |
| 108 | return CreateWakelockStateChangedAtomMatcher("AcquireWakelock", WakelockStateChanged::ACQUIRE); |
| 109 | } |
| 110 | |
| 111 | AtomMatcher CreateReleaseWakelockAtomMatcher() { |
| 112 | return CreateWakelockStateChangedAtomMatcher("ReleaseWakelock", WakelockStateChanged::RELEASE); |
| 113 | } |
| 114 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 115 | AtomMatcher CreateBatterySaverModeStateChangedAtomMatcher( |
| 116 | const string& name, BatterySaverModeStateChanged::State state) { |
| 117 | AtomMatcher atom_matcher; |
| 118 | atom_matcher.set_id(StringToId(name)); |
| 119 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 120 | simple_atom_matcher->set_atom_id(util::BATTERY_SAVER_MODE_STATE_CHANGED); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 121 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 122 | field_value_matcher->set_field(1); // State field. |
| 123 | field_value_matcher->set_eq_int(state); |
| 124 | return atom_matcher; |
| 125 | } |
| 126 | |
| 127 | AtomMatcher CreateBatterySaverModeStartAtomMatcher() { |
| 128 | return CreateBatterySaverModeStateChangedAtomMatcher( |
| 129 | "BatterySaverModeStart", BatterySaverModeStateChanged::ON); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | AtomMatcher CreateBatterySaverModeStopAtomMatcher() { |
| 134 | return CreateBatterySaverModeStateChangedAtomMatcher( |
| 135 | "BatterySaverModeStop", BatterySaverModeStateChanged::OFF); |
| 136 | } |
| 137 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 138 | AtomMatcher CreateBatteryStateChangedAtomMatcher(const string& name, |
| 139 | BatteryPluggedStateEnum state) { |
| 140 | AtomMatcher atom_matcher; |
| 141 | atom_matcher.set_id(StringToId(name)); |
| 142 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
| 143 | simple_atom_matcher->set_atom_id(util::PLUGGED_STATE_CHANGED); |
| 144 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 145 | field_value_matcher->set_field(1); // State field. |
| 146 | field_value_matcher->set_eq_int(state); |
| 147 | return atom_matcher; |
| 148 | } |
| 149 | |
| 150 | AtomMatcher CreateBatteryStateNoneMatcher() { |
| 151 | return CreateBatteryStateChangedAtomMatcher("BatteryPluggedNone", |
| 152 | BatteryPluggedStateEnum::BATTERY_PLUGGED_NONE); |
| 153 | } |
| 154 | |
| 155 | AtomMatcher CreateBatteryStateUsbMatcher() { |
| 156 | return CreateBatteryStateChangedAtomMatcher("BatteryPluggedUsb", |
| 157 | BatteryPluggedStateEnum::BATTERY_PLUGGED_USB); |
| 158 | } |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 159 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 160 | AtomMatcher CreateScreenStateChangedAtomMatcher( |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 161 | const string& name, android::view::DisplayStateEnum state) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 162 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 163 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 164 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 165 | simple_atom_matcher->set_atom_id(util::SCREEN_STATE_CHANGED); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 166 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 167 | field_value_matcher->set_field(1); // State field. |
| 168 | field_value_matcher->set_eq_int(state); |
| 169 | return atom_matcher; |
| 170 | } |
| 171 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 172 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 173 | AtomMatcher CreateScreenTurnedOnAtomMatcher() { |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 174 | return CreateScreenStateChangedAtomMatcher("ScreenTurnedOn", |
| 175 | android::view::DisplayStateEnum::DISPLAY_STATE_ON); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | AtomMatcher CreateScreenTurnedOffAtomMatcher() { |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 179 | return CreateScreenStateChangedAtomMatcher("ScreenTurnedOff", |
| 180 | ::android::view::DisplayStateEnum::DISPLAY_STATE_OFF); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | AtomMatcher CreateSyncStateChangedAtomMatcher( |
| 184 | const string& name, SyncStateChanged::State state) { |
| 185 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 186 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 187 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 188 | simple_atom_matcher->set_atom_id(util::SYNC_STATE_CHANGED); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 189 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 190 | field_value_matcher->set_field(3); // State field. |
| 191 | field_value_matcher->set_eq_int(state); |
| 192 | return atom_matcher; |
| 193 | } |
| 194 | |
| 195 | AtomMatcher CreateSyncStartAtomMatcher() { |
| 196 | return CreateSyncStateChangedAtomMatcher("SyncStart", SyncStateChanged::ON); |
| 197 | } |
| 198 | |
| 199 | AtomMatcher CreateSyncEndAtomMatcher() { |
| 200 | return CreateSyncStateChangedAtomMatcher("SyncEnd", SyncStateChanged::OFF); |
| 201 | } |
| 202 | |
| 203 | AtomMatcher CreateActivityForegroundStateChangedAtomMatcher( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 204 | const string& name, ActivityForegroundStateChanged::State state) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 205 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 206 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 207 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 208 | simple_atom_matcher->set_atom_id(util::ACTIVITY_FOREGROUND_STATE_CHANGED); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 209 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 210 | field_value_matcher->set_field(4); // Activity field. |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 211 | field_value_matcher->set_eq_int(state); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 212 | return atom_matcher; |
| 213 | } |
| 214 | |
| 215 | AtomMatcher CreateMoveToBackgroundAtomMatcher() { |
| 216 | return CreateActivityForegroundStateChangedAtomMatcher( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 217 | "Background", ActivityForegroundStateChanged::BACKGROUND); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | AtomMatcher CreateMoveToForegroundAtomMatcher() { |
| 221 | return CreateActivityForegroundStateChangedAtomMatcher( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 222 | "Foreground", ActivityForegroundStateChanged::FOREGROUND); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | AtomMatcher CreateProcessLifeCycleStateChangedAtomMatcher( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 226 | const string& name, ProcessLifeCycleStateChanged::State state) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 227 | AtomMatcher atom_matcher; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 228 | atom_matcher.set_id(StringToId(name)); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 229 | auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 230 | simple_atom_matcher->set_atom_id(util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 231 | auto field_value_matcher = simple_atom_matcher->add_field_value_matcher(); |
| 232 | field_value_matcher->set_field(3); // Process state field. |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 233 | field_value_matcher->set_eq_int(state); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 234 | return atom_matcher; |
| 235 | } |
| 236 | |
| 237 | AtomMatcher CreateProcessCrashAtomMatcher() { |
| 238 | return CreateProcessLifeCycleStateChangedAtomMatcher( |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 239 | "Crashed", ProcessLifeCycleStateChanged::CRASHED); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 242 | Predicate CreateScheduledJobPredicate() { |
| 243 | Predicate predicate; |
| 244 | predicate.set_id(StringToId("ScheduledJobRunningPredicate")); |
| 245 | predicate.mutable_simple_predicate()->set_start(StringToId("ScheduledJobStart")); |
| 246 | predicate.mutable_simple_predicate()->set_stop(StringToId("ScheduledJobFinish")); |
| 247 | return predicate; |
| 248 | } |
| 249 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 250 | Predicate CreateBatterySaverModePredicate() { |
| 251 | Predicate predicate; |
| 252 | predicate.set_id(StringToId("BatterySaverIsOn")); |
| 253 | predicate.mutable_simple_predicate()->set_start(StringToId("BatterySaverModeStart")); |
| 254 | predicate.mutable_simple_predicate()->set_stop(StringToId("BatterySaverModeStop")); |
| 255 | return predicate; |
| 256 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 257 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 258 | Predicate CreateDeviceUnpluggedPredicate() { |
| 259 | Predicate predicate; |
| 260 | predicate.set_id(StringToId("DeviceUnplugged")); |
| 261 | predicate.mutable_simple_predicate()->set_start(StringToId("BatteryPluggedNone")); |
| 262 | predicate.mutable_simple_predicate()->set_stop(StringToId("BatteryPluggedUsb")); |
| 263 | return predicate; |
| 264 | } |
| 265 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 266 | Predicate CreateScreenIsOnPredicate() { |
| 267 | Predicate predicate; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 268 | predicate.set_id(StringToId("ScreenIsOn")); |
| 269 | predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOn")); |
| 270 | predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOff")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 271 | return predicate; |
| 272 | } |
| 273 | |
| 274 | Predicate CreateScreenIsOffPredicate() { |
| 275 | Predicate predicate; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 276 | predicate.set_id(1111123); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 277 | predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOff")); |
| 278 | predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOn")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 279 | return predicate; |
| 280 | } |
| 281 | |
| 282 | Predicate CreateHoldingWakelockPredicate() { |
| 283 | Predicate predicate; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 284 | predicate.set_id(StringToId("HoldingWakelock")); |
| 285 | predicate.mutable_simple_predicate()->set_start(StringToId("AcquireWakelock")); |
| 286 | predicate.mutable_simple_predicate()->set_stop(StringToId("ReleaseWakelock")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 287 | return predicate; |
| 288 | } |
| 289 | |
| 290 | Predicate CreateIsSyncingPredicate() { |
| 291 | Predicate predicate; |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 292 | predicate.set_id(33333333333333); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 293 | predicate.mutable_simple_predicate()->set_start(StringToId("SyncStart")); |
| 294 | predicate.mutable_simple_predicate()->set_stop(StringToId("SyncEnd")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 295 | return predicate; |
| 296 | } |
| 297 | |
| 298 | Predicate CreateIsInBackgroundPredicate() { |
| 299 | Predicate predicate; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 300 | predicate.set_id(StringToId("IsInBackground")); |
Chenjie Yu | 5caaa9d | 2018-03-06 15:48:54 -0800 | [diff] [blame] | 301 | predicate.mutable_simple_predicate()->set_start(StringToId("Background")); |
| 302 | predicate.mutable_simple_predicate()->set_stop(StringToId("Foreground")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 303 | return predicate; |
| 304 | } |
| 305 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 306 | State CreateScreenState() { |
| 307 | State state; |
| 308 | state.set_id(StringToId("ScreenState")); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 309 | state.set_atom_id(util::SCREEN_STATE_CHANGED); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 310 | return state; |
| 311 | } |
| 312 | |
| 313 | State CreateUidProcessState() { |
| 314 | State state; |
| 315 | state.set_id(StringToId("UidProcessState")); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 316 | state.set_atom_id(util::UID_PROCESS_STATE_CHANGED); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 317 | return state; |
| 318 | } |
| 319 | |
| 320 | State CreateOverlayState() { |
| 321 | State state; |
| 322 | state.set_id(StringToId("OverlayState")); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 323 | state.set_atom_id(util::OVERLAY_STATE_CHANGED); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 324 | return state; |
| 325 | } |
| 326 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 327 | State CreateScreenStateWithOnOffMap(int64_t screenOnId, int64_t screenOffId) { |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 328 | State state; |
| 329 | state.set_id(StringToId("ScreenStateOnOff")); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 330 | state.set_atom_id(util::SCREEN_STATE_CHANGED); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 331 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 332 | auto map = CreateScreenStateOnOffMap(screenOnId, screenOffId); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 333 | *state.mutable_map() = map; |
| 334 | |
| 335 | return state; |
| 336 | } |
| 337 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 338 | StateMap_StateGroup CreateScreenStateOnGroup(int64_t screenOnId) { |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 339 | StateMap_StateGroup group; |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 340 | group.set_group_id(screenOnId); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 341 | group.add_value(2); |
| 342 | group.add_value(5); |
| 343 | group.add_value(6); |
| 344 | return group; |
| 345 | } |
| 346 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 347 | StateMap_StateGroup CreateScreenStateOffGroup(int64_t screenOffId) { |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 348 | StateMap_StateGroup group; |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 349 | group.set_group_id(screenOffId); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 350 | group.add_value(0); |
| 351 | group.add_value(1); |
| 352 | group.add_value(3); |
| 353 | group.add_value(4); |
| 354 | return group; |
| 355 | } |
| 356 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 357 | StateMap CreateScreenStateOnOffMap(int64_t screenOnId, int64_t screenOffId) { |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 358 | StateMap map; |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 359 | *map.add_group() = CreateScreenStateOnGroup(screenOnId); |
| 360 | *map.add_group() = CreateScreenStateOffGroup(screenOffId); |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 361 | return map; |
| 362 | } |
| 363 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 364 | void addPredicateToPredicateCombination(const Predicate& predicate, |
| 365 | Predicate* combinationPredicate) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 366 | combinationPredicate->mutable_combination()->add_predicate(predicate.id()); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | FieldMatcher CreateAttributionUidDimensions(const int atomId, |
| 370 | const std::vector<Position>& positions) { |
| 371 | FieldMatcher dimensions; |
| 372 | dimensions.set_field(atomId); |
| 373 | for (const auto position : positions) { |
| 374 | auto child = dimensions.add_child(); |
| 375 | child->set_field(1); |
| 376 | child->set_position(position); |
| 377 | child->add_child()->set_field(1); |
| 378 | } |
| 379 | return dimensions; |
| 380 | } |
| 381 | |
| 382 | FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId, |
| 383 | const std::vector<Position>& positions) { |
| 384 | FieldMatcher dimensions; |
| 385 | dimensions.set_field(atomId); |
| 386 | for (const auto position : positions) { |
| 387 | auto child = dimensions.add_child(); |
| 388 | child->set_field(1); |
| 389 | child->set_position(position); |
| 390 | child->add_child()->set_field(1); |
| 391 | child->add_child()->set_field(2); |
| 392 | } |
| 393 | return dimensions; |
| 394 | } |
| 395 | |
| 396 | FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields) { |
| 397 | FieldMatcher dimensions; |
| 398 | dimensions.set_field(atomId); |
| 399 | for (const int field : fields) { |
| 400 | dimensions.add_child()->set_field(field); |
| 401 | } |
| 402 | return dimensions; |
| 403 | } |
| 404 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 405 | FieldMatcher CreateAttributionUidAndOtherDimensions(const int atomId, |
| 406 | const std::vector<Position>& positions, |
| 407 | const std::vector<int>& fields) { |
| 408 | FieldMatcher dimensions = CreateAttributionUidDimensions(atomId, positions); |
| 409 | |
| 410 | for (const int field : fields) { |
| 411 | dimensions.add_child()->set_field(field); |
| 412 | } |
| 413 | return dimensions; |
| 414 | } |
| 415 | |
| 416 | // START: get primary key functions |
| 417 | void getUidProcessKey(int uid, HashableDimensionKey* key) { |
| 418 | int pos1[] = {1, 0, 0}; |
| 419 | Field field1(27 /* atom id */, pos1, 0 /* depth */); |
| 420 | Value value1((int32_t)uid); |
| 421 | |
| 422 | key->addValue(FieldValue(field1, value1)); |
| 423 | } |
| 424 | |
| 425 | void getOverlayKey(int uid, string packageName, HashableDimensionKey* key) { |
| 426 | int pos1[] = {1, 0, 0}; |
| 427 | int pos2[] = {2, 0, 0}; |
| 428 | |
| 429 | Field field1(59 /* atom id */, pos1, 0 /* depth */); |
| 430 | Field field2(59 /* atom id */, pos2, 0 /* depth */); |
| 431 | |
| 432 | Value value1((int32_t)uid); |
| 433 | Value value2(packageName); |
| 434 | |
| 435 | key->addValue(FieldValue(field1, value1)); |
| 436 | key->addValue(FieldValue(field2, value2)); |
| 437 | } |
| 438 | |
| 439 | void getPartialWakelockKey(int uid, const std::string& tag, HashableDimensionKey* key) { |
| 440 | int pos1[] = {1, 1, 1}; |
| 441 | int pos3[] = {2, 0, 0}; |
| 442 | int pos4[] = {3, 0, 0}; |
| 443 | |
| 444 | Field field1(10 /* atom id */, pos1, 2 /* depth */); |
| 445 | |
| 446 | Field field3(10 /* atom id */, pos3, 0 /* depth */); |
| 447 | Field field4(10 /* atom id */, pos4, 0 /* depth */); |
| 448 | |
| 449 | Value value1((int32_t)uid); |
| 450 | Value value3((int32_t)1 /*partial*/); |
| 451 | Value value4(tag); |
| 452 | |
| 453 | key->addValue(FieldValue(field1, value1)); |
| 454 | key->addValue(FieldValue(field3, value3)); |
| 455 | key->addValue(FieldValue(field4, value4)); |
| 456 | } |
| 457 | |
| 458 | void getPartialWakelockKey(int uid, HashableDimensionKey* key) { |
| 459 | int pos1[] = {1, 1, 1}; |
| 460 | int pos3[] = {2, 0, 0}; |
| 461 | |
| 462 | Field field1(10 /* atom id */, pos1, 2 /* depth */); |
| 463 | Field field3(10 /* atom id */, pos3, 0 /* depth */); |
| 464 | |
| 465 | Value value1((int32_t)uid); |
| 466 | Value value3((int32_t)1 /*partial*/); |
| 467 | |
| 468 | key->addValue(FieldValue(field1, value1)); |
| 469 | key->addValue(FieldValue(field3, value3)); |
| 470 | } |
| 471 | // END: get primary key functions |
| 472 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 473 | void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids, |
| 474 | const vector<string>& attributionTags) { |
| 475 | vector<const char*> cTags(attributionTags.size()); |
| 476 | for (int i = 0; i < cTags.size(); i++) { |
| 477 | cTags[i] = attributionTags[i].c_str(); |
| 478 | } |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 479 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 480 | AStatsEvent_writeAttributionChain(statsEvent, |
| 481 | reinterpret_cast<const uint32_t*>(attributionUids.data()), |
| 482 | cTags.data(), attributionUids.size()); |
| 483 | } |
| 484 | |
| 485 | void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent) { |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 486 | AStatsEvent_build(statsEvent); |
| 487 | |
| 488 | size_t size; |
| 489 | uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 490 | logEvent->parseBuffer(buf, size); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 491 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 492 | AStatsEvent_release(statsEvent); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 493 | } |
tsaichristine | d5800b2 | 2020-03-25 10:31:50 -0700 | [diff] [blame] | 494 | |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 495 | void CreateTwoValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1, |
| 496 | int32_t value2) { |
| 497 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 498 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 499 | AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); |
| 500 | |
| 501 | AStatsEvent_writeInt32(statsEvent, value1); |
| 502 | AStatsEvent_writeInt32(statsEvent, value2); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 503 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 504 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 505 | } |
| 506 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 507 | shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1, |
| 508 | int32_t value2) { |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 509 | shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 510 | CreateTwoValueLogEvent(logEvent.get(), atomId, eventTimeNs, value1, value2); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 511 | return logEvent; |
| 512 | } |
| 513 | |
| 514 | void CreateThreeValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1, |
| 515 | int32_t value2, int32_t value3) { |
| 516 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 517 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 518 | AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); |
| 519 | |
| 520 | AStatsEvent_writeInt32(statsEvent, value1); |
| 521 | AStatsEvent_writeInt32(statsEvent, value2); |
| 522 | AStatsEvent_writeInt32(statsEvent, value3); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 523 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 524 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 525 | } |
| 526 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 527 | shared_ptr<LogEvent> CreateThreeValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1, |
| 528 | int32_t value2, int32_t value3) { |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 529 | shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 530 | CreateThreeValueLogEvent(logEvent.get(), atomId, eventTimeNs, value1, value2, value3); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 531 | return logEvent; |
| 532 | } |
| 533 | |
| 534 | void CreateRepeatedValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, |
| 535 | int32_t value) { |
| 536 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 537 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 538 | AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); |
| 539 | |
| 540 | AStatsEvent_writeInt32(statsEvent, value); |
| 541 | AStatsEvent_writeInt32(statsEvent, value); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 542 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 543 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 544 | } |
| 545 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 546 | shared_ptr<LogEvent> CreateRepeatedValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value) { |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 547 | shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 548 | CreateRepeatedValueLogEvent(logEvent.get(), atomId, eventTimeNs, value); |
tsaichristine | bb33b80 | 2020-03-16 09:46:41 -0700 | [diff] [blame] | 549 | return logEvent; |
| 550 | } |
| 551 | |
tsaichristine | d5800b2 | 2020-03-25 10:31:50 -0700 | [diff] [blame] | 552 | void CreateNoValuesLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs) { |
| 553 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 554 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 555 | AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); |
tsaichristine | d5800b2 | 2020-03-25 10:31:50 -0700 | [diff] [blame] | 556 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 557 | parseStatsEventToLogEvent(statsEvent, logEvent); |
| 558 | } |
| 559 | |
| 560 | shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs) { |
| 561 | shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); |
| 562 | CreateNoValuesLogEvent(logEvent.get(), atomId, eventTimeNs); |
| 563 | return logEvent; |
tsaichristine | d5800b2 | 2020-03-25 10:31:50 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Muhammad Qureshi | b635b3a | 2020-04-22 20:57:41 -0700 | [diff] [blame] | 566 | shared_ptr<LogEvent> makeUidLogEvent(int atomId, int64_t eventTimeNs, int uid, int data1, |
| 567 | int data2) { |
| 568 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 569 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 570 | AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); |
| 571 | |
| 572 | AStatsEvent_writeInt32(statsEvent, uid); |
| 573 | AStatsEvent_addBoolAnnotation(statsEvent, ANNOTATION_ID_IS_UID, true); |
| 574 | AStatsEvent_writeInt32(statsEvent, data1); |
| 575 | AStatsEvent_writeInt32(statsEvent, data2); |
| 576 | |
| 577 | shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); |
| 578 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
| 579 | return logEvent; |
| 580 | } |
| 581 | |
| 582 | shared_ptr<LogEvent> makeAttributionLogEvent(int atomId, int64_t eventTimeNs, |
| 583 | const vector<int>& uids, const vector<string>& tags, |
| 584 | int data1, int data2) { |
| 585 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 586 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 587 | AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); |
| 588 | |
| 589 | writeAttribution(statsEvent, uids, tags); |
| 590 | AStatsEvent_writeInt32(statsEvent, data1); |
| 591 | AStatsEvent_writeInt32(statsEvent, data2); |
| 592 | |
| 593 | shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); |
| 594 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
| 595 | return logEvent; |
| 596 | } |
| 597 | |
| 598 | sp<MockUidMap> makeMockUidMapForOneHost(int hostUid, const vector<int>& isolatedUids) { |
| 599 | sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>(); |
| 600 | EXPECT_CALL(*uidMap, getHostUidOrSelf(_)).WillRepeatedly(ReturnArg<0>()); |
| 601 | for (const int isolatedUid : isolatedUids) { |
| 602 | EXPECT_CALL(*uidMap, getHostUidOrSelf(isolatedUid)).WillRepeatedly(Return(hostUid)); |
| 603 | } |
| 604 | |
| 605 | return uidMap; |
| 606 | } |
| 607 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 608 | std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( |
| 609 | uint64_t timestampNs, const android::view::DisplayStateEnum state) { |
| 610 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 611 | AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 612 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 613 | AStatsEvent_writeInt32(statsEvent, state); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 614 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true); |
| 615 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 616 | |
| 617 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 618 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 619 | return logEvent; |
| 620 | } |
| 621 | |
| 622 | std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs) { |
| 623 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 624 | AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 625 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 626 | AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::ON); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 627 | |
| 628 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 629 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 630 | return logEvent; |
| 631 | } |
| 632 | |
| 633 | std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs) { |
| 634 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 635 | AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 636 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 637 | AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::OFF); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 638 | |
| 639 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 640 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 641 | return logEvent; |
| 642 | } |
| 643 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 644 | std::unique_ptr<LogEvent> CreateBatteryStateChangedEvent(const uint64_t timestampNs, const BatteryPluggedStateEnum state) { |
| 645 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 646 | AStatsEvent_setAtomId(statsEvent, util::PLUGGED_STATE_CHANGED); |
| 647 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 648 | AStatsEvent_writeInt32(statsEvent, state); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 649 | |
| 650 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 651 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 652 | return logEvent; |
| 653 | } |
| 654 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 655 | std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(uint64_t timestampNs, int level) { |
| 656 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 657 | AStatsEvent_setAtomId(statsEvent, util::SCREEN_BRIGHTNESS_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 658 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 659 | AStatsEvent_writeInt32(statsEvent, level); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 660 | |
| 661 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 662 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 663 | return logEvent; |
| 664 | } |
| 665 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 666 | std::unique_ptr<LogEvent> CreateScheduledJobStateChangedEvent( |
| 667 | const vector<int>& attributionUids, const vector<string>& attributionTags, |
| 668 | const string& jobName, const ScheduledJobStateChanged::State state, uint64_t timestampNs) { |
| 669 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 670 | AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED); |
| 671 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 672 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 673 | writeAttribution(statsEvent, attributionUids, attributionTags); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 674 | AStatsEvent_writeString(statsEvent, jobName.c_str()); |
| 675 | AStatsEvent_writeInt32(statsEvent, state); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 676 | |
| 677 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 678 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 679 | return logEvent; |
| 680 | } |
| 681 | |
| 682 | std::unique_ptr<LogEvent> CreateStartScheduledJobEvent(uint64_t timestampNs, |
| 683 | const vector<int>& attributionUids, |
| 684 | const vector<string>& attributionTags, |
| 685 | const string& jobName) { |
| 686 | return CreateScheduledJobStateChangedEvent(attributionUids, attributionTags, jobName, |
| 687 | ScheduledJobStateChanged::STARTED, timestampNs); |
| 688 | } |
| 689 | |
| 690 | // Create log event when scheduled job finishes. |
| 691 | std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent(uint64_t timestampNs, |
| 692 | const vector<int>& attributionUids, |
| 693 | const vector<string>& attributionTags, |
| 694 | const string& jobName) { |
| 695 | return CreateScheduledJobStateChangedEvent(attributionUids, attributionTags, jobName, |
| 696 | ScheduledJobStateChanged::FINISHED, timestampNs); |
| 697 | } |
| 698 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 699 | std::unique_ptr<LogEvent> CreateWakelockStateChangedEvent(uint64_t timestampNs, |
| 700 | const vector<int>& attributionUids, |
| 701 | const vector<string>& attributionTags, |
| 702 | const string& wakelockName, |
| 703 | const WakelockStateChanged::State state) { |
| 704 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 705 | AStatsEvent_setAtomId(statsEvent, util::WAKELOCK_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 706 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 707 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 708 | writeAttribution(statsEvent, attributionUids, attributionTags); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 709 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID, true); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 710 | AStatsEvent_writeInt32(statsEvent, android::os::WakeLockLevelEnum::PARTIAL_WAKE_LOCK); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 711 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 712 | AStatsEvent_writeString(statsEvent, wakelockName.c_str()); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 713 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 714 | AStatsEvent_writeInt32(statsEvent, state); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 715 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true); |
| 716 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, true); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 717 | |
| 718 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 719 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 720 | return logEvent; |
| 721 | } |
| 722 | |
| 723 | std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(uint64_t timestampNs, |
| 724 | const vector<int>& attributionUids, |
| 725 | const vector<string>& attributionTags, |
| 726 | const string& wakelockName) { |
| 727 | return CreateWakelockStateChangedEvent(timestampNs, attributionUids, attributionTags, |
| 728 | wakelockName, WakelockStateChanged::ACQUIRE); |
| 729 | } |
| 730 | |
| 731 | std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(uint64_t timestampNs, |
| 732 | const vector<int>& attributionUids, |
| 733 | const vector<string>& attributionTags, |
| 734 | const string& wakelockName) { |
| 735 | return CreateWakelockStateChangedEvent(timestampNs, attributionUids, attributionTags, |
| 736 | wakelockName, WakelockStateChanged::RELEASE); |
| 737 | } |
| 738 | |
| 739 | std::unique_ptr<LogEvent> CreateActivityForegroundStateChangedEvent( |
| 740 | uint64_t timestampNs, const int uid, const ActivityForegroundStateChanged::State state) { |
| 741 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 742 | AStatsEvent_setAtomId(statsEvent, util::ACTIVITY_FOREGROUND_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 743 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 744 | |
| 745 | AStatsEvent_writeInt32(statsEvent, uid); |
| 746 | AStatsEvent_writeString(statsEvent, "pkg_name"); |
| 747 | AStatsEvent_writeString(statsEvent, "class_name"); |
| 748 | AStatsEvent_writeInt32(statsEvent, state); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 749 | |
| 750 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 751 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 752 | return logEvent; |
| 753 | } |
| 754 | |
| 755 | std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(uint64_t timestampNs, const int uid) { |
| 756 | return CreateActivityForegroundStateChangedEvent(timestampNs, uid, |
| 757 | ActivityForegroundStateChanged::BACKGROUND); |
| 758 | } |
| 759 | |
| 760 | std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(uint64_t timestampNs, const int uid) { |
| 761 | return CreateActivityForegroundStateChangedEvent(timestampNs, uid, |
| 762 | ActivityForegroundStateChanged::FOREGROUND); |
| 763 | } |
| 764 | |
| 765 | std::unique_ptr<LogEvent> CreateSyncStateChangedEvent(uint64_t timestampNs, |
| 766 | const vector<int>& attributionUids, |
| 767 | const vector<string>& attributionTags, |
| 768 | const string& name, |
| 769 | const SyncStateChanged::State state) { |
| 770 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 771 | AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 772 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 773 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 774 | writeAttribution(statsEvent, attributionUids, attributionTags); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 775 | AStatsEvent_writeString(statsEvent, name.c_str()); |
| 776 | AStatsEvent_writeInt32(statsEvent, state); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 777 | |
| 778 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 779 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 780 | return logEvent; |
| 781 | } |
| 782 | |
| 783 | std::unique_ptr<LogEvent> CreateSyncStartEvent(uint64_t timestampNs, |
| 784 | const vector<int>& attributionUids, |
| 785 | const vector<string>& attributionTags, |
| 786 | const string& name) { |
| 787 | return CreateSyncStateChangedEvent(timestampNs, attributionUids, attributionTags, name, |
| 788 | SyncStateChanged::ON); |
| 789 | } |
| 790 | |
| 791 | std::unique_ptr<LogEvent> CreateSyncEndEvent(uint64_t timestampNs, |
| 792 | const vector<int>& attributionUids, |
| 793 | const vector<string>& attributionTags, |
| 794 | const string& name) { |
| 795 | return CreateSyncStateChangedEvent(timestampNs, attributionUids, attributionTags, name, |
| 796 | SyncStateChanged::OFF); |
| 797 | } |
| 798 | |
| 799 | std::unique_ptr<LogEvent> CreateProcessLifeCycleStateChangedEvent( |
| 800 | uint64_t timestampNs, const int uid, const ProcessLifeCycleStateChanged::State state) { |
| 801 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 802 | AStatsEvent_setAtomId(statsEvent, util::PROCESS_LIFE_CYCLE_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 803 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 804 | |
| 805 | AStatsEvent_writeInt32(statsEvent, uid); |
| 806 | AStatsEvent_writeString(statsEvent, ""); |
| 807 | AStatsEvent_writeInt32(statsEvent, state); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 808 | |
| 809 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 810 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 811 | return logEvent; |
| 812 | } |
| 813 | |
| 814 | std::unique_ptr<LogEvent> CreateAppCrashEvent(uint64_t timestampNs, const int uid) { |
| 815 | return CreateProcessLifeCycleStateChangedEvent(timestampNs, uid, |
| 816 | ProcessLifeCycleStateChanged::CRASHED); |
| 817 | } |
| 818 | |
| 819 | std::unique_ptr<LogEvent> CreateAppCrashOccurredEvent(uint64_t timestampNs, const int uid) { |
| 820 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 821 | AStatsEvent_setAtomId(statsEvent, util::APP_CRASH_OCCURRED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 822 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 823 | |
| 824 | AStatsEvent_writeInt32(statsEvent, uid); |
| 825 | AStatsEvent_writeString(statsEvent, "eventType"); |
| 826 | AStatsEvent_writeString(statsEvent, "processName"); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 827 | |
| 828 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 829 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 830 | return logEvent; |
| 831 | } |
| 832 | |
| 833 | std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(uint64_t timestampNs, int hostUid, |
| 834 | int isolatedUid, bool is_create) { |
| 835 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 836 | AStatsEvent_setAtomId(statsEvent, util::ISOLATED_UID_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 837 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 838 | |
| 839 | AStatsEvent_writeInt32(statsEvent, hostUid); |
| 840 | AStatsEvent_writeInt32(statsEvent, isolatedUid); |
| 841 | AStatsEvent_writeInt32(statsEvent, is_create); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 842 | |
| 843 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 844 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 845 | return logEvent; |
| 846 | } |
| 847 | |
| 848 | std::unique_ptr<LogEvent> CreateUidProcessStateChangedEvent( |
| 849 | uint64_t timestampNs, int uid, const android::app::ProcessStateEnum state) { |
| 850 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
Jeffrey Huang | 3eb84d4 | 2020-03-17 10:31:22 -0700 | [diff] [blame] | 851 | AStatsEvent_setAtomId(statsEvent, util::UID_PROCESS_STATE_CHANGED); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 852 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 853 | |
| 854 | AStatsEvent_writeInt32(statsEvent, uid); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 855 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_IS_UID, true); |
| 856 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 857 | AStatsEvent_writeInt32(statsEvent, state); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 858 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true); |
| 859 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 860 | |
| 861 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 862 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 863 | return logEvent; |
| 864 | } |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 865 | |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 866 | std::unique_ptr<LogEvent> CreateBleScanStateChangedEvent(uint64_t timestampNs, |
| 867 | const vector<int>& attributionUids, |
| 868 | const vector<string>& attributionTags, |
| 869 | const BleScanStateChanged::State state, |
| 870 | const bool filtered, const bool firstMatch, |
| 871 | const bool opportunistic) { |
| 872 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 873 | AStatsEvent_setAtomId(statsEvent, util::BLE_SCAN_STATE_CHANGED); |
| 874 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 875 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 876 | writeAttribution(statsEvent, attributionUids, attributionTags); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 877 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD_FIRST_UID, true); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 878 | AStatsEvent_writeInt32(statsEvent, state); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 879 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true); |
| 880 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, true); |
| 881 | if (state == util::BLE_SCAN_STATE_CHANGED__STATE__RESET) { |
| 882 | AStatsEvent_addInt32Annotation(statsEvent, ANNOTATION_ID_TRIGGER_STATE_RESET, |
| 883 | util::BLE_SCAN_STATE_CHANGED__STATE__OFF); |
| 884 | } |
| 885 | AStatsEvent_writeBool(statsEvent, filtered); // filtered |
| 886 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
| 887 | AStatsEvent_writeBool(statsEvent, firstMatch); // first match |
| 888 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
| 889 | AStatsEvent_writeBool(statsEvent, opportunistic); // opportunistic |
| 890 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 891 | |
| 892 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 893 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 894 | return logEvent; |
| 895 | } |
| 896 | |
| 897 | std::unique_ptr<LogEvent> CreateOverlayStateChangedEvent(int64_t timestampNs, const int32_t uid, |
| 898 | const string& packageName, |
| 899 | const bool usingAlertWindow, |
| 900 | const OverlayStateChanged::State state) { |
| 901 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 902 | AStatsEvent_setAtomId(statsEvent, util::OVERLAY_STATE_CHANGED); |
| 903 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
| 904 | |
| 905 | AStatsEvent_writeInt32(statsEvent, uid); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 906 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_IS_UID, true); |
| 907 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 908 | AStatsEvent_writeString(statsEvent, packageName.c_str()); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 909 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_PRIMARY_FIELD, true); |
| 910 | AStatsEvent_writeBool(statsEvent, usingAlertWindow); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 911 | AStatsEvent_writeInt32(statsEvent, state); |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 912 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_EXCLUSIVE_STATE, true); |
| 913 | AStatsEvent_addBoolAnnotation(statsEvent, util::ANNOTATION_ID_STATE_NESTED, false); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 914 | |
| 915 | std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 916 | parseStatsEventToLogEvent(statsEvent, logEvent.get()); |
tsaichristine | a3d2ed8 | 2020-03-19 20:53:36 -0700 | [diff] [blame] | 917 | return logEvent; |
| 918 | } |
| 919 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 920 | sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 921 | const StatsdConfig& config, const ConfigKey& key, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 922 | const shared_ptr<IPullAtomCallback>& puller, |
Muhammad Qureshi | b635b3a | 2020-04-22 20:57:41 -0700 | [diff] [blame] | 923 | const int32_t atomTag, const sp<UidMap> uidMap) { |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 924 | sp<StatsPullerManager> pullerManager = new StatsPullerManager(); |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 925 | if (puller != nullptr) { |
| 926 | pullerManager->RegisterPullAtomCallback(/*uid=*/0, atomTag, NS_PER_SEC, NS_PER_SEC * 10, {}, |
| 927 | puller); |
| 928 | } |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 929 | sp<AlarmMonitor> anomalyAlarmMonitor = |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 930 | new AlarmMonitor(1, |
| 931 | [](const shared_ptr<IStatsCompanionService>&, int64_t){}, |
| 932 | [](const shared_ptr<IStatsCompanionService>&){}); |
Yangster-mac | 684d195 | 2018-03-24 16:47:16 -0700 | [diff] [blame] | 933 | sp<AlarmMonitor> periodicAlarmMonitor = |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 934 | new AlarmMonitor(1, |
| 935 | [](const shared_ptr<IStatsCompanionService>&, int64_t){}, |
| 936 | [](const shared_ptr<IStatsCompanionService>&){}); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 937 | sp<StatsLogProcessor> processor = |
| 938 | new StatsLogProcessor(uidMap, pullerManager, anomalyAlarmMonitor, periodicAlarmMonitor, |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 939 | timeBaseNs, [](const ConfigKey&) { return true; }, |
| 940 | [](const int&, const vector<int64_t>&) {return true;}); |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 941 | processor->OnConfigUpdated(currentTimeNs, key, config); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 942 | return processor; |
| 943 | } |
| 944 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 945 | void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events) { |
| 946 | std::sort(events->begin(), events->end(), |
| 947 | [](const std::unique_ptr<LogEvent>& a, const std::unique_ptr<LogEvent>& b) { |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 948 | return a->GetElapsedTimestampNs() < b->GetElapsedTimestampNs(); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 949 | }); |
| 950 | } |
| 951 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 952 | int64_t StringToId(const string& str) { |
| 953 | return static_cast<int64_t>(std::hash<std::string>()(str)); |
| 954 | } |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 955 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 956 | void ValidateWakelockAttributionUidAndTagDimension(const DimensionsValue& value, const int atomId, |
| 957 | const int uid, const string& tag) { |
| 958 | EXPECT_EQ(value.field(), atomId); |
| 959 | EXPECT_EQ(value.value_tuple().dimensions_value_size(), 2); |
| 960 | // Attribution field. |
| 961 | EXPECT_EQ(value.value_tuple().dimensions_value(0).field(), 1); |
| 962 | // Uid field. |
| 963 | EXPECT_EQ(value.value_tuple().dimensions_value(0).value_tuple().dimensions_value_size(), 1); |
| 964 | EXPECT_EQ(value.value_tuple().dimensions_value(0).value_tuple().dimensions_value(0).field(), 1); |
| 965 | EXPECT_EQ(value.value_tuple().dimensions_value(0).value_tuple().dimensions_value(0).value_int(), |
| 966 | uid); |
| 967 | // Tag field. |
| 968 | EXPECT_EQ(value.value_tuple().dimensions_value(1).field(), 3); |
| 969 | EXPECT_EQ(value.value_tuple().dimensions_value(1).value_str(), tag); |
| 970 | } |
| 971 | |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 972 | void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid) { |
| 973 | EXPECT_EQ(value.field(), atomId); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 974 | EXPECT_EQ(value.value_tuple().dimensions_value_size(), 1); |
| 975 | // Attribution field. |
| 976 | EXPECT_EQ(value.value_tuple().dimensions_value(0).field(), 1); |
| 977 | // Uid only. |
| 978 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 979 | .value_tuple().dimensions_value_size(), 1); |
| 980 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 981 | .value_tuple().dimensions_value(0).field(), 1); |
| 982 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 983 | .value_tuple().dimensions_value(0).value_int(), uid); |
| 984 | } |
| 985 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 986 | void ValidateUidDimension(const DimensionsValue& value, int node_idx, int atomId, int uid) { |
| 987 | EXPECT_EQ(value.field(), atomId); |
| 988 | EXPECT_GT(value.value_tuple().dimensions_value_size(), node_idx); |
| 989 | // Attribution field. |
| 990 | EXPECT_EQ(value.value_tuple().dimensions_value(node_idx).field(), 1); |
| 991 | EXPECT_EQ(value.value_tuple().dimensions_value(node_idx) |
| 992 | .value_tuple().dimensions_value(0).field(), 1); |
| 993 | EXPECT_EQ(value.value_tuple().dimensions_value(node_idx) |
| 994 | .value_tuple().dimensions_value(0).value_int(), uid); |
| 995 | } |
| 996 | |
| 997 | void ValidateAttributionUidAndTagDimension( |
| 998 | const DimensionsValue& value, int node_idx, int atomId, int uid, const std::string& tag) { |
| 999 | EXPECT_EQ(value.field(), atomId); |
| 1000 | EXPECT_GT(value.value_tuple().dimensions_value_size(), node_idx); |
| 1001 | // Attribution field. |
| 1002 | EXPECT_EQ(1, value.value_tuple().dimensions_value(node_idx).field()); |
| 1003 | // Uid only. |
| 1004 | EXPECT_EQ(2, value.value_tuple().dimensions_value(node_idx) |
| 1005 | .value_tuple().dimensions_value_size()); |
| 1006 | EXPECT_EQ(1, value.value_tuple().dimensions_value(node_idx) |
| 1007 | .value_tuple().dimensions_value(0).field()); |
| 1008 | EXPECT_EQ(uid, value.value_tuple().dimensions_value(node_idx) |
| 1009 | .value_tuple().dimensions_value(0).value_int()); |
| 1010 | EXPECT_EQ(2, value.value_tuple().dimensions_value(node_idx) |
| 1011 | .value_tuple().dimensions_value(1).field()); |
| 1012 | EXPECT_EQ(tag, value.value_tuple().dimensions_value(node_idx) |
| 1013 | .value_tuple().dimensions_value(1).value_str()); |
| 1014 | } |
| 1015 | |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 1016 | void ValidateAttributionUidAndTagDimension( |
| 1017 | const DimensionsValue& value, int atomId, int uid, const std::string& tag) { |
| 1018 | EXPECT_EQ(value.field(), atomId); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 1019 | EXPECT_EQ(1, value.value_tuple().dimensions_value_size()); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 1020 | // Attribution field. |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 1021 | EXPECT_EQ(1, value.value_tuple().dimensions_value(0).field()); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 1022 | // Uid only. |
| 1023 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 1024 | .value_tuple().dimensions_value_size(), 2); |
| 1025 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 1026 | .value_tuple().dimensions_value(0).field(), 1); |
| 1027 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 1028 | .value_tuple().dimensions_value(0).value_int(), uid); |
| 1029 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 1030 | .value_tuple().dimensions_value(1).field(), 2); |
| 1031 | EXPECT_EQ(value.value_tuple().dimensions_value(0) |
| 1032 | .value_tuple().dimensions_value(1).value_str(), tag); |
| 1033 | } |
| 1034 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 1035 | bool EqualsTo(const DimensionsValue& s1, const DimensionsValue& s2) { |
| 1036 | if (s1.field() != s2.field()) { |
| 1037 | return false; |
| 1038 | } |
| 1039 | if (s1.value_case() != s2.value_case()) { |
| 1040 | return false; |
| 1041 | } |
| 1042 | switch (s1.value_case()) { |
| 1043 | case DimensionsValue::ValueCase::kValueStr: |
| 1044 | return (s1.value_str() == s2.value_str()); |
| 1045 | case DimensionsValue::ValueCase::kValueInt: |
| 1046 | return s1.value_int() == s2.value_int(); |
| 1047 | case DimensionsValue::ValueCase::kValueLong: |
| 1048 | return s1.value_long() == s2.value_long(); |
| 1049 | case DimensionsValue::ValueCase::kValueBool: |
| 1050 | return s1.value_bool() == s2.value_bool(); |
| 1051 | case DimensionsValue::ValueCase::kValueFloat: |
| 1052 | return s1.value_float() == s2.value_float(); |
| 1053 | case DimensionsValue::ValueCase::kValueTuple: { |
| 1054 | if (s1.value_tuple().dimensions_value_size() != |
| 1055 | s2.value_tuple().dimensions_value_size()) { |
| 1056 | return false; |
| 1057 | } |
| 1058 | bool allMatched = true; |
| 1059 | for (int i = 0; allMatched && i < s1.value_tuple().dimensions_value_size(); ++i) { |
| 1060 | allMatched &= EqualsTo(s1.value_tuple().dimensions_value(i), |
| 1061 | s2.value_tuple().dimensions_value(i)); |
| 1062 | } |
| 1063 | return allMatched; |
| 1064 | } |
| 1065 | case DimensionsValue::ValueCase::VALUE_NOT_SET: |
| 1066 | default: |
| 1067 | return true; |
| 1068 | } |
| 1069 | } |
| 1070 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 1071 | bool LessThan(const google::protobuf::RepeatedPtrField<StateValue>& s1, |
| 1072 | const google::protobuf::RepeatedPtrField<StateValue>& s2) { |
| 1073 | if (s1.size() != s2.size()) { |
| 1074 | return s1.size() < s2.size(); |
| 1075 | } |
| 1076 | for (int i = 0; i < s1.size(); i++) { |
| 1077 | const StateValue& state1 = s1[i]; |
| 1078 | const StateValue& state2 = s2[i]; |
| 1079 | if (state1.atom_id() != state2.atom_id()) { |
| 1080 | return state1.atom_id() < state2.atom_id(); |
| 1081 | } |
| 1082 | if (state1.value() != state2.value()) { |
| 1083 | return state1.value() < state2.value(); |
| 1084 | } |
| 1085 | if (state1.group_id() != state2.group_id()) { |
| 1086 | return state1.group_id() < state2.group_id(); |
| 1087 | } |
| 1088 | } |
| 1089 | return false; |
| 1090 | } |
| 1091 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 1092 | bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2) { |
| 1093 | if (s1.field() != s2.field()) { |
| 1094 | return s1.field() < s2.field(); |
| 1095 | } |
| 1096 | if (s1.value_case() != s2.value_case()) { |
| 1097 | return s1.value_case() < s2.value_case(); |
| 1098 | } |
| 1099 | switch (s1.value_case()) { |
| 1100 | case DimensionsValue::ValueCase::kValueStr: |
| 1101 | return s1.value_str() < s2.value_str(); |
| 1102 | case DimensionsValue::ValueCase::kValueInt: |
| 1103 | return s1.value_int() < s2.value_int(); |
| 1104 | case DimensionsValue::ValueCase::kValueLong: |
| 1105 | return s1.value_long() < s2.value_long(); |
| 1106 | case DimensionsValue::ValueCase::kValueBool: |
| 1107 | return (int)s1.value_bool() < (int)s2.value_bool(); |
| 1108 | case DimensionsValue::ValueCase::kValueFloat: |
| 1109 | return s1.value_float() < s2.value_float(); |
| 1110 | case DimensionsValue::ValueCase::kValueTuple: { |
| 1111 | if (s1.value_tuple().dimensions_value_size() != |
| 1112 | s2.value_tuple().dimensions_value_size()) { |
| 1113 | return s1.value_tuple().dimensions_value_size() < |
| 1114 | s2.value_tuple().dimensions_value_size(); |
| 1115 | } |
| 1116 | for (int i = 0; i < s1.value_tuple().dimensions_value_size(); ++i) { |
| 1117 | if (EqualsTo(s1.value_tuple().dimensions_value(i), |
| 1118 | s2.value_tuple().dimensions_value(i))) { |
| 1119 | continue; |
| 1120 | } else { |
| 1121 | return LessThan(s1.value_tuple().dimensions_value(i), |
| 1122 | s2.value_tuple().dimensions_value(i)); |
| 1123 | } |
| 1124 | } |
| 1125 | return false; |
| 1126 | } |
| 1127 | case DimensionsValue::ValueCase::VALUE_NOT_SET: |
| 1128 | default: |
| 1129 | return false; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2) { |
| 1134 | if (LessThan(s1.dimInWhat, s2.dimInWhat)) { |
| 1135 | return true; |
| 1136 | } else if (LessThan(s2.dimInWhat, s1.dimInWhat)) { |
| 1137 | return false; |
| 1138 | } |
| 1139 | |
Tej Singh | 5d823b3 | 2019-05-21 20:13:21 -0700 | [diff] [blame] | 1140 | return LessThan(s1.stateValues, s2.stateValues); |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 1143 | void backfillStringInDimension(const std::map<uint64_t, string>& str_map, |
| 1144 | DimensionsValue* dimension) { |
| 1145 | if (dimension->has_value_str_hash()) { |
| 1146 | auto it = str_map.find((uint64_t)(dimension->value_str_hash())); |
| 1147 | if (it != str_map.end()) { |
| 1148 | dimension->clear_value_str_hash(); |
| 1149 | dimension->set_value_str(it->second); |
| 1150 | } else { |
| 1151 | ALOGE("Can not find the string hash: %llu", |
| 1152 | (unsigned long long)dimension->value_str_hash()); |
| 1153 | } |
| 1154 | } else if (dimension->has_value_tuple()) { |
| 1155 | auto value_tuple = dimension->mutable_value_tuple(); |
| 1156 | for (int i = 0; i < value_tuple->dimensions_value_size(); ++i) { |
| 1157 | backfillStringInDimension(str_map, value_tuple->mutable_dimensions_value(i)); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | void backfillStringInReport(ConfigMetricsReport *config_report) { |
| 1163 | std::map<uint64_t, string> str_map; |
| 1164 | for (const auto& str : config_report->strings()) { |
| 1165 | uint64_t hash = Hash64(str); |
| 1166 | if (str_map.find(hash) != str_map.end()) { |
| 1167 | ALOGE("String hash conflicts: %s %s", str.c_str(), str_map[hash].c_str()); |
| 1168 | } |
| 1169 | str_map[hash] = str; |
| 1170 | } |
| 1171 | for (int i = 0; i < config_report->metrics_size(); ++i) { |
| 1172 | auto metric_report = config_report->mutable_metrics(i); |
| 1173 | if (metric_report->has_count_metrics()) { |
| 1174 | backfillStringInDimension(str_map, metric_report->mutable_count_metrics()); |
| 1175 | } else if (metric_report->has_duration_metrics()) { |
| 1176 | backfillStringInDimension(str_map, metric_report->mutable_duration_metrics()); |
| 1177 | } else if (metric_report->has_gauge_metrics()) { |
| 1178 | backfillStringInDimension(str_map, metric_report->mutable_gauge_metrics()); |
| 1179 | } else if (metric_report->has_value_metrics()) { |
| 1180 | backfillStringInDimension(str_map, metric_report->mutable_value_metrics()); |
| 1181 | } |
| 1182 | } |
| 1183 | // Backfill the package names. |
| 1184 | for (int i = 0 ; i < config_report->uid_map().snapshots_size(); ++i) { |
| 1185 | auto snapshot = config_report->mutable_uid_map()->mutable_snapshots(i); |
| 1186 | for (int j = 0 ; j < snapshot->package_info_size(); ++j) { |
| 1187 | auto package_info = snapshot->mutable_package_info(j); |
| 1188 | if (package_info->has_name_hash()) { |
| 1189 | auto it = str_map.find((uint64_t)(package_info->name_hash())); |
| 1190 | if (it != str_map.end()) { |
| 1191 | package_info->clear_name_hash(); |
| 1192 | package_info->set_name(it->second); |
| 1193 | } else { |
| 1194 | ALOGE("Can not find the string package name hash: %llu", |
| 1195 | (unsigned long long)package_info->name_hash()); |
| 1196 | } |
| 1197 | |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | // Backfill the app name in app changes. |
| 1202 | for (int i = 0 ; i < config_report->uid_map().changes_size(); ++i) { |
| 1203 | auto change = config_report->mutable_uid_map()->mutable_changes(i); |
| 1204 | if (change->has_app_hash()) { |
| 1205 | auto it = str_map.find((uint64_t)(change->app_hash())); |
| 1206 | if (it != str_map.end()) { |
| 1207 | change->clear_app_hash(); |
| 1208 | change->set_app(it->second); |
| 1209 | } else { |
| 1210 | ALOGE("Can not find the string change app name hash: %llu", |
| 1211 | (unsigned long long)change->app_hash()); |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | void backfillStringInReport(ConfigMetricsReportList *config_report_list) { |
| 1218 | for (int i = 0; i < config_report_list->reports_size(); ++i) { |
| 1219 | backfillStringInReport(config_report_list->mutable_reports(i)); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | bool backfillDimensionPath(const DimensionsValue& path, |
| 1224 | const google::protobuf::RepeatedPtrField<DimensionsValue>& leafValues, |
| 1225 | int* leafIndex, |
| 1226 | DimensionsValue* dimension) { |
| 1227 | dimension->set_field(path.field()); |
| 1228 | if (path.has_value_tuple()) { |
| 1229 | for (int i = 0; i < path.value_tuple().dimensions_value_size(); ++i) { |
| 1230 | if (!backfillDimensionPath( |
| 1231 | path.value_tuple().dimensions_value(i), leafValues, leafIndex, |
| 1232 | dimension->mutable_value_tuple()->add_dimensions_value())) { |
| 1233 | return false; |
| 1234 | } |
| 1235 | } |
| 1236 | } else { |
| 1237 | if (*leafIndex < 0 || *leafIndex >= leafValues.size()) { |
| 1238 | return false; |
| 1239 | } |
| 1240 | dimension->MergeFrom(leafValues.Get(*leafIndex)); |
| 1241 | (*leafIndex)++; |
| 1242 | } |
| 1243 | return true; |
| 1244 | } |
| 1245 | |
| 1246 | bool backfillDimensionPath(const DimensionsValue& path, |
| 1247 | const google::protobuf::RepeatedPtrField<DimensionsValue>& leafValues, |
| 1248 | DimensionsValue* dimension) { |
| 1249 | int leafIndex = 0; |
| 1250 | return backfillDimensionPath(path, leafValues, &leafIndex, dimension); |
| 1251 | } |
| 1252 | |
| 1253 | void backfillDimensionPath(ConfigMetricsReportList *config_report_list) { |
| 1254 | for (int i = 0; i < config_report_list->reports_size(); ++i) { |
| 1255 | auto report = config_report_list->mutable_reports(i); |
| 1256 | for (int j = 0; j < report->metrics_size(); ++j) { |
| 1257 | auto metric_report = report->mutable_metrics(j); |
| 1258 | if (metric_report->has_dimensions_path_in_what() || |
| 1259 | metric_report->has_dimensions_path_in_condition()) { |
| 1260 | auto whatPath = metric_report->dimensions_path_in_what(); |
| 1261 | auto conditionPath = metric_report->dimensions_path_in_condition(); |
| 1262 | if (metric_report->has_count_metrics()) { |
| 1263 | backfillDimensionPath(whatPath, conditionPath, |
| 1264 | metric_report->mutable_count_metrics()); |
| 1265 | } else if (metric_report->has_duration_metrics()) { |
| 1266 | backfillDimensionPath(whatPath, conditionPath, |
| 1267 | metric_report->mutable_duration_metrics()); |
| 1268 | } else if (metric_report->has_gauge_metrics()) { |
| 1269 | backfillDimensionPath(whatPath, conditionPath, |
| 1270 | metric_report->mutable_gauge_metrics()); |
| 1271 | } else if (metric_report->has_value_metrics()) { |
| 1272 | backfillDimensionPath(whatPath, conditionPath, |
| 1273 | metric_report->mutable_value_metrics()); |
| 1274 | } |
| 1275 | metric_report->clear_dimensions_path_in_what(); |
| 1276 | metric_report->clear_dimensions_path_in_condition(); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | void backfillStartEndTimestamp(StatsLogReport *report) { |
| 1283 | const int64_t timeBaseNs = report->time_base_elapsed_nano_seconds(); |
| 1284 | const int64_t bucketSizeNs = report->bucket_size_nano_seconds(); |
| 1285 | if (report->has_count_metrics()) { |
| 1286 | backfillStartEndTimestampForMetrics( |
| 1287 | timeBaseNs, bucketSizeNs, report->mutable_count_metrics()); |
| 1288 | } else if (report->has_duration_metrics()) { |
| 1289 | backfillStartEndTimestampForMetrics( |
| 1290 | timeBaseNs, bucketSizeNs, report->mutable_duration_metrics()); |
| 1291 | } else if (report->has_gauge_metrics()) { |
| 1292 | backfillStartEndTimestampForMetrics( |
| 1293 | timeBaseNs, bucketSizeNs, report->mutable_gauge_metrics()); |
| 1294 | if (report->gauge_metrics().skipped_size() > 0) { |
| 1295 | backfillStartEndTimestampForSkippedBuckets( |
| 1296 | timeBaseNs, report->mutable_gauge_metrics()); |
| 1297 | } |
| 1298 | } else if (report->has_value_metrics()) { |
| 1299 | backfillStartEndTimestampForMetrics( |
| 1300 | timeBaseNs, bucketSizeNs, report->mutable_value_metrics()); |
| 1301 | if (report->value_metrics().skipped_size() > 0) { |
| 1302 | backfillStartEndTimestampForSkippedBuckets( |
| 1303 | timeBaseNs, report->mutable_value_metrics()); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | void backfillStartEndTimestamp(ConfigMetricsReport *config_report) { |
| 1309 | for (int j = 0; j < config_report->metrics_size(); ++j) { |
| 1310 | backfillStartEndTimestamp(config_report->mutable_metrics(j)); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | void backfillStartEndTimestamp(ConfigMetricsReportList *config_report_list) { |
| 1315 | for (int i = 0; i < config_report_list->reports_size(); ++i) { |
| 1316 | backfillStartEndTimestamp(config_report_list->mutable_reports(i)); |
| 1317 | } |
| 1318 | } |
| 1319 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1320 | Status FakeSubsystemSleepCallback::onPullAtom(int atomTag, |
| 1321 | const shared_ptr<IPullAtomResultReceiver>& resultReceiver) { |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1322 | // Convert stats_events into StatsEventParcels. |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1323 | std::vector<StatsEventParcel> parcels; |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1324 | for (int i = 1; i < 3; i++) { |
Tej Singh | b26d044 | 2020-01-31 16:18:21 -0800 | [diff] [blame] | 1325 | AStatsEvent* event = AStatsEvent_obtain(); |
| 1326 | AStatsEvent_setAtomId(event, atomTag); |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1327 | std::string subsystemName = "subsystem_name_"; |
| 1328 | subsystemName = subsystemName + std::to_string(i); |
Tej Singh | b26d044 | 2020-01-31 16:18:21 -0800 | [diff] [blame] | 1329 | AStatsEvent_writeString(event, subsystemName.c_str()); |
| 1330 | AStatsEvent_writeString(event, "subsystem_subname foo"); |
| 1331 | AStatsEvent_writeInt64(event, /*count= */ i); |
| 1332 | AStatsEvent_writeInt64(event, /*time_millis= */ i * 100); |
| 1333 | AStatsEvent_build(event); |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1334 | size_t size; |
Tej Singh | b26d044 | 2020-01-31 16:18:21 -0800 | [diff] [blame] | 1335 | uint8_t* buffer = AStatsEvent_getBuffer(event, &size); |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1336 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1337 | StatsEventParcel p; |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1338 | // vector.assign() creates a copy, but this is inevitable unless |
| 1339 | // stats_event.h/c uses a vector as opposed to a buffer. |
| 1340 | p.buffer.assign(buffer, buffer + size); |
| 1341 | parcels.push_back(std::move(p)); |
Tej Singh | b26d044 | 2020-01-31 16:18:21 -0800 | [diff] [blame] | 1342 | AStatsEvent_write(event); |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1343 | } |
| 1344 | resultReceiver->pullFinished(atomTag, /*success=*/true, parcels); |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 1345 | return Status::ok(); |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 1348 | } // namespace statsd |
| 1349 | } // namespace os |
Tej Singh | 6ede28b | 2019-01-29 17:06:54 -0800 | [diff] [blame] | 1350 | } // namespace android |