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) { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 41 | int64_t eventTimeNs = event.GetElapsedTimestampNs(); |
| 42 | |
| 43 | // Parse event for primary field values i.e. primary key. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 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."); |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 49 | handleReset(eventTimeNs); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | } else { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 53 | // Use an empty HashableDimensionKey if atom has no primary fields. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 54 | primaryKey = DEFAULT_DIMENSION_KEY; |
| 55 | } |
| 56 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 57 | // Parse event for state value. |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 58 | 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()); |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 64 | handlePartialReset(eventTimeNs, primaryKey); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 65 | return; |
| 66 | } |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 67 | state = stateValue.mValue.int_value; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 68 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 69 | if (state == mResetState) { |
| 70 | VLOG("StateTracker Reset state: %s", stateValue.mValue.toString().c_str()); |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 71 | handleReset(eventTimeNs); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 72 | } |
| 73 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 74 | // Track and update state. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 75 | int32_t oldState = 0; |
| 76 | int32_t newState = 0; |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 77 | updateState(primaryKey, state, &oldState, &newState); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 78 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 79 | // Notify all listeners if state has changed. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 80 | 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) { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 85 | sListener->onStateChanged(eventTimeNs, mAtomId, primaryKey, oldState, newState); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } else { |
| 89 | VLOG("StateTracker NO updated state"); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void StateTracker::registerListener(wp<StateListener> listener) { |
| 94 | mListeners.insert(listener); |
| 95 | } |
| 96 | |
| 97 | void StateTracker::unregisterListener(wp<StateListener> listener) { |
| 98 | mListeners.erase(listener); |
| 99 | } |
| 100 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 101 | bool 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. |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 105 | if (queryKey.getValues().size() == mPrimaryFields.size()) { |
| 106 | auto it = mStateMap.find(queryKey); |
| 107 | if (it != mStateMap.end()) { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 108 | output->mValue = it->second.state; |
| 109 | return true; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 110 | } |
| 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 | } |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 116 | |
| 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; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 122 | } |
| 123 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 124 | void StateTracker::handleReset(const int64_t eventTimeNs) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 125 | VLOG("StateTracker handle reset"); |
| 126 | for (const auto pair : mStateMap) { |
| 127 | for (auto l : mListeners) { |
| 128 | auto sl = l.promote(); |
| 129 | if (sl != nullptr) { |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 130 | sl->onStateChanged(eventTimeNs, mAtomId, pair.first, pair.second.state, |
| 131 | mDefaultState); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | mStateMap.clear(); |
| 136 | } |
| 137 | |
tsaichristine | 8d73dc9 | 2019-12-06 02:11:02 -0800 | [diff] [blame^] | 138 | void StateTracker::handlePartialReset(const int64_t eventTimeNs, |
| 139 | const HashableDimensionKey& primaryKey) { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 140 | VLOG("StateTracker handle partial reset"); |
| 141 | if (mStateMap.find(primaryKey) != mStateMap.end()) { |
| 142 | mStateMap.erase(primaryKey); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void 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 |