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 | |
| 15 | #pragma once |
| 16 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 18 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 19 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 20 | #include "src/StatsLogProcessor.h" |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 21 | #include "src/logd/LogEvent.h" |
| 22 | #include "statslog.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
| 27 | |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 28 | // Create AtomMatcher proto to simply match a specific atom type. |
| 29 | AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId); |
| 30 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 31 | // Create AtomMatcher proto for temperature atom. |
| 32 | AtomMatcher CreateTemperatureAtomMatcher(); |
| 33 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 34 | // Create AtomMatcher proto for scheduled job state changed. |
| 35 | AtomMatcher CreateScheduledJobStateChangedAtomMatcher(); |
| 36 | |
| 37 | // Create AtomMatcher proto for starting a scheduled job. |
| 38 | AtomMatcher CreateStartScheduledJobAtomMatcher(); |
| 39 | |
| 40 | // Create AtomMatcher proto for a scheduled job is done. |
| 41 | AtomMatcher CreateFinishScheduledJobAtomMatcher(); |
| 42 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 43 | // Create AtomMatcher proto for screen brightness state changed. |
| 44 | AtomMatcher CreateScreenBrightnessChangedAtomMatcher(); |
| 45 | |
| 46 | // Create AtomMatcher proto for starting battery save mode. |
| 47 | AtomMatcher CreateBatterySaverModeStartAtomMatcher(); |
| 48 | |
| 49 | // Create AtomMatcher proto for stopping battery save mode. |
| 50 | AtomMatcher CreateBatterySaverModeStopAtomMatcher(); |
| 51 | |
| 52 | // Create AtomMatcher proto for process state changed. |
| 53 | AtomMatcher CreateUidProcessStateChangedAtomMatcher(); |
| 54 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 55 | // Create AtomMatcher proto for acquiring wakelock. |
| 56 | AtomMatcher CreateAcquireWakelockAtomMatcher(); |
| 57 | |
| 58 | // Create AtomMatcher proto for releasing wakelock. |
| 59 | AtomMatcher CreateReleaseWakelockAtomMatcher() ; |
| 60 | |
| 61 | // Create AtomMatcher proto for screen turned on. |
| 62 | AtomMatcher CreateScreenTurnedOnAtomMatcher(); |
| 63 | |
| 64 | // Create AtomMatcher proto for screen turned off. |
| 65 | AtomMatcher CreateScreenTurnedOffAtomMatcher(); |
| 66 | |
| 67 | // Create AtomMatcher proto for app sync turned on. |
| 68 | AtomMatcher CreateSyncStartAtomMatcher(); |
| 69 | |
| 70 | // Create AtomMatcher proto for app sync turned off. |
| 71 | AtomMatcher CreateSyncEndAtomMatcher(); |
| 72 | |
| 73 | // Create AtomMatcher proto for app sync moves to background. |
| 74 | AtomMatcher CreateMoveToBackgroundAtomMatcher(); |
| 75 | |
| 76 | // Create AtomMatcher proto for app sync moves to foreground. |
| 77 | AtomMatcher CreateMoveToForegroundAtomMatcher(); |
| 78 | |
| 79 | // Create AtomMatcher proto for process crashes |
| 80 | AtomMatcher CreateProcessCrashAtomMatcher() ; |
| 81 | |
| 82 | // Create Predicate proto for screen is on. |
| 83 | Predicate CreateScreenIsOnPredicate(); |
| 84 | |
| 85 | // Create Predicate proto for screen is off. |
| 86 | Predicate CreateScreenIsOffPredicate(); |
| 87 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 88 | // Create Predicate proto for a running scheduled job. |
| 89 | Predicate CreateScheduledJobPredicate(); |
| 90 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 91 | // Create Predicate proto for battery saver mode. |
| 92 | Predicate CreateBatterySaverModePredicate(); |
| 93 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 94 | // Create Predicate proto for holding wakelock. |
| 95 | Predicate CreateHoldingWakelockPredicate(); |
| 96 | |
| 97 | // Create a Predicate proto for app syncing. |
| 98 | Predicate CreateIsSyncingPredicate(); |
| 99 | |
| 100 | // Create a Predicate proto for app is in background. |
| 101 | Predicate CreateIsInBackgroundPredicate(); |
| 102 | |
| 103 | // Add a predicate to the predicate combination. |
| 104 | void addPredicateToPredicateCombination(const Predicate& predicate, Predicate* combination); |
| 105 | |
| 106 | // Create dimensions from primitive fields. |
| 107 | FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields); |
| 108 | |
| 109 | // Create dimensions by attribution uid and tag. |
| 110 | FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId, |
| 111 | const std::vector<Position>& positions); |
| 112 | |
| 113 | // Create dimensions by attribution uid only. |
| 114 | FieldMatcher CreateAttributionUidDimensions(const int atomId, |
| 115 | const std::vector<Position>& positions); |
| 116 | |
| 117 | // Create log event for screen state changed. |
| 118 | std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( |
Bookatz | 1a1b046 | 2018-01-12 11:47:03 -0800 | [diff] [blame] | 119 | const android::view::DisplayStateEnum state, uint64_t timestampNs); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 120 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 121 | // Create log event for screen brightness state changed. |
| 122 | std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent( |
| 123 | int level, uint64_t timestampNs); |
| 124 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 125 | // Create log event when scheduled job starts. |
| 126 | std::unique_ptr<LogEvent> CreateStartScheduledJobEvent( |
| 127 | const std::vector<AttributionNodeInternal>& attributions, |
| 128 | const string& name, uint64_t timestampNs); |
| 129 | |
| 130 | // Create log event when scheduled job finishes. |
| 131 | std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent( |
| 132 | const std::vector<AttributionNodeInternal>& attributions, |
| 133 | const string& name, uint64_t timestampNs); |
| 134 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 135 | // Create log event when battery saver starts. |
| 136 | std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs); |
| 137 | // Create log event when battery saver stops. |
| 138 | std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs); |
| 139 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 140 | // Create log event for app moving to background. |
| 141 | std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(const int uid, uint64_t timestampNs); |
| 142 | |
| 143 | // Create log event for app moving to foreground. |
| 144 | std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(const int uid, uint64_t timestampNs); |
| 145 | |
| 146 | // Create log event when the app sync starts. |
| 147 | std::unique_ptr<LogEvent> CreateSyncStartEvent( |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 148 | const std::vector<AttributionNodeInternal>& attributions, const string& name, |
| 149 | uint64_t timestampNs); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 150 | |
| 151 | // Create log event when the app sync ends. |
| 152 | std::unique_ptr<LogEvent> CreateSyncEndEvent( |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 153 | const std::vector<AttributionNodeInternal>& attributions, const string& name, |
| 154 | uint64_t timestampNs); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 155 | |
| 156 | // Create log event when the app sync ends. |
| 157 | std::unique_ptr<LogEvent> CreateAppCrashEvent( |
| 158 | const int uid, uint64_t timestampNs); |
| 159 | |
| 160 | // Create log event for acquiring wakelock. |
| 161 | std::unique_ptr<LogEvent> CreateAcquireWakelockEvent( |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 162 | const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName, |
| 163 | uint64_t timestampNs); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 164 | |
| 165 | // Create log event for releasing wakelock. |
| 166 | std::unique_ptr<LogEvent> CreateReleaseWakelockEvent( |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 167 | const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName, |
| 168 | uint64_t timestampNs); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 169 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 170 | // Create log event for releasing wakelock. |
| 171 | std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent( |
| 172 | int isolatedUid, int hostUid, bool is_create, uint64_t timestampNs); |
| 173 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 174 | // Helper function to create an AttributionNodeInternal proto. |
| 175 | AttributionNodeInternal CreateAttribution(const int& uid, const string& tag); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 176 | |
| 177 | // Create a statsd log event processor upon the start time in seconds, config and key. |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 178 | sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, |
| 179 | const int64_t currentTimeNs, |
| 180 | const StatsdConfig& config, const ConfigKey& key); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 181 | |
| 182 | // Util function to sort the log events by timestamp. |
| 183 | void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events); |
| 184 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 185 | int64_t StringToId(const string& str); |
| 186 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 187 | void ValidateUidDimension(const DimensionsValue& value, int node_idx, int atomId, int uid); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 188 | void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid); |
| 189 | void ValidateAttributionUidAndTagDimension( |
| 190 | const DimensionsValue& value, int atomId, int uid, const std::string& tag); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 191 | void ValidateAttributionUidAndTagDimension( |
| 192 | const DimensionsValue& value, int node_idx, int atomId, int uid, const std::string& tag); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 193 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 194 | struct DimensionsPair { |
| 195 | DimensionsPair(DimensionsValue m1, DimensionsValue m2) : dimInWhat(m1), dimInCondition(m2){}; |
| 196 | |
| 197 | DimensionsValue dimInWhat; |
| 198 | DimensionsValue dimInCondition; |
| 199 | }; |
| 200 | |
| 201 | bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2); |
| 202 | bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2); |
| 203 | |
| 204 | struct DimensionCompare { |
| 205 | bool operator()(const DimensionsPair& s1, const DimensionsPair& s2) const { |
| 206 | return LessThan(s1, s2); |
| 207 | } |
| 208 | }; |
| 209 | |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 210 | template <typename T> |
| 211 | void sortMetricDataByDimensionsValue(const T& metricData, T* sortedMetricData) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 212 | std::map<DimensionsPair, int, DimensionCompare> dimensionIndexMap; |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 213 | for (int i = 0; i < metricData.data_size(); ++i) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 214 | dimensionIndexMap.insert( |
| 215 | std::make_pair(DimensionsPair(metricData.data(i).dimensions_in_what(), |
| 216 | metricData.data(i).dimensions_in_condition()), |
| 217 | i)); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 218 | } |
| 219 | for (const auto& itr : dimensionIndexMap) { |
| 220 | *sortedMetricData->add_data() = metricData.data(itr.second); |
| 221 | } |
| 222 | } |
| 223 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 224 | } // namespace statsd |
| 225 | } // namespace os |
| 226 | } // namespace android |