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
23 std::shared_ptr<std::vector<std::string>>
getStatisticsClassesForModule(std::string_view moduleName)24 getStatisticsClassesForModule(std::string_view moduleName) {
25 static const std::map<std::string, std::shared_ptr<std::vector<std::string>>,
26 std::less<> /* transparent comparator */> m {
27 {
28 METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL,
29 std::shared_ptr<std::vector<std::string>>(
30 new std::vector<std::string>{
31 "DeviceHalHidl",
32 "EffectHalHidl",
33 "StreamInHalHidl",
34 "StreamOutHalHidl",
35 })
36 },
37 };
38 auto it = m.find(moduleName);
39 if (it == m.end()) return {};
40 return it->second;
41 }
42
addClassesToMap(const std::shared_ptr<std::vector<std::string>> & classNames,std::map<std::string,std::shared_ptr<MethodStatistics<std::string>>,std::less<>> & map)43 static void addClassesToMap(const std::shared_ptr<std::vector<std::string>> &classNames,
44 std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>,
45 std::less<> /* transparent comparator */> &map) {
46 if (classNames) {
47 for (const auto& className : *classNames) {
48 map.emplace(className, std::make_shared<MethodStatistics<std::string>>());
49 }
50 }
51 }
52
53 // singleton statistics for DeviceHalHidl StreamOutHalHidl StreamInHalHidl
54 std::shared_ptr<MethodStatistics<std::string>>
getStatisticsForClass(std::string_view className)55 getStatisticsForClass(std::string_view className) {
56 static const std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>,
57 std::less<> /* transparent comparator */> m =
58 // copy elided initialization of map m.
59 [](){
60 std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<>> m;
61 addClassesToMap(
62 getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL),
63 m);
64 return m;
65 }();
66
67 auto it = m.find(className);
68 if (it == m.end()) return {};
69 return it->second;
70 }
71
72 } // android::mediautils
73