• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <inttypes.h>
22 #include <utils/SystemClock.h>
23 
24 namespace android {
25 namespace os {
26 namespace incidentd {
27 
Throttler(size_t limit,int64_t refractoryPeriodMs)28 Throttler::Throttler(size_t limit, int64_t refractoryPeriodMs)
29     : mSizeLimit(limit),
30       mRefractoryPeriodMs(refractoryPeriodMs),
31       mAccumulatedSize(0),
32       mLastRefractoryMs(android::elapsedRealtime()) {}
33 
~Throttler()34 Throttler::~Throttler() {}
35 
filterBatch(const sp<ReportBatch> & queued)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 
shouldThrottle()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 
addReportSize(size_t reportByteSize)60 void Throttler::addReportSize(size_t reportByteSize) {
61     VLOG("The current request took %zu bytes to dropbox", reportByteSize);
62     mAccumulatedSize += reportByteSize;
63 }
64 
dump(FILE * out)65 void Throttler::dump(FILE* out) {
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);
70 }
71 
72 }  // namespace incidentd
73 }  // namespace os
74 }  // namespace android
75