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 | */ |
| 16 | #ifndef STATS_LOG_PROCESSOR_H |
| 17 | #define STATS_LOG_PROCESSOR_H |
| 18 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 19 | #include "config/ConfigListener.h" |
| 20 | #include "logd/LogReader.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 21 | #include "metrics/MetricsManager.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | #include "packages/UidMap.h" |
| 23 | #include "storage/DropboxWriter.h" |
| 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: |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame^] | 36 | StatsLogProcessor(const sp<UidMap>& uidMap, |
| 37 | const std::function<void(const vector<uint8_t>&)>& pushLog); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 38 | virtual ~StatsLogProcessor(); |
| 39 | |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 40 | virtual void OnLogEvent(const LogEvent& event); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 41 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 42 | void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config); |
| 43 | void OnConfigRemoved(const ConfigKey& key); |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 44 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 45 | // TODO: Once we have the ProtoOutputStream in c++, we can just return byte array. |
| 46 | std::vector<StatsLogReport> onDumpReport(const ConfigKey& key); |
| 47 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame^] | 48 | /* Request a flush through a binder call. */ |
| 49 | void flush(); |
| 50 | |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 51 | private: |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 52 | // TODO: use EventMetrics to log the events. |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 53 | DropboxWriter m_dropbox_writer; |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 54 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 55 | std::unordered_map<ConfigKey, std::unique_ptr<MetricsManager>> mMetricsManagers; |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 56 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 57 | 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^] | 58 | |
| 59 | /* Max *serialized* size of the logs kept in memory before flushing through binder call. |
| 60 | Proto lite does not implement the SpaceUsed() function which gives the in memory byte size. |
| 61 | So we cap memory usage by limiting the serialized size. Note that protobuf's in memory size |
| 62 | is higher than its serialized size. |
| 63 | */ |
| 64 | static const size_t kMaxSerializedBytes = 16 * 1024; |
| 65 | |
| 66 | /* List of data that was captured for a single metric over a given interval of time. */ |
| 67 | vector<string> mEvents; |
| 68 | |
| 69 | /* Current *serialized* size of the logs kept in memory. |
| 70 | To save computation, we will not calculate the size of the StatsLogReport every time when a |
| 71 | new entry is added, which would recursively call ByteSize() on every log entry. Instead, we |
| 72 | keep the sum of all individual stats log entry sizes. The size of a proto is approximately |
| 73 | the sum of the size of all member protos. |
| 74 | */ |
| 75 | size_t mBufferSize = 0; |
| 76 | |
| 77 | /* Check if the buffer size exceeds the max buffer size when the new entry is added, and flush |
| 78 | the logs to dropbox if true. */ |
| 79 | void flushIfNecessary(const EventMetricData& eventMetricData); |
| 80 | |
| 81 | /* Append event metric data to StatsLogReport. */ |
| 82 | void addEventMetricData(const EventMetricData& eventMetricData); |
| 83 | |
| 84 | std::function<void(const vector<uint8_t>&)> mPushLog; |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 85 | }; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 86 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 87 | } // namespace statsd |
| 88 | } // namespace os |
| 89 | } // namespace android |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 90 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 91 | #endif // STATS_LOG_PROCESSOR_H |