• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common/bind.h"
21 #include "os/log.h"
22 #include "os/metrics.h"
23 
24 namespace bluetooth {
25 namespace metrics {
26 
27 const int COUNTER_METRICS_PERDIOD_MINUTES = 360; // Drain counters every 6 hours
28 
__anona88e22640102() 29 const ModuleFactory CounterMetrics::Factory = ModuleFactory([]() { return new CounterMetrics(); });
30 
ListDependencies(ModuleList * list) const31 void CounterMetrics::ListDependencies(ModuleList* list) const {
32 }
33 
Start()34 void CounterMetrics::Start() {
35   alarm_ = std::make_unique<os::RepeatingAlarm>(GetHandler());
36   alarm_->Schedule(
37       common::Bind(&CounterMetrics::DrainBufferedCounters,
38            bluetooth::common::Unretained(this)),
39       std::chrono::minutes(COUNTER_METRICS_PERDIOD_MINUTES));
40   LOG_INFO("Counter metrics initialized");
41   initialized_ = true;
42 }
43 
Stop()44 void CounterMetrics::Stop() {
45   DrainBufferedCounters();
46   initialized_ = false;
47   alarm_->Cancel();
48   alarm_.reset();
49   LOG_INFO("Counter metrics canceled");
50 }
51 
CacheCount(int32_t key,int64_t count)52 bool CounterMetrics::CacheCount(int32_t key, int64_t count) {
53   if (!IsInitialized()) {
54     LOG_WARN("Counter metrics isn't initialized");
55     return false;
56   }
57   if (count <= 0) {
58     LOG_WARN("count is not larger than 0. count: %s, key: %d", std::to_string(count).c_str(), key);
59     return false;
60   }
61   int64_t total = 0;
62   std::lock_guard<std::mutex> lock(mutex_);
63   if (counters_.find(key) != counters_.end()) {
64     total = counters_[key];
65   }
66   if (LLONG_MAX - total < count) {
67       LOG_WARN("Counter metric overflows. count %s current total: %s key: %d",
68                std::to_string(count).c_str(), std::to_string(total).c_str(), 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: %s, key: %d", std::to_string(count).c_str(), 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