blob: 4c676a70363fec9235f29d84f16cc81c93b9be46 [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 = {
Chenjie Yufeba3092018-02-08 14:33:37 -080051 // 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(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
63 // mobile_bytes_transfer
64 {android::util::MOBILE_BYTES_TRANSFER,
65 {{2, 3, 4, 5},
66 {},
67 1,
68 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
69 // mobile_bytes_transfer_by_fg_bg
70 {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
71 {{3, 4, 5, 6},
72 {2},
73 1,
74 new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
75 // bluetooth_bytes_transfer
76 {android::util::BLUETOOTH_BYTES_TRANSFER,
77 {{2, 3}, {}, 1, new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
78 // kernel_wakelock
79 {android::util::KERNEL_WAKELOCK,
80 {{}, {}, 1, new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
81 // subsystem_sleep_state
82 {android::util::SUBSYSTEM_SLEEP_STATE, {{}, {}, 1, new SubsystemSleepStatePuller()}},
83 // cpu_time_per_freq
84 {android::util::CPU_TIME_PER_FREQ,
85 {{3}, {2}, 1, new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
86 // cpu_time_per_uid
87 {android::util::CPU_TIME_PER_UID, {{2, 3}, {}, 1, new CpuTimePerUidPuller()}},
88 // cpu_time_per_uid_freq
89 {android::util::CPU_TIME_PER_UID_FREQ, {{3}, {2}, 1, new CpuTimePerUidFreqPuller()}},
90 // wifi_activity_energy_info
91 {android::util::WIFI_ACTIVITY_ENERGY_INFO,
92 {{}, {}, 1, new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_ENERGY_INFO)}},
93 // modem_activity_info
94 {android::util::MODEM_ACTIVITY_INFO,
95 {{}, {}, 1, new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
96 // bluetooth_activity_info
97 {android::util::BLUETOOTH_ACTIVITY_INFO,
98 {{}, {}, 1, new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
99 // system_elapsed_realtime
100 {android::util::SYSTEM_ELAPSED_REALTIME,
101 {{}, {}, 1, new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME)}},
102 // system_uptime
103 {android::util::SYSTEM_UPTIME,
104 {{}, {}, 1, new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
105 // cpu_active_time
106 {android::util::CPU_ACTIVE_TIME, {{3}, {2}, 1, new KernelUidCpuActiveTimeReader()}},
107 // cpu_cluster_time
108 {android::util::CPU_CLUSTER_TIME, {{3}, {2}, 1, new KernelUidCpuClusterTimeReader()}},
109 // disk_space
110 {android::util::DISK_SPACE,
111 {{}, {}, 1, new StatsCompanionServicePuller(android::util::DISK_SPACE)}},
112 // remaining_battery_capacity
113 {android::util::REMAINING_BATTERY_CAPACITY,
114 {{}, {}, 1, new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
115 // full_battery_capacity
116 {android::util::FULL_BATTERY_CAPACITY,
117 {{}, {}, 1, new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}}};
Chenjie Yu80f91122018-01-31 20:24:50 -0800118
Chenjie Yu6736c892017-11-09 10:50:09 -0800119StatsPullerManagerImpl::StatsPullerManagerImpl()
Chenjie Yu85ed8382017-12-14 16:48:54 -0800120 : mCurrentPullingInterval(LONG_MAX) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700121 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700122}
123
Chenjie Yu6736c892017-11-09 10:50:09 -0800124bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800125 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700126
Chenjie Yu80f91122018-01-31 20:24:50 -0800127 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Tej Singh484524a2018-02-01 15:10:05 -0800128 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
129 VLOG("pulled %d items", (int)data->size());
130 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700131 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800132 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700133 return false; // Return early since we don't know what to pull.
134 }
135}
Chenjie Yub3dda412017-10-24 13:41:59 -0700136
Chenjie Yu6736c892017-11-09 10:50:09 -0800137StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() {
138 static StatsPullerManagerImpl instance;
Chenjie Yub3dda412017-10-24 13:41:59 -0700139 return instance;
140}
141
Yangster7c334a12017-11-22 14:24:24 -0800142bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800143 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700144}
145
Chenjie Yu6736c892017-11-09 10:50:09 -0800146void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
147 long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700148 AutoMutex _l(mReceiversLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800149 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700150 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800151 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700152 VLOG("Receiver already registered of %d", (int)receivers.size());
153 return;
154 }
155 }
156 ReceiverInfo receiverInfo;
157 receiverInfo.receiver = receiver;
158 receiverInfo.timeInfo.first = intervalMs;
159 receivers.push_back(receiverInfo);
160
Chenjie Yu85ed8382017-12-14 16:48:54 -0800161 // Round it to the nearest minutes. This is the limit of alarm manager.
162 // In practice, we should limit it higher.
163 long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60;
Chenjie Yub3dda412017-10-24 13:41:59 -0700164 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu85ed8382017-12-14 16:48:54 -0800165 if (roundedIntervalMs < mCurrentPullingInterval) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700166 VLOG("Updating pulling interval %ld", intervalMs);
Chenjie Yu85ed8382017-12-14 16:48:54 -0800167 mCurrentPullingInterval = roundedIntervalMs;
168 long currentTimeMs = time(nullptr) * 1000;
169 long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval - (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval;
Chenjie Yub3dda412017-10-24 13:41:59 -0700170 if (mStatsCompanionService != nullptr) {
Chenjie Yu85ed8382017-12-14 16:48:54 -0800171 mStatsCompanionService->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval);
Chenjie Yub3dda412017-10-24 13:41:59 -0700172 } else {
173 VLOG("Failed to update pulling interval");
174 }
175 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700176 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700177}
178
Chenjie Yu6736c892017-11-09 10:50:09 -0800179void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700180 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700181 if (mReceivers.find(tagId) == mReceivers.end()) {
182 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700183 return;
184 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700185 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700186 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800187 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700188 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700189 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700190 return;
191 }
192 }
193}
194
Chenjie Yu6736c892017-11-09 10:50:09 -0800195void StatsPullerManagerImpl::OnAlarmFired() {
Chenjie Yub3dda412017-10-24 13:41:59 -0700196 AutoMutex _l(mReceiversLock);
197
Chenjie Yu85ed8382017-12-14 16:48:54 -0800198 uint64_t currentTimeMs = time(nullptr) /60 * 60 * 1000;
Chenjie Yub3dda412017-10-24 13:41:59 -0700199
200 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
201 vector<pair<int, vector<ReceiverInfo*>>>();
202 for (auto& pair : mReceivers) {
203 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700204 if (pair.second.size() != 0) {
205 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700206 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
207 receivers.push_back(&receiverInfo);
208 }
209 }
210 if (receivers.size() > 0) {
211 needToPull.push_back(make_pair(pair.first, receivers));
212 }
213 }
214 }
215
216 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700217 vector<shared_ptr<LogEvent>> data;
218 if (Pull(pullInfo.first, &data)) {
219 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800220 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
221 if (receiverPtr != nullptr) {
222 receiverPtr->onDataPulled(data);
223 receiverInfo->timeInfo.second = currentTimeMs;
224 } else {
225 VLOG("receiver already gone.");
226 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700227 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700228 }
229 }
230}
231
Chenjie Yufa22d652018-02-05 14:37:48 -0800232int StatsPullerManagerImpl::ForceClearPullerCache() {
233 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800234 for (const auto& pulledAtom : kAllPullAtomInfo) {
235 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800236 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800237 return totalCleared;
238}
239
240int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(long timestampSec) {
241 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800242 for (const auto& pulledAtom : kAllPullAtomInfo) {
243 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampSec);
Chenjie Yufa22d652018-02-05 14:37:48 -0800244 }
245 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800246}
247
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700248} // namespace statsd
249} // namespace os
250} // namespace android