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