Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 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 | */ |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 16 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <gtest/gtest_prod.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "config/ConfigListener.h" |
| 21 | #include "logd/LogReader.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 22 | #include "metrics/MetricsManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 23 | #include "packages/UidMap.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 24 | |
| 25 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 26 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 28 | #include <unordered_map> |
| 29 | |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 33 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 34 | class StatsLogProcessor : public ConfigListener { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 35 | public: |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 36 | StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AnomalyMonitor>& anomalyMonitor, |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 37 | const long timeBaseSec, |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 38 | const std::function<void(const ConfigKey&)>& sendBroadcast); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 39 | virtual ~StatsLogProcessor(); |
| 40 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 41 | void OnLogEvent(LogEvent* event); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 42 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 43 | void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config); |
| 44 | void OnConfigRemoved(const ConfigKey& key); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 45 | |
Yangster | 7c334a1 | 2017-11-22 14:24:24 -0800 | [diff] [blame] | 46 | size_t GetMetricsSize(const ConfigKey& key) const; |
| 47 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 48 | void onDumpReport(const ConfigKey& key, vector<uint8_t>* outData); |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 49 | void onDumpReport(const ConfigKey& key, const uint64_t& dumpTimeStampNs, |
| 50 | ConfigMetricsReportList* report); |
Bookatz | cc5adef | 2017-11-21 14:36:23 -0800 | [diff] [blame] | 51 | |
| 52 | /* Tells MetricsManager that the alarms in anomalySet have fired. Modifies anomalySet. */ |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 53 | void onAnomalyAlarmFired( |
| 54 | const uint64_t timestampNs, |
| 55 | unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 56 | |
yro | 947fbce | 2017-11-15 22:50:23 -0800 | [diff] [blame] | 57 | /* Flushes data to disk. Data on memory will be gone after written to disk. */ |
| 58 | void WriteDataToDisk(); |
| 59 | |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 60 | inline sp<UidMap> getUidMap() { |
| 61 | return mUidMap; |
| 62 | } |
| 63 | |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 64 | private: |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 65 | mutable mutex mMetricsMutex; |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 66 | |
Yao Chen | d10f7b1 | 2017-12-18 12:53:50 -0800 | [diff] [blame] | 67 | std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 68 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 69 | std::unordered_map<ConfigKey, long> mLastBroadcastTimes; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 70 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 71 | // Tracks when we last checked the bytes consumed for each config key. |
| 72 | std::unordered_map<ConfigKey, long> mLastByteSizeTimes; |
| 73 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 74 | sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid. |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 75 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 76 | sp<AnomalyMonitor> mAnomalyMonitor; |
| 77 | |
Yangster-mac | 8617950 | 2018-01-23 15:47:15 -0800 | [diff] [blame] | 78 | void onDumpReportLocked(const ConfigKey& key, vector<uint8_t>* outData); |
| 79 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 80 | /* Check if we should send a broadcast if approaching memory limits and if we're over, we |
| 81 | * actually delete the data. */ |
Yangster-mac | b0d0628 | 2018-01-05 15:44:07 -0800 | [diff] [blame] | 82 | void flushIfNecessaryLocked(uint64_t timestampNs, const ConfigKey& key, |
| 83 | MetricsManager& metricsManager); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 84 | |
Yangster-mac | d40053e | 2018-01-09 16:29:22 -0800 | [diff] [blame] | 85 | // Maps the isolated uid in the log event to host uid if the log event contains uid fields. |
| 86 | void mapIsolatedUidToHostUidIfNecessaryLocked(LogEvent* event) const; |
| 87 | |
| 88 | // Handler over the isolated uid change event. |
| 89 | void onIsolatedUidChangedEventLocked(const LogEvent& event); |
| 90 | |
David Chen | 1d7b0cd | 2017-11-15 14:20:04 -0800 | [diff] [blame] | 91 | // Function used to send a broadcast so that receiver for the config key can call getData |
| 92 | // to retrieve the stored data. |
| 93 | std::function<void(const ConfigKey& key)> mSendBroadcast; |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 94 | |
Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 95 | const long mTimeBaseSec; |
| 96 | |
David Chen | d9269e2 | 2017-12-05 13:43:51 -0800 | [diff] [blame] | 97 | FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize); |
| 98 | FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast); |
| 99 | FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 100 | FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge); |
| 101 | FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions); |
| 102 | FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks); |
Yangster-mac | b5bc741 | 2018-01-06 23:17:45 -0800 | [diff] [blame] | 103 | FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice); |
Yangster-mac | 87718e2 | 2018-01-11 16:16:26 -0800 | [diff] [blame] | 104 | FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 105 | |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 106 | }; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 107 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 108 | } // namespace statsd |
| 109 | } // namespace os |
| 110 | } // namespace android |