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 | #define DEBUG false |
| 17 | #include "Log.h" |
| 18 | |
| 19 | #include "Throttler.h" |
| 20 | |
Yi Jin | 8cb370f | 2018-04-23 13:03:14 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 22 | #include <utils/SystemClock.h> |
| 23 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace incidentd { |
| 27 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 28 | Throttler::Throttler(size_t limit, int64_t refractoryPeriodMs) |
| 29 | : mSizeLimit(limit), |
| 30 | mRefractoryPeriodMs(refractoryPeriodMs), |
| 31 | mAccumulatedSize(0), |
| 32 | mLastRefractoryMs(android::elapsedRealtime()) {} |
| 33 | |
| 34 | Throttler::~Throttler() {} |
| 35 | |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 36 | sp<ReportBatch> Throttler::filterBatch(const sp<ReportBatch>& queued) { |
| 37 | sp<ReportBatch> result = new ReportBatch(); |
| 38 | |
| 39 | // We will never throttle the streaming ones. |
| 40 | queued->transferStreamingRequests(result); |
| 41 | |
| 42 | // If the persisted ones aren't to be throttled, then add them to the |
| 43 | // batch we're going to do. |
| 44 | if (!shouldThrottle()) { |
| 45 | queued->transferPersistedRequests(result); |
| 46 | } |
| 47 | |
| 48 | return result; |
| 49 | } |
| 50 | |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 51 | bool Throttler::shouldThrottle() { |
| 52 | int64_t now = android::elapsedRealtime(); |
| 53 | if (now > mRefractoryPeriodMs + mLastRefractoryMs) { |
| 54 | mLastRefractoryMs = now; |
| 55 | mAccumulatedSize = 0; |
| 56 | } |
| 57 | return mAccumulatedSize > mSizeLimit; |
| 58 | } |
| 59 | |
| 60 | void Throttler::addReportSize(size_t reportByteSize) { |
Yi Jin | 8cb370f | 2018-04-23 13:03:14 -0700 | [diff] [blame] | 61 | VLOG("The current request took %zu bytes to dropbox", reportByteSize); |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 62 | mAccumulatedSize += reportByteSize; |
| 63 | } |
| 64 | |
| 65 | void Throttler::dump(FILE* out) { |
Yi Jin | 8cb370f | 2018-04-23 13:03:14 -0700 | [diff] [blame] | 66 | fprintf(out, "mSizeLimit=%zu\n", mSizeLimit); |
| 67 | fprintf(out, "mAccumulatedSize=%zu\n", mAccumulatedSize); |
| 68 | fprintf(out, "mRefractoryPeriodMs=%" PRIi64 "\n", mRefractoryPeriodMs); |
| 69 | fprintf(out, "mLastRefractoryMs=%" PRIi64 "\n", mLastRefractoryMs); |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 70 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 71 | |
| 72 | } // namespace incidentd |
| 73 | } // namespace os |
Joe Onorato | e547205 | 2019-04-24 16:27:33 -0700 | [diff] [blame] | 74 | } // namespace android |