xref: /aosp_15_r20/frameworks/base/cmds/incidentd/src/Throttler.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker /*
2*d57664e9SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker  *
4*d57664e9SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker  *
8*d57664e9SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker  *
10*d57664e9SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker  * limitations under the License.
15*d57664e9SAndroid Build Coastguard Worker  */
16*d57664e9SAndroid Build Coastguard Worker #define DEBUG false
17*d57664e9SAndroid Build Coastguard Worker #include "Log.h"
18*d57664e9SAndroid Build Coastguard Worker 
19*d57664e9SAndroid Build Coastguard Worker #include "Throttler.h"
20*d57664e9SAndroid Build Coastguard Worker 
21*d57664e9SAndroid Build Coastguard Worker #include <inttypes.h>
22*d57664e9SAndroid Build Coastguard Worker #include <utils/SystemClock.h>
23*d57664e9SAndroid Build Coastguard Worker 
24*d57664e9SAndroid Build Coastguard Worker namespace android {
25*d57664e9SAndroid Build Coastguard Worker namespace os {
26*d57664e9SAndroid Build Coastguard Worker namespace incidentd {
27*d57664e9SAndroid Build Coastguard Worker 
Throttler(size_t limit,int64_t refractoryPeriodMs)28*d57664e9SAndroid Build Coastguard Worker Throttler::Throttler(size_t limit, int64_t refractoryPeriodMs)
29*d57664e9SAndroid Build Coastguard Worker     : mSizeLimit(limit),
30*d57664e9SAndroid Build Coastguard Worker       mRefractoryPeriodMs(refractoryPeriodMs),
31*d57664e9SAndroid Build Coastguard Worker       mAccumulatedSize(0),
32*d57664e9SAndroid Build Coastguard Worker       mLastRefractoryMs(android::elapsedRealtime()) {}
33*d57664e9SAndroid Build Coastguard Worker 
~Throttler()34*d57664e9SAndroid Build Coastguard Worker Throttler::~Throttler() {}
35*d57664e9SAndroid Build Coastguard Worker 
filterBatch(const sp<ReportBatch> & queued)36*d57664e9SAndroid Build Coastguard Worker sp<ReportBatch> Throttler::filterBatch(const sp<ReportBatch>& queued) {
37*d57664e9SAndroid Build Coastguard Worker     sp<ReportBatch> result = new ReportBatch();
38*d57664e9SAndroid Build Coastguard Worker 
39*d57664e9SAndroid Build Coastguard Worker     // We will never throttle the streaming ones.
40*d57664e9SAndroid Build Coastguard Worker     queued->transferStreamingRequests(result);
41*d57664e9SAndroid Build Coastguard Worker 
42*d57664e9SAndroid Build Coastguard Worker     // If the persisted ones aren't to be throttled, then add them to the
43*d57664e9SAndroid Build Coastguard Worker     // batch we're going to do.
44*d57664e9SAndroid Build Coastguard Worker     if (!shouldThrottle()) {
45*d57664e9SAndroid Build Coastguard Worker         queued->transferPersistedRequests(result);
46*d57664e9SAndroid Build Coastguard Worker     }
47*d57664e9SAndroid Build Coastguard Worker 
48*d57664e9SAndroid Build Coastguard Worker     return result;
49*d57664e9SAndroid Build Coastguard Worker }
50*d57664e9SAndroid Build Coastguard Worker 
shouldThrottle()51*d57664e9SAndroid Build Coastguard Worker bool Throttler::shouldThrottle() {
52*d57664e9SAndroid Build Coastguard Worker     int64_t now = android::elapsedRealtime();
53*d57664e9SAndroid Build Coastguard Worker     if (now > mRefractoryPeriodMs + mLastRefractoryMs) {
54*d57664e9SAndroid Build Coastguard Worker         mLastRefractoryMs = now;
55*d57664e9SAndroid Build Coastguard Worker         mAccumulatedSize = 0;
56*d57664e9SAndroid Build Coastguard Worker     }
57*d57664e9SAndroid Build Coastguard Worker     return mAccumulatedSize > mSizeLimit;
58*d57664e9SAndroid Build Coastguard Worker }
59*d57664e9SAndroid Build Coastguard Worker 
addReportSize(size_t reportByteSize)60*d57664e9SAndroid Build Coastguard Worker void Throttler::addReportSize(size_t reportByteSize) {
61*d57664e9SAndroid Build Coastguard Worker     VLOG("The current request took %zu bytes to dropbox", reportByteSize);
62*d57664e9SAndroid Build Coastguard Worker     mAccumulatedSize += reportByteSize;
63*d57664e9SAndroid Build Coastguard Worker }
64*d57664e9SAndroid Build Coastguard Worker 
dump(FILE * out)65*d57664e9SAndroid Build Coastguard Worker void Throttler::dump(FILE* out) {
66*d57664e9SAndroid Build Coastguard Worker     fprintf(out, "mSizeLimit=%zu\n", mSizeLimit);
67*d57664e9SAndroid Build Coastguard Worker     fprintf(out, "mAccumulatedSize=%zu\n", mAccumulatedSize);
68*d57664e9SAndroid Build Coastguard Worker     fprintf(out, "mRefractoryPeriodMs=%" PRIi64 "\n", mRefractoryPeriodMs);
69*d57664e9SAndroid Build Coastguard Worker     fprintf(out, "mLastRefractoryMs=%" PRIi64 "\n", mLastRefractoryMs);
70*d57664e9SAndroid Build Coastguard Worker }
71*d57664e9SAndroid Build Coastguard Worker 
72*d57664e9SAndroid Build Coastguard Worker }  // namespace incidentd
73*d57664e9SAndroid Build Coastguard Worker }  // namespace os
74*d57664e9SAndroid Build Coastguard Worker }  // namespace android
75