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) |
| 29 | : mAtomId(atomId), mStateField(getSimpleMatcher(atomId, stateAtomInfo.exclusiveField)) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 30 | // create matcher for each primary field |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 31 | // TODO(tsaichristine): b/142108433 handle when primary field is first uid in chain |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 32 | for (const auto& primary : stateAtomInfo.primaryFields) { |
| 33 | Matcher matcher = getSimpleMatcher(atomId, primary); |
| 34 | mPrimaryFields.push_back(matcher); |
| 35 | } |
| 36 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 37 | // TODO(tsaichristine): b/142108433 set default state, reset state, and nesting |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void 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 |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame^] | 56 | 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()); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 62 | handlePartialReset(primaryKey); |
| 63 | return; |
| 64 | } |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame^] | 65 | state = stateValue.mValue.int_value; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 66 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame^] | 67 | if (state == mResetState) { |
| 68 | VLOG("StateTracker Reset state: %s", stateValue.mValue.toString().c_str()); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 69 | handleReset(); |
| 70 | } |
| 71 | |
| 72 | // track and update state |
| 73 | int32_t oldState = 0; |
| 74 | int32_t newState = 0; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame^] | 75 | updateState(primaryKey, state, &oldState, &newState); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 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 | |
| 91 | void StateTracker::registerListener(wp<StateListener> listener) { |
| 92 | mListeners.insert(listener); |
| 93 | } |
| 94 | |
| 95 | void StateTracker::unregisterListener(wp<StateListener> listener) { |
| 96 | mListeners.erase(listener); |
| 97 | } |
| 98 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame^] | 99 | bool 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. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 103 | if (queryKey.getValues().size() == mPrimaryFields.size()) { |
| 104 | auto it = mStateMap.find(queryKey); |
| 105 | if (it != mStateMap.end()) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame^] | 106 | output->mValue = it->second.state; |
| 107 | return true; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 108 | } |
| 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 | } |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame^] | 114 | |
| 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; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void 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 | |
| 135 | void 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 | |
| 142 | void 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 |