blob: dd6406bb90ca103192437340dd217c50cade7402 [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 Yuaa5b2012018-03-21 13:53:15 -070024#include "../logd/LogEvent.h"
25#include "../stats_log_util.h"
26#include "../statscompanion_util.h"
Tej Singh40298312018-02-16 00:15:09 -080027#include "ResourceHealthManagerPuller.h"
28#include "ResourceThermalManagerPuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070029#include "StatsCompanionServicePuller.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070030#include "StatsService.h"
Tej Singhbf972d92018-01-10 20:51:13 -080031#include "SubsystemSleepStatePuller.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070032#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070033
34#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070035
Yao Chen93fe3a32017-11-02 13:52:59 -070036using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070037using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070038using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070039using std::string;
40using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080041using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070042
43namespace android {
44namespace os {
45namespace statsd {
46
Chenjie Yu80f91122018-01-31 20:24:50 -080047const std::map<int, PullAtomInfo> StatsPullerManagerImpl::kAllPullAtomInfo = {
Chenjie Yufeba3092018-02-08 14:33:37 -080048 // wifi_bytes_transfer
49 {android::util::WIFI_BYTES_TRANSFER,
Chenjie Yuec676612018-03-07 09:19:17 -080050 {{2, 3, 4, 5}, {}, 1,
Chenjie Yufeba3092018-02-08 14:33:37 -080051 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
52 // wifi_bytes_transfer_by_fg_bg
53 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yuec676612018-03-07 09:19:17 -080054 {{3, 4, 5, 6}, {2}, 1,
Chenjie Yufeba3092018-02-08 14:33:37 -080055 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
56 // mobile_bytes_transfer
57 {android::util::MOBILE_BYTES_TRANSFER,
Chenjie Yuec676612018-03-07 09:19:17 -080058 {{2, 3, 4, 5}, {}, 1,
Chenjie Yufeba3092018-02-08 14:33:37 -080059 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
60 // mobile_bytes_transfer_by_fg_bg
61 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
Chenjie Yuec676612018-03-07 09:19:17 -080062 {{3, 4, 5, 6}, {2}, 1,
Chenjie Yufeba3092018-02-08 14:33:37 -080063 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
64 // bluetooth_bytes_transfer
65 {android::util::BLUETOOTH_BYTES_TRANSFER,
66 {{2, 3}, {}, 1, new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
67 // kernel_wakelock
68 {android::util::KERNEL_WAKELOCK,
69 {{}, {}, 1, new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
70 // subsystem_sleep_state
Chenjie Yuec676612018-03-07 09:19:17 -080071 {android::util::SUBSYSTEM_SLEEP_STATE,
72 {{}, {}, 1, new SubsystemSleepStatePuller()}},
Chenjie Yufeba3092018-02-08 14:33:37 -080073 // cpu_time_per_freq
74 {android::util::CPU_TIME_PER_FREQ,
75 {{3}, {2}, 1, new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
76 // cpu_time_per_uid
Chenjie Yuec676612018-03-07 09:19:17 -080077 {android::util::CPU_TIME_PER_UID,
78 {{2, 3}, {}, 1, new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080079 // cpu_time_per_uid_freq
Chenjie Yuec676612018-03-07 09:19:17 -080080 // the throttling is 3sec, handled in frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
81 {android::util::CPU_TIME_PER_UID_FREQ,
82 {{4}, {2,3}, 0, new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
83 // cpu_active_time
84 // the throttling is 3sec, handled in frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
85 {android::util::CPU_ACTIVE_TIME,
86 {{2}, {}, 0, new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
87 // cpu_cluster_time
88 // the throttling is 3sec, handled in frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
89 {android::util::CPU_CLUSTER_TIME,
90 {{3}, {2}, 0, new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -080091 // wifi_activity_energy_info
92 {android::util::WIFI_ACTIVITY_ENERGY_INFO,
93 {{}, {}, 1, new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_ENERGY_INFO)}},
94 // modem_activity_info
95 {android::util::MODEM_ACTIVITY_INFO,
96 {{}, {}, 1, new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
97 // bluetooth_activity_info
98 {android::util::BLUETOOTH_ACTIVITY_INFO,
99 {{}, {}, 1, new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
100 // system_elapsed_realtime
101 {android::util::SYSTEM_ELAPSED_REALTIME,
102 {{}, {}, 1, new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
103 // system_uptime
104 {android::util::SYSTEM_UPTIME,
105 {{}, {}, 1, new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
Chenjie Yufeba3092018-02-08 14:33:37 -0800106 // disk_space
107 {android::util::DISK_SPACE,
108 {{}, {}, 1, new StatsCompanionServicePuller(android::util::DISK_SPACE)}},
109 // remaining_battery_capacity
110 {android::util::REMAINING_BATTERY_CAPACITY,
111 {{}, {}, 1, new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
112 // full_battery_capacity
113 {android::util::FULL_BATTERY_CAPACITY,
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800114 {{}, {}, 1, new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
115 // process_memory_state
116 {android::util::PROCESS_MEMORY_STATE,
Chenjie Yuec676612018-03-07 09:19:17 -0800117 {{4,5,6,7,8},
118 {2,3},
119 0,
120 new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
Tej Singh40298312018-02-16 00:15:09 -0800121 // temperature
122 {android::util::TEMPERATURE, {{}, {}, 1, new ResourceThermalManagerPuller()}}};
Chenjie Yu80f91122018-01-31 20:24:50 -0800123
Chenjie Yu6736c892017-11-09 10:50:09 -0800124StatsPullerManagerImpl::StatsPullerManagerImpl()
Chenjie Yu85ed8382017-12-14 16:48:54 -0800125 : mCurrentPullingInterval(LONG_MAX) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700126}
127
Chenjie Yu6736c892017-11-09 10:50:09 -0800128bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800129 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700130
Chenjie Yu80f91122018-01-31 20:24:50 -0800131 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Tej Singh484524a2018-02-01 15:10:05 -0800132 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
133 VLOG("pulled %d items", (int)data->size());
134 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700135 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800136 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700137 return false; // Return early since we don't know what to pull.
138 }
139}
Chenjie Yub3dda412017-10-24 13:41:59 -0700140
Chenjie Yu6736c892017-11-09 10:50:09 -0800141StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() {
142 static StatsPullerManagerImpl instance;
Chenjie Yub3dda412017-10-24 13:41:59 -0700143 return instance;
144}
145
Yangster7c334a12017-11-22 14:24:24 -0800146bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800147 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700148}
149
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700150void StatsPullerManagerImpl::updateAlarmLocked() {
151 long currentTimeMs = getElapsedRealtimeMillis();
152 long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval -
153 (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval;
154 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
155 if (statsCompanionServiceCopy != nullptr) {
156 statsCompanionServiceCopy->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval);
157 } else {
158 VLOG("StatsCompanionService not available. Alarm not set.");
159 }
160 return;
161}
162
163void StatsPullerManagerImpl::SetStatsCompanionService(
164 sp<IStatsCompanionService> statsCompanionService) {
165 AutoMutex _l(mLock);
166 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
167 mStatsCompanionService = statsCompanionService;
168 for (const auto& pulledAtom : kAllPullAtomInfo) {
169 pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
170 }
171 if (mStatsCompanionService != nullptr) {
172 updateAlarmLocked();
173 }
174}
175
Chenjie Yu6736c892017-11-09 10:50:09 -0800176void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
177 long intervalMs) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700178 AutoMutex _l(mLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800179 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700180 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800181 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700182 VLOG("Receiver already registered of %d", (int)receivers.size());
183 return;
184 }
185 }
186 ReceiverInfo receiverInfo;
187 receiverInfo.receiver = receiver;
188 receiverInfo.timeInfo.first = intervalMs;
189 receivers.push_back(receiverInfo);
190
Chenjie Yu85ed8382017-12-14 16:48:54 -0800191 // Round it to the nearest minutes. This is the limit of alarm manager.
192 // In practice, we should limit it higher.
193 long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60;
Chenjie Yu83baaa12018-03-19 10:41:35 -0700194 // Scheduled pulling should be at least 1 min apart.
195 // This can be lower in cts tests, in which case we round it to 1 min.
196 if (roundedIntervalMs < 60 * 1000) {
197 roundedIntervalMs = 60 * 1000;
198 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700199 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu85ed8382017-12-14 16:48:54 -0800200 if (roundedIntervalMs < mCurrentPullingInterval) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700201 VLOG("Updating pulling interval %ld", intervalMs);
Chenjie Yu85ed8382017-12-14 16:48:54 -0800202 mCurrentPullingInterval = roundedIntervalMs;
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700203 updateAlarmLocked();
Chenjie Yub3dda412017-10-24 13:41:59 -0700204 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700205 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700206}
207
Chenjie Yu6736c892017-11-09 10:50:09 -0800208void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700209 AutoMutex _l(mLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700210 if (mReceivers.find(tagId) == mReceivers.end()) {
211 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700212 return;
213 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700214 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700215 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800216 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700217 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700218 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700219 return;
220 }
221 }
222}
223
Chenjie Yu6736c892017-11-09 10:50:09 -0800224void StatsPullerManagerImpl::OnAlarmFired() {
Chenjie Yuaa5b2012018-03-21 13:53:15 -0700225 AutoMutex _l(mLock);
Chenjie Yub3dda412017-10-24 13:41:59 -0700226
Yangster-mac330af582018-02-08 15:24:38 -0800227 uint64_t currentTimeMs = getElapsedRealtimeMillis();
Chenjie Yub3dda412017-10-24 13:41:59 -0700228
229 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
230 vector<pair<int, vector<ReceiverInfo*>>>();
231 for (auto& pair : mReceivers) {
232 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700233 if (pair.second.size() != 0) {
234 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700235 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
236 receivers.push_back(&receiverInfo);
237 }
238 }
239 if (receivers.size() > 0) {
240 needToPull.push_back(make_pair(pair.first, receivers));
241 }
242 }
243 }
244
245 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700246 vector<shared_ptr<LogEvent>> data;
247 if (Pull(pullInfo.first, &data)) {
248 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800249 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
250 if (receiverPtr != nullptr) {
251 receiverPtr->onDataPulled(data);
252 receiverInfo->timeInfo.second = currentTimeMs;
253 } else {
254 VLOG("receiver already gone.");
255 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700256 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700257 }
258 }
259}
260
Chenjie Yufa22d652018-02-05 14:37:48 -0800261int StatsPullerManagerImpl::ForceClearPullerCache() {
262 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800263 for (const auto& pulledAtom : kAllPullAtomInfo) {
264 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800265 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800266 return totalCleared;
267}
268
269int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(long timestampSec) {
270 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800271 for (const auto& pulledAtom : kAllPullAtomInfo) {
272 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampSec);
Chenjie Yufa22d652018-02-05 14:37:48 -0800273 }
274 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800275}
276
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700277} // namespace statsd
278} // namespace os
279} // namespace android