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 #pragma once 17 18 #include <unordered_map> 19 20 #include "module.h" 21 #include "os/repeating_alarm.h" 22 23 namespace bluetooth { 24 namespace metrics { 25 26 class CounterMetrics : public bluetooth::Module { 27 public: 28 bool CacheCount(int32_t key, int64_t value); 29 virtual bool Count(int32_t key, int64_t count); 30 void Stop() override; 31 static const ModuleFactory Factory; 32 33 protected: 34 void ListDependencies(ModuleList* list) const override; 35 void Start() override; ToString()36 std::string ToString() const override { 37 return std::string("BluetoothCounterMetrics"); 38 } 39 void DrainBufferedCounters(); IsInitialized()40 virtual bool IsInitialized() { 41 return initialized_; 42 } 43 44 private: 45 std::unordered_map<int32_t, int64_t> counters_; 46 mutable std::mutex mutex_; 47 std::unique_ptr<os::RepeatingAlarm> alarm_; 48 bool initialized_ {false}; 49 }; 50 51 } // namespace metrics 52 } // namespace bluetooth