blob: 43543cceeb1cf4f34b7cc06e82a2341dab1a91dd [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
Yao Chen93fe3a32017-11-02 13:52:59 -070033using std::make_shared;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070034using std::map;
Yao Chen93fe3a32017-11-02 13:52:59 -070035using std::shared_ptr;
Chenjie Yub3dda412017-10-24 13:41:59 -070036using std::string;
37using std::vector;
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()) {
Yao Chen93fe3a32017-11-02 13:52:59 -070045 shared_ptr<StatsPuller> statsCompanionServicePuller =
46 make_shared<StatsCompanionServicePuller>();
47 shared_ptr<StatsPuller> resourcePowerManagerPuller = make_shared<ResourcePowerManagerPuller>();
Chenjie Yu5305e1d2017-10-31 13:49:36 -070048
Yao Chen93fe3a32017-11-02 13:52:59 -070049 mPullers.insert({android::util::KERNEL_WAKELOCK_PULLED, statsCompanionServicePuller});
50 mPullers.insert({android::util::WIFI_BYTES_TRANSFERRED, statsCompanionServicePuller});
51 mPullers.insert({android::util::MOBILE_BYTES_TRANSFERRED, statsCompanionServicePuller});
52 mPullers.insert({android::util::WIFI_BYTES_TRANSFERRED_BY_FG_BG, statsCompanionServicePuller});
53 mPullers.insert(
54 {android::util::MOBILE_BYTES_TRANSFERRED_BY_FG_BG, statsCompanionServicePuller});
55 mPullers.insert(
56 {android::util::POWER_STATE_PLATFORM_SLEEP_STATE_PULLED, resourcePowerManagerPuller});
57 mPullers.insert({android::util::POWER_STATE_VOTER_PULLED, resourcePowerManagerPuller});
58 mPullers.insert(
59 {android::util::POWER_STATE_SUBSYSTEM_SLEEP_STATE_PULLED, resourcePowerManagerPuller});
Chenjie Yu5305e1d2017-10-31 13:49:36 -070060
61 mStatsCompanionService = StatsService::getStatsCompanionService();
Chenjie Yu1a317ba2017-10-05 16:05:32 -070062}
63
Yao Chen93fe3a32017-11-02 13:52:59 -070064bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
65 if (DEBUG) ALOGD("Initiating pulling %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -070066
Yao Chen93fe3a32017-11-02 13:52:59 -070067 if (mPullers.find(tagId) != mPullers.end()) {
68 return mPullers.find(tagId)->second->Pull(tagId, data);
69 } else {
70 ALOGD("Unknown tagId %d", tagId);
71 return false; // Return early since we don't know what to pull.
72 }
73}
Chenjie Yub3dda412017-10-24 13:41:59 -070074
75StatsPullerManager& StatsPullerManager::GetInstance() {
76 static StatsPullerManager instance;
77 return instance;
78}
79
Chenjie Yu5305e1d2017-10-31 13:49:36 -070080bool StatsPullerManager::PullerForMatcherExists(int tagId) {
81 return mPullers.find(tagId) != mPullers.end();
Chenjie Yub3dda412017-10-24 13:41:59 -070082}
83
84long StatsPullerManager::get_pull_start_time_ms() {
85 // TODO: limit and align pull intervals to 10min boundaries if this turns out to be a problem
86 return time(nullptr) * 1000;
87}
88
Yao Chen93fe3a32017-11-02 13:52:59 -070089void StatsPullerManager::RegisterReceiver(int tagId, sp<PullDataReceiver> receiver,
90 long intervalMs) {
Chenjie Yub3dda412017-10-24 13:41:59 -070091 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070092 vector<ReceiverInfo>& receivers = mReceivers[tagId];
Chenjie Yub3dda412017-10-24 13:41:59 -070093 for (auto it = receivers.begin(); it != receivers.end(); it++) {
94 if (it->receiver.get() == receiver.get()) {
95 VLOG("Receiver already registered of %d", (int)receivers.size());
96 return;
97 }
98 }
99 ReceiverInfo receiverInfo;
100 receiverInfo.receiver = receiver;
101 receiverInfo.timeInfo.first = intervalMs;
102 receivers.push_back(receiverInfo);
103
104 // There is only one alarm for all pulled events. So only set it to the smallest denom.
105 if (intervalMs < mCurrentPullingInterval) {
106 VLOG("Updating pulling interval %ld", intervalMs);
107 mCurrentPullingInterval = intervalMs;
108 if (mStatsCompanionService != nullptr) {
109 mStatsCompanionService->setPullingAlarms(mPullStartTimeMs, mCurrentPullingInterval);
110 } else {
111 VLOG("Failed to update pulling interval");
112 }
113 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700114 VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700115}
116
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700117void StatsPullerManager::UnRegisterReceiver(int tagId, sp<PullDataReceiver> receiver) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700118 AutoMutex _l(mReceiversLock);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700119 if (mReceivers.find(tagId) == mReceivers.end()) {
120 VLOG("Unknown pull code or no receivers: %d", tagId);
Chenjie Yub3dda412017-10-24 13:41:59 -0700121 return;
122 }
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700123 auto& receivers = mReceivers.find(tagId)->second;
Chenjie Yub3dda412017-10-24 13:41:59 -0700124 for (auto it = receivers.begin(); it != receivers.end(); it++) {
125 if (receiver.get() == it->receiver.get()) {
126 receivers.erase(it);
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700127 VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
Chenjie Yub3dda412017-10-24 13:41:59 -0700128 return;
129 }
130 }
131}
132
133void StatsPullerManager::OnAlarmFired() {
134 AutoMutex _l(mReceiversLock);
135
136 uint64_t currentTimeMs = time(nullptr) * 1000;
137
138 vector<pair<int, vector<ReceiverInfo*>>> needToPull =
139 vector<pair<int, vector<ReceiverInfo*>>>();
140 for (auto& pair : mReceivers) {
141 vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
Yao Chen93fe3a32017-11-02 13:52:59 -0700142 if (pair.second.size() != 0) {
143 for (auto& receiverInfo : pair.second) {
Chenjie Yub3dda412017-10-24 13:41:59 -0700144 if (receiverInfo.timeInfo.first + receiverInfo.timeInfo.second > currentTimeMs) {
145 receivers.push_back(&receiverInfo);
146 }
147 }
148 if (receivers.size() > 0) {
149 needToPull.push_back(make_pair(pair.first, receivers));
150 }
151 }
152 }
153
154 for (const auto& pullInfo : needToPull) {
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700155 vector<shared_ptr<LogEvent>> data;
156 if (Pull(pullInfo.first, &data)) {
157 for (const auto& receiverInfo : pullInfo.second) {
158 receiverInfo->receiver->onDataPulled(data);
159 receiverInfo->timeInfo.second = currentTimeMs;
160 }
Chenjie Yub3dda412017-10-24 13:41:59 -0700161 }
162 }
163}
164
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700165} // namespace statsd
166} // namespace os
167} // namespace android