Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
| 17 | #include <mediautils/MethodStatistics.h> |
| 18 | |
| 19 | namespace android::mediautils { |
| 20 | |
| 21 | // Repository for MethodStatistics Objects |
| 22 | |
Shunkai Yao | d4532c8 | 2023-12-17 20:01:45 +0000 | [diff] [blame] | 23 | // It's important to have the HAL class name defined with suffix "Hidl/Aidl" because |
| 24 | // TimerThread::isRequestFromHal use this string to match binder call to/from hal. |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 25 | std::shared_ptr<std::vector<std::string>> |
| 26 | getStatisticsClassesForModule(std::string_view moduleName) { |
Andy Hung | a773368 | 2022-07-08 17:19:46 -0700 | [diff] [blame] | 27 | static const std::map<std::string, std::shared_ptr<std::vector<std::string>>, |
| 28 | std::less<> /* transparent comparator */> m { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 29 | { |
| 30 | METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL, |
| 31 | std::shared_ptr<std::vector<std::string>>( |
| 32 | new std::vector<std::string>{ |
| 33 | "DeviceHalHidl", |
Andy Hung | 2924a29 | 2022-04-04 22:14:57 -0700 | [diff] [blame] | 34 | "EffectHalHidl", |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 35 | "StreamInHalHidl", |
| 36 | "StreamOutHalHidl", |
| 37 | }) |
| 38 | }, |
Shunkai Yao | d4532c8 | 2023-12-17 20:01:45 +0000 | [diff] [blame] | 39 | { |
| 40 | METHOD_STATISTICS_MODULE_NAME_AUDIO_AIDL, |
| 41 | std::shared_ptr<std::vector<std::string>>( |
| 42 | new std::vector<std::string>{ |
| 43 | "DeviceHalAidl", |
| 44 | "EffectHalAidl", |
| 45 | "StreamHalAidl", |
| 46 | }) |
| 47 | }, |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 48 | }; |
Andy Hung | a773368 | 2022-07-08 17:19:46 -0700 | [diff] [blame] | 49 | auto it = m.find(moduleName); |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 50 | if (it == m.end()) return {}; |
| 51 | return it->second; |
| 52 | } |
| 53 | |
| 54 | static void addClassesToMap(const std::shared_ptr<std::vector<std::string>> &classNames, |
Andy Hung | a773368 | 2022-07-08 17:19:46 -0700 | [diff] [blame] | 55 | std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, |
| 56 | std::less<> /* transparent comparator */> &map) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 57 | if (classNames) { |
| 58 | for (const auto& className : *classNames) { |
| 59 | map.emplace(className, std::make_shared<MethodStatistics<std::string>>()); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // singleton statistics for DeviceHalHidl StreamOutHalHidl StreamInHalHidl |
| 65 | std::shared_ptr<MethodStatistics<std::string>> |
| 66 | getStatisticsForClass(std::string_view className) { |
Andy Hung | a773368 | 2022-07-08 17:19:46 -0700 | [diff] [blame] | 67 | static const std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, |
| 68 | std::less<> /* transparent comparator */> m = |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 69 | // copy elided initialization of map m. |
| 70 | [](){ |
Andy Hung | a773368 | 2022-07-08 17:19:46 -0700 | [diff] [blame] | 71 | std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<>> m; |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 72 | addClassesToMap( |
| 73 | getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL), |
| 74 | m); |
Shunkai Yao | d4532c8 | 2023-12-17 20:01:45 +0000 | [diff] [blame] | 75 | addClassesToMap( |
| 76 | getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_AIDL), |
| 77 | m); |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 78 | return m; |
| 79 | }(); |
| 80 | |
Andy Hung | a773368 | 2022-07-08 17:19:46 -0700 | [diff] [blame] | 81 | auto it = m.find(className); |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 82 | if (it == m.end()) return {}; |
| 83 | return it->second; |
| 84 | } |
| 85 | |
| 86 | } // android::mediautils |