| 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 | |
| 36 | bool Throttler::shouldThrottle() { |
| 37 | int64_t now = android::elapsedRealtime(); |
| 38 | if (now > mRefractoryPeriodMs + mLastRefractoryMs) { |
| 39 | mLastRefractoryMs = now; |
| 40 | mAccumulatedSize = 0; |
| 41 | } |
| 42 | return mAccumulatedSize > mSizeLimit; |
| 43 | } |
| 44 | |
| 45 | void Throttler::addReportSize(size_t reportByteSize) { |
| Yi Jin | 8cb370f | 2018-04-23 13:03:14 -0700 | [diff] [blame] | 46 | VLOG("The current request took %zu bytes to dropbox", reportByteSize); |
| Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 47 | mAccumulatedSize += reportByteSize; |
| 48 | } |
| 49 | |
| 50 | void Throttler::dump(FILE* out) { |
| Yi Jin | 8cb370f | 2018-04-23 13:03:14 -0700 | [diff] [blame] | 51 | fprintf(out, "mSizeLimit=%zu\n", mSizeLimit); |
| 52 | fprintf(out, "mAccumulatedSize=%zu\n", mAccumulatedSize); |
| 53 | fprintf(out, "mRefractoryPeriodMs=%" PRIi64 "\n", mRefractoryPeriodMs); |
| 54 | fprintf(out, "mLastRefractoryMs=%" PRIi64 "\n", mLastRefractoryMs); |
| Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 55 | } |
| Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 56 | |
| 57 | } // namespace incidentd |
| 58 | } // namespace os |
| 59 | } // namespace android |