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 | #pragma once |
| 17 | |
| 18 | #include <statslog.h> |
| 19 | #include <utils/RefBase.h> |
| 20 | #include "HashableDimensionKey.h" |
| 21 | #include "logd/LogEvent.h" |
| 22 | |
| 23 | #include "state/StateListener.h" |
| 24 | |
| 25 | #include <unordered_map> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace statsd { |
| 30 | |
| 31 | class StateTracker : public virtual RefBase { |
| 32 | public: |
| 33 | StateTracker(const int atomId, const util::StateAtomFieldOptions& stateAtomInfo); |
| 34 | |
| 35 | virtual ~StateTracker(){}; |
| 36 | |
| 37 | // Updates state map and notifies all listeners if a state change occurs. |
| 38 | // Checks if a state change has occurred by getting the state value from |
| 39 | // the log event and comparing the old and new states. |
| 40 | void onLogEvent(const LogEvent& event); |
| 41 | |
| 42 | // Adds new listeners to set of StateListeners. If a listener is already |
| 43 | // registered, it is ignored. |
| 44 | void registerListener(wp<StateListener> listener); |
| 45 | |
| 46 | void unregisterListener(wp<StateListener> listener); |
| 47 | |
| 48 | // Returns the state value mapped to the given query key. |
| 49 | // If the key isn't mapped to a state or the key size doesn't match the |
| 50 | // primary key size, the default state is returned. |
| 51 | int getState(const HashableDimensionKey& queryKey) const; |
| 52 | |
| 53 | inline int getListenersCount() const { |
| 54 | return mListeners.size(); |
| 55 | } |
| 56 | |
| 57 | const static int kStateUnknown = -1; |
| 58 | |
| 59 | private: |
| 60 | struct StateValueInfo { |
| 61 | int32_t state; // state value |
| 62 | int count; // nested count (only used for binary states) |
| 63 | }; |
| 64 | |
| 65 | const int32_t mAtomId; // id of the state atom being tracked |
| 66 | |
| 67 | Matcher mStateField; // matches the atom's exclusive state field |
| 68 | |
| 69 | std::vector<Matcher> mPrimaryFields; // matches the atom's primary fields |
| 70 | |
| 71 | int32_t mDefaultState = kStateUnknown; |
| 72 | |
| 73 | int32_t mResetState; |
| 74 | |
| 75 | // Maps primary key to state value info |
| 76 | std::unordered_map<HashableDimensionKey, StateValueInfo> mStateMap; |
| 77 | |
| 78 | // Set of all StateListeners (objects listening for state changes) |
| 79 | std::set<wp<StateListener>> mListeners; |
| 80 | |
| 81 | // Reset all state values in map to default state |
| 82 | void handleReset(); |
| 83 | |
| 84 | // Reset only the state value mapped to primary key to default state |
| 85 | void handlePartialReset(const HashableDimensionKey& primaryKey); |
| 86 | |
| 87 | // Update the StateMap based on the received state value. |
| 88 | // Store the old and new states. |
| 89 | void updateState(const HashableDimensionKey& primaryKey, const int32_t eventState, |
| 90 | int32_t* oldState, int32_t* newState); |
| 91 | }; |
| 92 | |
| 93 | } // namespace statsd |
| 94 | } // namespace os |
| 95 | } // namespace android |