blob: 57928d07a568a6394732c575fb4b489c46d32c35 [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
Joe Onoratoc4dfae52017-10-17 23:38:21 -070039 virtual 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);
Yangster-mace2cd6d52017-11-09 20:38:30 -080047 void onAnomalyAlarmFired(
48 const uint64_t timestampNs,
49 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> anomalySet);
yro31eb67b2017-10-24 13:33:21 -070050
yro947fbce2017-11-15 22:50:23 -080051 /* Flushes data to disk. Data on memory will be gone after written to disk. */
52 void WriteDataToDisk();
53
Yao Chenab273e22017-09-06 12:53:50 -070054private:
David Chen1d7b0cd2017-11-15 14:20:04 -080055 mutable mutex mBroadcastTimesMutex;
56
Joe Onorato9fc9edf2017-10-15 20:08:52 -070057 std::unordered_map<ConfigKey, std::unique_ptr<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -070058
David Chen1d7b0cd2017-11-15 14:20:04 -080059 std::unordered_map<ConfigKey, long> mLastBroadcastTimes;
yro69007c82017-10-26 20:42:57 -070060
Joe Onorato9fc9edf2017-10-15 20:08:52 -070061 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -070062
Yangster-mace2cd6d52017-11-09 20:38:30 -080063 sp<AnomalyMonitor> mAnomalyMonitor;
64
yro31eb67b2017-10-24 13:33:21 -070065 /* Max *serialized* size of the logs kept in memory before flushing through binder call.
66 Proto lite does not implement the SpaceUsed() function which gives the in memory byte size.
67 So we cap memory usage by limiting the serialized size. Note that protobuf's in memory size
68 is higher than its serialized size.
69 */
70 static const size_t kMaxSerializedBytes = 16 * 1024;
71
David Chen1d7b0cd2017-11-15 14:20:04 -080072 /* Check if we should send a broadcast if approaching memory limits and if we're over, we
73 * actually delete the data. */
yro69007c82017-10-26 20:42:57 -070074 void flushIfNecessary(uint64_t timestampNs,
75 const ConfigKey& key,
76 const unique_ptr<MetricsManager>& metricsManager);
yro31eb67b2017-10-24 13:33:21 -070077
David Chen1d7b0cd2017-11-15 14:20:04 -080078 // Function used to send a broadcast so that receiver for the config key can call getData
79 // to retrieve the stored data.
80 std::function<void(const ConfigKey& key)> mSendBroadcast;
yro69007c82017-10-26 20:42:57 -070081
David Chen1d7b0cd2017-11-15 14:20:04 -080082 /* Minimum period between two broadcasts in nanoseconds. Currently set to 60 seconds. */
83 static const unsigned long long kMinBroadcastPeriod = 60 * NS_PER_SEC;
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