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 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 17 | #include <aidl/android/os/BnPullAtomCallback.h> |
| 18 | #include <aidl/android/os/IPullAtomCallback.h> |
| 19 | #include <aidl/android/os/IPullAtomResultReceiver.h> |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 21 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 22 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 23 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 24 | #include "src/StatsLogProcessor.h" |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 25 | #include "src/hash.h" |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 26 | #include "src/logd/LogEvent.h" |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 27 | #include "src/stats_log_util.h" |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 28 | #include "statslog.h" |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 34 | using ::aidl::android::os::BnPullAtomCallback; |
| 35 | using ::aidl::android::os::IPullAtomCallback; |
| 36 | using ::aidl::android::os::IPullAtomResultReceiver; |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 37 | using android::util::ProtoReader; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 38 | using google::protobuf::RepeatedPtrField; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 39 | using Status = ::ndk::ScopedAStatus; |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 40 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 41 | const int SCREEN_STATE_ATOM_ID = android::util::SCREEN_STATE_CHANGED; |
| 42 | const int UID_PROCESS_STATE_ATOM_ID = android::util::UID_PROCESS_STATE_CHANGED; |
| 43 | |
tsaichristine | b87ca15 | 2019-12-09 15:19:41 -0800 | [diff] [blame] | 44 | // Converts a ProtoOutputStream to a StatsLogReport proto. |
| 45 | StatsLogReport outputStreamToProto(ProtoOutputStream* proto); |
| 46 | |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 47 | // Create AtomMatcher proto to simply match a specific atom type. |
| 48 | AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId); |
| 49 | |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 50 | // Create AtomMatcher proto for temperature atom. |
| 51 | AtomMatcher CreateTemperatureAtomMatcher(); |
| 52 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 53 | // Create AtomMatcher proto for scheduled job state changed. |
| 54 | AtomMatcher CreateScheduledJobStateChangedAtomMatcher(); |
| 55 | |
| 56 | // Create AtomMatcher proto for starting a scheduled job. |
| 57 | AtomMatcher CreateStartScheduledJobAtomMatcher(); |
| 58 | |
| 59 | // Create AtomMatcher proto for a scheduled job is done. |
| 60 | AtomMatcher CreateFinishScheduledJobAtomMatcher(); |
| 61 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 62 | // Create AtomMatcher proto for screen brightness state changed. |
| 63 | AtomMatcher CreateScreenBrightnessChangedAtomMatcher(); |
| 64 | |
| 65 | // Create AtomMatcher proto for starting battery save mode. |
| 66 | AtomMatcher CreateBatterySaverModeStartAtomMatcher(); |
| 67 | |
| 68 | // Create AtomMatcher proto for stopping battery save mode. |
| 69 | AtomMatcher CreateBatterySaverModeStopAtomMatcher(); |
| 70 | |
| 71 | // Create AtomMatcher proto for process state changed. |
| 72 | AtomMatcher CreateUidProcessStateChangedAtomMatcher(); |
| 73 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 74 | // Create AtomMatcher proto for acquiring wakelock. |
| 75 | AtomMatcher CreateAcquireWakelockAtomMatcher(); |
| 76 | |
| 77 | // Create AtomMatcher proto for releasing wakelock. |
| 78 | AtomMatcher CreateReleaseWakelockAtomMatcher() ; |
| 79 | |
| 80 | // Create AtomMatcher proto for screen turned on. |
| 81 | AtomMatcher CreateScreenTurnedOnAtomMatcher(); |
| 82 | |
| 83 | // Create AtomMatcher proto for screen turned off. |
| 84 | AtomMatcher CreateScreenTurnedOffAtomMatcher(); |
| 85 | |
| 86 | // Create AtomMatcher proto for app sync turned on. |
| 87 | AtomMatcher CreateSyncStartAtomMatcher(); |
| 88 | |
| 89 | // Create AtomMatcher proto for app sync turned off. |
| 90 | AtomMatcher CreateSyncEndAtomMatcher(); |
| 91 | |
| 92 | // Create AtomMatcher proto for app sync moves to background. |
| 93 | AtomMatcher CreateMoveToBackgroundAtomMatcher(); |
| 94 | |
| 95 | // Create AtomMatcher proto for app sync moves to foreground. |
| 96 | AtomMatcher CreateMoveToForegroundAtomMatcher(); |
| 97 | |
| 98 | // Create AtomMatcher proto for process crashes |
| 99 | AtomMatcher CreateProcessCrashAtomMatcher() ; |
| 100 | |
| 101 | // Create Predicate proto for screen is on. |
| 102 | Predicate CreateScreenIsOnPredicate(); |
| 103 | |
| 104 | // Create Predicate proto for screen is off. |
| 105 | Predicate CreateScreenIsOffPredicate(); |
| 106 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 107 | // Create Predicate proto for a running scheduled job. |
| 108 | Predicate CreateScheduledJobPredicate(); |
| 109 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 110 | // Create Predicate proto for battery saver mode. |
| 111 | Predicate CreateBatterySaverModePredicate(); |
| 112 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 113 | // Create Predicate proto for holding wakelock. |
| 114 | Predicate CreateHoldingWakelockPredicate(); |
| 115 | |
| 116 | // Create a Predicate proto for app syncing. |
| 117 | Predicate CreateIsSyncingPredicate(); |
| 118 | |
| 119 | // Create a Predicate proto for app is in background. |
| 120 | Predicate CreateIsInBackgroundPredicate(); |
| 121 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 122 | // Create State proto for screen state atom. |
| 123 | State CreateScreenState(); |
| 124 | |
| 125 | // Create State proto for uid process state atom. |
| 126 | State CreateUidProcessState(); |
| 127 | |
| 128 | // Create State proto for overlay state atom. |
| 129 | State CreateOverlayState(); |
| 130 | |
| 131 | State CreateScreenStateWithOnOffMap(); |
| 132 | |
| 133 | State CreateScreenStateWithInDozeMap(); |
| 134 | |
| 135 | // Create StateGroup proto for ScreenState ON group |
| 136 | StateMap_StateGroup CreateScreenStateOnGroup(); |
| 137 | |
| 138 | // Create StateGroup proto for ScreenState OFF group |
| 139 | StateMap_StateGroup CreateScreenStateOffGroup(); |
| 140 | |
| 141 | // Create StateMap proto for ScreenState ON/OFF map |
| 142 | StateMap CreateScreenStateOnOffMap(); |
| 143 | |
| 144 | // Create StateGroup proto for ScreenState IN DOZE group |
| 145 | StateMap_StateGroup CreateScreenStateInDozeGroup(); |
| 146 | |
| 147 | // Create StateGroup proto for ScreenState NOT IN DOZE group |
| 148 | StateMap_StateGroup CreateScreenStateNotDozeGroup(); |
| 149 | |
| 150 | // Create StateMap proto for ScreenState IN DOZE map |
| 151 | StateMap CreateScreenStateInDozeMap(); |
| 152 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 153 | // Add a predicate to the predicate combination. |
| 154 | void addPredicateToPredicateCombination(const Predicate& predicate, Predicate* combination); |
| 155 | |
| 156 | // Create dimensions from primitive fields. |
| 157 | FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields); |
| 158 | |
| 159 | // Create dimensions by attribution uid and tag. |
| 160 | FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId, |
| 161 | const std::vector<Position>& positions); |
| 162 | |
| 163 | // Create dimensions by attribution uid only. |
| 164 | FieldMatcher CreateAttributionUidDimensions(const int atomId, |
| 165 | const std::vector<Position>& positions); |
| 166 | |
| 167 | // Create log event for screen state changed. |
| 168 | std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 169 | uint64_t timestampNs, const android::view::DisplayStateEnum state); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 170 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 171 | // Create log event for screen brightness state changed. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 172 | std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(uint64_t timestampNs, int level); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 173 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 174 | // Create log event when scheduled job starts. |
| 175 | std::unique_ptr<LogEvent> CreateStartScheduledJobEvent( |
| 176 | const std::vector<AttributionNodeInternal>& attributions, |
| 177 | const string& name, uint64_t timestampNs); |
| 178 | |
| 179 | // Create log event when scheduled job finishes. |
| 180 | std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent( |
| 181 | const std::vector<AttributionNodeInternal>& attributions, |
| 182 | const string& name, uint64_t timestampNs); |
| 183 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 184 | // Create log event when battery saver starts. |
| 185 | std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs); |
| 186 | // Create log event when battery saver stops. |
| 187 | std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs); |
| 188 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 189 | // Create log event for app moving to background. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 190 | std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(uint64_t timestampNs, const int uid); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 191 | |
| 192 | // Create log event for app moving to foreground. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 193 | std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(uint64_t timestampNs, const int uid); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 194 | |
| 195 | // Create log event when the app sync starts. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 196 | std::unique_ptr<LogEvent> CreateSyncStartEvent(uint64_t timestampNs, const vector<int>& uids, |
| 197 | const vector<string>& tags, const string& name); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 198 | |
| 199 | // Create log event when the app sync ends. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 200 | std::unique_ptr<LogEvent> CreateSyncEndEvent(uint64_t timestampNs, const vector<int>& uids, |
| 201 | const vector<string>& tags, const string& name); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 202 | |
| 203 | // Create log event when the app sync ends. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 204 | std::unique_ptr<LogEvent> CreateAppCrashEvent(uint64_t timestampNs, const int uid); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 205 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 206 | // Create log event for an app crash. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 207 | std::unique_ptr<LogEvent> CreateAppCrashOccurredEvent(uint64_t timestampNs, const int uid); |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 208 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 209 | // Create log event for acquiring wakelock. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 210 | std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(uint64_t timestampNs, const vector<int>& uids, |
| 211 | const vector<string>& tags, |
| 212 | const string& wakelockName); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 213 | |
| 214 | // Create log event for releasing wakelock. |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 215 | std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(uint64_t timestampNs, const vector<int>& uids, |
| 216 | const vector<string>& tags, |
| 217 | const string& wakelockName); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 218 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 219 | // Create log event for releasing wakelock. |
tsaichristine | 0defefc | 2020-03-11 07:52:37 -0700 | [diff] [blame] | 220 | std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(uint64_t timestampNs, int hostUid, |
| 221 | int isolatedUid, bool is_create); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 222 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 223 | // Create log event for uid process state change. |
| 224 | std::unique_ptr<LogEvent> CreateUidProcessStateChangedEvent( |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 225 | uint64_t timestampNs, int uid, const android::app::ProcessStateEnum state); |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 226 | |
Yao Chen | 9c1debe | 2018-02-19 14:39:19 -0800 | [diff] [blame] | 227 | // Helper function to create an AttributionNodeInternal proto. |
| 228 | AttributionNodeInternal CreateAttribution(const int& uid, const string& tag); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 229 | |
| 230 | // Create a statsd log event processor upon the start time in seconds, config and key. |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 231 | sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, |
| 232 | const StatsdConfig& config, const ConfigKey& key, |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 233 | const shared_ptr<IPullAtomCallback>& puller = nullptr, |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 234 | const int32_t atomTag = 0 /*for puller only*/); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 235 | |
| 236 | // Util function to sort the log events by timestamp. |
| 237 | void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events); |
| 238 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 239 | int64_t StringToId(const string& str); |
| 240 | |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 241 | void ValidateUidDimension(const DimensionsValue& value, int node_idx, int atomId, int uid); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 242 | void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid); |
| 243 | void ValidateAttributionUidAndTagDimension( |
| 244 | const DimensionsValue& value, int atomId, int uid, const std::string& tag); |
Yangster-mac | e06cfd7 | 2018-03-10 23:22:59 -0800 | [diff] [blame] | 245 | void ValidateAttributionUidAndTagDimension( |
| 246 | 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] | 247 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 248 | struct DimensionsPair { |
| 249 | DimensionsPair(DimensionsValue m1, DimensionsValue m2) : dimInWhat(m1), dimInCondition(m2){}; |
| 250 | |
| 251 | DimensionsValue dimInWhat; |
| 252 | DimensionsValue dimInCondition; |
| 253 | }; |
| 254 | |
| 255 | bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2); |
| 256 | bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2); |
| 257 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 258 | |
| 259 | void backfillStartEndTimestamp(ConfigMetricsReport *config_report); |
| 260 | void backfillStartEndTimestamp(ConfigMetricsReportList *config_report_list); |
| 261 | |
| 262 | void backfillStringInReport(ConfigMetricsReportList *config_report_list); |
| 263 | void backfillStringInDimension(const std::map<uint64_t, string>& str_map, |
| 264 | DimensionsValue* dimension); |
| 265 | |
| 266 | template <typename T> |
| 267 | void backfillStringInDimension(const std::map<uint64_t, string>& str_map, |
| 268 | T* metrics) { |
| 269 | for (int i = 0; i < metrics->data_size(); ++i) { |
| 270 | auto data = metrics->mutable_data(i); |
| 271 | if (data->has_dimensions_in_what()) { |
| 272 | backfillStringInDimension(str_map, data->mutable_dimensions_in_what()); |
| 273 | } |
| 274 | if (data->has_dimensions_in_condition()) { |
| 275 | backfillStringInDimension(str_map, data->mutable_dimensions_in_condition()); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void backfillDimensionPath(ConfigMetricsReportList* config_report_list); |
| 281 | |
| 282 | bool backfillDimensionPath(const DimensionsValue& path, |
| 283 | const google::protobuf::RepeatedPtrField<DimensionsValue>& leafValues, |
| 284 | DimensionsValue* dimension); |
| 285 | |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 286 | class FakeSubsystemSleepCallback : public BnPullAtomCallback { |
| 287 | public: |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 288 | Status onPullAtom(int atomTag, |
| 289 | const shared_ptr<IPullAtomResultReceiver>& resultReceiver) override; |
Tej Singh | 730ed29 | 2020-02-03 17:24:27 -0800 | [diff] [blame] | 290 | }; |
| 291 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 292 | template <typename T> |
| 293 | void backfillDimensionPath(const DimensionsValue& whatPath, |
| 294 | const DimensionsValue& conditionPath, |
| 295 | T* metricData) { |
| 296 | for (int i = 0; i < metricData->data_size(); ++i) { |
| 297 | auto data = metricData->mutable_data(i); |
| 298 | if (data->dimension_leaf_values_in_what_size() > 0) { |
| 299 | backfillDimensionPath(whatPath, data->dimension_leaf_values_in_what(), |
| 300 | data->mutable_dimensions_in_what()); |
| 301 | data->clear_dimension_leaf_values_in_what(); |
| 302 | } |
| 303 | if (data->dimension_leaf_values_in_condition_size() > 0) { |
| 304 | backfillDimensionPath(conditionPath, data->dimension_leaf_values_in_condition(), |
| 305 | data->mutable_dimensions_in_condition()); |
| 306 | data->clear_dimension_leaf_values_in_condition(); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 311 | struct DimensionCompare { |
| 312 | bool operator()(const DimensionsPair& s1, const DimensionsPair& s2) const { |
| 313 | return LessThan(s1, s2); |
| 314 | } |
| 315 | }; |
| 316 | |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 317 | template <typename T> |
| 318 | void sortMetricDataByDimensionsValue(const T& metricData, T* sortedMetricData) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 319 | std::map<DimensionsPair, int, DimensionCompare> dimensionIndexMap; |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 320 | for (int i = 0; i < metricData.data_size(); ++i) { |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 321 | dimensionIndexMap.insert( |
| 322 | std::make_pair(DimensionsPair(metricData.data(i).dimensions_in_what(), |
| 323 | metricData.data(i).dimensions_in_condition()), |
| 324 | i)); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 325 | } |
| 326 | for (const auto& itr : dimensionIndexMap) { |
| 327 | *sortedMetricData->add_data() = metricData.data(itr.second); |
| 328 | } |
| 329 | } |
| 330 | |
Yangster-mac | 9def8e3 | 2018-04-17 13:55:51 -0700 | [diff] [blame] | 331 | template <typename T> |
| 332 | void backfillStartEndTimestampForFullBucket( |
| 333 | const int64_t timeBaseNs, const int64_t bucketSizeNs, T* bucket) { |
| 334 | bucket->set_start_bucket_elapsed_nanos(timeBaseNs + bucketSizeNs * bucket->bucket_num()); |
| 335 | bucket->set_end_bucket_elapsed_nanos( |
| 336 | timeBaseNs + bucketSizeNs * bucket->bucket_num() + bucketSizeNs); |
| 337 | bucket->clear_bucket_num(); |
| 338 | } |
| 339 | |
| 340 | template <typename T> |
| 341 | void backfillStartEndTimestampForPartialBucket(const int64_t timeBaseNs, T* bucket) { |
| 342 | if (bucket->has_start_bucket_elapsed_millis()) { |
| 343 | bucket->set_start_bucket_elapsed_nanos( |
| 344 | MillisToNano(bucket->start_bucket_elapsed_millis())); |
| 345 | bucket->clear_start_bucket_elapsed_millis(); |
| 346 | } |
| 347 | if (bucket->has_end_bucket_elapsed_millis()) { |
| 348 | bucket->set_end_bucket_elapsed_nanos( |
| 349 | MillisToNano(bucket->end_bucket_elapsed_millis())); |
| 350 | bucket->clear_end_bucket_elapsed_millis(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | template <typename T> |
| 355 | void backfillStartEndTimestampForMetrics(const int64_t timeBaseNs, const int64_t bucketSizeNs, |
| 356 | T* metrics) { |
| 357 | for (int i = 0; i < metrics->data_size(); ++i) { |
| 358 | auto data = metrics->mutable_data(i); |
| 359 | for (int j = 0; j < data->bucket_info_size(); ++j) { |
| 360 | auto bucket = data->mutable_bucket_info(j); |
| 361 | if (bucket->has_bucket_num()) { |
| 362 | backfillStartEndTimestampForFullBucket(timeBaseNs, bucketSizeNs, bucket); |
| 363 | } else { |
| 364 | backfillStartEndTimestampForPartialBucket(timeBaseNs, bucket); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | template <typename T> |
| 371 | void backfillStartEndTimestampForSkippedBuckets(const int64_t timeBaseNs, T* metrics) { |
| 372 | for (int i = 0; i < metrics->skipped_size(); ++i) { |
| 373 | backfillStartEndTimestampForPartialBucket(timeBaseNs, metrics->mutable_skipped(i)); |
| 374 | } |
| 375 | } |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 376 | } // namespace statsd |
| 377 | } // namespace os |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 378 | } // namespace android |