blob: 148c9ae9b249a847d9205331918902a4cc83470f [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 Yuc8b7f222018-01-11 23:25:57 -080026#include "SubsystemSleepStatePuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070027#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 Yu85ed8382017-12-14 16:48:54 -080047 : mCurrentPullingInterval(LONG_MAX) {
Chenjie Yu31d14d72017-12-12 17:54:33 -080048 mPullers.insert({android::util::KERNEL_WAKELOCK,
Chenjie Yub038b702017-12-18 15:15:34 -080049 make_shared<StatsCompanionServicePuller>(android::util::KERNEL_WAKELOCK)});
Chenjie Yu31d14d72017-12-12 17:54:33 -080050 mPullers.insert({android::util::WIFI_BYTES_TRANSFER,
Chenjie Yub038b702017-12-18 15:15:34 -080051 make_shared<StatsCompanionServicePuller>(android::util::WIFI_BYTES_TRANSFER)});
52 mPullers.insert(
53 {android::util::MOBILE_BYTES_TRANSFER,
54 make_shared<StatsCompanionServicePuller>(android::util::MOBILE_BYTES_TRANSFER)});
Chenjie Yu31d14d72017-12-12 17:54:33 -080055 mPullers.insert({android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yub038b702017-12-18 15:15:34 -080056 make_shared<StatsCompanionServicePuller>(
57 android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)});
Chenjie Yu31d14d72017-12-12 17:54:33 -080058 mPullers.insert({android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yub038b702017-12-18 15:15:34 -080059 make_shared<StatsCompanionServicePuller>(
60 android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)});
Chenjie Yub038b702017-12-18 15:15:34 -080061 mPullers.insert(
62 {android::util::SUBSYSTEM_SLEEP_STATE,
Chenjie Yuc8b7f222018-01-11 23:25:57 -080063 make_shared<SubsystemSleepStatePuller>()});
Chenjie Yub038b702017-12-18 15:15:34 -080064 mPullers.insert({android::util::CPU_TIME_PER_FREQ, make_shared<StatsCompanionServicePuller>(android::util::CPU_TIME_PER_FREQ)});
65 mPullers.insert({android::util::CPU_TIME_PER_UID, make_shared<CpuTimePerUidPuller>()});
66 mPullers.insert({android::util::CPU_TIME_PER_UID_FREQ, make_shared<CpuTimePerUidFreqPuller>()});
Chenjie Yu9d7720b2018-01-24 10:34:48 -080067 mPullers.insert(
68 {android::util::SYSTEM_ELAPSED_REALTIME,
69 make_shared<StatsCompanionServicePuller>(android::util::SYSTEM_ELAPSED_REALTIME)});
Chenjie Yu937d7422018-01-10 16:37:53 -080070 mPullers.insert({android::util::SYSTEM_UPTIME,
71 make_shared<StatsCompanionServicePuller>(android::util::SYSTEM_UPTIME)});
Chenjie Yu9d7720b2018-01-24 10:34:48 -080072 mPullers.insert({android::util::DISK_SPACE,
73 make_shared<StatsCompanionServicePuller>(android::util::DISK_SPACE)});
74 mPullers.insert(
75 {android::util::BLUETOOTH_ACTIVITY_INFO,
76 make_shared<StatsCompanionServicePuller>(android::util::BLUETOOTH_ACTIVITY_INFO)});
77
78 mPullers.insert(
79 {android::util::BLUETOOTH_BYTES_TRANSFER,
80 make_shared<StatsCompanionServicePuller>(android::util::BLUETOOTH_BYTES_TRANSFER)});
Chenjie Yu937d7422018-01-10 16:37:53 -080081 mPullers.insert(
82 {android::util::WIFI_ACTIVITY_ENERGY_INFO,
83 make_shared<StatsCompanionServicePuller>(android::util::WIFI_ACTIVITY_ENERGY_INFO)});
84 mPullers.insert({android::util::MODEM_ACTIVITY_INFO,
85 make_shared<StatsCompanionServicePuller>(android::util::MODEM_ACTIVITY_INFO)});
Chenjie Yu5305e1d2017-10-31 13:49:36 -070086
87 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -070088}
89
Chenjie Yu6736c892017-11-09 10:50:09 -080090bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Yao Chen93fe3a32017-11-02 13:52:59 -070091 if (DEBUG) ALOGD("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070092
Yao Chen93fe3a32017-11-02 13:52:59 -070093 if (mPullers.find(tagId) != mPullers.end()) {
Chenjie Yub038b702017-12-18 15:15:34 -080094 bool ret = mPullers.find(tagId)->second->Pull(data);
Chenjie Yu032fefc2017-12-01 23:30:59 -080095 ALOGD("pulled %d items", (int)data->size());
96 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -070097 } else {
98 ALOGD("Unknown tagId %d", tagId);
99 return false; // Return early since we don't know what to pull.
100 }
101}
Chenjie Yub3dda412017-10-24 13:41:59 -0700102
Chenjie Yu6736c892017-11-09 10:50:09 -0800103StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() {
104 static StatsPullerManagerImpl instance;
Chenjie Yub3dda412017-10-24 13:41:59 -0700105 return instance;
106}
107
Yangster7c334a12017-11-22 14:24:24 -0800108bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700109 return mPullers.find(tagId) != mPullers.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700110}
111
Chenjie Yu6736c892017-11-09 10:50:09 -0800112void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
113 long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700114 AutoMutex _l(mReceiversLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800115 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700116 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800117 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700118 VLOG("Receiver already registered of %d", (int)receivers.size());
119 return;
120 }
121 }
122 ReceiverInfo receiverInfo;
123 receiverInfo.receiver = receiver;
124 receiverInfo.timeInfo.first = intervalMs;
125 receivers.push_back(receiverInfo);
126
Chenjie Yu85ed8382017-12-14 16:48:54 -0800127 // Round it to the nearest minutes. This is the limit of alarm manager.
128 // In practice, we should limit it higher.
129 long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60;
Chenjie Yub3dda412017-10-24 13:41:59 -0700130 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu85ed8382017-12-14 16:48:54 -0800131 if (roundedIntervalMs < mCurrentPullingInterval) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700132 VLOG("Updating pulling interval %ld", intervalMs);
Chenjie Yu85ed8382017-12-14 16:48:54 -0800133 mCurrentPullingInterval = roundedIntervalMs;
134 long currentTimeMs = time(nullptr) * 1000;
135 long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval - (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval;
Chenjie Yub3dda412017-10-24 13:41:59 -0700136 if (mStatsCompanionService != nullptr) {
Chenjie Yu85ed8382017-12-14 16:48:54 -0800137 mStatsCompanionService->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval);
Chenjie Yub3dda412017-10-24 13:41:59 -0700138 } else {
139 VLOG("Failed to update pulling interval");
140 }
141 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700142 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700143}
144
Chenjie Yu6736c892017-11-09 10:50:09 -0800145void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700146 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700147 if (mReceivers.find(tagId) == mReceivers.end()) {
148 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700149 return;
150 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700151 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700152 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800153 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700154 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700155 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700156 return;
157 }
158 }
159}
160
Chenjie Yu6736c892017-11-09 10:50:09 -0800161void StatsPullerManagerImpl::OnAlarmFired() {
Chenjie Yub3dda412017-10-24 13:41:59 -0700162 AutoMutex _l(mReceiversLock);
163
Chenjie Yu85ed8382017-12-14 16:48:54 -0800164 uint64_t currentTimeMs = time(nullptr) /60 * 60 * 1000;
Chenjie Yub3dda412017-10-24 13:41:59 -0700165
166 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
167 vector<pair<int, vector<ReceiverInfo*>>>();
168 for (auto& pair : mReceivers) {
169 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700170 if (pair.second.size() != 0) {
171 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700172 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
173 receivers.push_back(&receiverInfo);
174 }
175 }
176 if (receivers.size() > 0) {
177 needToPull.push_back(make_pair(pair.first, receivers));
178 }
179 }
180 }
181
182 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700183 vector<shared_ptr<LogEvent>> data;
184 if (Pull(pullInfo.first, &data)) {
185 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800186 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
187 if (receiverPtr != nullptr) {
188 receiverPtr->onDataPulled(data);
189 receiverInfo->timeInfo.second = currentTimeMs;
190 } else {
191 VLOG("receiver already gone.");
192 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700193 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700194 }
195 }
196}
197
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700198} // namespace statsd
199} // namespace os
200} // namespace android