Yao Chen | ab273e2 | 2017-09-06 12:53: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 | #include <StatsLogProcessor.h> |
| 18 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 19 | #include <cutils/log.h> |
| 20 | #include <frameworks/base/cmds/statsd/src/stats_log.pb.h> |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 21 | #include <log/log_event_list.h> |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 22 | #include <metrics/CountMetricProducer.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 23 | #include <utils/Errors.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 24 | |
| 25 | using namespace android; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 26 | using std::make_unique; |
| 27 | using std::unique_ptr; |
| 28 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 33 | |
David Chen | de70169 | 2017-10-05 13:16:02 -0700 | [diff] [blame] | 34 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap> &uidMap) |
| 35 | : m_dropbox_writer("all-logs"), m_UidMap(uidMap) |
| 36 | { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 37 | // hardcoded config |
| 38 | // this should be called from StatsService when it receives a statsd_config |
| 39 | UpdateConfig(0, buildFakeConfig()); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 42 | StatsLogProcessor::~StatsLogProcessor() { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 45 | // TODO: what if statsd service restarts? How do we know what logs are already processed before? |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 46 | void StatsLogProcessor::OnLogEvent(const log_msg& msg) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 47 | // TODO: Use EventMetric to filter the events we want to log. |
| 48 | EventMetricData eventMetricData = parse(msg); |
| 49 | m_dropbox_writer.addEventMetricData(eventMetricData); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 50 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 51 | // pass the event to metrics managers. |
| 52 | for (auto& pair : mMetricsManagers) { |
| 53 | pair.second->onLogEvent(msg); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 57 | void StatsLogProcessor::UpdateConfig(const int config_source, const StatsdConfig& config) { |
| 58 | auto it = mMetricsManagers.find(config_source); |
| 59 | if (it != mMetricsManagers.end()) { |
| 60 | it->second->finish(); |
| 61 | } |
| 62 | |
David Chen | 0656b7a | 2017-09-13 15:53:39 -0700 | [diff] [blame] | 63 | ALOGD("Updated configuration for source %i", config_source); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 64 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 65 | unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config); |
| 66 | if (newMetricsManager->isConfigValid()) { |
| 67 | mMetricsManagers.insert({config_source, std::move(newMetricsManager)}); |
| 68 | ALOGD("StatsdConfig valid"); |
| 69 | } else { |
| 70 | // If there is any error in the config, don't use it. |
| 71 | ALOGD("StatsdConfig NOT valid"); |
| 72 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 73 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 74 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 75 | } // namespace statsd |
| 76 | } // namespace os |
| 77 | } // namespace android |