blob: 2f3fad930bd6854d51fbf13172918332d9e17c2e [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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070019#include "condition/ConditionTracker.h"
20#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
21#include "matchers/LogMatchingTracker.h"
22#include "metrics/MetricProducer.h"
23
Yao Chen44cf27c2017-09-14 22:32:50 -070024#include <log/logprint.h>
25#include <unordered_map>
Yao Chen44cf27c2017-09-14 22:32:50 -070026
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
Yao Chencaf339d2017-10-06 16:01:10 -070038 // Return whether the configuration is valid.
39 bool isConfigValid() const;
40
Yao Chen44cf27c2017-09-14 22:32:50 -070041 void onLogEvent(const log_msg& logMsg);
42
Yao Chencaf339d2017-10-06 16:01:10 -070043 // Called when everything should wrap up. We are about to finish (e.g., new config comes).
Yao Chen44cf27c2017-09-14 22:32:50 -070044 void finish();
45
46private:
Yao Chen44cf27c2017-09-14 22:32:50 -070047 // All event tags that are interesting to my metrics.
48 std::set<int> mTagIds;
49
Yao Chencaf339d2017-10-06 16:01:10 -070050 // 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?
Yao Chen44cf27c2017-09-14 22:32:50 -070057
Yao Chencaf339d2017-10-06 16:01:10 -070058 // Hold all the log entry matchers from the config.
59 std::vector<sp<LogMatchingTracker>> mAllLogEntryMatchers;
Yao Chen44cf27c2017-09-14 22:32:50 -070060
Yao Chencaf339d2017-10-06 16:01:10 -070061 // 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;
Yao Chen44cf27c2017-09-14 22:32:50 -070090};
91
92} // namespace statsd
93} // namespace os
94} // namespace android
95