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 <utils/RefBase.h> 21 #include <mutex> 22 #include <vector> 23 #include "packages/UidMap.h" 24 25 #include "guardrail/StatsdStats.h" 26 #include "logd/LogEvent.h" 27 #include "puller_util.h" 28 29 namespace android { 30 namespace os { 31 namespace statsd { 32 33 class StatsPuller : public virtual RefBase { 34 public: 35 explicit StatsPuller(const int tagId); 36 ~StatsPuller()37 virtual ~StatsPuller() {} 38 39 // Pulls the most recent data. 40 // The data may be served from cache if consecutive pulls come within 41 // predefined cooldown time. 42 // Returns true if the pull was successful. 43 // Returns false when 44 // 1) the pull fails 45 // 2) pull takes longer than mPullTimeoutNs (intrinsic to puller) 46 // If a metric wants to make any change to the data, like timestamps, it 47 // should make a copy as this data may be shared with multiple metrics. 48 bool Pull(std::vector<std::shared_ptr<LogEvent>>* data); 49 50 // Clear cache immediately 51 int ForceClearCache(); 52 53 // Clear cache if elapsed time is more than cooldown time 54 int ClearCacheIfNecessary(int64_t timestampNs); 55 56 static void SetUidMap(const sp<UidMap>& uidMap); 57 SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService)58 virtual void SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService){}; 59 60 protected: 61 const int mTagId; 62 63 private: 64 mutable std::mutex mLock; 65 66 // Real puller impl. 67 virtual bool PullInternal(std::vector<std::shared_ptr<LogEvent>>* data) = 0; 68 69 bool mHasGoodData = false; 70 71 int64_t mLastPullTimeNs; 72 73 // Cache of data from last pull. If next request comes before cool down finishes, 74 // cached data will be returned. 75 // Cached data is cleared when 76 // 1) A pull fails 77 // 2) A new pull request comes after cooldown time. 78 // 3) clearCache is called. 79 std::vector<std::shared_ptr<LogEvent>> mCachedData; 80 81 int clearCache(); 82 83 int clearCacheLocked(); 84 85 static sp<UidMap> mUidMap; 86 }; 87 88 } // namespace statsd 89 } // namespace os 90 } // namespace android 91