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 <utils/String16.h>
22 #include <mutex>
23 #include <vector>
24 #include "packages/UidMap.h"
25 
26 #include "guardrail/StatsdStats.h"
27 #include "logd/LogEvent.h"
28 #include "puller_util.h"
29 
30 namespace android {
31 namespace os {
32 namespace statsd {
33 
34 class StatsPuller : public virtual RefBase {
35 public:
36     StatsPuller(const int tagId);
37 
~StatsPuller()38     virtual ~StatsPuller() {}
39 
40     bool Pull(const int64_t timeNs, std::vector<std::shared_ptr<LogEvent>>* data);
41 
42     // Clear cache immediately
43     int ForceClearCache();
44 
45     // Clear cache if elapsed time is more than cooldown time
46     int ClearCacheIfNecessary(int64_t timestampNs);
47 
48     static void SetUidMap(const sp<UidMap>& uidMap);
49 
SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService)50     virtual void SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService){};
51 
52 protected:
53     // The atom tag id this puller pulls
54     const int mTagId;
55 
56 private:
57     mutable std::mutex mLock;
58     // Minimum time before this puller does actual pull again.
59     // If a pull request comes before cooldown, a cached version from purevious pull
60     // will be returned.
61     // The actual value should be determined by individual pullers.
62     int64_t mCoolDownNs;
63     // For puller stats
64     int64_t mMinPullIntervalNs = LONG_MAX;
65 
66     virtual bool PullInternal(std::vector<std::shared_ptr<LogEvent>>* data) = 0;
67 
68     // Cache of data from last pull. If next request comes before cool down finishes,
69     // cached data will be returned.
70     std::vector<std::shared_ptr<LogEvent>> mCachedData;
71 
72     int64_t mLastPullTimeNs;
73 
74     int clearCache();
75 
76     static sp<UidMap> mUidMap;
77 };
78 
79 }  // namespace statsd
80 }  // namespace os
81 }  // namespace android
82