blob: 90ce1e90142e3240f6e7950cc5f47e248254ab44 [file] [log] [blame]
tsaichristine10978642019-09-10 14:12:49 -07001/*
2 * Copyright (C) 2019, 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
17#define DEBUG true // STOPSHIP if true
18#include "Log.h"
19
20#include "stats_util.h"
21
22#include "StateTracker.h"
23
24namespace android {
25namespace os {
26namespace statsd {
27
tsaichristine69000e62019-10-18 17:34:52 -070028StateTracker::StateTracker(const int32_t atomId, const util::StateAtomFieldOptions& stateAtomInfo)
29 : mAtomId(atomId), mStateField(getSimpleMatcher(atomId, stateAtomInfo.exclusiveField)) {
tsaichristine10978642019-09-10 14:12:49 -070030 // create matcher for each primary field
tsaichristined21aacf2019-10-07 14:47:38 -070031 // TODO(tsaichristine): b/142108433 handle when primary field is first uid in chain
tsaichristine10978642019-09-10 14:12:49 -070032 for (const auto& primary : stateAtomInfo.primaryFields) {
33 Matcher matcher = getSimpleMatcher(atomId, primary);
34 mPrimaryFields.push_back(matcher);
35 }
36
tsaichristined21aacf2019-10-07 14:47:38 -070037 // TODO(tsaichristine): b/142108433 set default state, reset state, and nesting
tsaichristine10978642019-09-10 14:12:49 -070038}
39
40void StateTracker::onLogEvent(const LogEvent& event) {
tsaichristine8d73dc92019-12-06 02:11:02 -080041 int64_t eventTimeNs = event.GetElapsedTimestampNs();
42
43 // Parse event for primary field values i.e. primary key.
tsaichristine10978642019-09-10 14:12:49 -070044 HashableDimensionKey primaryKey;
45 if (mPrimaryFields.size() > 0) {
46 if (!filterValues(mPrimaryFields, event.getValues(), &primaryKey) ||
47 primaryKey.getValues().size() != mPrimaryFields.size()) {
48 ALOGE("StateTracker error extracting primary key from log event.");
tsaichristine8d73dc92019-12-06 02:11:02 -080049 handleReset(eventTimeNs);
tsaichristine10978642019-09-10 14:12:49 -070050 return;
51 }
52 } else {
tsaichristine8d73dc92019-12-06 02:11:02 -080053 // Use an empty HashableDimensionKey if atom has no primary fields.
tsaichristine10978642019-09-10 14:12:49 -070054 primaryKey = DEFAULT_DIMENSION_KEY;
55 }
56
tsaichristine8d73dc92019-12-06 02:11:02 -080057 // Parse event for state value.
tsaichristine69000e62019-10-18 17:34:52 -070058 FieldValue stateValue;
59 int32_t state;
60 if (!filterValues(mStateField, event.getValues(), &stateValue) ||
61 stateValue.mValue.getType() != INT) {
62 ALOGE("StateTracker error extracting state from log event. Type: %d",
63 stateValue.mValue.getType());
tsaichristine8d73dc92019-12-06 02:11:02 -080064 handlePartialReset(eventTimeNs, primaryKey);
tsaichristine10978642019-09-10 14:12:49 -070065 return;
66 }
tsaichristine69000e62019-10-18 17:34:52 -070067 state = stateValue.mValue.int_value;
tsaichristine10978642019-09-10 14:12:49 -070068
tsaichristine69000e62019-10-18 17:34:52 -070069 if (state == mResetState) {
70 VLOG("StateTracker Reset state: %s", stateValue.mValue.toString().c_str());
tsaichristine8d73dc92019-12-06 02:11:02 -080071 handleReset(eventTimeNs);
tsaichristine10978642019-09-10 14:12:49 -070072 }
73
tsaichristine8d73dc92019-12-06 02:11:02 -080074 // Track and update state.
tsaichristine10978642019-09-10 14:12:49 -070075 int32_t oldState = 0;
76 int32_t newState = 0;
tsaichristine69000e62019-10-18 17:34:52 -070077 updateState(primaryKey, state, &oldState, &newState);
tsaichristine10978642019-09-10 14:12:49 -070078
tsaichristine8d73dc92019-12-06 02:11:02 -080079 // Notify all listeners if state has changed.
tsaichristine10978642019-09-10 14:12:49 -070080 if (oldState != newState) {
81 VLOG("StateTracker updated state");
82 for (auto listener : mListeners) {
83 auto sListener = listener.promote(); // safe access to wp<>
84 if (sListener != nullptr) {
tsaichristine8d73dc92019-12-06 02:11:02 -080085 sListener->onStateChanged(eventTimeNs, mAtomId, primaryKey, oldState, newState);
tsaichristine10978642019-09-10 14:12:49 -070086 }
87 }
88 } else {
89 VLOG("StateTracker NO updated state");
90 }
91}
92
93void StateTracker::registerListener(wp<StateListener> listener) {
94 mListeners.insert(listener);
95}
96
97void StateTracker::unregisterListener(wp<StateListener> listener) {
98 mListeners.erase(listener);
99}
100
tsaichristine69000e62019-10-18 17:34:52 -0700101bool StateTracker::getStateValue(const HashableDimensionKey& queryKey, FieldValue* output) const {
102 output->mField = mStateField.mMatcher;
103
104 // Check that the query key has the correct number of primary fields.
tsaichristine10978642019-09-10 14:12:49 -0700105 if (queryKey.getValues().size() == mPrimaryFields.size()) {
106 auto it = mStateMap.find(queryKey);
107 if (it != mStateMap.end()) {
tsaichristine69000e62019-10-18 17:34:52 -0700108 output->mValue = it->second.state;
109 return true;
tsaichristine10978642019-09-10 14:12:49 -0700110 }
111 } else if (queryKey.getValues().size() > mPrimaryFields.size()) {
112 ALOGE("StateTracker query key size > primary key size is illegal");
113 } else {
114 ALOGE("StateTracker query key size < primary key size is not supported");
115 }
tsaichristine69000e62019-10-18 17:34:52 -0700116
117 // Set the state value to unknown if:
118 // - query key size is incorrect
119 // - query key is not found in state map
120 output->mValue = StateTracker::kStateUnknown;
121 return false;
tsaichristine10978642019-09-10 14:12:49 -0700122}
123
tsaichristine8d73dc92019-12-06 02:11:02 -0800124void StateTracker::handleReset(const int64_t eventTimeNs) {
tsaichristine10978642019-09-10 14:12:49 -0700125 VLOG("StateTracker handle reset");
126 for (const auto pair : mStateMap) {
127 for (auto l : mListeners) {
128 auto sl = l.promote();
129 if (sl != nullptr) {
tsaichristine8d73dc92019-12-06 02:11:02 -0800130 sl->onStateChanged(eventTimeNs, mAtomId, pair.first, pair.second.state,
131 mDefaultState);
tsaichristine10978642019-09-10 14:12:49 -0700132 }
133 }
134 }
135 mStateMap.clear();
136}
137
tsaichristine8d73dc92019-12-06 02:11:02 -0800138void StateTracker::handlePartialReset(const int64_t eventTimeNs,
139 const HashableDimensionKey& primaryKey) {
tsaichristine10978642019-09-10 14:12:49 -0700140 VLOG("StateTracker handle partial reset");
141 if (mStateMap.find(primaryKey) != mStateMap.end()) {
142 mStateMap.erase(primaryKey);
143 }
144}
145
146void StateTracker::updateState(const HashableDimensionKey& primaryKey, const int32_t eventState,
147 int32_t* oldState, int32_t* newState) {
148 // get old state (either current state in map or default state)
149 auto it = mStateMap.find(primaryKey);
150 if (it != mStateMap.end()) {
151 *oldState = it->second.state;
152 } else {
153 *oldState = mDefaultState;
154 }
155
156 // update state map
157 if (eventState == mDefaultState) {
158 // remove (key, state) pair if state returns to default state
159 VLOG("\t StateTracker changed to default state")
160 mStateMap.erase(primaryKey);
161 } else {
162 mStateMap[primaryKey].state = eventState;
163 mStateMap[primaryKey].count = 1;
164 }
165 *newState = eventState;
166
167 // TODO(tsaichristine): support atoms with nested counting
168}
169
170} // namespace statsd
171} // namespace os
172} // namespace android