• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #pragma once
18 
19 #include "Reporter.h"
20 
21 #include <utils/RefBase.h>
22 
23 #include <vector>
24 #include <unistd.h>
25 
26 namespace android {
27 namespace os {
28 namespace incidentd {
29 
30 /**
31  * This is a size-based throttler which prevents incidentd to take more data.
32  */
33 class Throttler : public virtual android::RefBase {
34 public:
35     Throttler(size_t limit, int64_t refractoryPeriodMs);
36     ~Throttler();
37 
38     /**
39      * Return a batch containing reports, if any that should be executed.
40      * Those will be removed from 'queued'.
41      */
42     sp<ReportBatch> filterBatch(const sp<ReportBatch>& queued);
43 
44     bool shouldThrottle();
45 
46     void addReportSize(size_t reportByteSize);
47 
48     void dump(FILE* out);
49 
50 private:
51     const size_t mSizeLimit;
52     const int64_t mRefractoryPeriodMs;
53 
54     size_t mAccumulatedSize;
55     int64_t mLastRefractoryMs;
56 };
57 
58 }  // namespace incidentd
59 }  // namespace os
60 }  // namespace android
61