blob: 79925ff138cfd28ba51dcc473dbdb43996fbf383 [file] [log] [blame]
Chenjie Yu1a317ba2017-10-05 16:05:32 -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
Chenjie Yu1a317ba2017-10-05 16:05:32 -070017#define DEBUG true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Chenjie Yu1a317ba2017-10-05 16:05:32 -070019
Chenjie Yu1a317ba2017-10-05 16:05:32 -070020#include <android/os/IStatsCompanionService.h>
David Chen1481fe12017-10-16 13:16:34 -070021#include <cutils/log.h>
22#include <algorithm>
Chenjie Yub3dda412017-10-24 13:41:59 -070023#include <climits>
Chenjie Yue33bc3b2017-11-06 17:56:44 -080024#include "CpuTimePerUidFreqPuller.h"
25#include "CpuTimePerUidPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070026#include "ResourcePowerManagerPuller.h"
27#include "StatsCompanionServicePuller.h"
Chenjie Yu6736c892017-11-09 10:50:09 -080028#include "StatsPullerManagerImpl.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070029#include "StatsService.h"
30#include "logd/LogEvent.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070031#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070032
33#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070034
Yao Chen93fe3a32017-11-02 13:52:59 -070035using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070036using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070037using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070038using std::string;
39using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080040using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070041
42namespace android {
43namespace os {
44namespace statsd {
45
Chenjie Yu6736c892017-11-09 10:50:09 -080046StatsPullerManagerImpl::StatsPullerManagerImpl()
Chenjie Yub3dda412017-10-24 13:41:59 -070047 : mCurrentPullingInterval(LONG_MAX), mPullStartTimeMs(get_pull_start_time_ms()) {
Chenjie Yue33bc3b2017-11-06 17:56:44 -080048 shared_ptr<StatsPuller> statsCompanionServicePuller = make_shared<StatsCompanionServicePuller>();
Yao Chen93fe3a32017-11-02 13:52:59 -070049 shared_ptr<StatsPuller> resourcePowerManagerPuller = make_shared<ResourcePowerManagerPuller>();
Chenjie Yue33bc3b2017-11-06 17:56:44 -080050 shared_ptr<StatsPuller> cpuTimePerUidPuller = make_shared<CpuTimePerUidPuller>();
51 shared_ptr<StatsPuller> cpuTimePerUidFreqPuller = make_shared<CpuTimePerUidFreqPuller>();
Chenjie Yu5305e1d2017-10-31 13:49:36 -070052
Chenjie Yu31d14d72017-12-12 17:54:33 -080053 mPullers.insert({android::util::KERNEL_WAKELOCK,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080054 statsCompanionServicePuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080055 mPullers.insert({android::util::WIFI_BYTES_TRANSFER,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080056 statsCompanionServicePuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080057 mPullers.insert({android::util::MOBILE_BYTES_TRANSFER,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080058 statsCompanionServicePuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080059 mPullers.insert({android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080060 statsCompanionServicePuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080061 mPullers.insert({android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080062 statsCompanionServicePuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080063 mPullers.insert({android::util::PLATFORM_SLEEP_STATE,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080064 resourcePowerManagerPuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080065 mPullers.insert({android::util::SLEEP_STATE_VOTER,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080066 resourcePowerManagerPuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080067 mPullers.insert({android::util::SUBSYSTEM_SLEEP_STATE,
Chenjie Yue33bc3b2017-11-06 17:56:44 -080068 resourcePowerManagerPuller});
Chenjie Yu31d14d72017-12-12 17:54:33 -080069 mPullers.insert({android::util::CPU_TIME_PER_UID, cpuTimePerUidPuller});
70 mPullers.insert({android::util::CPU_TIME_PER_UID_FREQ, cpuTimePerUidFreqPuller});
Chenjie Yu5305e1d2017-10-31 13:49:36 -070071
72 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -070073}
74
Chenjie Yu6736c892017-11-09 10:50:09 -080075bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Yao Chen93fe3a32017-11-02 13:52:59 -070076 if (DEBUG) ALOGD("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070077
Yao Chen93fe3a32017-11-02 13:52:59 -070078 if (mPullers.find(tagId) != mPullers.end()) {
Chenjie Yu032fefc2017-12-01 23:30:59 -080079 bool ret = mPullers.find(tagId)->second->Pull(tagId, data);
80 ALOGD("pulled %d items", (int)data->size());
81 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -070082 } else {
83 ALOGD("Unknown tagId %d", tagId);
84 return false; // Return early since we don't know what to pull.
85 }
86}
Chenjie Yub3dda412017-10-24 13:41:59 -070087
Chenjie Yu6736c892017-11-09 10:50:09 -080088StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() {
89 static StatsPullerManagerImpl instance;
Chenjie Yub3dda412017-10-24 13:41:59 -070090 return instance;
91}
92
Yangster7c334a12017-11-22 14:24:24 -080093bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const {
Chenjie Yu5305e1d2017-10-31 13:49:36 -070094 return mPullers.find(tagId) != mPullers.end();
Chenjie Yub3dda412017-10-24 13:41:59 -070095}
96
Yangster7c334a12017-11-22 14:24:24 -080097long StatsPullerManagerImpl::get_pull_start_time_ms() const {
Chenjie Yub3dda412017-10-24 13:41:59 -070098 // TODO: limit and align pull intervals to 10min boundaries if this turns out to be a problem
99 return time(nullptr) * 1000;
100}
101
Chenjie Yu6736c892017-11-09 10:50:09 -0800102void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
103 long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700104 AutoMutex _l(mReceiversLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800105 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700106 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800107 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700108 VLOG("Receiver already registered of %d", (int)receivers.size());
109 return;
110 }
111 }
112 ReceiverInfo receiverInfo;
113 receiverInfo.receiver = receiver;
114 receiverInfo.timeInfo.first = intervalMs;
115 receivers.push_back(receiverInfo);
116
117 // There is only one alarm for all pulled events. So only set it to the smallest denom.
118 if (intervalMs < mCurrentPullingInterval) {
119 VLOG("Updating pulling interval %ld", intervalMs);
120 mCurrentPullingInterval = intervalMs;
121 if (mStatsCompanionService != nullptr) {
122 mStatsCompanionService->setPullingAlarms(mPullStartTimeMs, mCurrentPullingInterval);
123 } else {
124 VLOG("Failed to update pulling interval");
125 }
126 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700127 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700128}
129
Chenjie Yu6736c892017-11-09 10:50:09 -0800130void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700131 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700132 if (mReceivers.find(tagId) == mReceivers.end()) {
133 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700134 return;
135 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700136 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700137 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800138 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700139 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700140 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700141 return;
142 }
143 }
144}
145
Chenjie Yu6736c892017-11-09 10:50:09 -0800146void StatsPullerManagerImpl::OnAlarmFired() {
Chenjie Yub3dda412017-10-24 13:41:59 -0700147 AutoMutex _l(mReceiversLock);
148
149 uint64_t currentTimeMs = time(nullptr) * 1000;
150
151 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
152 vector<pair<int, vector<ReceiverInfo*>>>();
153 for (auto& pair : mReceivers) {
154 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700155 if (pair.second.size() != 0) {
156 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700157 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
158 receivers.push_back(&receiverInfo);
159 }
160 }
161 if (receivers.size() > 0) {
162 needToPull.push_back(make_pair(pair.first, receivers));
163 }
164 }
165 }
166
167 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700168 vector<shared_ptr<LogEvent>> data;
169 if (Pull(pullInfo.first, &data)) {
170 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800171 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
172 if (receiverPtr != nullptr) {
173 receiverPtr->onDataPulled(data);
174 receiverInfo->timeInfo.second = currentTimeMs;
175 } else {
176 VLOG("receiver already gone.");
177 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700178 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700179 }
180 }
181}
182
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700183} // namespace statsd
184} // namespace os
185} // namespace android