MetricsManager.h revision cc5adef2d0c5f96a225fd69517fd1eecb557f46d
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
17#pragma once
18
19#include "anomaly/AnomalyMonitor.h"
20#include "anomaly/AnomalyTracker.h"
21#include "condition/ConditionTracker.h"
22#include "config/ConfigKey.h"
23#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
24#include "logd/LogEvent.h"
25#include "matchers/LogMatchingTracker.h"
26#include "metrics/MetricProducer.h"
27
28#include <unordered_map>
29
30namespace android {
31namespace os {
32namespace statsd {
33
34// A MetricsManager is responsible for managing metrics from one single config source.
35class MetricsManager {
36public:
37    MetricsManager(const ConfigKey& configKey, const StatsdConfig& config);
38
39    ~MetricsManager();
40
41    // Return whether the configuration is valid.
42    bool isConfigValid() const;
43
44    void onLogEvent(const LogEvent& event);
45
46    // Called when everything should wrap up. We are about to finish (e.g., new config comes).
47    void finish();
48
49    void onAnomalyAlarmFired(const uint64_t timestampNs,
50                         unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>>& anomalySet);
51
52    void setAnomalyMonitor(const sp<AnomalyMonitor>& anomalyMonitor);
53
54    // Config source owner can call onDumpReport() to get all the metrics collected.
55    std::vector<std::unique_ptr<std::vector<uint8_t>>> onDumpReport();
56
57    // Computes the total byte size of all metrics managed by a single config source.
58    // Does not change the state.
59    size_t byteSize();
60
61private:
62    const ConfigKey mConfigKey;
63
64    // All event tags that are interesting to my metrics.
65    std::set<int> mTagIds;
66
67    // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in
68    // 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.
73
74    // Hold all the log entry matchers from the config.
75    std::vector<sp<LogMatchingTracker>> mAllLogEntryMatchers;
76
77    // 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
83    // Hold all alert trackers.
84    std::vector<sp<AnomalyTracker>> mAllAnomalyTrackers;
85
86    // 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
108    bool mConfigValid = false;
109};
110
111}  // namespace statsd
112}  // namespace os
113}  // namespace android
114