blob: 46a1472482d13f38d6f3d19c5148bcd982f8cc94 [file] [log] [blame]
Yi Jin4e843102018-02-14 15:36:18 -08001/*
2 * Copyright (C) 2018 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
Joe Onoratoe5472052019-04-24 16:27:33 -070017#pragma once
18
19#include "Reporter.h"
Yi Jin4e843102018-02-14 15:36:18 -080020
21#include <utils/RefBase.h>
22
Joe Onoratoe5472052019-04-24 16:27:33 -070023#include <vector>
Yi Jin4e843102018-02-14 15:36:18 -080024#include <unistd.h>
Yi Jin6cacbcb2018-03-30 14:04:52 -070025
26namespace android {
27namespace os {
28namespace incidentd {
Joe Onoratoe5472052019-04-24 16:27:33 -070029
Yi Jin4e843102018-02-14 15:36:18 -080030/**
31 * This is a size-based throttler which prevents incidentd to take more data.
32 */
33class Throttler : public virtual android::RefBase {
34public:
35 Throttler(size_t limit, int64_t refractoryPeriodMs);
36 ~Throttler();
37
38 /**
Joe Onoratoe5472052019-04-24 16:27:33 -070039 * Return a batch containing reports, if any that should be executed.
40 * Those will be removed from 'queued'.
Yi Jin4e843102018-02-14 15:36:18 -080041 */
Joe Onoratoe5472052019-04-24 16:27:33 -070042 sp<ReportBatch> filterBatch(const sp<ReportBatch>& queued);
43
Yi Jin4e843102018-02-14 15:36:18 -080044 bool shouldThrottle();
45
46 void addReportSize(size_t reportByteSize);
47
48 void dump(FILE* out);
49
50private:
51 const size_t mSizeLimit;
52 const int64_t mRefractoryPeriodMs;
53
54 size_t mAccumulatedSize;
55 int64_t mLastRefractoryMs;
56};
57
Yi Jin6cacbcb2018-03-30 14:04:52 -070058} // namespace incidentd
59} // namespace os
60} // namespace android