Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #pragma once
     18 
     19 #include <gtest/gtest_prod.h>
     20 #include "config/ConfigListener.h"
     21 #include "metrics/MetricsManager.h"
     22 #include "packages/UidMap.h"
     23 #include "external/StatsPullerManager.h"
     24 
     25 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
     26 
     27 #include <stdio.h>
     28 #include <unordered_map>
     29 
     30 namespace android {
     31 namespace os {
     32 namespace statsd {
     33 
     34 
     35 class StatsLogProcessor : public ConfigListener {
     36 public:
     37     StatsLogProcessor(const sp<UidMap>& uidMap, const sp<StatsPullerManager>& pullerManager,
     38                       const sp<AlarmMonitor>& anomalyAlarmMonitor,
     39                       const sp<AlarmMonitor>& subscriberTriggerAlarmMonitor,
     40                       const int64_t timeBaseNs,
     41                       const std::function<bool(const ConfigKey&)>& sendBroadcast,
     42                       const std::function<bool(const int&,
     43                                                const vector<int64_t>&)>& sendActivationBroadcast);
     44     virtual ~StatsLogProcessor();
     45 
     46     void OnLogEvent(LogEvent* event);
     47 
     48     void OnConfigUpdated(const int64_t timestampNs, const ConfigKey& key,
     49                          const StatsdConfig& config);
     50     void OnConfigRemoved(const ConfigKey& key);
     51 
     52     size_t GetMetricsSize(const ConfigKey& key) const;
     53 
     54     void GetActiveConfigs(const int uid, vector<int64_t>& outActiveConfigs);
     55 
     56     void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
     57                       const bool include_current_partial_bucket, const bool erase_data,
     58                       const DumpReportReason dumpReportReason,
     59                       const DumpLatency dumpLatency,
     60                       vector<uint8_t>* outData);
     61     void onDumpReport(const ConfigKey& key, const int64_t dumpTimeNs,
     62                       const bool include_current_partial_bucket, const bool erase_data,
     63                       const DumpReportReason dumpReportReason,
     64                       const DumpLatency dumpLatency,
     65                       ProtoOutputStream* proto);
     66 
     67     /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies anomaly alarmSet. */
     68     void onAnomalyAlarmFired(
     69             const int64_t& timestampNs,
     70             unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
     71 
     72     /* Tells MetricsManager that the alarms in alarmSet have fired. Modifies periodic alarmSet. */
     73     void onPeriodicAlarmFired(
     74             const int64_t& timestampNs,
     75             unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> alarmSet);
     76 
     77     /* Flushes data to disk. Data on memory will be gone after written to disk. */
     78     void WriteDataToDisk(const DumpReportReason dumpReportReason,
     79                          const DumpLatency dumpLatency);
     80 
     81     /* Persist configs containing metrics with active activations to disk. */
     82     void SaveActiveConfigsToDisk(int64_t currentTimeNs);
     83 
     84     /* Writes the current active status/ttl for all configs and metrics to ProtoOutputStream. */
     85     void WriteActiveConfigsToProtoOutputStream(
     86             int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
     87 
     88     /* Load configs containing metrics with active activations from disk. */
     89     void LoadActiveConfigsFromDisk();
     90 
     91     /* Sets the active status/ttl for all configs and metrics to the status in ActiveConfigList. */
     92     void SetConfigsActiveState(const ActiveConfigList& activeConfigList, int64_t currentTimeNs);
     93 
     94     // Reset all configs.
     95     void resetConfigs();
     96 
     97     inline sp<UidMap> getUidMap() {
     98         return mUidMap;
     99     }
    100 
    101     void dumpStates(int outFd, bool verbose);
    102 
    103     void informPullAlarmFired(const int64_t timestampNs);
    104 
    105     int64_t getLastReportTimeNs(const ConfigKey& key);
    106 
    107     inline void setPrintLogs(bool enabled) {
    108 #ifdef VERY_VERBOSE_PRINTING
    109         std::lock_guard<std::mutex> lock(mMetricsMutex);
    110         mPrintAllLogs = enabled;
    111 #endif
    112     }
    113 
    114     // Add a specific config key to the possible configs to dump ASAP.
    115     void noteOnDiskData(const ConfigKey& key);
    116 
    117 private:
    118     // For testing only.
    119     inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
    120         return mAnomalyAlarmMonitor;
    121     }
    122 
    123     inline sp<AlarmMonitor> getPeriodicAlarmMonitor() const {
    124         return mPeriodicAlarmMonitor;
    125     }
    126 
    127     mutable mutex mMetricsMutex;
    128 
    129     std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
    130 
    131     std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
    132 
    133     // Last time we sent a broadcast to this uid that the active configs had changed.
    134     std::unordered_map<int, long> mLastActivationBroadcastTimes;
    135 
    136     // Tracks when we last checked the bytes consumed for each config key.
    137     std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
    138 
    139     // Tracks which config keys has metric reports on disk
    140     std::set<ConfigKey> mOnDiskDataConfigs;
    141 
    142     sp<UidMap> mUidMap;  // Reference to the UidMap to lookup app name and version for each uid.
    143 
    144     sp<StatsPullerManager> mPullerManager;  // Reference to StatsPullerManager
    145 
    146     sp<AlarmMonitor> mAnomalyAlarmMonitor;
    147 
    148     sp<AlarmMonitor> mPeriodicAlarmMonitor;
    149 
    150     void resetIfConfigTtlExpiredLocked(const int64_t timestampNs);
    151 
    152     void OnConfigUpdatedLocked(
    153         const int64_t currentTimestampNs, const ConfigKey& key, const StatsdConfig& config);
    154 
    155     void GetActiveConfigsLocked(const int uid, vector<int64_t>& outActiveConfigs);
    156 
    157     void WriteActiveConfigsToProtoOutputStreamLocked(
    158             int64_t currentTimeNs, const DumpReportReason reason, ProtoOutputStream* proto);
    159 
    160     void SetConfigsActiveStateLocked(const ActiveConfigList& activeConfigList,
    161                                      int64_t currentTimeNs);
    162 
    163     void WriteDataToDiskLocked(const DumpReportReason dumpReportReason,
    164                                const DumpLatency dumpLatency);
    165     void WriteDataToDiskLocked(const ConfigKey& key, const int64_t timestampNs,
    166                                const DumpReportReason dumpReportReason,
    167                                const DumpLatency dumpLatency);
    168 
    169     void onConfigMetricsReportLocked(
    170             const ConfigKey& key, const int64_t dumpTimeStampNs,
    171             const bool include_current_partial_bucket, const bool erase_data,
    172             const DumpReportReason dumpReportReason, const DumpLatency dumpLatency,
    173             /*if dataSavedToDisk is true, it indicates the caller will write the data to disk
    174              (e.g., before reboot). So no need to further persist local history.*/
    175             const bool dataSavedToDisk, vector<uint8_t>* proto);
    176 
    177     /* Check if we should send a broadcast if approaching memory limits and if we're over, we
    178      * actually delete the data. */
    179     void flushIfNecessaryLocked(int64_t timestampNs, const ConfigKey& key,
    180                                 MetricsManager& metricsManager);
    181 
    182     // Maps the isolated uid in the log event to host uid if the log event contains uid fields.
    183     void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const;
    184 
    185     // Handler over the isolated uid change event.
    186     void onIsolatedUidChangedEventLocked(const LogEvent& event);
    187 
    188     // Reset all configs.
    189     void resetConfigsLocked(const int64_t timestampNs);
    190     // Reset the specified configs.
    191     void resetConfigsLocked(const int64_t timestampNs, const std::vector<ConfigKey>& configs);
    192 
    193     // Function used to send a broadcast so that receiver for the config key can call getData
    194     // to retrieve the stored data.
    195     std::function<bool(const ConfigKey& key)> mSendBroadcast;
    196 
    197     // Function used to send a broadcast so that receiver can be notified of which configs
    198     // are currently active.
    199     std::function<bool(const int& uid, const vector<int64_t>& configIds)> mSendActivationBroadcast;
    200 
    201     const int64_t mTimeBaseNs;
    202 
    203     // Largest timestamp of the events that we have processed.
    204     int64_t mLargestTimestampSeen = 0;
    205 
    206     int64_t mLastTimestampSeen = 0;
    207 
    208     long mLastPullerCacheClearTimeSec = 0;
    209 
    210     // Last time we wrote data to disk.
    211     int64_t mLastWriteTimeNs = 0;
    212 
    213     // Last time we wrote active metrics to disk.
    214     int64_t mLastActiveMetricsWriteNs = 0;
    215 
    216 #ifdef VERY_VERBOSE_PRINTING
    217     bool mPrintAllLogs = false;
    218 #endif
    219 
    220     FRIEND_TEST(StatsLogProcessorTest, TestOutOfOrderLogs);
    221     FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
    222     FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
    223     FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
    224     FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
    225     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
    226     FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBootMultipleActivations);
    227     FRIEND_TEST(StatsLogProcessorTest,
    228             TestActivationOnBootMultipleActivationsDifferentActivationTypes);
    229     FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
    230 
    231     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1);
    232     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2);
    233     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration3);
    234     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration1);
    235     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration2);
    236     FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForMaxDuration3);
    237     FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks1);
    238     FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks2);
    239     FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByFirstUid);
    240     FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSliceByChain);
    241     FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
    242     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
    243     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
    244     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation);
    245     FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition);
    246     FRIEND_TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents);
    247     FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
    248     FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
    249     FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation);
    250 
    251     FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_NoLink_OR_CombinationCondition);
    252     FRIEND_TEST(DimensionInConditionE2eTest, TestCreateCountMetric_Link_OR_CombinationCondition);
    253     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_OR_CombinationCondition);
    254     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_OR_CombinationCondition);
    255 
    256     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_SimpleCondition);
    257     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_SimpleCondition);
    258     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_SimpleCondition);
    259 
    260     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_PartialLink_AND_CombinationCondition);
    261     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_NoLink_AND_CombinationCondition);
    262     FRIEND_TEST(DimensionInConditionE2eTest, TestDurationMetric_Link_AND_CombinationCondition);
    263 
    264     FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
    265     FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
    266     FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_single_bucket);
    267     FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_multiple_buckets);
    268     FRIEND_TEST(AnomalyDetectionE2eTest, TestDurationMetric_SUM_long_refractory_period);
    269 
    270     FRIEND_TEST(AlarmE2eTest, TestMultipleAlarms);
    271     FRIEND_TEST(ConfigTtlE2eTest, TestCountMetric);
    272     FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);
    273     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithOneDeactivation);
    274     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoDeactivations);
    275     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
    276     FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
    277 
    278     FRIEND_TEST(DurationMetricE2eTest, TestOneBucket);
    279     FRIEND_TEST(DurationMetricE2eTest, TestTwoBuckets);
    280     FRIEND_TEST(DurationMetricE2eTest, TestWithActivation);
    281     FRIEND_TEST(DurationMetricE2eTest, TestWithCondition);
    282     FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedCondition);
    283     FRIEND_TEST(DurationMetricE2eTest, TestWithActivationAndSlicedCondition);
    284 };
    285 
    286 }  // namespace statsd
    287 }  // namespace os
    288 }  // namespace android
    289