| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 17 | #pragma once |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 18 | |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 19 | #include <android/os/IStatsCompanionService.h> |
| 20 | #include <binder/IServiceManager.h> |
| 21 | #include <utils/RefBase.h> |
| 22 | #include <utils/threads.h> |
| 23 | #include <list> |
| 24 | #include <string> |
| 25 | #include <unordered_map> |
| 26 | #include <vector> |
| 27 | #include "PullDataReceiver.h" |
| 28 | #include "StatsPuller.h" |
| 29 | #include "logd/LogEvent.h" |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace os { |
| 33 | namespace statsd { |
| 34 | |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 35 | typedef struct { |
| 36 | // The field numbers of the fields that need to be summed when merging |
| 37 | // isolated uid with host uid. |
| 38 | std::vector<int> additiveFields; |
| 39 | // The field numbers of the fields that can't be merged when merging |
| 40 | // data belong to isolated uid and host uid. |
| 41 | std::vector<int> nonAdditiveFields; |
| 42 | // How long should the puller wait before doing an actual pull again. Default |
| 43 | // 1 sec. Set this to 0 if this is handled elsewhere. |
| 44 | int64_t coolDownNs = 1 * NS_PER_SEC; |
| 45 | // The actual puller |
| 46 | sp<StatsPuller> puller; |
| 47 | } PullAtomInfo; |
| 48 | |
| 49 | class StatsPullerManager : public virtual RefBase { |
| 50 | public: |
| 51 | StatsPullerManager(); |
| 52 | |
| 53 | virtual ~StatsPullerManager() { |
| 54 | } |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 55 | |
| Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 56 | // Registers a receiver for tagId. It will be pulled on the nextPullTimeNs |
| 57 | // and then every intervalNs thereafter. |
| Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 58 | virtual void RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, int64_t nextPullTimeNs, |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 59 | int64_t intervalNs); |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 60 | |
| Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 61 | // Stop listening on a tagId. |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 62 | virtual void UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver); |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 63 | |
| Chenjie Yu | 85ed838 | 2017-12-14 16:48:54 -0800 | [diff] [blame] | 64 | // Verify if we know how to pull for this matcher |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 65 | bool PullerForMatcherExists(int tagId) const; |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 66 | |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 67 | void OnAlarmFired(const int64_t timeNs); |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 68 | |
| Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 69 | // Use respective puller to pull the data. The returned data will have |
| 70 | // elapsedTimeNs set as timeNs and will have wallClockTimeNs set as current |
| 71 | // wall clock time. |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 72 | virtual bool Pull(const int tagId, const int64_t timeNs, |
| 73 | vector<std::shared_ptr<LogEvent>>* data); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 74 | |
| Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 75 | // Clear pull data cache immediately. |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 76 | int ForceClearPullerCache(); |
| Chenjie Yu | fa22d65 | 2018-02-05 14:37:48 -0800 | [diff] [blame] | 77 | |
| Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 78 | // Clear pull data cache if it is beyond respective cool down time. |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 79 | int ClearPullerCacheIfNecessary(int64_t timestampNs); |
| Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 80 | |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 81 | void SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService); |
| Chenjie Yu | e72252b | 2018-02-01 13:19:35 -0800 | [diff] [blame] | 82 | |
| Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 83 | const static std::map<int, PullAtomInfo> kAllPullAtomInfo; |
| 84 | |
| 85 | private: |
| 86 | sp<IStatsCompanionService> mStatsCompanionService = nullptr; |
| 87 | |
| 88 | typedef struct { |
| 89 | int64_t nextPullTimeNs; |
| 90 | int64_t intervalNs; |
| 91 | wp<PullDataReceiver> receiver; |
| 92 | } ReceiverInfo; |
| 93 | |
| 94 | // mapping from simple matcher tagId to receivers |
| 95 | std::map<int, std::list<ReceiverInfo>> mReceivers; |
| 96 | |
| 97 | // locks for data receiver and StatsCompanionService changes |
| 98 | Mutex mLock; |
| 99 | |
| 100 | void updateAlarmLocked(); |
| 101 | |
| 102 | int64_t mNextPullTimeNs; |
| 103 | |
| 104 | FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents); |
| 105 | FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm); |
| 106 | FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents); |
| 107 | FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm); |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 110 | } // namespace statsd |
| 111 | } // namespace os |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 112 | } // namespace android |