• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 
17 #pragma once
18 
19 #include <gtest/gtest_prod.h>
20 #include <stdint.h>
21 
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace android {
26 namespace os {
27 namespace statsd {
28 
29 /**
30  * @brief This class tracks the logging rate for each atom id.
31  *        The rate is calculated as a fixed time window counter
32  */
33 class LoggingRate {
34 public:
35     LoggingRate(int maxStatsNum, int64_t logFrequencyWindowNs);
36 
37     void noteLogEvent(uint32_t atomId, int64_t eventTimestampNs);
38 
39     // returns max logging rate recorded for atomId if available, 0 otherwise
40     int32_t getMaxRate(uint32_t atomId) const;
41 
42     using PeakRatePerAtomId = std::pair<int32_t, int32_t>;
43 
44     std::vector<PeakRatePerAtomId> getMaxRates(size_t topN) const;
45 
46     void reset();
47 
48 private:
49     struct RateInfo {
50         int64_t intervalStartNs;
51         int32_t rate;
52         int32_t maxRate;
53     };
54 
55     const size_t mMaxRateInfoSize;
56     const int64_t mLogFrequencyWindowNs;
57 
58     std::unordered_map<uint32_t, RateInfo> mRateInfo;
59 
60     FRIEND_TEST(StatsdStatsTest, TestLoggingRateReportReset);
61 };
62 
63 }  // namespace statsd
64 }  // namespace os
65 }  // namespace android
66