blob: 880dfd1cd6c776d68c2cf1c0e36b5bd306219525 [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"
Chenjie Yu80f91122018-01-31 20:24:50 -080026#include "KernelUidCpuActiveTimeReader.h"
27#include "KernelUidCpuClusterTimeReader.h"
Tej Singh40298312018-02-16 00:15:09 -080028#include "ResourceHealthManagerPuller.h"
29#include "ResourceThermalManagerPuller.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,
Rajeev Kumar22d92b72018-02-07 18:38:36 -0800118 {{}, {}, 1, new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
119 // process_memory_state
120 {android::util::PROCESS_MEMORY_STATE,
Tej Singh40298312018-02-16 00:15:09 -0800121 {{4,5,6,7,8}, {2,3}, 0, new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
122 // temperature
123 {android::util::TEMPERATURE, {{}, {}, 1, new ResourceThermalManagerPuller()}}};
Chenjie Yu80f91122018-01-31 20:24:50 -0800124
Chenjie Yu6736c892017-11-09 10:50:09 -0800125StatsPullerManagerImpl::StatsPullerManagerImpl()
Chenjie Yu85ed8382017-12-14 16:48:54 -0800126 : mCurrentPullingInterval(LONG_MAX) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700127 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700128}
129
Chenjie Yu6736c892017-11-09 10:50:09 -0800130bool StatsPullerManagerImpl::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
Tej Singh484524a2018-02-01 15:10:05 -0800131 VLOG("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700132
Chenjie Yu80f91122018-01-31 20:24:50 -0800133 if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
Tej Singh484524a2018-02-01 15:10:05 -0800134 bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
135 VLOG("pulled %d items", (int)data->size());
136 return ret;
Yao Chen93fe3a32017-11-02 13:52:59 -0700137 } else {
Tej Singh484524a2018-02-01 15:10:05 -0800138 VLOG("Unknown tagId %d", tagId);
Yao Chen93fe3a32017-11-02 13:52:59 -0700139 return false; // Return early since we don't know what to pull.
140 }
141}
Chenjie Yub3dda412017-10-24 13:41:59 -0700142
Chenjie Yu6736c892017-11-09 10:50:09 -0800143StatsPullerManagerImpl& StatsPullerManagerImpl::GetInstance() {
144 static StatsPullerManagerImpl instance;
Chenjie Yub3dda412017-10-24 13:41:59 -0700145 return instance;
146}
147
Yangster7c334a12017-11-22 14:24:24 -0800148bool StatsPullerManagerImpl::PullerForMatcherExists(int tagId) const {
Chenjie Yufeba3092018-02-08 14:33:37 -0800149 return kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
Chenjie Yub3dda412017-10-24 13:41:59 -0700150}
151
Chenjie Yu6736c892017-11-09 10:50:09 -0800152void StatsPullerManagerImpl::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
153 long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700154 AutoMutex _l(mReceiversLock);
Chenjie Yu6736c892017-11-09 10:50:09 -0800155 auto& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -0700156 for (auto it = receivers.begin(); it != receivers.end(); it++) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800157 if (it->receiver == receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700158 VLOG("Receiver already registered of %d", (int)receivers.size());
159 return;
160 }
161 }
162 ReceiverInfo receiverInfo;
163 receiverInfo.receiver = receiver;
164 receiverInfo.timeInfo.first = intervalMs;
165 receivers.push_back(receiverInfo);
166
Chenjie Yu85ed8382017-12-14 16:48:54 -0800167 // Round it to the nearest minutes. This is the limit of alarm manager.
168 // In practice, we should limit it higher.
169 long roundedIntervalMs = intervalMs/1000/60 * 1000 * 60;
Chenjie Yub3dda412017-10-24 13:41:59 -0700170 // There is only one alarm for all pulled events. So only set it to the smallest denom.
Chenjie Yu85ed8382017-12-14 16:48:54 -0800171 if (roundedIntervalMs < mCurrentPullingInterval) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700172 VLOG("Updating pulling interval %ld", intervalMs);
Chenjie Yu85ed8382017-12-14 16:48:54 -0800173 mCurrentPullingInterval = roundedIntervalMs;
Yangster-mac330af582018-02-08 15:24:38 -0800174 long currentTimeMs = getElapsedRealtimeMillis();
175 long nextAlarmTimeMs = currentTimeMs + mCurrentPullingInterval -
176 (currentTimeMs - mTimeBaseSec * 1000) % mCurrentPullingInterval;
Chenjie Yub3dda412017-10-24 13:41:59 -0700177 if (mStatsCompanionService != nullptr) {
Chenjie Yu85ed8382017-12-14 16:48:54 -0800178 mStatsCompanionService->setPullingAlarms(nextAlarmTimeMs, mCurrentPullingInterval);
Chenjie Yub3dda412017-10-24 13:41:59 -0700179 } else {
180 VLOG("Failed to update pulling interval");
181 }
182 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700183 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700184}
185
Chenjie Yu6736c892017-11-09 10:50:09 -0800186void StatsPullerManagerImpl::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700187 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700188 if (mReceivers.find(tagId) == mReceivers.end()) {
189 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700190 return;
191 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700192 auto& receivers = mReceivers.find(tagId)->second;
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 (receiver == it->receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700195 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700196 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700197 return;
198 }
199 }
200}
201
Chenjie Yu6736c892017-11-09 10:50:09 -0800202void StatsPullerManagerImpl::OnAlarmFired() {
Chenjie Yub3dda412017-10-24 13:41:59 -0700203 AutoMutex _l(mReceiversLock);
204
Yangster-mac330af582018-02-08 15:24:38 -0800205 uint64_t currentTimeMs = getElapsedRealtimeMillis();
Chenjie Yub3dda412017-10-24 13:41:59 -0700206
207 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
208 vector<pair<int, vector<ReceiverInfo*>>>();
209 for (auto& pair : mReceivers) {
210 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700211 if (pair.second.size() != 0) {
212 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700213 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
214 receivers.push_back(&receiverInfo);
215 }
216 }
217 if (receivers.size() > 0) {
218 needToPull.push_back(make_pair(pair.first, receivers));
219 }
220 }
221 }
222
223 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700224 vector<shared_ptr<LogEvent>> data;
225 if (Pull(pullInfo.first, &data)) {
226 for (const auto& receiverInfo : pullInfo.second) {
Chenjie Yu6736c892017-11-09 10:50:09 -0800227 sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
228 if (receiverPtr != nullptr) {
229 receiverPtr->onDataPulled(data);
230 receiverInfo->timeInfo.second = currentTimeMs;
231 } else {
232 VLOG("receiver already gone.");
233 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700234 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700235 }
236 }
237}
238
Chenjie Yufa22d652018-02-05 14:37:48 -0800239int StatsPullerManagerImpl::ForceClearPullerCache() {
240 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800241 for (const auto& pulledAtom : kAllPullAtomInfo) {
242 totalCleared += pulledAtom.second.puller->ForceClearCache();
Chenjie Yue72252b2018-02-01 13:19:35 -0800243 }
Chenjie Yufa22d652018-02-05 14:37:48 -0800244 return totalCleared;
245}
246
247int StatsPullerManagerImpl::ClearPullerCacheIfNecessary(long timestampSec) {
248 int totalCleared = 0;
Chenjie Yu80f91122018-01-31 20:24:50 -0800249 for (const auto& pulledAtom : kAllPullAtomInfo) {
250 totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampSec);
Chenjie Yufa22d652018-02-05 14:37:48 -0800251 }
252 return totalCleared;
Chenjie Yue72252b2018-02-01 13:19:35 -0800253}
254
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700255} // namespace statsd
256} // namespace os
257} // namespace android