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