Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame^] | 17 | #pragma once |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 18 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame^] | 19 | #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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 24 | #include <log/logprint.h> |
| 25 | #include <unordered_map> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
| 30 | |
| 31 | // A MetricsManager is responsible for managing metrics from one single config source. |
| 32 | class MetricsManager { |
| 33 | public: |
| 34 | MetricsManager(const StatsdConfig& config); |
| 35 | |
| 36 | ~MetricsManager(); |
| 37 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 38 | // Return whether the configuration is valid. |
| 39 | bool isConfigValid() const; |
| 40 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 41 | void onLogEvent(const log_msg& logMsg); |
| 42 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 43 | // Called when everything should wrap up. We are about to finish (e.g., new config comes). |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 44 | void finish(); |
| 45 | |
| 46 | private: |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 47 | // All event tags that are interesting to my metrics. |
| 48 | std::set<int> mTagIds; |
| 49 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 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? |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 57 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 58 | // Hold all the log entry matchers from the config. |
| 59 | std::vector<sp<LogMatchingTracker>> mAllLogEntryMatchers; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 60 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 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; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace statsd |
| 93 | } // namespace os |
| 94 | } // namespace android |
| 95 | |