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 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 17 | #include "Log.h" |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame^] | 18 | #include "statslog.h" |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 19 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 20 | #include "StatsLogProcessor.h" |
| 21 | #include "frameworks/base/cmds/statsd/src/stats_log.pb.h" |
| 22 | #include "metrics/CountMetricProducer.h" |
| 23 | #include "stats_util.h" |
| 24 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 25 | #include <log/log_event_list.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 26 | #include <utils/Errors.h> |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 27 | |
| 28 | using namespace android; |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 29 | using std::make_unique; |
| 30 | using std::unique_ptr; |
| 31 | using std::vector; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 36 | |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 37 | StatsLogProcessor::StatsLogProcessor(const sp<UidMap>& uidMap, |
| 38 | const std::function<void(const vector<uint8_t>&)>& pushLog) |
yro | fd05a4e | 2017-10-26 14:08:26 -0700 | [diff] [blame] | 39 | : mUidMap(uidMap), mPushLog(pushLog) { |
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? |
Joe Onorato | c4dfae5 | 2017-10-17 23:38:21 -0700 | [diff] [blame] | 46 | void StatsLogProcessor::OnLogEvent(const LogEvent& msg) { |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 47 | // pass the event to metrics managers. |
| 48 | for (auto& pair : mMetricsManagers) { |
| 49 | pair.second->onLogEvent(msg); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 50 | flushIfNecessary(msg.GetTimestampNs(), pair.first, pair.second); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 51 | } |
David Chen | 2158296 | 2017-11-01 17:32:46 -0700 | [diff] [blame^] | 52 | |
| 53 | // Hard-coded logic to update the isolated uid's in the uid-map. |
| 54 | // The field numbers need to be currently updated by hand with stats_events.proto |
| 55 | if (msg.GetTagId() == android::util::ISOLATED_UID_CHANGED) { |
| 56 | status_t err = NO_ERROR, err2 = NO_ERROR, err3 = NO_ERROR; |
| 57 | bool is_create = msg.GetBool(3, &err); |
| 58 | auto parent_uid = int(msg.GetLong(1, &err2)); |
| 59 | auto isolated_uid = int(msg.GetLong(2, &err3)); |
| 60 | if (err == NO_ERROR && err2 == NO_ERROR && err3 == NO_ERROR) { |
| 61 | if (is_create) { |
| 62 | mUidMap->assignIsolatedUid(isolated_uid, parent_uid); |
| 63 | } else { |
| 64 | mUidMap->removeIsolatedUid(isolated_uid, parent_uid); |
| 65 | } |
| 66 | } |
| 67 | } |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 70 | void StatsLogProcessor::OnConfigUpdated(const ConfigKey& key, const StatsdConfig& config) { |
| 71 | auto it = mMetricsManagers.find(key); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 72 | if (it != mMetricsManagers.end()) { |
| 73 | it->second->finish(); |
| 74 | } |
| 75 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 76 | ALOGD("Updated configuration for key %s", key.ToString().c_str()); |
Yao Chen | 44cf27c | 2017-09-14 22:32:50 -0700 | [diff] [blame] | 77 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 78 | unique_ptr<MetricsManager> newMetricsManager = std::make_unique<MetricsManager>(config); |
| 79 | if (newMetricsManager->isConfigValid()) { |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 80 | mUidMap->OnConfigUpdated(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 81 | mMetricsManagers[key] = std::move(newMetricsManager); |
| 82 | // Why doesn't this work? mMetricsManagers.insert({key, std::move(newMetricsManager)}); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 83 | ALOGD("StatsdConfig valid"); |
| 84 | } else { |
| 85 | // If there is any error in the config, don't use it. |
| 86 | ALOGD("StatsdConfig NOT valid"); |
| 87 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 88 | } |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 89 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 90 | ConfigMetricsReport StatsLogProcessor::onDumpReport(const ConfigKey& key) { |
| 91 | ConfigMetricsReport report; |
| 92 | |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 93 | auto it = mMetricsManagers.find(key); |
| 94 | if (it == mMetricsManagers.end()) { |
| 95 | ALOGW("Config source %s does not exist", key.ToString().c_str()); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 96 | return report; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 97 | } |
| 98 | |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 99 | auto set_key = report.mutable_config_key(); |
| 100 | set_key->set_uid(key.GetUid()); |
| 101 | set_key->set_name(key.GetName()); |
| 102 | for (auto m : it->second->onDumpReport()) { |
| 103 | // Transfer the vector of StatsLogReport into a field |
| 104 | // TODO: perhaps we just have bytes being returned from onDumpReport and transfer bytes |
| 105 | auto dest = report.add_metrics(); |
| 106 | *dest = m; |
| 107 | } |
| 108 | auto temp = mUidMap->getOutput(key); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 109 | report.mutable_uid_map()->Swap(&temp); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 110 | return report; |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 113 | void StatsLogProcessor::OnConfigRemoved(const ConfigKey& key) { |
| 114 | auto it = mMetricsManagers.find(key); |
| 115 | if (it != mMetricsManagers.end()) { |
| 116 | it->second->finish(); |
| 117 | mMetricsManagers.erase(it); |
David Chen | d689689 | 2017-10-25 11:49:03 -0700 | [diff] [blame] | 118 | mUidMap->OnConfigRemoved(key); |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 119 | } |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 120 | auto flushTime = mLastFlushTimes.find(key); |
| 121 | if (flushTime != mLastFlushTimes.end()) { |
| 122 | mLastFlushTimes.erase(flushTime); |
| 123 | } |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 124 | } |
| 125 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 126 | void StatsLogProcessor::flushIfNecessary(uint64_t timestampNs, |
| 127 | const ConfigKey& key, |
| 128 | const unique_ptr<MetricsManager>& metricsManager) { |
| 129 | auto lastFlushNs = mLastFlushTimes.find(key); |
| 130 | if (lastFlushNs != mLastFlushTimes.end()) { |
| 131 | if (timestampNs - lastFlushNs->second < kMinFlushPeriod) { |
| 132 | return; |
| 133 | } |
| 134 | } |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 135 | |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 136 | size_t totalBytes = metricsManager->byteSize(); |
| 137 | if (totalBytes > kMaxSerializedBytes) { |
| 138 | flush(); |
| 139 | mLastFlushTimes[key] = std::move(timestampNs); |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | void StatsLogProcessor::flush() { |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 144 | // TODO: Take ConfigKey as an argument and flush metrics related to the |
| 145 | // ConfigKey. Also, create a wrapper that holds a repeated field of |
| 146 | // StatsLogReport's. |
| 147 | /* |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 148 | StatsLogReport logReport; |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 149 | const int numBytes = logReport.ByteSize(); |
| 150 | vector<uint8_t> logReportBuffer(numBytes); |
| 151 | logReport.SerializeToArray(&logReportBuffer[0], numBytes); |
| 152 | mPushLog(logReportBuffer); |
yro | 69007c8 | 2017-10-26 20:42:57 -0700 | [diff] [blame] | 153 | */ |
yro | 31eb67b | 2017-10-24 13:33:21 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 156 | } // namespace statsd |
| 157 | } // namespace os |
| 158 | } // namespace android |