blob: 11136ecca091d0291715005e4555d9bb5bbe5750 [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#define DEBUG false
17#include "Log.h"
18
19#include "Throttler.h"
20
Yi Jin8cb370f2018-04-23 13:03:14 -070021#include <inttypes.h>
Yi Jin4e843102018-02-14 15:36:18 -080022#include <utils/SystemClock.h>
23
Yi Jin6cacbcb2018-03-30 14:04:52 -070024namespace android {
25namespace os {
26namespace incidentd {
27
Yi Jin4e843102018-02-14 15:36:18 -080028Throttler::Throttler(size_t limit, int64_t refractoryPeriodMs)
29 : mSizeLimit(limit),
30 mRefractoryPeriodMs(refractoryPeriodMs),
31 mAccumulatedSize(0),
32 mLastRefractoryMs(android::elapsedRealtime()) {}
33
34Throttler::~Throttler() {}
35
36bool 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
45void Throttler::addReportSize(size_t reportByteSize) {
Yi Jin8cb370f2018-04-23 13:03:14 -070046 VLOG("The current request took %zu bytes to dropbox", reportByteSize);
Yi Jin4e843102018-02-14 15:36:18 -080047 mAccumulatedSize += reportByteSize;
48}
49
50void Throttler::dump(FILE* out) {
Yi Jin8cb370f2018-04-23 13:03:14 -070051 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 Jin4e843102018-02-14 15:36:18 -080055}
Yi Jin6cacbcb2018-03-30 14:04:52 -070056
57} // namespace incidentd
58} // namespace os
59} // namespace android