• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <aidl/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 using aidl::android::os::IStatsCompanionService;
30 using std::shared_ptr;
31 
32 namespace android {
33 namespace os {
34 namespace statsd {
35 
36 enum PullErrorCode {
37     PULL_SUCCESS = 0,
38     PULL_FAIL = 1,
39     PULL_DEAD_OBJECT = 2,
40 };
41 
42 class StatsPuller : public virtual RefBase {
43 public:
44     explicit StatsPuller(const int tagId,
45                          const int64_t coolDownNs = NS_PER_SEC,
46                          const int64_t pullTimeoutNs = StatsdStats::kPullMaxDelayNs,
47                          const std::vector<int> additiveFields = std::vector<int>());
48 
~StatsPuller()49     virtual ~StatsPuller() {}
50 
51     // Pulls the most recent data.
52     // The data may be served from cache if consecutive pulls come within
53     // predefined cooldown time.
54     // Returns PULL_SUCCESS if the pull was successful.
55     // Returns PULL_DEAD_OBJECT if a dead object exception occurred when making a pull.
56     // Returns PULL_FAIL when
57     //   1) the pull fails
58     //   2) pull takes longer than mPullTimeoutNs (intrinsic to puller)
59     // If a metric wants to make any change to the data, like timestamps, it
60     // should make a copy as this data may be shared with multiple metrics.
61     PullErrorCode Pull(const int64_t eventTimeNs, std::vector<std::shared_ptr<LogEvent>>* data);
62 
63     // Clear cache immediately
64     int ForceClearCache();
65 
66     // Clear cache if elapsed time is more than cooldown time
67     int ClearCacheIfNecessary(int64_t timestampNs);
68 
69     static void SetUidMap(const sp<UidMap>& uidMap);
70 
SetStatsCompanionService(shared_ptr<IStatsCompanionService> statsCompanionService)71     virtual void SetStatsCompanionService(
72             shared_ptr<IStatsCompanionService> statsCompanionService) {};
73 
74 protected:
75     const int mTagId;
76 
77     // Max time allowed to pull this atom.
78     // We cannot reliably kill a pull thread. So we don't terminate the puller.
79     // The data is discarded if the pull takes longer than this and mHasGoodData
80     // marked as false.
81     const int64_t mPullTimeoutNs = StatsdStats::kPullMaxDelayNs;
82 
83 private:
84     mutable std::mutex mLock;
85 
86     // Real puller impl.
87     virtual PullErrorCode PullInternal(std::vector<std::shared_ptr<LogEvent>>* data) = 0;
88 
89     bool mHasGoodData = false;
90 
91     // Minimum time before this puller does actual pull again.
92     // Pullers can cause significant impact to system health and battery.
93     // So that we don't pull too frequently.
94     // If a pull request comes before cooldown, a cached version from previous pull
95     // will be returned.
96     const int64_t mCoolDownNs = 1 * NS_PER_SEC;
97 
98     // The field numbers of the fields that need to be summed when merging
99     // isolated uid with host uid.
100     const std::vector<int> mAdditiveFields;
101 
102     int64_t mLastPullTimeNs;
103 
104     // All pulls happen due to an event (app upgrade, bucket boundary, condition change, etc).
105     // If multiple pulls need to be done at the same event time, we will always use the cache after
106     // the first pull.
107     int64_t mLastEventTimeNs;
108 
109     // Cache of data from last pull. If next request comes before cool down finishes,
110     // cached data will be returned.
111     // Cached data is cleared when
112     //   1) A pull fails
113     //   2) A new pull request comes after cooldown time.
114     //   3) clearCache is called.
115     std::vector<std::shared_ptr<LogEvent>> mCachedData;
116 
117     int clearCache();
118 
119     int clearCacheLocked();
120 
121     static sp<UidMap> mUidMap;
122 };
123 
124 }  // namespace statsd
125 }  // namespace os
126 }  // namespace android
127