blob: 306cc32c5f16dcbaa6cd9bea12b0e27b3686b571 [file] [log] [blame]
Chenjie Yu6736c892017-11-09 10:50:09 -08001/*
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 <binder/IServiceManager.h>
21#include <utils/RefBase.h>
22#include <utils/threads.h>
23#include <string>
24#include <unordered_map>
25#include <vector>
26#include <list>
27#include "PullDataReceiver.h"
28#include "StatsPuller.h"
29#include "logd/LogEvent.h"
30
31namespace android {
32namespace os {
33namespace statsd {
34
35class StatsPullerManagerImpl : public virtual RefBase {
36public:
37 static StatsPullerManagerImpl& GetInstance();
38
39 void RegisterReceiver(int tagId, wp<PullDataReceiver> receiver, long intervalMs);
40
41 void UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver);
42
43 // Verify if we know how to pull for this matcher
Yangster7c334a12017-11-22 14:24:24 -080044 bool PullerForMatcherExists(int tagId) const;
Chenjie Yu6736c892017-11-09 10:50:09 -080045
46 void OnAlarmFired();
47
48 bool Pull(const int tagId, vector<std::shared_ptr<LogEvent>>* data);
49
50private:
51 StatsPullerManagerImpl();
52
53 // use this to update alarm
54 sp<IStatsCompanionService> mStatsCompanionService = nullptr;
55
56 sp<IStatsCompanionService> get_stats_companion_service();
57
58 // mapping from simple matcher tagId to puller
59 std::map<int, std::shared_ptr<StatsPuller>> mPullers;
60
61 typedef struct {
62 // pull_interval_sec : last_pull_time_sec
63 std::pair<uint64_t, uint64_t> timeInfo;
64 wp<PullDataReceiver> receiver;
65 } ReceiverInfo;
66
67 // mapping from simple matcher tagId to receivers
68 std::map<int, std::list<ReceiverInfo>> mReceivers;
69
70 Mutex mReceiversLock;
71
72 long mCurrentPullingInterval;
73
74 // for pulled metrics, it is important for the buckets to be aligned to multiple of smallest
75 // bucket size. All pulled metrics start pulling based on this time, so that they can be
76 // correctly attributed to the correct buckets. Pulled data attach a timestamp which is the
77 // request time.
78 const long mPullStartTimeMs;
79
Yangster7c334a12017-11-22 14:24:24 -080080 long get_pull_start_time_ms() const;
Chenjie Yu6736c892017-11-09 10:50:09 -080081
82 LogEvent parse_pulled_data(String16 data);
83};
84
85} // namespace statsd
86} // namespace os
87} // namespace android