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 17 #pragma once 18 19 #include <android/os/IStatsCompanionService.h> 20 #include <android/os/IStatsPullerCallback.h> 21 #include <binder/IServiceManager.h> 22 #include <utils/RefBase.h> 23 #include <utils/threads.h> 24 #include <list> 25 #include <vector> 26 #include "PullDataReceiver.h" 27 #include "StatsPuller.h" 28 #include "guardrail/StatsdStats.h" 29 #include "logd/LogEvent.h" 30 31 namespace android { 32 namespace os { 33 namespace statsd { 34 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 // Minimum time before this puller does actual pull again. 40 // Pullers can cause significant impact to system health and battery. 41 // So that we don't pull too frequently. 42 // If a pull request comes before cooldown, a cached version from previous pull 43 // will be returned. 44 int64_t coolDownNs = 1 * NS_PER_SEC; 45 // The actual puller 46 sp<StatsPuller> puller; 47 // Max time allowed to pull this atom. 48 // We cannot reliably kill a pull thread. So we don't terminate the puller. 49 // The data is discarded if the pull takes longer than this and mHasGoodData 50 // marked as false. 51 int64_t pullTimeoutNs = StatsdStats::kPullMaxDelayNs; 52 } PullAtomInfo; 53 54 class StatsPullerManager : public virtual RefBase { 55 public: 56 StatsPullerManager(); 57 ~StatsPullerManager()58 virtual ~StatsPullerManager() { 59 } 60 61 // Registers a receiver for tagId. It will be pulled on the nextPullTimeNs 62 // and then every intervalNs thereafter. 63 virtual void RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, int64_t nextPullTimeNs, 64 int64_t intervalNs); 65 66 // Stop listening on a tagId. 67 virtual void UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver); 68 69 // Verify if we know how to pull for this matcher 70 bool PullerForMatcherExists(int tagId) const; 71 72 void OnAlarmFired(int64_t elapsedTimeNs); 73 74 // Pulls the most recent data. 75 // The data may be served from cache if consecutive pulls come within 76 // mCoolDownNs. 77 // Returns true if the pull was successful. 78 // Returns false when 79 // 1) the pull fails 80 // 2) pull takes longer than mPullTimeoutNs (intrinsic to puller) 81 // If the metric wants to make any change to the data, like timestamps, they 82 // should make a copy as this data may be shared with multiple metrics. 83 virtual bool Pull(int tagId, vector<std::shared_ptr<LogEvent>>* data); 84 85 // Clear pull data cache immediately. 86 int ForceClearPullerCache(); 87 88 // Clear pull data cache if it is beyond respective cool down time. 89 int ClearPullerCacheIfNecessary(int64_t timestampNs); 90 91 void SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService); 92 93 void RegisterPullerCallback(int32_t atomTag, 94 const sp<IStatsPullerCallback>& callback); 95 96 void UnregisterPullerCallback(int32_t atomTag); 97 98 static std::map<int, PullAtomInfo> kAllPullAtomInfo; 99 100 private: 101 sp<IStatsCompanionService> mStatsCompanionService = nullptr; 102 103 typedef struct { 104 int64_t nextPullTimeNs; 105 int64_t intervalNs; 106 wp<PullDataReceiver> receiver; 107 } ReceiverInfo; 108 109 // mapping from simple matcher tagId to receivers 110 std::map<int, std::list<ReceiverInfo>> mReceivers; 111 112 // locks for data receiver and StatsCompanionService changes 113 Mutex mLock; 114 115 void updateAlarmLocked(); 116 117 int64_t mNextPullTimeNs; 118 119 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents); 120 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm); 121 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation); 122 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition); 123 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents); 124 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm); 125 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation); 126 }; 127 128 } // namespace statsd 129 } // namespace os 130 } // namespace android 131