blob: 08090c11f7245b8106cc53e77135004b6b42ee0a [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"
23#include "storage/DropboxWriter.h"
24
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:
yro31eb67b2017-10-24 13:33:21 -070036 StatsLogProcessor(const sp<UidMap>& uidMap,
37 const std::function<void(const vector<uint8_t>&)>& pushLog);
Yao Chenab273e22017-09-06 12:53:50 -070038 virtual ~StatsLogProcessor();
39
Joe Onoratoc4dfae52017-10-17 23:38:21 -070040 virtual void OnLogEvent(const LogEvent& event);
Yao Chenab273e22017-09-06 12:53:50 -070041
Joe Onorato9fc9edf2017-10-15 20:08:52 -070042 void OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config);
43 void OnConfigRemoved(const ConfigKey& key);
David Chen0656b7a2017-09-13 15:53:39 -070044
Yao Chen729093d2017-10-16 10:33:26 -070045 // TODO: Once we have the ProtoOutputStream in c++, we can just return byte array.
46 std::vector<StatsLogReport> onDumpReport(const ConfigKey& key);
47
yro31eb67b2017-10-24 13:33:21 -070048 /* Request a flush through a binder call. */
49 void flush();
50
Yao Chenab273e22017-09-06 12:53:50 -070051private:
Yao Chen44cf27c2017-09-14 22:32:50 -070052 // TODO: use EventMetrics to log the events.
Yao Chenab273e22017-09-06 12:53:50 -070053 DropboxWriter m_dropbox_writer;
David Chen0656b7a2017-09-13 15:53:39 -070054
Joe Onorato9fc9edf2017-10-15 20:08:52 -070055 std::unordered_map<ConfigKey, std::unique_ptr<MetricsManager>> mMetricsManagers;
David Chende701692017-10-05 13:16:02 -070056
Joe Onorato9fc9edf2017-10-15 20:08:52 -070057 sp<UidMap> mUidMap; // Reference to the UidMap to lookup app name and version for each uid.
yro31eb67b2017-10-24 13:33:21 -070058
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 Chenab273e22017-09-06 12:53:50 -070085};
Bookatz906a35c2017-09-20 15:26:44 -070086
Yao Chenef99c4f2017-09-22 16:26:54 -070087} // namespace statsd
88} // namespace os
89} // namespace android
Bookatz906a35c2017-09-20 15:26:44 -070090
Yao Chenef99c4f2017-09-22 16:26:54 -070091#endif // STATS_LOG_PROCESSOR_H