blob: 5f90ab3d50c2ba2a46df4d2adf16b92b5296214e [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
Joe Onoratoe5472052019-04-24 16:27:33 -070036sp<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 Jin4e843102018-02-14 15:36:18 -080051bool 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
60void Throttler::addReportSize(size_t reportByteSize) {
Yi Jin8cb370f2018-04-23 13:03:14 -070061 VLOG("The current request took %zu bytes to dropbox", reportByteSize);
Yi Jin4e843102018-02-14 15:36:18 -080062 mAccumulatedSize += reportByteSize;
63}
64
65void Throttler::dump(FILE* out) {
Yi Jin8cb370f2018-04-23 13:03:14 -070066 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 Jin4e843102018-02-14 15:36:18 -080070}
Yi Jin6cacbcb2018-03-30 14:04:52 -070071
72} // namespace incidentd
73} // namespace os
Joe Onoratoe5472052019-04-24 16:27:33 -070074} // namespace android