1 /*
2  * Copyright 2021 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 LOG_TAG "BluetoothCounterMetrics"
17 
18 #include "metrics/counter_metrics.h"
19 
20 #include <bluetooth/log.h>
21 
22 #include "common/bind.h"
23 #include "os/metrics.h"
24 
25 namespace bluetooth {
26 namespace metrics {
27 
28 const int COUNTER_METRICS_PERDIOD_MINUTES = 360;  // Drain counters every 6 hours
29 
__anon2080992e0102() 30 const ModuleFactory CounterMetrics::Factory = ModuleFactory([]() { return new CounterMetrics(); });
31 
ListDependencies(ModuleList *) const32 void CounterMetrics::ListDependencies(ModuleList* /* list */) const {}
33 
Start()34 void CounterMetrics::Start() {
35   alarm_ = std::make_unique<os::RepeatingAlarm>(GetHandler());
36   alarm_->Schedule(
37           common::Bind(&CounterMetrics::DrainBufferedCounters, bluetooth::common::Unretained(this)),
38           std::chrono::minutes(COUNTER_METRICS_PERDIOD_MINUTES));
39   log::info("Counter metrics initialized");
40   initialized_ = true;
41 }
42 
Stop()43 void CounterMetrics::Stop() {
44   DrainBufferedCounters();
45   initialized_ = false;
46   alarm_->Cancel();
47   alarm_.reset();
48   log::info("Counter metrics canceled");
49 }
50 
CacheCount(int32_t key,int64_t count)51 bool CounterMetrics::CacheCount(int32_t key, int64_t count) {
52   if (!IsInitialized()) {
53     log::warn("Counter metrics isn't initialized");
54     return false;
55   }
56   if (count <= 0) {
57     log::warn("count is not larger than 0. count: {}, key: {}", count, key);
58     return false;
59   }
60   int64_t total = 0;
61   std::lock_guard<std::mutex> lock(mutex_);
62   if (counters_.find(key) != counters_.end()) {
63     total = counters_[key];
64   }
65   if (LLONG_MAX - total < count) {
66     log::warn("Counter metric overflows. count {} current total: {} key: {}", count, total, key);
67     counters_[key] = LLONG_MAX;
68     return false;
69   }
70   counters_[key] = total + count;
71   return true;
72 }
73 
Count(int32_t key,int64_t count)74 bool CounterMetrics::Count(int32_t key, int64_t count) {
75   if (!IsInitialized()) {
76     log::warn("Counter metrics isn't initialized");
77     return false;
78   }
79   if (count <= 0) {
80     log::warn("count is not larger than 0. count: {}, key: {}", count, key);
81     return false;
82   }
83   os::LogMetricBluetoothCodePathCounterMetrics(key, count);
84   return true;
85 }
86 
DrainBufferedCounters()87 void CounterMetrics::DrainBufferedCounters() {
88   if (!IsInitialized()) {
89     log::warn("Counter metrics isn't initialized");
90     return;
91   }
92   std::lock_guard<std::mutex> lock(mutex_);
93   log::info("Draining buffered counters");
94   for (auto const& pair : counters_) {
95     Count(pair.first, pair.second);
96   }
97   counters_.clear();
98 }
99 
100 }  // namespace metrics
101 }  // namespace bluetooth
102