tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
| 27 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 28 | StateTracker::StateTracker(const int32_t atomId, const util::StateAtomFieldOptions& stateAtomInfo) |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 29 | : mAtomId(atomId), |
| 30 | mStateField(getSimpleMatcher(atomId, stateAtomInfo.exclusiveField)), |
| 31 | mNested(stateAtomInfo.nested) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 32 | // create matcher for each primary field |
tsaichristine | ed61564 | 2020-01-02 12:53:41 -0800 | [diff] [blame] | 33 | for (const auto& primaryField : stateAtomInfo.primaryFields) { |
| 34 | if (primaryField == util::FIRST_UID_IN_CHAIN) { |
| 35 | Matcher matcher = getFirstUidMatcher(atomId); |
| 36 | mPrimaryFields.push_back(matcher); |
| 37 | } else { |
| 38 | Matcher matcher = getSimpleMatcher(atomId, primaryField); |
| 39 | mPrimaryFields.push_back(matcher); |
| 40 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 41 | } |
| 42 | |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 43 | if (stateAtomInfo.defaultState != util::UNSET_VALUE) { |
| 44 | mDefaultState = stateAtomInfo.defaultState; |
| 45 | } |
| 46 | |
| 47 | if (stateAtomInfo.resetState != util::UNSET_VALUE) { |
| 48 | mResetState = stateAtomInfo.resetState; |
| 49 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void StateTracker::onLogEvent(const LogEvent& event) { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 53 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
| 54 | |
| 55 | // Parse event for primary field values i.e. primary key. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 56 | HashableDimensionKey primaryKey; |
| 57 | if (mPrimaryFields.size() > 0) { |
| 58 | if (!filterValues(mPrimaryFields, event.getValues(), &primaryKey) || |
| 59 | primaryKey.getValues().size() != mPrimaryFields.size()) { |
| 60 | ALOGE("StateTracker error extracting primary key from log event."); |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 61 | handleReset(eventTimeNs); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | } else { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 65 | // Use an empty HashableDimensionKey if atom has no primary fields. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 66 | primaryKey = DEFAULT_DIMENSION_KEY; |
| 67 | } |
| 68 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 69 | // Parse event for state value. |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 70 | FieldValue stateValue; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 71 | if (!filterValues(mStateField, event.getValues(), &stateValue) || |
| 72 | stateValue.mValue.getType() != INT) { |
| 73 | ALOGE("StateTracker error extracting state from log event. Type: %d", |
| 74 | stateValue.mValue.getType()); |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 75 | handlePartialReset(eventTimeNs, primaryKey); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 76 | return; |
| 77 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 78 | |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 79 | int32_t state = stateValue.mValue.int_value; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 80 | if (state == mResetState) { |
| 81 | VLOG("StateTracker Reset state: %s", stateValue.mValue.toString().c_str()); |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 82 | handleReset(eventTimeNs); |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 83 | return; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 84 | } |
| 85 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 86 | // Track and update state. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 87 | int32_t oldState = 0; |
| 88 | int32_t newState = 0; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 89 | updateState(primaryKey, state, &oldState, &newState); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 90 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 91 | // Notify all listeners if state has changed. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 92 | if (oldState != newState) { |
| 93 | VLOG("StateTracker updated state"); |
| 94 | for (auto listener : mListeners) { |
| 95 | auto sListener = listener.promote(); // safe access to wp<> |
| 96 | if (sListener != nullptr) { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 97 | sListener->onStateChanged(eventTimeNs, mAtomId, primaryKey, oldState, newState); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } else { |
| 101 | VLOG("StateTracker NO updated state"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void StateTracker::registerListener(wp<StateListener> listener) { |
| 106 | mListeners.insert(listener); |
| 107 | } |
| 108 | |
| 109 | void StateTracker::unregisterListener(wp<StateListener> listener) { |
| 110 | mListeners.erase(listener); |
| 111 | } |
| 112 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 113 | bool StateTracker::getStateValue(const HashableDimensionKey& queryKey, FieldValue* output) const { |
| 114 | output->mField = mStateField.mMatcher; |
| 115 | |
| 116 | // Check that the query key has the correct number of primary fields. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 117 | if (queryKey.getValues().size() == mPrimaryFields.size()) { |
| 118 | auto it = mStateMap.find(queryKey); |
| 119 | if (it != mStateMap.end()) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 120 | output->mValue = it->second.state; |
| 121 | return true; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 122 | } |
| 123 | } else if (queryKey.getValues().size() > mPrimaryFields.size()) { |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 124 | ALOGE("StateTracker query key size %zu > primary key size %zu is illegal", |
| 125 | queryKey.getValues().size(), mPrimaryFields.size()); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 126 | } else { |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 127 | ALOGE("StateTracker query key size %zu < primary key size %zu is not supported", |
| 128 | queryKey.getValues().size(), mPrimaryFields.size()); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 129 | } |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 130 | |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 131 | // Set the state value to default state if: |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 132 | // - query key size is incorrect |
| 133 | // - query key is not found in state map |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 134 | output->mValue = mDefaultState; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 135 | return false; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 136 | } |
| 137 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 138 | void StateTracker::handleReset(const int64_t eventTimeNs) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 139 | VLOG("StateTracker handle reset"); |
| 140 | for (const auto pair : mStateMap) { |
| 141 | for (auto l : mListeners) { |
| 142 | auto sl = l.promote(); |
| 143 | if (sl != nullptr) { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 144 | sl->onStateChanged(eventTimeNs, mAtomId, pair.first, pair.second.state, |
| 145 | mDefaultState); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | mStateMap.clear(); |
| 150 | } |
| 151 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame] | 152 | void StateTracker::handlePartialReset(const int64_t eventTimeNs, |
| 153 | const HashableDimensionKey& primaryKey) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 154 | VLOG("StateTracker handle partial reset"); |
| 155 | if (mStateMap.find(primaryKey) != mStateMap.end()) { |
tsaichristine | 50f5bad | 2019-12-06 02:56:56 -0800 | [diff] [blame] | 156 | for (auto l : mListeners) { |
| 157 | auto sl = l.promote(); |
| 158 | if (sl != nullptr) { |
| 159 | sl->onStateChanged(eventTimeNs, mAtomId, primaryKey, |
| 160 | mStateMap.find(primaryKey)->second.state, mDefaultState); |
| 161 | } |
| 162 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 163 | mStateMap.erase(primaryKey); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void StateTracker::updateState(const HashableDimensionKey& primaryKey, const int32_t eventState, |
| 168 | int32_t* oldState, int32_t* newState) { |
| 169 | // get old state (either current state in map or default state) |
| 170 | auto it = mStateMap.find(primaryKey); |
| 171 | if (it != mStateMap.end()) { |
| 172 | *oldState = it->second.state; |
| 173 | } else { |
| 174 | *oldState = mDefaultState; |
| 175 | } |
| 176 | |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 177 | // Update state map for non-nested counting case. |
| 178 | // Every state event triggers a state overwrite. |
| 179 | if (!mNested) { |
| 180 | if (eventState == mDefaultState) { |
| 181 | // remove (key, state) pair if state returns to default state |
| 182 | VLOG("\t StateTracker changed to default state") |
| 183 | mStateMap.erase(primaryKey); |
| 184 | } else { |
| 185 | mStateMap[primaryKey].state = eventState; |
| 186 | mStateMap[primaryKey].count = 1; |
| 187 | } |
| 188 | *newState = eventState; |
| 189 | return; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 190 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 191 | |
tsaichristine | 5adc7e0 | 2020-01-14 17:07:39 -0800 | [diff] [blame] | 192 | // Update state map for nested counting case. |
| 193 | // |
| 194 | // Nested counting is only allowed for binary state events such as ON/OFF or |
| 195 | // ACQUIRE/RELEASE. For example, WakelockStateChanged might have the state |
| 196 | // events: ON, ON, OFF. The state will still be ON until we see the same |
| 197 | // number of OFF events as ON events. |
| 198 | // |
| 199 | // In atoms.proto, a state atom with nested counting enabled |
| 200 | // must only have 2 states and one of the states must be the default state. |
| 201 | it = mStateMap.find(primaryKey); |
| 202 | if (it != mStateMap.end()) { |
| 203 | *newState = it->second.state; |
| 204 | if (eventState == it->second.state) { |
| 205 | it->second.count++; |
| 206 | } else if (eventState == mDefaultState) { |
| 207 | if ((--it->second.count) == 0) { |
| 208 | mStateMap.erase(primaryKey); |
| 209 | *newState = mDefaultState; |
| 210 | } |
| 211 | } else { |
| 212 | ALOGE("StateTracker Nest counting state has a third state instead of the binary state " |
| 213 | "limit."); |
| 214 | return; |
| 215 | } |
| 216 | } else { |
| 217 | if (eventState != mDefaultState) { |
| 218 | mStateMap[primaryKey].state = eventState; |
| 219 | mStateMap[primaryKey].count = 1; |
| 220 | } |
| 221 | *newState = eventState; |
| 222 | } |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | } // namespace statsd |
| 226 | } // namespace os |
| 227 | } // namespace android |