diff options
Diffstat (limited to 'cmds/statsd/tests/statsd_test_util.h')
-rw-r--r-- | cmds/statsd/tests/statsd_test_util.h | 235 |
1 files changed, 196 insertions, 39 deletions
diff --git a/cmds/statsd/tests/statsd_test_util.h b/cmds/statsd/tests/statsd_test_util.h index 635c5835333a..3dcf4ecce054 100644 --- a/cmds/statsd/tests/statsd_test_util.h +++ b/cmds/statsd/tests/statsd_test_util.h @@ -14,20 +14,47 @@ #pragma once +#include <aidl/android/os/BnPullAtomCallback.h> +#include <aidl/android/os/IPullAtomCallback.h> +#include <aidl/android/os/IPullAtomResultReceiver.h> +#include <gmock/gmock.h> #include <gtest/gtest.h> + #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" #include "src/StatsLogProcessor.h" -#include "src/logd/LogEvent.h" #include "src/hash.h" +#include "src/logd/LogEvent.h" +#include "src/packages/UidMap.h" #include "src/stats_log_util.h" -#include "statslog.h" +#include "stats_event.h" +#include "statslog_statsdtest.h" namespace android { namespace os { namespace statsd { +using namespace testing; +using ::aidl::android::os::BnPullAtomCallback; +using ::aidl::android::os::IPullAtomCallback; +using ::aidl::android::os::IPullAtomResultReceiver; +using android::util::ProtoReader; using google::protobuf::RepeatedPtrField; +using Status = ::ndk::ScopedAStatus; + +const int SCREEN_STATE_ATOM_ID = util::SCREEN_STATE_CHANGED; +const int UID_PROCESS_STATE_ATOM_ID = util::UID_PROCESS_STATE_CHANGED; + +enum BucketSplitEvent { APP_UPGRADE, BOOT_COMPLETE }; + +class MockUidMap : public UidMap { +public: + MOCK_METHOD(int, getHostUidOrSelf, (int uid), (const)); + MOCK_METHOD(std::set<int32_t>, getAppUid, (const string& package), (const)); +}; + +// Converts a ProtoOutputStream to a StatsLogReport proto. +StatsLogReport outputStreamToProto(ProtoOutputStream* proto); // Create AtomMatcher proto to simply match a specific atom type. AtomMatcher CreateSimpleAtomMatcher(const string& name, int atomId); @@ -53,6 +80,12 @@ AtomMatcher CreateBatterySaverModeStartAtomMatcher(); // Create AtomMatcher proto for stopping battery save mode. AtomMatcher CreateBatterySaverModeStopAtomMatcher(); +// Create AtomMatcher proto for battery state none mode. +AtomMatcher CreateBatteryStateNoneMatcher(); + +// Create AtomMatcher proto for battery state usb mode. +AtomMatcher CreateBatteryStateUsbMatcher(); + // Create AtomMatcher proto for process state changed. AtomMatcher CreateUidProcessStateChangedAtomMatcher(); @@ -95,6 +128,9 @@ Predicate CreateScheduledJobPredicate(); // Create Predicate proto for battery saver mode. Predicate CreateBatterySaverModePredicate(); +// Create Predicate proto for device unplogged mode. +Predicate CreateDeviceUnpluggedPredicate(); + // Create Predicate proto for holding wakelock. Predicate CreateHoldingWakelockPredicate(); @@ -104,6 +140,39 @@ Predicate CreateIsSyncingPredicate(); // Create a Predicate proto for app is in background. Predicate CreateIsInBackgroundPredicate(); +// Create State proto for screen state atom. +State CreateScreenState(); + +// Create State proto for uid process state atom. +State CreateUidProcessState(); + +// Create State proto for overlay state atom. +State CreateOverlayState(); + +// Create State proto for screen state atom with on/off map. +State CreateScreenStateWithOnOffMap(int64_t screenOnId, int64_t screenOffId); + +// Create State proto for screen state atom with simple on/off map. +State CreateScreenStateWithSimpleOnOffMap(int64_t screenOnId, int64_t screenOffId); + +// Create StateGroup proto for ScreenState ON group +StateMap_StateGroup CreateScreenStateOnGroup(int64_t screenOnId); + +// Create StateGroup proto for ScreenState OFF group +StateMap_StateGroup CreateScreenStateOffGroup(int64_t screenOffId); + +// Create StateGroup proto for simple ScreenState ON group +StateMap_StateGroup CreateScreenStateSimpleOnGroup(int64_t screenOnId); + +// Create StateGroup proto for simple ScreenState OFF group +StateMap_StateGroup CreateScreenStateSimpleOffGroup(int64_t screenOffId); + +// Create StateMap proto for ScreenState ON/OFF map +StateMap CreateScreenStateOnOffMap(int64_t screenOnId, int64_t screenOffId); + +// Create StateMap proto for simple ScreenState ON/OFF map +StateMap CreateScreenStateSimpleOnOffMap(int64_t screenOnId, int64_t screenOffId); + // Add a predicate to the predicate combination. void addPredicateToPredicateCombination(const Predicate& predicate, Predicate* combination); @@ -118,76 +187,156 @@ FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId, FieldMatcher CreateAttributionUidDimensions(const int atomId, const std::vector<Position>& positions); +FieldMatcher CreateAttributionUidAndOtherDimensions(const int atomId, + const std::vector<Position>& positions, + const std::vector<int>& fields); + +// START: get primary key functions +// These functions take in atom field information and create FieldValues which are stored in the +// given HashableDimensionKey. +void getUidProcessKey(int uid, HashableDimensionKey* key); + +void getOverlayKey(int uid, string packageName, HashableDimensionKey* key); + +void getPartialWakelockKey(int uid, const std::string& tag, HashableDimensionKey* key); + +void getPartialWakelockKey(int uid, HashableDimensionKey* key); +// END: get primary key functions + +void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids, + const vector<string>& attributionTags); + +// Builds statsEvent to get buffer that is parsed into logEvent then releases statsEvent. +void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent); + +shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1, + int32_t value2); + +void CreateTwoValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1, + int32_t value2); + +shared_ptr<LogEvent> CreateThreeValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1, + int32_t value2, int32_t value3); + +void CreateThreeValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1, + int32_t value2, int32_t value3); + +// The repeated value log event helpers create a log event with two int fields, both +// set to the same value. This is useful for testing metrics that are only interested +// in the value of the second field but still need the first field to be populated. +std::shared_ptr<LogEvent> CreateRepeatedValueLogEvent(int atomId, int64_t eventTimeNs, + int32_t value); + +void CreateRepeatedValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, + int32_t value); + +std::shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs); + +void CreateNoValuesLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs); + +std::shared_ptr<LogEvent> makeUidLogEvent(int atomId, int64_t eventTimeNs, int uid, int data1, + int data2); + +std::shared_ptr<LogEvent> makeAttributionLogEvent(int atomId, int64_t eventTimeNs, + const vector<int>& uids, + const vector<string>& tags, int data1, int data2); + +sp<MockUidMap> makeMockUidMapForOneHost(int hostUid, const vector<int>& isolatedUids); + +sp<MockUidMap> makeMockUidMapForPackage(const string& pkg, const set<int32_t>& uids); + // Create log event for screen state changed. -std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( - const android::view::DisplayStateEnum state, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(uint64_t timestampNs, + const android::view::DisplayStateEnum state, + int loggerUid = 0); // Create log event for screen brightness state changed. -std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent( - int level, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(uint64_t timestampNs, int level); // Create log event when scheduled job starts. -std::unique_ptr<LogEvent> CreateStartScheduledJobEvent( - const std::vector<AttributionNodeInternal>& attributions, - const string& name, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateStartScheduledJobEvent(uint64_t timestampNs, + const vector<int>& attributionUids, + const vector<string>& attributionTags, + const string& jobName); // Create log event when scheduled job finishes. -std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent( - const std::vector<AttributionNodeInternal>& attributions, - const string& name, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateFinishScheduledJobEvent(uint64_t timestampNs, + const vector<int>& attributionUids, + const vector<string>& attributionTags, + const string& jobName); // Create log event when battery saver starts. std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs); // Create log event when battery saver stops. std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs); +// Create log event when battery state changes. +std::unique_ptr<LogEvent> CreateBatteryStateChangedEvent(const uint64_t timestampNs, const BatteryPluggedStateEnum state); + // Create log event for app moving to background. -std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(const int uid, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateMoveToBackgroundEvent(uint64_t timestampNs, const int uid); // Create log event for app moving to foreground. -std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(const int uid, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateMoveToForegroundEvent(uint64_t timestampNs, const int uid); // Create log event when the app sync starts. -std::unique_ptr<LogEvent> CreateSyncStartEvent( - const std::vector<AttributionNodeInternal>& attributions, const string& name, - uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateSyncStartEvent(uint64_t timestampNs, const vector<int>& uids, + const vector<string>& tags, const string& name); // Create log event when the app sync ends. -std::unique_ptr<LogEvent> CreateSyncEndEvent( - const std::vector<AttributionNodeInternal>& attributions, const string& name, - uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateSyncEndEvent(uint64_t timestampNs, const vector<int>& uids, + const vector<string>& tags, const string& name); // Create log event when the app sync ends. -std::unique_ptr<LogEvent> CreateAppCrashEvent( - const int uid, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateAppCrashEvent(uint64_t timestampNs, const int uid); + +// Create log event for an app crash. +std::unique_ptr<LogEvent> CreateAppCrashOccurredEvent(uint64_t timestampNs, const int uid); // Create log event for acquiring wakelock. -std::unique_ptr<LogEvent> CreateAcquireWakelockEvent( - const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName, - uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateAcquireWakelockEvent(uint64_t timestampNs, const vector<int>& uids, + const vector<string>& tags, + const string& wakelockName); // Create log event for releasing wakelock. -std::unique_ptr<LogEvent> CreateReleaseWakelockEvent( - const std::vector<AttributionNodeInternal>& attributions, const string& wakelockName, - uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateReleaseWakelockEvent(uint64_t timestampNs, const vector<int>& uids, + const vector<string>& tags, + const string& wakelockName); // Create log event for releasing wakelock. -std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent( - int isolatedUid, int hostUid, bool is_create, uint64_t timestampNs); +std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(uint64_t timestampNs, int hostUid, + int isolatedUid, bool is_create); + +// Create log event for uid process state change. +std::unique_ptr<LogEvent> CreateUidProcessStateChangedEvent( + uint64_t timestampNs, int uid, const android::app::ProcessStateEnum state); + +std::unique_ptr<LogEvent> CreateBleScanStateChangedEvent(uint64_t timestampNs, + const vector<int>& attributionUids, + const vector<string>& attributionTags, + const BleScanStateChanged::State state, + const bool filtered, const bool firstMatch, + const bool opportunistic); -// Helper function to create an AttributionNodeInternal proto. -AttributionNodeInternal CreateAttribution(const int& uid, const string& tag); +std::unique_ptr<LogEvent> CreateOverlayStateChangedEvent(int64_t timestampNs, const int32_t uid, + const string& packageName, + const bool usingAlertWindow, + const OverlayStateChanged::State state); // Create a statsd log event processor upon the start time in seconds, config and key. -sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, - const int64_t currentTimeNs, - const StatsdConfig& config, const ConfigKey& key); +sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, + const StatsdConfig& config, const ConfigKey& key, + const shared_ptr<IPullAtomCallback>& puller = nullptr, + const int32_t atomTag = 0 /*for puller only*/, + const sp<UidMap> = new UidMap()); // Util function to sort the log events by timestamp. void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events); int64_t StringToId(const string& str); +void ValidateWakelockAttributionUidAndTagDimension(const DimensionsValue& value, const int atomId, + const int uid, const string& tag); void ValidateUidDimension(const DimensionsValue& value, int node_idx, int atomId, int uid); void ValidateAttributionUidDimension(const DimensionsValue& value, int atomId, int uid); void ValidateAttributionUidAndTagDimension( @@ -196,12 +345,14 @@ void ValidateAttributionUidAndTagDimension( const DimensionsValue& value, int node_idx, int atomId, int uid, const std::string& tag); struct DimensionsPair { - DimensionsPair(DimensionsValue m1, DimensionsValue m2) : dimInWhat(m1), dimInCondition(m2){}; + DimensionsPair(DimensionsValue m1, google::protobuf::RepeatedPtrField<StateValue> m2) + : dimInWhat(m1), stateValues(m2){}; DimensionsValue dimInWhat; - DimensionsValue dimInCondition; + google::protobuf::RepeatedPtrField<StateValue> stateValues; }; +bool LessThan(const StateValue& s1, const StateValue& s2); bool LessThan(const DimensionsValue& s1, const DimensionsValue& s2); bool LessThan(const DimensionsPair& s1, const DimensionsPair& s2); @@ -233,6 +384,12 @@ bool backfillDimensionPath(const DimensionsValue& path, const google::protobuf::RepeatedPtrField<DimensionsValue>& leafValues, DimensionsValue* dimension); +class FakeSubsystemSleepCallback : public BnPullAtomCallback { +public: + Status onPullAtom(int atomTag, + const shared_ptr<IPullAtomResultReceiver>& resultReceiver) override; +}; + template <typename T> void backfillDimensionPath(const DimensionsValue& whatPath, const DimensionsValue& conditionPath, @@ -264,7 +421,7 @@ void sortMetricDataByDimensionsValue(const T& metricData, T* sortedMetricData) { for (int i = 0; i < metricData.data_size(); ++i) { dimensionIndexMap.insert( std::make_pair(DimensionsPair(metricData.data(i).dimensions_in_what(), - metricData.data(i).dimensions_in_condition()), + metricData.data(i).slice_by_state()), i)); } for (const auto& itr : dimensionIndexMap) { @@ -319,4 +476,4 @@ void backfillStartEndTimestampForSkippedBuckets(const int64_t timeBaseNs, T* met } } // namespace statsd } // namespace os -} // namespace android
\ No newline at end of file +} // namespace android |