MetricsManager.h revision c4dfae56c10a1dd571baa78c750f2e68c919d74f
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 "condition/ConditionTracker.h"
20#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
21#include "logd/LogEvent.h"
22#include "matchers/LogMatchingTracker.h"
23#include "metrics/MetricProducer.h"
24
25#include <unordered_map>
26
27namespace android {
28namespace os {
29namespace statsd {
30
31// A MetricsManager is responsible for managing metrics from one single config source.
32class MetricsManager {
33public:
34    MetricsManager(const StatsdConfig& config);
35
36    ~MetricsManager();
37
38    // Return whether the configuration is valid.
39    bool isConfigValid() const;
40
41    void onLogEvent(const LogEvent& event);
42
43    // Called when everything should wrap up. We are about to finish (e.g., new config comes).
44    void finish();
45
46private:
47    // All event tags that are interesting to my metrics.
48    std::set<int> mTagIds;
49
50    // We only store the sp of LogMatchingTracker, MetricProducer, and ConditionTracker in
51    // MetricManager. There are relationship between them, and the relationship are denoted by index
52    // instead of poiters. The reasons for this are: (1) the relationship between them are
53    // complicated, store index instead of pointers reduce the risk of A holds B's sp, and B holds
54    // A's sp. (2) When we evaluate matcher results, or condition results, we can quickly get the
55    // related results from a cache using the index.
56    // TODO: using unique_ptr may be more appriopreate?
57
58    // Hold all the log entry matchers from the config.
59    std::vector<sp<LogMatchingTracker>> mAllLogEntryMatchers;
60
61    // Hold all the conditions from the config.
62    std::vector<sp<ConditionTracker>> mAllConditionTrackers;
63
64    // Hold all metrics from the config.
65    std::vector<sp<MetricProducer>> mAllMetricProducers;
66
67    // To make the log processing more efficient, we want to do as much filtering as possible
68    // before we go into individual trackers and conditions to match.
69
70    // 1st filter: check if the event tag id is in mTagIds.
71    // 2nd filter: if it is, we parse the event because there is at least one member is interested.
72    //             then pass to all LogMatchingTrackers (itself also filter events by ids).
73    // 3nd filter: for LogMatchingTrackers that matched this event, we pass this event to the
74    //             ConditionTrackers and MetricProducers that use this matcher.
75    // 4th filter: for ConditionTrackers that changed value due to this event, we pass
76    //             new conditions to  metrics that use this condition.
77
78    // The following map is initialized from the statsd_config.
79
80    // maps from the index of the LogMatchingTracker to index of MetricProducer.
81    std::unordered_map<int, std::vector<int>> mTrackerToMetricMap;
82
83    // maps from LogMatchingTracker to ConditionTracker
84    std::unordered_map<int, std::vector<int>> mTrackerToConditionMap;
85
86    // maps from ConditionTracker to MetricProducer
87    std::unordered_map<int, std::vector<int>> mConditionToMetricMap;
88
89    bool mConfigValid;
90};
91
92}  // namespace statsd
93}  // namespace os
94}  // namespace android
95
96