blob: 08c59cfed51895476730d5effe7cd9699c6e56c0 [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"
Yangster-mac330af582018-02-08 15:24:38 -080036#include "stats_log_util.h"
David Chen1481fe12017-10-16 13:16:34 -070037
38#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070039
Yao Chen93fe3a32017-11-02 13:52:59 -070040using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070041using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070042using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070043using std::string;
44using std::vector;
Chenjie Yu6736c892017-11-09 10:50:09 -080045using std::list;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070046
47namespace android {
48namespace os {
49namespace statsd {
50
Chenjie Yu80f91122018-01-31 20:24:50 -080051const std::map<int, PullAtomInfo> StatsPullerManagerImpl::kAllPullAtomInfo = {
Chenjie Yufeba3092018-02-08 14:33:37 -080052 // wifi_bytes_transfer
53 {android::util::WIFI_BYTES_TRANSFER,
54 {{2, 3, 4, 5},
55 {},
56 1,
57 new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
58 // wifi_bytes_transfer_by_fg_bg
59 {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
60 {{3, 4, 5, 6},
61 {2},
62 1,
63 new StatsCompanionServicePuller(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(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
76 // bluetooth_bytes_transfer
77 {android::util::BLUETOOTH_BYTES_TRANSFER,
78 {{2, 3}, {}, 1, new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
79 // kernel_wakelock
80 {android::util::KERNEL_WAKELOCK,
81 {{}, {}, 1, new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
82 // subsystem_sleep_state
83 {android::util::SUBSYSTEM_SLEEP_STATE, {{}, {}, 1, new SubsystemSleepStatePuller()}},
84 // cpu_time_per_freq
85 {android::util::CPU_TIME_PER_FREQ,
86 {{3}, {2}, 1, new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
87 // cpu_time_per_uid
88 {android::util::CPU_TIME_PER_UID, {{2, 3}, {}, 1, new CpuTimePerUidPuller()}},
89 // cpu_time_per_uid_freq
90 {android::util::CPU_TIME_PER_UID_FREQ, {{3}, {2}, 1, new CpuTimePerUidFreqPuller()}},
91 // 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)}},
106 // cpu_active_time
107 {android::util::CPU_ACTIVE_TIME, {{3}, {2}, 1, new KernelUidCpuActiveTimeReader()}},
108 // cpu_cluster_time
109 {android::util::CPU_CLUSTER_TIME, {{3}, {2}, 1, new KernelUidCpuClusterTimeReader()}},
110 // disk_space
111 {android::util::DISK_SPACE,
112 {{}, {}, 1, new StatsCompanionServicePuller(android::util::DISK_SPACE)}},
113 // remaining_battery_capacity
114 {android::util::REMAINING_BATTERY_CAPACITY,
115 {{}, {}, 1, new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
116 // full_battery_capacity
117 {android::util::FULL_BATTERY_CAPACITY,
118 {{}, {}, 1, new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}}};
Chenjie Yu80f91122018-01-31 20:24:50 -0800119
Chenjie Yu6736c892017-11-09 10:50:09 -0800120StatsPullerManagerImpl::StatsPullerManagerImpl()
Chenjie Yu85ed8382017-12-14 16:48:54 -0800121 : mCurrentPullingInterval(LONG_MAX) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700122 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700123}
124
Chenjie Yu6736c892017-11-09 10:50:09 -0800125bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800126 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700127
Chenjie Yu80f91122018-01-31 20:24:50 -0800128 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Tej Singh484524a2018-02-01 15:10:05 -0800129 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
130 VLOG("pulled %d items", (int)data->size());
131 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700132 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800133 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700134 return false; // Return early since we don't know what to pull.
135 }
136}
Chenjie Yub3dda412017-10-24 13:41:59 -0700137
Chenjie Yu6736c892017-11-09 10:50:09 -0800138StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() {
139 static StatsPullerManagerImpl instance;
Chenjie Yub3dda412017-10-24 13:41:59 -0700140 return instance;
141}
142
Yangster7c334a12017-11-22 14:24:24 -0800143bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800144 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700145}
146
Chenjie Yu6736c892017-11-09 10:50:09 -0800147void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
148 long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700149 AutoMutex _l(mReceiversLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800150 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700151 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800152 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700153 VLOG("Receiver already registered of %d", (int)receivers.size());
154 return;
155 }
156 }
157 ReceiverInfo receiverInfo;
158 receiverInfo.receiver = receiver;
159 receiverInfo.timeInfo.first = intervalMs;
160 receivers.push_back(receiverInfo);
161
Chenjie Yu85ed8382017-12-14 16:48:54 -0800162 // Round it to the nearest minutes. This is the limit of alarm manager.
163 // In practice, we should limit it higher.
164 long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60;
Chenjie Yub3dda412017-10-24 13:41:59 -0700165 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu85ed8382017-12-14 16:48:54 -0800166 if (roundedIntervalMs < mCurrentPullingInterval) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700167 VLOG("Updating pulling interval %ld", intervalMs);
Chenjie Yu85ed8382017-12-14 16:48:54 -0800168 mCurrentPullingInterval = roundedIntervalMs;
Yangster-mac330af582018-02-08 15:24:38 -0800169 long currentTimeMs = getElapsedRealtimeMillis();
170 long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval -
171 (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval;
Chenjie Yub3dda412017-10-24 13:41:59 -0700172 if (mStatsCompanionService != nullptr) {
Chenjie Yu85ed8382017-12-14 16:48:54 -0800173 mStatsCompanionService->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval);
Chenjie Yub3dda412017-10-24 13:41:59 -0700174 } else {
175 VLOG("Failed to update pulling interval");
176 }
177 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700178 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700179}
180
Chenjie Yu6736c892017-11-09 10:50:09 -0800181void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700182 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700183 if (mReceivers.find(tagId) == mReceivers.end()) {
184 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700185 return;
186 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700187 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700188 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800189 if (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700190 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700191 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700192 return;
193 }
194 }
195}
196
Chenjie Yu6736c892017-11-09 10:50:09 -0800197void StatsPullerManagerImpl::OnAlarmFired() {
Chenjie Yub3dda412017-10-24 13:41:59 -0700198 AutoMutex _l(mReceiversLock);
199
Yangster-mac330af582018-02-08 15:24:38 -0800200 uint64_t currentTimeMs = getElapsedRealtimeMillis();
Chenjie Yub3dda412017-10-24 13:41:59 -0700201
202 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
203 vector<pair<int, vector<ReceiverInfo*>>>();
204 for (auto& pair : mReceivers) {
205 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700206 if (pair.second.size() != 0) {
207 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700208 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
209 receivers.push_back(&receiverInfo);
210 }
211 }
212 if (receivers.size() > 0) {
213 needToPull.push_back(make_pair(pair.first, receivers));
214 }
215 }
216 }
217
218 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700219 vector<shared_ptr<LogEvent>> data;
220 if (Pull(pullInfo.first, &data)) {
221 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800222 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
223 if (receiverPtr != nullptr) {
224 receiverPtr->onDataPulled(data);
225 receiverInfo->timeInfo.second = currentTimeMs;
226 } else {
227 VLOG("receiver already gone.");
228 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700229 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700230 }
231 }
232}
233
Chenjie Yufa22d652018-02-05 14:37:48 -0800234int StatsPullerManagerImpl::ForceClearPullerCache() {
235 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800236 for (const auto& pulledAtom : kAllPullAtomInfo) {
237 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800238 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800239 return totalCleared;
240}
241
242int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(long timestampSec) {
243 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800244 for (const auto& pulledAtom : kAllPullAtomInfo) {
245 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampSec);
Chenjie Yufa22d652018-02-05 14:37:48 -0800246 }
247 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800248}
249
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700250} // namespace statsd
251} // namespace os
252} // namespace android