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 #pragma once 17 18 #include <com_android_bluetooth_flags.h> 19 20 #include <unordered_map> 21 22 #include "module.h" 23 #include "os/repeating_alarm.h" 24 25 namespace bluetooth { 26 namespace metrics { 27 28 class CounterMetrics : public bluetooth::Module { 29 public: CounterMetrics(os::Handler * handler)30 CounterMetrics(os::Handler* handler) : Module(handler) {} ~CounterMetrics()31 ~CounterMetrics() { 32 if (!com::android::bluetooth::flags::same_handler_for_all_modules()) { 33 GetHandler()->Clear(); 34 GetHandler()->WaitUntilStopped(std::chrono::milliseconds(2000)); 35 delete GetHandler(); 36 } 37 } 38 39 bool CacheCount(int32_t key, int64_t value); 40 virtual bool Count(int32_t key, int64_t count); 41 void Stop() override; 42 void Start() override; 43 44 protected: 45 CounterMetrics() = default; 46 void ListDependencies(ModuleList* list) const override; ToString()47 std::string ToString() const override { return std::string("BluetoothCounterMetrics"); } 48 void DrainBufferedCounters(); IsInitialized()49 virtual bool IsInitialized() { return initialized_; } 50 51 private: 52 std::unordered_map<int32_t, int64_t> counters_; 53 mutable std::mutex mutex_; 54 std::unique_ptr<os::RepeatingAlarm> alarm_; 55 bool initialized_{false}; 56 }; 57 58 } // namespace metrics 59 } // namespace bluetooth 60