blob: 4e6a0ce5439b25cb3bc0a7f3662e6061f8155c4f [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
Bookatzcc5adef22017-11-21 14:36:23 -080049 void onAnomalyAlarmFired(const uint64_t timestampNs,
50 unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet);
Yangster-mace2cd6d52017-11-09 20:38:30 -080051
52 void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor);
53
Yao Chen729093d2017-10-16 10:33:26 -070054 // Config source owner can call onDumpReport() to get all the metrics collected.
yro17adac92017-11-08 23:16:29 -080055 std::vector<std::unique_ptr<std::vector<uint8_t>>> onDumpReport();
Yao Chen729093d2017-10-16 10:33:26 -070056
David Chen1d7b0cd2017-11-15 14:20:04 -080057 // Computes the total byte size of all metrics managed by a single config source.
58 // Does not change the state.
yro69007c82017-10-26 20:42:57 -070059 size_t byteSize();
60
Yao Chen44cf27c2017-09-14 22:32:50 -070061private:
Yao Chenb3561512017-11-21 18:07:17 -080062 const ConfigKey mConfigKey;
63
Yao Chen44cf27c2017-09-14 22:32:50 -070064 // All event tags that are interesting to my metrics.
65 std::set<int> mTagIds;
66
Yao Chencaf339d2017-10-06 16:01:10 -070067 // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in
Bookatzd3606c72017-10-19 10:13:49 -070068 // MetricsManager. There are relationships between them, and the relationships are denoted by
69 // index instead of pointers. The reasons for this are: (1) the relationship between them are
70 // complicated, so storing index instead of pointers reduces the risk that A holds B's sp, and B
71 // holds A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get
72 // the related results from a cache using the index.
Yao Chen44cf27c2017-09-14 22:32:50 -070073
Stefan Lafonb8c9aa82017-12-03 14:27:25 -080074 // Hold all the atom matchers from the config.
75 std::vector<sp<LogMatchingTracker>> mAllAtomMatchers;
Yao Chen44cf27c2017-09-14 22:32:50 -070076
Yao Chencaf339d2017-10-06 16:01:10 -070077 // Hold all the conditions from the config.
78 std::vector<sp<ConditionTracker>> mAllConditionTrackers;
79
80 // Hold all metrics from the config.
81 std::vector<sp<MetricProducer>> mAllMetricProducers;
82
Yangster-mace2cd6d52017-11-09 20:38:30 -080083 // Hold all alert trackers.
84 std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers;
85
Yao Chencaf339d2017-10-06 16:01:10 -070086 // To make the log processing more efficient, we want to do as much filtering as possible
87 // before we go into individual trackers and conditions to match.
88
89 // 1st filter: check if the event tag id is in mTagIds.
90 // 2nd filter: if it is, we parse the event because there is at least one member is interested.
91 // then pass to all LogMatchingTrackers (itself also filter events by ids).
92 // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the
93 // ConditionTrackers and MetricProducers that use this matcher.
94 // 4th filter: for ConditionTrackers that changed value due to this event, we pass
95 // new conditions to metrics that use this condition.
96
97 // The following map is initialized from the statsd_config.
98
99 // maps from the index of the LogMatchingTracker to index of MetricProducer.
100 std::unordered_map<int, std::vector<int>> mTrackerToMetricMap;
101
102 // maps from LogMatchingTracker to ConditionTracker
103 std::unordered_map<int, std::vector<int>> mTrackerToConditionMap;
104
105 // maps from ConditionTracker to MetricProducer
106 std::unordered_map<int, std::vector<int>> mConditionToMetricMap;
107
Yangster-mac756cd482017-11-21 21:58:44 -0800108 bool mConfigValid = false;
Yao Chen44cf27c2017-09-14 22:32:50 -0700109};
110
111} // namespace statsd
112} // namespace os
113} // namespace android