blob: 517831d1b09d425a94f4d3e64d555b6951309b61 [file] [log] [blame]
Yao Chen44cf27c2017-09-14 22:32: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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#pragma once
Yao Chen44cf27c2017-09-14 22:32:50 -070018
Yangster-mace2cd6d52017-11-09 20:38:30 -080019#include "anomaly/AnomalyMonitor.h"
20#include "anomaly/AnomalyTracker.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021#include "condition/ConditionTracker.h"
Yao Chenb3561512017-11-21 18:07:17 -080022#include "config/ConfigKey.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
Joe Onoratoc4dfae52017-10-17 23:38:21 -070024#include "logd/LogEvent.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070025#include "matchers/LogMatchingTracker.h"
26#include "metrics/MetricProducer.h"
27
Yao Chen44cf27c2017-09-14 22:32:50 -070028#include <unordered_map>
Yao Chen44cf27c2017-09-14 22:32:50 -070029
30namespace android {
31namespace os {
32namespace statsd {
33
34// A MetricsManager is responsible for managing metrics from one single config source.
35class MetricsManager {
36public:
Yao Chenb3561512017-11-21 18:07:17 -080037 MetricsManager(const ConfigKey& configKey, const StatsdConfig& config);
Yao Chen44cf27c2017-09-14 22:32:50 -070038
39 ~MetricsManager();
40
Yao Chencaf339d2017-10-06 16:01:10 -070041 // Return whether the configuration is valid.
42 bool isConfigValid() const;
43
Joe Onoratoc4dfae52017-10-17 23:38:21 -070044 void onLogEvent(const LogEvent& event);
Yao Chen44cf27c2017-09-14 22:32:50 -070045
Yao Chencaf339d2017-10-06 16:01:10 -070046 // Called when everything should wrap up. We are about to finish (e.g., new config comes).
Yao Chen44cf27c2017-09-14 22:32:50 -070047 void finish();
48
Yangster-mace2cd6d52017-11-09 20:38:30 -080049 void onAnomalyAlarmFired(const uint64_t timestampNs, sp<const AnomalyAlarm> anomaly);
50
51 void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor);
52
Yao Chen729093d2017-10-16 10:33:26 -070053 // Config source owner can call onDumpReport() to get all the metrics collected.
yro17adac92017-11-08 23:16:29 -080054 std::vector<std::unique_ptr<std::vector<uint8_t>>> onDumpReport();
Yao Chen729093d2017-10-16 10:33:26 -070055
David Chen1d7b0cd2017-11-15 14:20:04 -080056 // Computes the total byte size of all metrics managed by a single config source.
57 // Does not change the state.
yro69007c82017-10-26 20:42:57 -070058 size_t byteSize();
59
Yao Chen44cf27c2017-09-14 22:32:50 -070060private:
Yao Chenb3561512017-11-21 18:07:17 -080061 const ConfigKey mConfigKey;
62
Yao Chen44cf27c2017-09-14 22:32:50 -070063 // All event tags that are interesting to my metrics.
64 std::set<int> mTagIds;
65
Yao Chencaf339d2017-10-06 16:01:10 -070066 // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in
Bookatzd3606c72017-10-19 10:13:49 -070067 // MetricsManager. There are relationships between them, and the relationships are denoted by
68 // index instead of pointers. The reasons for this are: (1) the relationship between them are
69 // complicated, so storing index instead of pointers reduces the risk that A holds B's sp, and B
70 // holds A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get
71 // the related results from a cache using the index.
Yao Chen44cf27c2017-09-14 22:32:50 -070072
Yao Chencaf339d2017-10-06 16:01:10 -070073 // Hold all the log entry matchers from the config.
74 std::vector<sp<LogMatchingTracker>> mAllLogEntryMatchers;
Yao Chen44cf27c2017-09-14 22:32:50 -070075
Yao Chencaf339d2017-10-06 16:01:10 -070076 // Hold all the conditions from the config.
77 std::vector<sp<ConditionTracker>> mAllConditionTrackers;
78
79 // Hold all metrics from the config.
80 std::vector<sp<MetricProducer>> mAllMetricProducers;
81
Yangster-mace2cd6d52017-11-09 20:38:30 -080082 // Hold all alert trackers.
83 std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers;
84
Yao Chencaf339d2017-10-06 16:01:10 -070085 // To make the log processing more efficient, we want to do as much filtering as possible
86 // before we go into individual trackers and conditions to match.
87
88 // 1st filter: check if the event tag id is in mTagIds.
89 // 2nd filter: if it is, we parse the event because there is at least one member is interested.
90 // then pass to all LogMatchingTrackers (itself also filter events by ids).
91 // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the
92 // ConditionTrackers and MetricProducers that use this matcher.
93 // 4th filter: for ConditionTrackers that changed value due to this event, we pass
94 // new conditions to metrics that use this condition.
95
96 // The following map is initialized from the statsd_config.
97
98 // maps from the index of the LogMatchingTracker to index of MetricProducer.
99 std::unordered_map<int, std::vector<int>> mTrackerToMetricMap;
100
101 // maps from LogMatchingTracker to ConditionTracker
102 std::unordered_map<int, std::vector<int>> mTrackerToConditionMap;
103
104 // maps from ConditionTracker to MetricProducer
105 std::unordered_map<int, std::vector<int>> mConditionToMetricMap;
106
Yangster-mac756cd482017-11-21 21:58:44 -0800107 bool mConfigValid = false;
Yao Chen44cf27c2017-09-14 22:32:50 -0700108};
109
110} // namespace statsd
111} // namespace os
112} // namespace android