blob: 0b772b3752de75e2004f897407e3540be9758eb5 [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 Yu80f91122018-01-31 20:24:50 -080017#define DEBUG false
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"
Tej Singhbf972d92018-01-10 20:51:13 -080026#include "ResourceHealthManagerPuller.h"
Chenjie Yu80f91122018-01-31 20:24:50 -080027#include "KernelUidCpuActiveTimeReader.h"
28#include "KernelUidCpuClusterTimeReader.h"
29#include "SubsystemSleepStatePuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070030#include "StatsCompanionServicePuller.h"
Chenjie Yu6736c892017-11-09 10:50:09 -080031#include "StatsPullerManagerImpl.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070032#include "StatsService.h"
Tej Singhbf972d92018-01-10 20:51:13 -080033#include "SubsystemSleepStatePuller.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070034#include "logd/LogEvent.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070035#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070036
37#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070038
Yao Chen93fe3a32017-11-02 13:52:59 -070039using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070040using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070041using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070042using std::string;
43using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080044using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070045
46namespace android {
47namespace os {
48namespace statsd {
49
Chenjie Yu80f91122018-01-31 20:24:50 -080050const std::map<int, PullAtomInfo> StatsPullerManagerImpl::kAllPullAtomInfo = {
51 // wifi_bytes_transfer
52 {android::util::WIFI_BYTES_TRANSFER,
53 {{2, 3, 4, 5},
54 {},
55 1,
56 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
57 // wifi_bytes_transfer_by_fg_bg
58 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
59 {{3, 4, 5, 6},
60 {2},
61 1,
62 new StatsCompanionServicePuller(
63 android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
64 // mobile_bytes_transfer
65 {android::util::MOBILE_BYTES_TRANSFER,
66 {{2, 3, 4, 5},
67 {},
68 1,
69 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
70 // mobile_bytes_transfer_by_fg_bg
71 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
72 {{3, 4, 5, 6},
73 {2},
74 1,
75 new StatsCompanionServicePuller(
76 android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
77 // bluetooth_bytes_transfer
78 {android::util::BLUETOOTH_BYTES_TRANSFER,
79 {{2, 3},
80 {},
81 1,
82 new StatsCompanionServicePuller(
83 android::util::BLUETOOTH_BYTES_TRANSFER)}},
84 // kernel_wakelock
85 {android::util::KERNEL_WAKELOCK,
86 {{},
87 {},
88 1,
89 new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
90 // subsystem_sleep_state
91 {android::util::SUBSYSTEM_SLEEP_STATE,
92 {{},
93 {},
94 1,
95 new StatsCompanionServicePuller(android::util::SUBSYSTEM_SLEEP_STATE)}},
96 // cpu_time_per_freq
97 {android::util::CPU_TIME_PER_FREQ,
98 {{3},
99 {2},
100 1,
101 new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
102 // cpu_time_per_uid
103 {android::util::CPU_TIME_PER_UID,
104 {{2, 3}, {}, 1, new CpuTimePerUidPuller()}},
105 // cpu_time_per_uid_freq
106 {android::util::CPU_TIME_PER_UID_FREQ,
107 {{3}, {2}, 1, new CpuTimePerUidFreqPuller()}},
108 // wifi_activity_energy_info
109 {android::util::WIFI_ACTIVITY_ENERGY_INFO,
110 {{},
111 {},
112 1,
113 new StatsCompanionServicePuller(
114 android::util::WIFI_ACTIVITY_ENERGY_INFO)}},
115 // modem_activity_info
116 {android::util::MODEM_ACTIVITY_INFO,
117 {{},
118 {},
119 1,
120 new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
121 // bluetooth_activity_info
122 {android::util::BLUETOOTH_ACTIVITY_INFO,
123 {{},
124 {},
125 1,
126 new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
127 // system_elapsed_realtime
128 {android::util::SYSTEM_ELAPSED_REALTIME,
129 {{},
130 {},
131 1,
132 new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
133 // system_uptime
134 {android::util::SYSTEM_UPTIME,
135 {{},
136 {},
137 1,
138 new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
139 // cpu_active_time
140 {android::util::CPU_ACTIVE_TIME,
141 {{3}, {2}, 1, new KernelUidCpuActiveTimeReader()}},
142 // cpu_cluster_time
143 {android::util::CPU_CLUSTER_TIME,
144 {{3}, {2}, 1, new KernelUidCpuClusterTimeReader()}},
145 // disk_space
146 {android::util::DISK_SPACE,
147 {{}, {}, 1, new StatsCompanionServicePuller(android::util::DISK_SPACE)}},
148 // remaining_battery_capacity
149 {android::util::REMAINING_BATTERY_CAPACITY,
150 {{},
151 {},
152 1,
153 new ResourceHealthManagerPuller(
154 android::util::REMAINING_BATTERY_CAPACITY)}},
155 // full_battery_capacity
156 {android::util::FULL_BATTERY_CAPACITY,
157 {{},
158 {},
159 1,
160 new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}}};
161
Chenjie Yu6736c892017-11-09 10:50:09 -0800162StatsPullerManagerImpl::StatsPullerManagerImpl()
Chenjie Yu85ed8382017-12-14 16:48:54 -0800163 : mCurrentPullingInterval(LONG_MAX) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700164 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700165}
166
Chenjie Yu6736c892017-11-09 10:50:09 -0800167bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Yao Chen93fe3a32017-11-02 13:52:59 -0700168 if (DEBUG) ALOGD("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700169
Chenjie Yu80f91122018-01-31 20:24:50 -0800170 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
171 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
172 ALOGD("pulled %d items", (int)data->size());
173 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700174 } else {
175 ALOGD("Unknown tagId %d", tagId);
176 return false; // Return early since we don't know what to pull.
177 }
178}
Chenjie Yub3dda412017-10-24 13:41:59 -0700179
Chenjie Yu6736c892017-11-09 10:50:09 -0800180StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() {
181 static StatsPullerManagerImpl instance;
Chenjie Yub3dda412017-10-24 13:41:59 -0700182 return instance;
183}
184
Yangster7c334a12017-11-22 14:24:24 -0800185bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const {
Chenjie Yu80f91122018-01-31 20:24:50 -0800186 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700187}
188
Chenjie Yu6736c892017-11-09 10:50:09 -0800189void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
190 long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700191 AutoMutex _l(mReceiversLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800192 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700193 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800194 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700195 VLOG("Receiver already registered of %d", (int)receivers.size());
196 return;
197 }
198 }
199 ReceiverInfo receiverInfo;
200 receiverInfo.receiver = receiver;
201 receiverInfo.timeInfo.first = intervalMs;
202 receivers.push_back(receiverInfo);
203
Chenjie Yu85ed8382017-12-14 16:48:54 -0800204 // Round it to the nearest minutes. This is the limit of alarm manager.
205 // In practice, we should limit it higher.
206 long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60;
Chenjie Yub3dda412017-10-24 13:41:59 -0700207 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu85ed8382017-12-14 16:48:54 -0800208 if (roundedIntervalMs < mCurrentPullingInterval) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700209 VLOG("Updating pulling interval %ld", intervalMs);
Chenjie Yu85ed8382017-12-14 16:48:54 -0800210 mCurrentPullingInterval = roundedIntervalMs;
211 long currentTimeMs = time(nullptr) * 1000;
212 long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval - (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval;
Chenjie Yub3dda412017-10-24 13:41:59 -0700213 if (mStatsCompanionService != nullptr) {
Chenjie Yu85ed8382017-12-14 16:48:54 -0800214 mStatsCompanionService->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval);
Chenjie Yub3dda412017-10-24 13:41:59 -0700215 } else {
216 VLOG("Failed to update pulling interval");
217 }
218 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700219 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700220}
221
Chenjie Yu6736c892017-11-09 10:50:09 -0800222void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700223 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700224 if (mReceivers.find(tagId) == mReceivers.end()) {
225 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700226 return;
227 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700228 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700229 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800230 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700231 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700232 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700233 return;
234 }
235 }
236}
237
Chenjie Yu6736c892017-11-09 10:50:09 -0800238void StatsPullerManagerImpl::OnAlarmFired() {
Chenjie Yub3dda412017-10-24 13:41:59 -0700239 AutoMutex _l(mReceiversLock);
240
Chenjie Yu85ed8382017-12-14 16:48:54 -0800241 uint64_t currentTimeMs = time(nullptr) /60 * 60 * 1000;
Chenjie Yub3dda412017-10-24 13:41:59 -0700242
243 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
244 vector<pair<int, vector<ReceiverInfo*>>>();
245 for (auto& pair : mReceivers) {
246 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700247 if (pair.second.size() != 0) {
248 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700249 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
250 receivers.push_back(&receiverInfo);
251 }
252 }
253 if (receivers.size() > 0) {
254 needToPull.push_back(make_pair(pair.first, receivers));
255 }
256 }
257 }
258
259 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700260 vector<shared_ptr<LogEvent>> data;
261 if (Pull(pullInfo.first, &data)) {
262 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800263 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
264 if (receiverPtr != nullptr) {
265 receiverPtr->onDataPulled(data);
266 receiverInfo->timeInfo.second = currentTimeMs;
267 } else {
268 VLOG("receiver already gone.");
269 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700270 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700271 }
272 }
273}
274
Chenjie Yufa22d652018-02-05 14:37:48 -0800275int StatsPullerManagerImpl::ForceClearPullerCache() {
276 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800277 for (const auto& pulledAtom : kAllPullAtomInfo) {
278 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800279 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800280 return totalCleared;
281}
282
283int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(long timestampSec) {
284 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800285 for (const auto& pulledAtom : kAllPullAtomInfo) {
286 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampSec);
Chenjie Yufa22d652018-02-05 14:37:48 -0800287 }
288 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800289}
290
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700291} // namespace statsd
292} // namespace os
293} // namespace android