blob: fb85aa837242671eb33f0a57541f63507150f06c [file] [log] [blame]
Yao Chenab273e22017-09-06 12:53:50 -07001/*
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 Chenab273e22017-09-06 12:53:50 -070016
Yangster-mac20877162017-12-22 17:19:39 -080017#pragma once
18
19#include <gtest/gtest_prod.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070020#include "config/ConfigListener.h"
21#include "logd/LogReader.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070022#include "metrics/MetricsManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "packages/UidMap.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070024
25#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Yao Chenab273e22017-09-06 12:53:50 -070026
Yao Chen44cf27c2017-09-14 22:32:50 -070027#include <stdio.h>
David Chen0656b7a2017-09-13 15:53:39 -070028#include <unordered_map>
29
Bookatz906a35c2017-09-20 15:26:44 -070030namespace android {
31namespace os {
32namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070033
Joe Onorato9fc9edf2017-10-15 20:08:52 -070034class StatsLogProcessor : public ConfigListener {
Yao Chenab273e22017-09-06 12:53:50 -070035public:
Yangster-mace2cd6d52017-11-09 20:38:30 -080036 StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AnomalyMonitor>& anomalyMonitor,
Yangster-mac20877162017-12-22 17:19:39 -080037 const long timeBaseSec,
David Chen1d7b0cd2017-11-15 14:20:04 -080038 const std::function<void(const ConfigKey&)>& sendBroadcast);
Yao Chenab273e22017-09-06 12:53:50 -070039 virtual ~StatsLogProcessor();
40
Yangster-macd40053e2018-01-09 16:29:22 -080041 void OnLogEvent(LogEvent* event);
Yao Chenab273e22017-09-06 12:53:50 -070042
Joe Onorato9fc9edf2017-10-15 20:08:52 -070043 void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config);
44 void OnConfigRemoved(const ConfigKey& key);
David Chen0656b7a2017-09-13 15:53:39 -070045
Yangster7c334a12017-11-22 14:24:24 -080046 size_t GetMetricsSize(const ConfigKey& key) const;
47
David Chen1d7b0cd2017-11-15 14:20:04 -080048 void onDumpReport(const ConfigKey& key, vector<uint8_t>* outData);
Yangster-macd40053e2018-01-09 16:29:22 -080049 void onDumpReport(const ConfigKey& key, const uint64_t& dumpTimeStampNs,
50 ConfigMetricsReportList* report);
Bookatzcc5adef2017-11-21 14:36:23 -080051
52 /* Tells MetricsManager that the alarms in anomalySet have fired. Modifies anomalySet. */
Yangster-mace2cd6d52017-11-09 20:38:30 -080053 void onAnomalyAlarmFired(
54 const uint64_t timestampNs,
55 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet);
yro31eb67b2017-10-24 13:33:21 -070056
yro947fbce2017-11-15 22:50:23 -080057 /* Flushes data to disk. Data on memory will be gone after written to disk. */
58 void WriteDataToDisk();
59
Yangster-macb5bc7412018-01-06 23:17:45 -080060 inline sp<UidMap> getUidMap() {
61 return mUidMap;
62 }
63
Yao Chenab273e22017-09-06 12:53:50 -070064private:
Yangster-macb0d06282018-01-05 15:44:07 -080065 mutable mutex mMetricsMutex;
David Chen1d7b0cd2017-11-15 14:20:04 -080066
Yao Chend10f7b12017-12-18 12:53:50 -080067 std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -070068
David Chen1d7b0cd2017-11-15 14:20:04 -080069 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -070070
David Chend9269e22017-12-05 13:43:51 -080071 // Tracks when we last checked the bytes consumed for each config key.
72 std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
73
Joe Onorato9fc9edf2017-10-15 20:08:52 -070074 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -070075
Yangster-mace2cd6d52017-11-09 20:38:30 -080076 sp<AnomalyMonitor> mAnomalyMonitor;
77
Yangster-mac86179502018-01-23 15:47:15 -080078 void onDumpReportLocked(const ConfigKey& key, vector<uint8_t>* outData);
79
David Chen1d7b0cd2017-11-15 14:20:04 -080080 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
81 * actually delete the data. */
Yangster-macb0d06282018-01-05 15:44:07 -080082 void flushIfNecessaryLocked(uint64_t timestampNs, const ConfigKey& key,
83 MetricsManager& metricsManager);
yro31eb67b2017-10-24 13:33:21 -070084
Yangster-macd40053e2018-01-09 16:29:22 -080085 // 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 Chen1d7b0cd2017-11-15 14:20:04 -080091 // 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;
yro69007c82017-10-26 20:42:57 -070094
Chenjie Yu85ed8382017-12-14 16:48:54 -080095 const long mTimeBaseSec;
96
David Chend9269e22017-12-05 13:43:51 -080097 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
98 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
99 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
Yangster-mac20877162017-12-22 17:19:39 -0800100 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
101 FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensions);
102 FRIEND_TEST(MetricConditionLinkE2eTest, TestMultiplePredicatesAndLinks);
Yangster-macb5bc7412018-01-06 23:17:45 -0800103 FRIEND_TEST(AttributionE2eTest, TestAttributionMatchAndSlice);
Yangster-mac87718e22018-01-11 16:16:26 -0800104 FRIEND_TEST(GaugeMetricE2eTest, TestMultipleFieldsForPushedEvent);
Yangster-mac20877162017-12-22 17:19:39 -0800105
Yao Chenab273e22017-09-06 12:53:50 -0700106};
Bookatz906a35c2017-09-20 15:26:44 -0700107
Yao Chenef99c4f2017-09-22 16:26:54 -0700108} // namespace statsd
109} // namespace os
110} // namespace android