blob: 323fc0e94ab2d2da6ffc97b9bf3c30ca179686d5 [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
28StateTracker::StateTracker(const int atomId,
29 const util::StateAtomFieldOptions& stateAtomInfo)
30 : mAtomId(atomId),
31 mStateField(getSimpleMatcher(atomId, stateAtomInfo.exclusiveField)) {
32 // create matcher for each primary field
tsaichristined21aacf2019-10-07 14:47:38 -070033 // TODO(tsaichristine): b/142108433 handle when primary field is first uid in chain
tsaichristine10978642019-09-10 14:12:49 -070034 for (const auto& primary : stateAtomInfo.primaryFields) {
35 Matcher matcher = getSimpleMatcher(atomId, primary);
36 mPrimaryFields.push_back(matcher);
37 }
38
tsaichristined21aacf2019-10-07 14:47:38 -070039 // TODO(tsaichristine): b/142108433 set default state, reset state, and nesting
tsaichristine10978642019-09-10 14:12:49 -070040}
41
42void StateTracker::onLogEvent(const LogEvent& event) {
43 // parse event for primary field values i.e. primary key
44 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.");
49 handleReset();
50 return;
51 }
52 } else {
53 // atom has no primary fields
54 primaryKey = DEFAULT_DIMENSION_KEY;
55 }
56
57 // parse event for state value
58 Value state;
59 int32_t stateValue;
60 if (!filterValues(mStateField, event.getValues(), &state) || state.getType() != INT) {
61 ALOGE("StateTracker error extracting state from log event. Type: %d", state.getType());
62 handlePartialReset(primaryKey);
63 return;
64 }
65 stateValue = state.int_value;
66
67 if (stateValue == mResetState) {
68 VLOG("StateTracker Reset state: %s", state.toString().c_str());
69 handleReset();
70 }
71
72 // track and update state
73 int32_t oldState = 0;
74 int32_t newState = 0;
75 updateState(primaryKey, stateValue, &oldState, &newState);
76
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
tsaichristined21aacf2019-10-07 14:47:38 -070099int StateTracker::getStateValue(const HashableDimensionKey& queryKey) const {
tsaichristine10978642019-09-10 14:12:49 -0700100 if (queryKey.getValues().size() == mPrimaryFields.size()) {
101 auto it = mStateMap.find(queryKey);
102 if (it != mStateMap.end()) {
103 return it->second.state;
104 }
105 } else if (queryKey.getValues().size() > mPrimaryFields.size()) {
106 ALOGE("StateTracker query key size > primary key size is illegal");
107 } else {
108 ALOGE("StateTracker query key size < primary key size is not supported");
109 }
110 return mDefaultState;
111}
112
113void StateTracker::handleReset() {
114 VLOG("StateTracker handle reset");
115 for (const auto pair : mStateMap) {
116 for (auto l : mListeners) {
117 auto sl = l.promote();
118 if (sl != nullptr) {
119 sl->onStateChanged(mAtomId, pair.first, pair.second.state, mDefaultState);
120 }
121 }
122 }
123 mStateMap.clear();
124}
125
126void StateTracker::handlePartialReset(const HashableDimensionKey& primaryKey) {
127 VLOG("StateTracker handle partial reset");
128 if (mStateMap.find(primaryKey) != mStateMap.end()) {
129 mStateMap.erase(primaryKey);
130 }
131}
132
133void StateTracker::updateState(const HashableDimensionKey& primaryKey, const int32_t eventState,
134 int32_t* oldState, int32_t* newState) {
135 // get old state (either current state in map or default state)
136 auto it = mStateMap.find(primaryKey);
137 if (it != mStateMap.end()) {
138 *oldState = it->second.state;
139 } else {
140 *oldState = mDefaultState;
141 }
142
143 // update state map
144 if (eventState == mDefaultState) {
145 // remove (key, state) pair if state returns to default state
146 VLOG("\t StateTracker changed to default state")
147 mStateMap.erase(primaryKey);
148 } else {
149 mStateMap[primaryKey].state = eventState;
150 mStateMap[primaryKey].count = 1;
151 }
152 *newState = eventState;
153
154 // TODO(tsaichristine): support atoms with nested counting
155}
156
157} // namespace statsd
158} // namespace os
159} // namespace android