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 | |
| 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 Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 23 | #include "../condition/ConditionTracker.h" |
| 24 | #include "../matchers/LogMatchingTracker.h" |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 25 | #include "MetricProducer.h" |
| 26 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace os { |
| 30 | namespace statsd { |
| 31 | |
| 32 | // A MetricsManager is responsible for managing metrics from one single config source. |
| 33 | class MetricsManager { |
| 34 | public: |
| 35 | MetricsManager(const StatsdConfig& config); |
| 36 | |
| 37 | ~MetricsManager(); |
| 38 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 39 | // Return whether the configuration is valid. |
| 40 | bool isConfigValid() const; |
| 41 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 42 | void onLogEvent(const log_msg& logMsg); |
| 43 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 44 | // 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] | 45 | void finish(); |
| 46 | |
| 47 | private: |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 48 | // All event tags that are interesting to my metrics. |
| 49 | std::set<int> mTagIds; |
| 50 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 51 | // 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 58 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 59 | // Hold all the log entry matchers from the config. |
| 60 | std::vector<sp<LogMatchingTracker>> mAllLogEntryMatchers; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 61 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 62 | // 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 Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | } // namespace statsd |
| 94 | } // namespace os |
| 95 | } // namespace android |
| 96 | |
| 97 | #endif // METRICS_MANAGER_H |