blob: e6f61226018e7cac3651e98ee4b61f36114d2e8d [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) {
41 // parse event for primary field values i.e. primary key
42 HashableDimensionKey primaryKey;
43 if (mPrimaryFields.size() > 0) {
44 if (!filterValues(mPrimaryFields, event.getValues(), &primaryKey) ||
45 primaryKey.getValues().size() != mPrimaryFields.size()) {
46 ALOGE("StateTracker error extracting primary key from log event.");
47 handleReset();
48 return;
49 }
50 } else {
51 // atom has no primary fields
52 primaryKey = DEFAULT_DIMENSION_KEY;
53 }
54
55 // parse event for state value
tsaichristine69000e62019-10-18 17:34:52 -070056 FieldValue stateValue;
57 int32_t state;
58 if (!filterValues(mStateField, event.getValues(), &stateValue) ||
59 stateValue.mValue.getType() != INT) {
60 ALOGE("StateTracker error extracting state from log event. Type: %d",
61 stateValue.mValue.getType());
tsaichristine10978642019-09-10 14:12:49 -070062 handlePartialReset(primaryKey);
63 return;
64 }
tsaichristine69000e62019-10-18 17:34:52 -070065 state = stateValue.mValue.int_value;
tsaichristine10978642019-09-10 14:12:49 -070066
tsaichristine69000e62019-10-18 17:34:52 -070067 if (state == mResetState) {
68 VLOG("StateTracker Reset state: %s", stateValue.mValue.toString().c_str());
tsaichristine10978642019-09-10 14:12:49 -070069 handleReset();
70 }
71
72 // track and update state
73 int32_t oldState = 0;
74 int32_t newState = 0;
tsaichristine69000e62019-10-18 17:34:52 -070075 updateState(primaryKey, state, &oldState, &newState);
tsaichristine10978642019-09-10 14:12:49 -070076
77 // notify all listeners if state has changed
78 if (oldState != newState) {
79 VLOG("StateTracker updated state");
80 for (auto listener : mListeners) {
81 auto sListener = listener.promote(); // safe access to wp<>
82 if (sListener != nullptr) {
83 sListener->onStateChanged(mAtomId, primaryKey, oldState, newState);
84 }
85 }
86 } else {
87 VLOG("StateTracker NO updated state");
88 }
89}
90
91void StateTracker::registerListener(wp<StateListener> listener) {
92 mListeners.insert(listener);
93}
94
95void StateTracker::unregisterListener(wp<StateListener> listener) {
96 mListeners.erase(listener);
97}
98
tsaichristine69000e62019-10-18 17:34:52 -070099bool StateTracker::getStateValue(const HashableDimensionKey& queryKey, FieldValue* output) const {
100 output->mField = mStateField.mMatcher;
101
102 // Check that the query key has the correct number of primary fields.
tsaichristine10978642019-09-10 14:12:49 -0700103 if (queryKey.getValues().size() == mPrimaryFields.size()) {
104 auto it = mStateMap.find(queryKey);
105 if (it != mStateMap.end()) {
tsaichristine69000e62019-10-18 17:34:52 -0700106 output->mValue = it->second.state;
107 return true;
tsaichristine10978642019-09-10 14:12:49 -0700108 }
109 } else if (queryKey.getValues().size() > mPrimaryFields.size()) {
110 ALOGE("StateTracker query key size > primary key size is illegal");
111 } else {
112 ALOGE("StateTracker query key size < primary key size is not supported");
113 }
tsaichristine69000e62019-10-18 17:34:52 -0700114
115 // Set the state value to unknown if:
116 // - query key size is incorrect
117 // - query key is not found in state map
118 output->mValue = StateTracker::kStateUnknown;
119 return false;
tsaichristine10978642019-09-10 14:12:49 -0700120}
121
122void StateTracker::handleReset() {
123 VLOG("StateTracker handle reset");
124 for (const auto pair : mStateMap) {
125 for (auto l : mListeners) {
126 auto sl = l.promote();
127 if (sl != nullptr) {
128 sl->onStateChanged(mAtomId, pair.first, pair.second.state, mDefaultState);
129 }
130 }
131 }
132 mStateMap.clear();
133}
134
135void StateTracker::handlePartialReset(const HashableDimensionKey& primaryKey) {
136 VLOG("StateTracker handle partial reset");
137 if (mStateMap.find(primaryKey) != mStateMap.end()) {
138 mStateMap.erase(primaryKey);
139 }
140}
141
142void StateTracker::updateState(const HashableDimensionKey& primaryKey, const int32_t eventState,
143 int32_t* oldState, int32_t* newState) {
144 // get old state (either current state in map or default state)
145 auto it = mStateMap.find(primaryKey);
146 if (it != mStateMap.end()) {
147 *oldState = it->second.state;
148 } else {
149 *oldState = mDefaultState;
150 }
151
152 // update state map
153 if (eventState == mDefaultState) {
154 // remove (key, state) pair if state returns to default state
155 VLOG("\t StateTracker changed to default state")
156 mStateMap.erase(primaryKey);
157 } else {
158 mStateMap[primaryKey].state = eventState;
159 mStateMap[primaryKey].count = 1;
160 }
161 *newState = eventState;
162
163 // TODO(tsaichristine): support atoms with nested counting
164}
165
166} // namespace statsd
167} // namespace os
168} // namespace android