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 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 18 | #include <inttypes.h> |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 19 | #include <utils/RefBase.h> |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 20 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 21 | #include "HashableDimensionKey.h" |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 22 | #include "state/StateListener.h" |
| 23 | #include "state/StateTracker.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace os { |
| 27 | namespace statsd { |
| 28 | |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 29 | /** |
| 30 | * This class is NOT thread safe. |
| 31 | * It should only be used while StatsLogProcessor's lock is held. |
| 32 | */ |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 33 | class StateManager : public virtual RefBase { |
| 34 | public: |
| 35 | StateManager(){}; |
| 36 | |
| 37 | ~StateManager(){}; |
| 38 | |
| 39 | // Returns a pointer to the single, shared StateManager object. |
| 40 | static StateManager& getInstance(); |
| 41 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 42 | // Unregisters all listeners and removes all trackers from StateManager. |
| 43 | void clear(); |
| 44 | |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 45 | // Notifies the correct StateTracker of an event. |
| 46 | void onLogEvent(const LogEvent& event); |
| 47 | |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 48 | // Returns true if atomId is being tracked and is associated with a state |
| 49 | // atom. StateManager notifies the correct StateTracker to register listener. |
| 50 | // If the correct StateTracker does not exist, a new StateTracker is created. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 51 | bool registerListener(const int32_t atomId, wp<StateListener> listener); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 52 | |
| 53 | // Notifies the correct StateTracker to unregister a listener |
| 54 | // and removes the tracker if it no longer has any listeners. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 55 | void unregisterListener(const int32_t atomId, wp<StateListener> listener); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 56 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 57 | // Returns true if the StateTracker exists and queries for the |
| 58 | // original state value mapped to the given query key. The state value is |
| 59 | // stored and output in a FieldValue class. |
| 60 | // Returns false if the StateTracker doesn't exist. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 61 | bool getStateValue(const int32_t atomId, const HashableDimensionKey& queryKey, |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 62 | FieldValue* output) const; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 63 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 64 | inline int getStateTrackersCount() const { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 65 | return mStateTrackers.size(); |
| 66 | } |
| 67 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 68 | inline int getListenersCount(const int32_t atomId) const { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 69 | auto it = mStateTrackers.find(atomId); |
| 70 | if (it != mStateTrackers.end()) { |
| 71 | return it->second->getListenersCount(); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 72 | } |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | private: |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 77 | mutable std::mutex mMutex; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 78 | |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 79 | // Maps state atom ids to StateTrackers |
| 80 | std::unordered_map<int32_t, sp<StateTracker>> mStateTrackers; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespace statsd |
| 84 | } // namespace os |
| 85 | } // namespace android |