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