blob: 003b5c41a5070d4c08612431db05afe2b411c1b3 [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 Yu1a317ba2017-10-05 16:05:32 -070017#define DEBUG true
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 Yu5305e1d2017-10-31 13:49:36 -070024#include "ResourcePowerManagerPuller.h"
25#include "StatsCompanionServicePuller.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070026#include "StatsPullerManager.h"
27#include "StatsService.h"
28#include "logd/LogEvent.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070029#include "statslog.h"
David Chen1481fe12017-10-16 13:16:34 -070030
31#include <iostream>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070032
Chenjie Yu5305e1d2017-10-31 13:49:36 -070033using std::map;
Chenjie Yub3dda412017-10-24 13:41:59 -070034using std::string;
35using std::vector;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070036using std::make_shared;
37using std::shared_ptr;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070038
39namespace android {
40namespace os {
41namespace statsd {
42
Chenjie Yub3dda412017-10-24 13:41:59 -070043StatsPullerManager::StatsPullerManager()
44 : mCurrentPullingInterval(LONG_MAX), mPullStartTimeMs(get_pull_start_time_ms()) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -070045 shared_ptr<StatsPuller> statsCompanionServicePuller = make_shared<StatsCompanionServicePuller>();
46 shared_ptr <StatsPuller>
47 resourcePowerManagerPuller = make_shared<ResourcePowerManagerPuller>();
48
49 mPullers.insert({android::util::KERNEL_WAKELOCK_PULLED,
50 statsCompanionServicePuller});
51 mPullers.insert({android::util::WIFI_BYTES_TRANSFERRED,
52 statsCompanionServicePuller});
53 mPullers.insert({android::util::MOBILE_BYTES_TRANSFERRED,
54 statsCompanionServicePuller});
55 mPullers.insert({android::util::WIFI_BYTES_TRANSFERRED_BY_FG_BG,
56 statsCompanionServicePuller});
57 mPullers.insert({android::util::MOBILE_BYTES_TRANSFERRED_BY_FG_BG,
58 statsCompanionServicePuller});
59 mPullers.insert({android::util::POWER_STATE_PLATFORM_SLEEP_STATE_PULLED,
60 resourcePowerManagerPuller});
61 mPullers.insert({android::util::POWER_STATE_VOTER_PULLED,
62 resourcePowerManagerPuller});
63 mPullers.insert({android::util::POWER_STATE_SUBSYSTEM_SLEEP_STATE_PULLED,
64 resourcePowerManagerPuller});
65
66 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -070067}
68
Chenjie Yu5305e1d2017-10-31 13:49:36 -070069 bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
70 if (DEBUG) ALOGD("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070071
Chenjie Yu5305e1d2017-10-31 13:49:36 -070072 if (mPullers.find(tagId) != mPullers.end()) {
73 return mPullers.find(tagId)->second->Pull(tagId, data);
74 } else {
75 ALOGD("Unknown tagId %d", tagId);
76 return false; // Return early since we don't know what to pull.
77 }
David Chen1481fe12017-10-16 13:16:34 -070078 }
Chenjie Yub3dda412017-10-24 13:41:59 -070079
80StatsPullerManager& StatsPullerManager::GetInstance() {
81 static StatsPullerManager instance;
82 return instance;
83}
84
Chenjie Yu5305e1d2017-10-31 13:49:36 -070085bool StatsPullerManager::PullerForMatcherExists(int tagId) {
86 return mPullers.find(tagId) != mPullers.end();
Chenjie Yub3dda412017-10-24 13:41:59 -070087}
88
89long StatsPullerManager::get_pull_start_time_ms() {
90 // TODO: limit and align pull intervals to 10min boundaries if this turns out to be a problem
91 return time(nullptr) * 1000;
92}
93
Chenjie Yu5305e1d2017-10-31 13:49:36 -070094void StatsPullerManager::RegisterReceiver(int tagId, sp<PullDataReceiver> receiver, long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -070095 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070096 vector<ReceiverInfo>& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -070097 for (auto it = receivers.begin(); it != receivers.end(); it++) {
98 if (it->receiver.get() == receiver.get()) {
99 VLOG("Receiver already registered of %d", (int)receivers.size());
100 return;
101 }
102 }
103 ReceiverInfo receiverInfo;
104 receiverInfo.receiver = receiver;
105 receiverInfo.timeInfo.first = intervalMs;
106 receivers.push_back(receiverInfo);
107
108 // There is only one alarm for all pulled events. So only set it to the smallest denom.
109 if (intervalMs < mCurrentPullingInterval) {
110 VLOG("Updating pulling interval %ld", intervalMs);
111 mCurrentPullingInterval = intervalMs;
112 if (mStatsCompanionService != nullptr) {
113 mStatsCompanionService->setPullingAlarms(mPullStartTimeMs, mCurrentPullingInterval);
114 } else {
115 VLOG("Failed to update pulling interval");
116 }
117 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700118 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700119}
120
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700121void StatsPullerManager::UnRegisterReceiver(int tagId, sp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700122 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700123 if (mReceivers.find(tagId) == mReceivers.end()) {
124 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700125 return;
126 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700127 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700128 for (auto it = receivers.begin(); it != receivers.end(); it++) {
129 if (receiver.get() == it->receiver.get()) {
130 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700131 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700132 return;
133 }
134 }
135}
136
137void StatsPullerManager::OnAlarmFired() {
138 AutoMutex _l(mReceiversLock);
139
140 uint64_t currentTimeMs = time(nullptr) * 1000;
141
142 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
143 vector<pair<int, vector<ReceiverInfo*>>>();
144 for (auto& pair : mReceivers) {
145 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
146 if (pair.second.size() != 0){
147 for(auto& receiverInfo : pair.second) {
148 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
149 receivers.push_back(&receiverInfo);
150 }
151 }
152 if (receivers.size() > 0) {
153 needToPull.push_back(make_pair(pair.first, receivers));
154 }
155 }
156 }
157
158 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700159 vector<shared_ptr<LogEvent>> data;
160 if (Pull(pullInfo.first, &data)) {
161 for (const auto& receiverInfo : pullInfo.second) {
162 receiverInfo->receiver->onDataPulled(data);
163 receiverInfo->timeInfo.second = currentTimeMs;
164 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700165 }
166 }
167}
168
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700169} // namespace statsd
170} // namespace os
171} // namespace android