blob: 1e5c426efd02fbcf64ce7afa87c52c6337165f14 [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 */
16#ifndef STATS_LOG_PROCESSOR_H
17#define STATS_LOG_PROCESSOR_H
18
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019#include "config/ConfigListener.h"
20#include "logd/LogReader.h"
Yao Chen44cf27c2017-09-14 22:32:50 -070021#include "metrics/MetricsManager.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022#include "packages/UidMap.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023
24#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Yao Chenab273e22017-09-06 12:53:50 -070025
Yao Chen44cf27c2017-09-14 22:32:50 -070026#include <stdio.h>
David Chen0656b7a2017-09-13 15:53:39 -070027#include <unordered_map>
28
Bookatz906a35c2017-09-20 15:26:44 -070029namespace android {
30namespace os {
31namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070032
Joe Onorato9fc9edf2017-10-15 20:08:52 -070033class StatsLogProcessor : public ConfigListener {
Yao Chenab273e22017-09-06 12:53:50 -070034public:
Yangster-mace2cd6d52017-11-09 20:38:30 -080035 StatsLogProcessor(const sp<UidMap>& uidMap, const sp<AnomalyMonitor>& anomalyMonitor,
David Chen1d7b0cd2017-11-15 14:20:04 -080036 const std::function<void(const ConfigKey&)>& sendBroadcast);
Yao Chenab273e22017-09-06 12:53:50 -070037 virtual ~StatsLogProcessor();
38
Yangsterf2bee6f2017-11-29 12:01:05 -080039 void OnLogEvent(const LogEvent& event);
Yao Chenab273e22017-09-06 12:53:50 -070040
Joe Onorato9fc9edf2017-10-15 20:08:52 -070041 void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config);
42 void OnConfigRemoved(const ConfigKey& key);
David Chen0656b7a2017-09-13 15:53:39 -070043
Yangster7c334a12017-11-22 14:24:24 -080044 size_t GetMetricsSize(const ConfigKey& key) const;
45
David Chen1d7b0cd2017-11-15 14:20:04 -080046 void onDumpReport(const ConfigKey& key, vector<uint8_t>* outData);
Bookatzcc5adef22017-11-21 14:36:23 -080047
48 /* Tells MetricsManager that the alarms in anomalySet have fired. Modifies anomalySet. */
Yangster-mace2cd6d52017-11-09 20:38:30 -080049 void onAnomalyAlarmFired(
50 const uint64_t timestampNs,
51 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet);
yro31eb67b2017-10-24 13:33:21 -070052
yro947fbce2017-11-15 22:50:23 -080053 /* Flushes data to disk. Data on memory will be gone after written to disk. */
54 void WriteDataToDisk();
55
Yao Chenab273e22017-09-06 12:53:50 -070056private:
David Chen1d7b0cd2017-11-15 14:20:04 -080057 mutable mutex mBroadcastTimesMutex;
58
Yao Chend10f7b12017-12-18 12:53:50 -080059 std::unordered_map<ConfigKey, sp<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -070060
David Chen1d7b0cd2017-11-15 14:20:04 -080061 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -070062
David Chend9269e22017-12-05 13:43:51 -080063 // Tracks when we last checked the bytes consumed for each config key.
64 std::unordered_map<ConfigKey, long> mLastByteSizeTimes;
65
Joe Onorato9fc9edf2017-10-15 20:08:52 -070066 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -070067
Yangster-mace2cd6d52017-11-09 20:38:30 -080068 sp<AnomalyMonitor> mAnomalyMonitor;
69
David Chen1d7b0cd2017-11-15 14:20:04 -080070 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
71 * actually delete the data. */
David Chend9269e22017-12-05 13:43:51 -080072 void flushIfNecessary(uint64_t timestampNs, const ConfigKey& key,
73 MetricsManager& metricsManager);
yro31eb67b2017-10-24 13:33:21 -070074
David Chen1d7b0cd2017-11-15 14:20:04 -080075 // Function used to send a broadcast so that receiver for the config key can call getData
76 // to retrieve the stored data.
77 std::function<void(const ConfigKey& key)> mSendBroadcast;
yro69007c82017-10-26 20:42:57 -070078
Chenjie Yu85ed8382017-12-14 16:48:54 -080079 const long mTimeBaseSec;
80
David Chend9269e22017-12-05 13:43:51 -080081 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
82 FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
83 FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
Yao Chenab273e22017-09-06 12:53:50 -070084};
Bookatz906a35c2017-09-20 15:26:44 -070085
Yao Chenef99c4f2017-09-22 16:26:54 -070086} // namespace statsd
87} // namespace os
88} // namespace android
Bookatz906a35c2017-09-20 15:26:44 -070089
Yao Chenef99c4f2017-09-22 16:26:54 -070090#endif // STATS_LOG_PROCESSOR_H