• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <aidl/android/os/BnPendingIntentRef.h>
18 #include <aidl/android/os/BnPullAtomCallback.h>
19 #include <aidl/android/os/IPullAtomCallback.h>
20 #include <aidl/android/os/IPullAtomResultReceiver.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
24 #include "src/StatsLogProcessor.h"
25 #include "src/flags/FlagProvider.h"
26 #include "src/hash.h"
27 #include "src/logd/LogEvent.h"
28 #include "src/matchers/EventMatcherWizard.h"
29 #include "src/packages/UidMap.h"
30 #include "src/stats_log.pb.h"
31 #include "src/stats_log_util.h"
32 #include "src/statsd_config.pb.h"
33 #include "stats_event.h"
34 #include "statslog_statsdtest.h"
35 
36 namespace android {
37 namespace os {
38 namespace statsd {
39 
40 using namespace testing;
41 using ::aidl::android::os::BnPullAtomCallback;
42 using ::aidl::android::os::IPullAtomCallback;
43 using ::aidl::android::os::IPullAtomResultReceiver;
44 using android::util::ProtoReader;
45 using google::protobuf::RepeatedPtrField;
46 using Status = ::ndk::ScopedAStatus;
47 
48 // Wrapper for assertion helpers called from tests to keep track of source location of failures.
49 // Example usage:
50 //      static void myTestVerificationHelper(Foo foo) {
51 //          EXPECT_EQ(...);
52 //          ASSERT_EQ(...);
53 //      }
54 //
55 //      TEST_F(MyTest, TestFoo) {
56 //          ...
57 //          TRACE_CALL(myTestVerificationHelper, foo);
58 //          ...
59 //      }
60 //
61 #define TRACE_CALL(function, ...) \
62     do {                          \
63         SCOPED_TRACE("");         \
64         (function)(__VA_ARGS__);  \
65     } while (false)
66 
67 const int SCREEN_STATE_ATOM_ID = util::SCREEN_STATE_CHANGED;
68 const int UID_PROCESS_STATE_ATOM_ID = util::UID_PROCESS_STATE_CHANGED;
69 
70 enum BucketSplitEvent { APP_UPGRADE, BOOT_COMPLETE };
71 
72 class MockUidMap : public UidMap {
73 public:
74     MOCK_METHOD(int, getHostUidOrSelf, (int uid), (const));
75     MOCK_METHOD(std::set<int32_t>, getAppUid, (const string& package), (const));
76 };
77 
78 class MockPendingIntentRef : public aidl::android::os::BnPendingIntentRef {
79 public:
80     MOCK_METHOD1(sendDataBroadcast, Status(int64_t lastReportTimeNs));
81     MOCK_METHOD1(sendActiveConfigsChangedBroadcast, Status(const vector<int64_t>& configIds));
82     MOCK_METHOD6(sendSubscriberBroadcast,
83                  Status(int64_t configUid, int64_t configId, int64_t subscriptionId,
84                         int64_t subscriptionRuleId, const vector<string>& cookies,
85                         const StatsDimensionsValueParcel& dimensionsValueParcel));
86 };
87 
88 // Converts a ProtoOutputStream to a StatsLogReport proto.
89 StatsLogReport outputStreamToProto(ProtoOutputStream* proto);
90 
91 // Create AtomMatcher proto to simply match a specific atom type.
92 AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId);
93 
94 // Create AtomMatcher proto for temperature atom.
95 AtomMatcher CreateTemperatureAtomMatcher();
96 
97 // Create AtomMatcher proto for scheduled job state changed.
98 AtomMatcher CreateScheduledJobStateChangedAtomMatcher();
99 
100 // Create AtomMatcher proto for starting a scheduled job.
101 AtomMatcher CreateStartScheduledJobAtomMatcher();
102 
103 // Create AtomMatcher proto for a scheduled job is done.
104 AtomMatcher CreateFinishScheduledJobAtomMatcher();
105 
106 // Create AtomMatcher proto for screen brightness state changed.
107 AtomMatcher CreateScreenBrightnessChangedAtomMatcher();
108 
109 // Create AtomMatcher proto for starting battery save mode.
110 AtomMatcher CreateBatterySaverModeStartAtomMatcher();
111 
112 // Create AtomMatcher proto for stopping battery save mode.
113 AtomMatcher CreateBatterySaverModeStopAtomMatcher();
114 
115 // Create AtomMatcher proto for battery state none mode.
116 AtomMatcher CreateBatteryStateNoneMatcher();
117 
118 // Create AtomMatcher proto for battery state usb mode.
119 AtomMatcher CreateBatteryStateUsbMatcher();
120 
121 // Create AtomMatcher proto for process state changed.
122 AtomMatcher CreateUidProcessStateChangedAtomMatcher();
123 
124 // Create AtomMatcher proto for acquiring wakelock.
125 AtomMatcher CreateAcquireWakelockAtomMatcher();
126 
127 // Create AtomMatcher proto for releasing wakelock.
128 AtomMatcher CreateReleaseWakelockAtomMatcher() ;
129 
130 // Create AtomMatcher proto for screen turned on.
131 AtomMatcher CreateScreenTurnedOnAtomMatcher();
132 
133 // Create AtomMatcher proto for screen turned off.
134 AtomMatcher CreateScreenTurnedOffAtomMatcher();
135 
136 // Create AtomMatcher proto for app sync turned on.
137 AtomMatcher CreateSyncStartAtomMatcher();
138 
139 // Create AtomMatcher proto for app sync turned off.
140 AtomMatcher CreateSyncEndAtomMatcher();
141 
142 // Create AtomMatcher proto for app sync moves to background.
143 AtomMatcher CreateMoveToBackgroundAtomMatcher();
144 
145 // Create AtomMatcher proto for app sync moves to foreground.
146 AtomMatcher CreateMoveToForegroundAtomMatcher();
147 
148 // Create AtomMatcher proto for process crashes
149 AtomMatcher CreateProcessCrashAtomMatcher();
150 
151 // Create AtomMatcher proto for app launches.
152 AtomMatcher CreateAppStartOccurredAtomMatcher();
153 
154 // Create AtomMatcher proto for test atom repeated state.
155 AtomMatcher CreateTestAtomRepeatedStateAtomMatcher(const string& name,
156                                                    TestAtomReported::State state,
157                                                    Position position);
158 
159 // Create AtomMatcher proto for test atom repeated state is off, first position.
160 AtomMatcher CreateTestAtomRepeatedStateFirstOffAtomMatcher();
161 
162 // Create AtomMatcher proto for test atom repeated state is on, first position.
163 AtomMatcher CreateTestAtomRepeatedStateFirstOnAtomMatcher();
164 
165 // Create AtomMatcher proto for test atom repeated state is on, any position.
166 AtomMatcher CreateTestAtomRepeatedStateAnyOnAtomMatcher();
167 
168 // Add an AtomMatcher to a combination AtomMatcher.
169 void addMatcherToMatcherCombination(const AtomMatcher& matcher, AtomMatcher* combinationMatcher);
170 
171 // Create Predicate proto for screen is on.
172 Predicate CreateScreenIsOnPredicate();
173 
174 // Create Predicate proto for screen is off.
175 Predicate CreateScreenIsOffPredicate();
176 
177 // Create Predicate proto for a running scheduled job.
178 Predicate CreateScheduledJobPredicate();
179 
180 // Create Predicate proto for battery saver mode.
181 Predicate CreateBatterySaverModePredicate();
182 
183 // Create Predicate proto for device unplogged mode.
184 Predicate CreateDeviceUnpluggedPredicate();
185 
186 // Create Predicate proto for holding wakelock.
187 Predicate CreateHoldingWakelockPredicate();
188 
189 // Create a Predicate proto for app syncing.
190 Predicate CreateIsSyncingPredicate();
191 
192 // Create a Predicate proto for app is in background.
193 Predicate CreateIsInBackgroundPredicate();
194 
195 // Create a Predicate proto for test atom repeated state field is off.
196 Predicate CreateTestAtomRepeatedStateFirstOffPredicate();
197 
198 // Create State proto for screen state atom.
199 State CreateScreenState();
200 
201 // Create State proto for uid process state atom.
202 State CreateUidProcessState();
203 
204 // Create State proto for overlay state atom.
205 State CreateOverlayState();
206 
207 // Create State proto for screen state atom with on/off map.
208 State CreateScreenStateWithOnOffMap(int64_t screenOnId, int64_t screenOffId);
209 
210 // Create State proto for screen state atom with simple on/off map.
211 State CreateScreenStateWithSimpleOnOffMap(int64_t screenOnId, int64_t screenOffId);
212 
213 // Create StateGroup proto for ScreenState ON group
214 StateMap_StateGroup CreateScreenStateOnGroup(int64_t screenOnId);
215 
216 // Create StateGroup proto for ScreenState OFF group
217 StateMap_StateGroup CreateScreenStateOffGroup(int64_t screenOffId);
218 
219 // Create StateGroup proto for simple ScreenState ON group
220 StateMap_StateGroup CreateScreenStateSimpleOnGroup(int64_t screenOnId);
221 
222 // Create StateGroup proto for simple ScreenState OFF group
223 StateMap_StateGroup CreateScreenStateSimpleOffGroup(int64_t screenOffId);
224 
225 // Create StateMap proto for ScreenState ON/OFF map
226 StateMap CreateScreenStateOnOffMap(int64_t screenOnId, int64_t screenOffId);
227 
228 // Create StateMap proto for simple ScreenState ON/OFF map
229 StateMap CreateScreenStateSimpleOnOffMap(int64_t screenOnId, int64_t screenOffId);
230 
231 // Add a predicate to the predicate combination.
232 void addPredicateToPredicateCombination(const Predicate& predicate, Predicate* combination);
233 
234 // Create dimensions from primitive fields.
235 FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields);
236 
237 // Create dimensions from repeated primitive fields.
238 FieldMatcher CreateRepeatedDimensions(const int atomId, const std::vector<int>& fields,
239                                       const std::vector<Position>& positions);
240 
241 // Create dimensions by attribution uid and tag.
242 FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
243                                                   const std::vector<Position>& positions);
244 
245 // Create dimensions by attribution uid only.
246 FieldMatcher CreateAttributionUidDimensions(const int atomId,
247                                             const std::vector<Position>& positions);
248 
249 FieldMatcher CreateAttributionUidAndOtherDimensions(const int atomId,
250                                                     const std::vector<Position>& positions,
251                                                     const std::vector<int>& fields);
252 
253 EventMetric createEventMetric(const string& name, const int64_t what,
254                               const optional<int64_t>& condition);
255 
256 CountMetric createCountMetric(const string& name, const int64_t what,
257                               const optional<int64_t>& condition, const vector<int64_t>& states);
258 
259 DurationMetric createDurationMetric(const string& name, const int64_t what,
260                                     const optional<int64_t>& condition,
261                                     const vector<int64_t>& states);
262 
263 GaugeMetric createGaugeMetric(const string& name, const int64_t what,
264                               const GaugeMetric::SamplingType samplingType,
265                               const optional<int64_t>& condition,
266                               const optional<int64_t>& triggerEvent);
267 
268 ValueMetric createValueMetric(const string& name, const AtomMatcher& what, const int valueField,
269                               const optional<int64_t>& condition, const vector<int64_t>& states);
270 
271 KllMetric createKllMetric(const string& name, const AtomMatcher& what, const int valueField,
272                           const optional<int64_t>& condition);
273 
274 Alert createAlert(const string& name, const int64_t metricId, const int buckets,
275                   const int64_t triggerSum);
276 
277 Alarm createAlarm(const string& name, const int64_t offsetMillis, const int64_t periodMillis);
278 
279 Subscription createSubscription(const string& name, const Subscription_RuleType type,
280                                 const int64_t ruleId);
281 
282 // START: get primary key functions
283 // These functions take in atom field information and create FieldValues which are stored in the
284 // given HashableDimensionKey.
285 void getUidProcessKey(int uid, HashableDimensionKey* key);
286 
287 void getOverlayKey(int uid, string packageName, HashableDimensionKey* key);
288 
289 void getPartialWakelockKey(int uid, const std::string& tag, HashableDimensionKey* key);
290 
291 void getPartialWakelockKey(int uid, HashableDimensionKey* key);
292 // END: get primary key functions
293 
294 void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
295                       const vector<string>& attributionTags);
296 
297 // Builds statsEvent to get buffer that is parsed into logEvent then releases statsEvent.
298 void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent);
299 
300 shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1,
301                                             int32_t value2);
302 
303 void CreateTwoValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1,
304                             int32_t value2);
305 
306 shared_ptr<LogEvent> CreateThreeValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1,
307                                               int32_t value2, int32_t value3);
308 
309 void CreateThreeValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1,
310                               int32_t value2, int32_t value3);
311 
312 // The repeated value log event helpers create a log event with two int fields, both
313 // set to the same value. This is useful for testing metrics that are only interested
314 // in the value of the second field but still need the first field to be populated.
315 std::shared_ptr<LogEvent> CreateRepeatedValueLogEvent(int atomId, int64_t eventTimeNs,
316                                                       int32_t value);
317 
318 void CreateRepeatedValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs,
319                                  int32_t value);
320 
321 std::shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs);
322 
323 void CreateNoValuesLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs);
324 
325 AStatsEvent* makeUidStatsEvent(int atomId, int64_t eventTimeNs, int uid, int data1, int data2);
326 
327 AStatsEvent* makeUidStatsEvent(int atomId, int64_t eventTimeNs, int uid, int data1,
328                                const vector<int>& data2);
329 
330 std::shared_ptr<LogEvent> makeUidLogEvent(int atomId, int64_t eventTimeNs, int uid, int data1,
331                                           int data2);
332 
333 std::shared_ptr<LogEvent> makeUidLogEvent(int atomId, int64_t eventTimeNs, int uid, int data1,
334                                           const vector<int>& data2);
335 
336 shared_ptr<LogEvent> makeExtraUidsLogEvent(int atomId, int64_t eventTimeNs, int uid1, int data1,
337                                            int data2, const std::vector<int>& extraUids);
338 
339 std::shared_ptr<LogEvent> makeRepeatedUidLogEvent(int atomId, int64_t eventTimeNs,
340                                                   const std::vector<int>& uids);
341 
342 shared_ptr<LogEvent> makeRepeatedUidLogEvent(int atomId, int64_t eventTimeNs,
343                                              const vector<int>& uids, int data1, int data2);
344 
345 shared_ptr<LogEvent> makeRepeatedUidLogEvent(int atomId, int64_t eventTimeNs,
346                                              const vector<int>& uids, int data1,
347                                              const vector<int>& data2);
348 
349 std::shared_ptr<LogEvent> makeAttributionLogEvent(int atomId, int64_t eventTimeNs,
350                                                   const vector<int>& uids,
351                                                   const vector<string>& tags, int data1, int data2);
352 
353 sp<MockUidMap> makeMockUidMapForHosts(const map<int, vector<int>>& hostUidToIsolatedUidsMap);
354 
355 sp<MockUidMap> makeMockUidMapForPackage(const string& pkg, const set<int32_t>& uids);
356 
357 // Create log event for screen state changed.
358 std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(uint64_t timestampNs,
359                                                         const android::view::DisplayStateEnum state,
360                                                         int loggerUid = 0);
361 
362 // Create log event for screen brightness state changed.
363 std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(uint64_t timestampNs, int level);
364 
365 // Create log event when scheduled job starts.
366 std::unique_ptr<LogEvent> CreateStartScheduledJobEvent(uint64_t timestampNs,
367                                                        const vector<int>& attributionUids,
368                                                        const vector<string>& attributionTags,
369                                                        const string& jobName);
370 
371 // Create log event when scheduled job finishes.
372 std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent(uint64_t timestampNs,
373                                                         const vector<int>& attributionUids,
374                                                         const vector<string>& attributionTags,
375                                                         const string& jobName);
376 
377 // Create log event when battery saver starts.
378 std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs);
379 // Create log event when battery saver stops.
380 std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs);
381 
382 // Create log event when battery state changes.
383 std::unique_ptr<LogEvent> CreateBatteryStateChangedEvent(const uint64_t timestampNs, const BatteryPluggedStateEnum state);
384 
385 // Create log event for app moving to background.
386 std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(uint64_t timestampNs, const int uid);
387 
388 // Create log event for app moving to foreground.
389 std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(uint64_t timestampNs, const int uid);
390 
391 // Create log event when the app sync starts.
392 std::unique_ptr<LogEvent> CreateSyncStartEvent(uint64_t timestampNs, const vector<int>& uids,
393                                                const vector<string>& tags, const string& name);
394 
395 // Create log event when the app sync ends.
396 std::unique_ptr<LogEvent> CreateSyncEndEvent(uint64_t timestampNs, const vector<int>& uids,
397                                              const vector<string>& tags, const string& name);
398 
399 // Create log event when the app sync ends.
400 std::unique_ptr<LogEvent> CreateAppCrashEvent(uint64_t timestampNs, const int uid);
401 
402 // Create log event for an app crash.
403 std::unique_ptr<LogEvent> CreateAppCrashOccurredEvent(uint64_t timestampNs, const int uid);
404 
405 // Create log event for acquiring wakelock.
406 std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(uint64_t timestampNs, const vector<int>& uids,
407                                                      const vector<string>& tags,
408                                                      const string& wakelockName);
409 
410 // Create log event for releasing wakelock.
411 std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(uint64_t timestampNs, const vector<int>& uids,
412                                                      const vector<string>& tags,
413                                                      const string& wakelockName);
414 
415 // Create log event for releasing wakelock.
416 std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(uint64_t timestampNs, int hostUid,
417                                                         int isolatedUid, bool is_create);
418 
419 // Create log event for uid process state change.
420 std::unique_ptr<LogEvent> CreateUidProcessStateChangedEvent(
421         uint64_t timestampNs, int uid, const android::app::ProcessStateEnum state);
422 
423 std::unique_ptr<LogEvent> CreateBleScanStateChangedEvent(uint64_t timestampNs,
424                                                          const vector<int>& attributionUids,
425                                                          const vector<string>& attributionTags,
426                                                          const BleScanStateChanged::State state,
427                                                          const bool filtered, const bool firstMatch,
428                                                          const bool opportunistic);
429 
430 std::unique_ptr<LogEvent> CreateOverlayStateChangedEvent(int64_t timestampNs, const int32_t uid,
431                                                          const string& packageName,
432                                                          const bool usingAlertWindow,
433                                                          const OverlayStateChanged::State state);
434 
435 std::unique_ptr<LogEvent> CreateAppStartOccurredEvent(
436         uint64_t timestampNs, const int uid, const string& pkg_name,
437         AppStartOccurred::TransitionType type, const string& activity_name,
438         const string& calling_pkg_name, const bool is_instant_app, int64_t activity_start_msec);
439 
440 std::unique_ptr<LogEvent> CreateTestAtomReportedEventVariableRepeatedFields(
441         uint64_t timestampNs, const vector<int>& repeatedIntField,
442         const vector<int64_t>& repeatedLongField, const vector<float>& repeatedFloatField,
443         const vector<string>& repeatedStringField, const bool* repeatedBoolField,
444         const size_t repeatedBoolFieldLength, const vector<int>& repeatedEnumField);
445 
446 std::unique_ptr<LogEvent> CreateTestAtomReportedEvent(
447         uint64_t timestampNs, const vector<int>& attributionUids,
448         const vector<string>& attributionTags, const int intField, const long longField,
449         const float floatField, const string& stringField, const bool boolField,
450         const TestAtomReported::State enumField, const vector<uint8_t>& bytesField,
451         const vector<int>& repeatedIntField, const vector<int64_t>& repeatedLongField,
452         const vector<float>& repeatedFloatField, const vector<string>& repeatedStringField,
453         const bool* repeatedBoolField, const size_t repeatedBoolFieldLength,
454         const vector<int>& repeatedEnumField);
455 
456 // Create a statsd log event processor upon the start time in seconds, config and key.
457 sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs,
458                                               const StatsdConfig& config, const ConfigKey& key,
459                                               const shared_ptr<IPullAtomCallback>& puller = nullptr,
460                                               const int32_t atomTag = 0 /*for puller only*/,
461                                               const sp<UidMap> = new UidMap());
462 
463 // Util function to sort the log events by timestamp.
464 void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events);
465 
466 int64_t StringToId(const string& str);
467 
468 sp<EventMatcherWizard> createEventMatcherWizard(
469         int tagId, int matcherIndex, const std::vector<FieldValueMatcher>& fieldValueMatchers = {});
470 
471 StatsDimensionsValueParcel CreateAttributionUidDimensionsValueParcel(const int atomId,
472                                                                      const int uid);
473 
474 void ValidateUidDimension(const DimensionsValue& value, int atomId, int uid);
475 void ValidateWakelockAttributionUidAndTagDimension(const DimensionsValue& value, const int atomId,
476                                                    const int uid, const string& tag);
477 void ValidateUidDimension(const DimensionsValue& value, int node_idx, int atomId, int uid);
478 void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid);
479 void ValidateAttributionUidAndTagDimension(
480     const DimensionsValue& value, int atomId, int uid, const std::string& tag);
481 void ValidateAttributionUidAndTagDimension(
482     const DimensionsValue& value, int node_idx, int atomId, int uid, const std::string& tag);
483 void ValidateStateValue(const google::protobuf::RepeatedPtrField<StateValue>& stateValues,
484                         int atomId, int64_t value);
485 
486 void ValidateCountBucket(const CountBucketInfo& countBucket, int64_t startTimeNs, int64_t endTimeNs,
487                          int64_t count);
488 void ValidateDurationBucket(const DurationBucketInfo& bucket, int64_t startTimeNs,
489                             int64_t endTimeNs, int64_t durationNs);
490 void ValidateGaugeBucketTimes(const GaugeBucketInfo& gaugeBucket, int64_t startTimeNs,
491                               int64_t endTimeNs, vector<int64_t> eventTimesNs);
492 void ValidateValueBucket(const ValueBucketInfo& bucket, int64_t startTimeNs, int64_t endTimeNs,
493                          const vector<int64_t>& values, int64_t conditionTrueNs,
494                          int64_t conditionCorrectionNs);
495 void ValidateKllBucket(const KllBucketInfo& bucket, int64_t startTimeNs, int64_t endTimeNs,
496                        const std::vector<int64_t> sketchSizes, int64_t conditionTrueNs);
497 
498 struct DimensionsPair {
DimensionsPairDimensionsPair499     DimensionsPair(DimensionsValue m1, google::protobuf::RepeatedPtrField<StateValue> m2)
500         : dimInWhat(m1), stateValues(m2){};
501 
502     DimensionsValue dimInWhat;
503     google::protobuf::RepeatedPtrField<StateValue> stateValues;
504 };
505 
506 bool LessThan(const StateValue& s1, const StateValue& s2);
507 bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2);
508 bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2);
509 
510 void backfillStartEndTimestamp(StatsLogReport* report);
511 void backfillStartEndTimestamp(ConfigMetricsReport *config_report);
512 void backfillStartEndTimestamp(ConfigMetricsReportList *config_report_list);
513 
514 void backfillStringInReport(ConfigMetricsReportList *config_report_list);
515 void backfillStringInDimension(const std::map<uint64_t, string>& str_map,
516                                DimensionsValue* dimension);
517 
518 void backfillAggregatedAtoms(ConfigMetricsReportList* config_report_list);
519 void backfillAggregatedAtoms(ConfigMetricsReport* config_report);
520 void backfillAggregatedAtoms(StatsLogReport* report);
521 void backfillAggregatedAtomsInEventMetric(StatsLogReport::EventMetricDataWrapper* wrapper);
522 void backfillAggregatedAtomsInGaugeMetric(StatsLogReport::GaugeMetricDataWrapper* wrapper);
523 
524 vector<pair<Atom, int64_t>> unnestGaugeAtomData(const GaugeBucketInfo& bucketInfo);
525 
526 template <typename T>
backfillStringInDimension(const std::map<uint64_t,string> & str_map,T * metrics)527 void backfillStringInDimension(const std::map<uint64_t, string>& str_map,
528                                T* metrics) {
529     for (int i = 0; i < metrics->data_size(); ++i) {
530         auto data = metrics->mutable_data(i);
531         if (data->has_dimensions_in_what()) {
532             backfillStringInDimension(str_map, data->mutable_dimensions_in_what());
533         }
534         if (data->has_dimensions_in_condition()) {
535             backfillStringInDimension(str_map, data->mutable_dimensions_in_condition());
536         }
537     }
538 }
539 
540 void backfillDimensionPath(StatsLogReport* report);
541 void backfillDimensionPath(ConfigMetricsReport* config_report);
542 void backfillDimensionPath(ConfigMetricsReportList* config_report_list);
543 
544 bool backfillDimensionPath(const DimensionsValue& path,
545                            const google::protobuf::RepeatedPtrField<DimensionsValue>& leafValues,
546                            DimensionsValue* dimension);
547 
548 void sortReportsByElapsedTime(ConfigMetricsReportList* configReportList);
549 
550 class FakeSubsystemSleepCallback : public BnPullAtomCallback {
551 public:
552     // Track the number of pulls.
553     int pullNum = 1;
554     Status onPullAtom(int atomTag,
555                       const shared_ptr<IPullAtomResultReceiver>& resultReceiver) override;
556 };
557 
558 template <typename T>
backfillDimensionPath(const DimensionsValue & whatPath,const DimensionsValue & conditionPath,T * metricData)559 void backfillDimensionPath(const DimensionsValue& whatPath,
560                            const DimensionsValue& conditionPath,
561                            T* metricData) {
562     for (int i = 0; i < metricData->data_size(); ++i) {
563         auto data = metricData->mutable_data(i);
564         if (data->dimension_leaf_values_in_what_size() > 0) {
565             backfillDimensionPath(whatPath, data->dimension_leaf_values_in_what(),
566                                   data->mutable_dimensions_in_what());
567             data->clear_dimension_leaf_values_in_what();
568         }
569         if (data->dimension_leaf_values_in_condition_size() > 0) {
570             backfillDimensionPath(conditionPath, data->dimension_leaf_values_in_condition(),
571                                   data->mutable_dimensions_in_condition());
572             data->clear_dimension_leaf_values_in_condition();
573         }
574     }
575 }
576 
577 struct DimensionCompare {
operatorDimensionCompare578     bool operator()(const DimensionsPair& s1, const DimensionsPair& s2) const {
579         return LessThan(s1, s2);
580     }
581 };
582 
583 template <typename T>
sortMetricDataByDimensionsValue(const T & metricData,T * sortedMetricData)584 void sortMetricDataByDimensionsValue(const T& metricData, T* sortedMetricData) {
585     std::map<DimensionsPair, int, DimensionCompare> dimensionIndexMap;
586     for (int i = 0; i < metricData.data_size(); ++i) {
587         dimensionIndexMap.insert(
588                 std::make_pair(DimensionsPair(metricData.data(i).dimensions_in_what(),
589                                               metricData.data(i).slice_by_state()),
590                                i));
591     }
592     for (const auto& itr : dimensionIndexMap) {
593         *sortedMetricData->add_data() = metricData.data(itr.second);
594     }
595 }
596 
597 template <typename T>
sortMetricDataByFirstDimensionLeafValue(const T & metricData,T * sortedMetricData)598 void sortMetricDataByFirstDimensionLeafValue(const T& metricData, T* sortedMetricData) {
599     std::map<DimensionsPair, int, DimensionCompare> dimensionIndexMap;
600     for (int i = 0; i < metricData.data_size(); ++i) {
601         dimensionIndexMap.insert(
602                 std::make_pair(DimensionsPair(metricData.data(i).dimension_leaf_values_in_what()[0],
603                                               metricData.data(i).slice_by_state()),
604                                i));
605     }
606     for (const auto& itr : dimensionIndexMap) {
607         *sortedMetricData->add_data() = metricData.data(itr.second);
608     }
609 }
610 
611 template <typename T>
backfillStartEndTimestampForFullBucket(const int64_t timeBaseNs,const int64_t bucketSizeNs,T * bucket)612 void backfillStartEndTimestampForFullBucket(
613     const int64_t timeBaseNs, const int64_t bucketSizeNs, T* bucket) {
614     bucket->set_start_bucket_elapsed_nanos(timeBaseNs + bucketSizeNs * bucket->bucket_num());
615     bucket->set_end_bucket_elapsed_nanos(
616         timeBaseNs + bucketSizeNs * bucket->bucket_num() + bucketSizeNs);
617     bucket->clear_bucket_num();
618 }
619 
620 template <typename T>
backfillStartEndTimestampForPartialBucket(const int64_t timeBaseNs,T * bucket)621 void backfillStartEndTimestampForPartialBucket(const int64_t timeBaseNs, T* bucket) {
622     if (bucket->has_start_bucket_elapsed_millis()) {
623         bucket->set_start_bucket_elapsed_nanos(
624             MillisToNano(bucket->start_bucket_elapsed_millis()));
625         bucket->clear_start_bucket_elapsed_millis();
626     }
627     if (bucket->has_end_bucket_elapsed_millis()) {
628         bucket->set_end_bucket_elapsed_nanos(
629             MillisToNano(bucket->end_bucket_elapsed_millis()));
630         bucket->clear_end_bucket_elapsed_millis();
631     }
632 }
633 
634 template <typename T>
backfillStartEndTimestampForMetrics(const int64_t timeBaseNs,const int64_t bucketSizeNs,T * metrics)635 void backfillStartEndTimestampForMetrics(const int64_t timeBaseNs, const int64_t bucketSizeNs,
636                                          T* metrics) {
637     for (int i = 0; i < metrics->data_size(); ++i) {
638         auto data = metrics->mutable_data(i);
639         for (int j = 0; j < data->bucket_info_size(); ++j) {
640             auto bucket = data->mutable_bucket_info(j);
641             if (bucket->has_bucket_num()) {
642                 backfillStartEndTimestampForFullBucket(timeBaseNs, bucketSizeNs, bucket);
643             } else {
644                 backfillStartEndTimestampForPartialBucket(timeBaseNs, bucket);
645             }
646         }
647     }
648 }
649 
650 template <typename T>
backfillStartEndTimestampForSkippedBuckets(const int64_t timeBaseNs,T * metrics)651 void backfillStartEndTimestampForSkippedBuckets(const int64_t timeBaseNs, T* metrics) {
652     for (int i = 0; i < metrics->skipped_size(); ++i) {
653         backfillStartEndTimestampForPartialBucket(timeBaseNs, metrics->mutable_skipped(i));
654     }
655 }
656 
isAtLeastSFuncTrue()657 inline bool isAtLeastSFuncTrue() {
658     return true;
659 }
660 
isAtLeastSFuncFalse()661 inline bool isAtLeastSFuncFalse() {
662     return false;
663 }
664 
getServerFlagFuncTrue(const std::string & flagNamespace,const std::string & flagName,const std::string & defaultValue)665 inline std::string getServerFlagFuncTrue(const std::string& flagNamespace,
666                                          const std::string& flagName,
667                                          const std::string& defaultValue) {
668     return FLAG_TRUE;
669 }
670 
getServerFlagFuncFalse(const std::string & flagNamespace,const std::string & flagName,const std::string & defaultValue)671 inline std::string getServerFlagFuncFalse(const std::string& flagNamespace,
672                                           const std::string& flagName,
673                                           const std::string& defaultValue) {
674     return FLAG_FALSE;
675 }
676 
677 void writeFlag(const std::string& flagName, const std::string& flagValue);
678 
679 void writeBootFlag(const std::string& flagName, const std::string& flagValue);
680 
681 }  // namespace statsd
682 }  // namespace os
683 }  // namespace android
684