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