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 | |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 48 | // Notifies the StateTracker for the given atomId to register listener. |
tsaichristine | d21aacf | 2019-10-07 14:47:38 -0700 | [diff] [blame] | 49 | // If the correct StateTracker does not exist, a new StateTracker is created. |
Muhammad Qureshi | bfc4bdb | 2020-04-08 06:26:49 -0700 | [diff] [blame] | 50 | // Note: StateTrackers can be created for non-state atoms. They are essentially empty and |
| 51 | // do not perform any actions. |
| 52 | void registerListener(const int32_t atomId, wp<StateListener> listener); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 53 | |
| 54 | // Notifies the correct StateTracker to unregister a listener |
| 55 | // and removes the tracker if it no longer has any listeners. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 56 | void unregisterListener(const int32_t atomId, wp<StateListener> listener); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 57 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 58 | // Returns true if the StateTracker exists and queries for the |
| 59 | // original state value mapped to the given query key. The state value is |
| 60 | // stored and output in a FieldValue class. |
| 61 | // Returns false if the StateTracker doesn't exist. |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 62 | bool getStateValue(const int32_t atomId, const HashableDimensionKey& queryKey, |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 63 | FieldValue* output) const; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 64 | |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 65 | inline int getStateTrackersCount() const { |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 66 | return mStateTrackers.size(); |
| 67 | } |
| 68 | |
tsaichristine | c876b49 | 2019-12-10 13:47:05 -0800 | [diff] [blame] | 69 | inline int getListenersCount(const int32_t atomId) const { |
tsaichristine | 69000e6 | 2019-10-18 17:34:52 -0700 | [diff] [blame] | 70 | auto it = mStateTrackers.find(atomId); |
| 71 | if (it != mStateTrackers.end()) { |
| 72 | return it->second->getListenersCount(); |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 73 | } |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | private: |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 78 | mutable std::mutex mMutex; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 79 | |
tsaichristine | 7192e0a | 2019-12-06 02:20:00 -0800 | [diff] [blame] | 80 | // Maps state atom ids to StateTrackers |
| 81 | std::unordered_map<int32_t, sp<StateTracker>> mStateTrackers; |
tsaichristine | 1097864 | 2019-09-10 14:12:49 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace statsd |
| 85 | } // namespace os |
| 86 | } // namespace android |