Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 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 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include "Reporter.h" |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 20 | |
| 21 | #include <utils/RefBase.h> |
| 22 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 23 | #include <vector> |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace incidentd { |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 29 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 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 | /** |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 39 | * Return a batch containing reports, if any that should be executed. |
| 40 | * Those will be removed from 'queued'. |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 41 | */ |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 42 | sp<ReportBatch> filterBatch(const sp<ReportBatch>& queued); |
| 43 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 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 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 58 | } // namespace incidentd |
| 59 | } // namespace os |
| 60 | } // namespace android |