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