blob: bbf5d9dc69db87b122301023a955e7a28dbd6ef9 [file] [log] [blame]
Chenjie Yu1a317ba2017-10-05 16:05:32 -07001/*
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
Chenjie Yub3dda412017-10-24 13:41:59 -070017#pragma once
Chenjie Yu1a317ba2017-10-05 16:05:32 -070018
Chenjie Yue2219202018-06-08 10:07:51 -070019#include <android/os/IStatsCompanionService.h>
20#include <binder/IServiceManager.h>
21#include <utils/RefBase.h>
22#include <utils/threads.h>
23#include <list>
24#include <string>
25#include <unordered_map>
26#include <vector>
27#include "PullDataReceiver.h"
28#include "StatsPuller.h"
29#include "logd/LogEvent.h"
Chenjie Yu1a317ba2017-10-05 16:05:32 -070030
31namespace android {
32namespace os {
33namespace statsd {
34
Chenjie Yue2219202018-06-08 10:07:51 -070035typedef 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 // The field numbers of the fields that can't be merged when merging
40 // data belong to isolated uid and host uid.
41 std::vector<int> nonAdditiveFields;
42 // How long should the puller wait before doing an actual pull again. Default
43 // 1 sec. Set this to 0 if this is handled elsewhere.
44 int64_t coolDownNs = 1 * NS_PER_SEC;
45 // The actual puller
46 sp<StatsPuller> puller;
47} PullAtomInfo;
48
49class StatsPullerManager : public virtual RefBase {
50public:
51 StatsPullerManager();
52
53 virtual ~StatsPullerManager() {
54 }
Chenjie Yub3dda412017-10-24 13:41:59 -070055
Chenjie Yue1361ed2018-07-23 17:33:09 -070056 // Registers a receiver for tagId. It will be pulled on the nextPullTimeNs
57 // and then every intervalNs thereafter.
Chenjie Yu1a0a9412018-03-28 10:07:22 -070058 virtual void RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, int64_t nextPullTimeNs,
Chenjie Yue2219202018-06-08 10:07:51 -070059 int64_t intervalNs);
Chenjie Yub3dda412017-10-24 13:41:59 -070060
Chenjie Yue1361ed2018-07-23 17:33:09 -070061 // Stop listening on a tagId.
Chenjie Yue2219202018-06-08 10:07:51 -070062 virtual void UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver);
Chenjie Yu1a317ba2017-10-05 16:05:32 -070063
Chenjie Yu85ed8382017-12-14 16:48:54 -080064 // Verify if we know how to pull for this matcher
Chenjie Yue2219202018-06-08 10:07:51 -070065 bool PullerForMatcherExists(int tagId) const;
Chenjie Yub3dda412017-10-24 13:41:59 -070066
Chenjie Yue2219202018-06-08 10:07:51 -070067 void OnAlarmFired(const int64_t timeNs);
Chenjie Yu1a317ba2017-10-05 16:05:32 -070068
Chenjie Yue1361ed2018-07-23 17:33:09 -070069 // Use respective puller to pull the data. The returned data will have
70 // elapsedTimeNs set as timeNs and will have wallClockTimeNs set as current
71 // wall clock time.
Chenjie Yue2219202018-06-08 10:07:51 -070072 virtual bool Pull(const int tagId, const int64_t timeNs,
73 vector<std::shared_ptr<LogEvent>>* data);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070074
Chenjie Yue1361ed2018-07-23 17:33:09 -070075 // Clear pull data cache immediately.
Chenjie Yue2219202018-06-08 10:07:51 -070076 int ForceClearPullerCache();
Chenjie Yufa22d652018-02-05 14:37:48 -080077
Chenjie Yue1361ed2018-07-23 17:33:09 -070078 // Clear pull data cache if it is beyond respective cool down time.
Chenjie Yue2219202018-06-08 10:07:51 -070079 int ClearPullerCacheIfNecessary(int64_t timestampNs);
Chenjie Yuaa5b2012018-03-21 13:53:15 -070080
Chenjie Yue2219202018-06-08 10:07:51 -070081 void SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService);
Chenjie Yue72252b2018-02-01 13:19:35 -080082
Chenjie Yue2219202018-06-08 10:07:51 -070083 const static std::map<int, PullAtomInfo> kAllPullAtomInfo;
84
85private:
86 sp<IStatsCompanionService> mStatsCompanionService = nullptr;
87
88 typedef struct {
89 int64_t nextPullTimeNs;
90 int64_t intervalNs;
91 wp<PullDataReceiver> receiver;
92 } ReceiverInfo;
93
94 // mapping from simple matcher tagId to receivers
95 std::map<int, std::list<ReceiverInfo>> mReceivers;
96
97 // locks for data receiver and StatsCompanionService changes
98 Mutex mLock;
99
100 void updateAlarmLocked();
101
102 int64_t mNextPullTimeNs;
103
104 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvents);
105 FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEvent_LateAlarm);
106 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
107 FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700108};
109
Chenjie Yu1a317ba2017-10-05 16:05:32 -0700110} // namespace statsd
111} // namespace os
Chenjie Yu5305e1d2017-10-31 13:49:36 -0700112} // namespace android