xref: /aosp_15_r20/frameworks/av/media/utils/MethodStatistics.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker  *
4*ec779b8eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker  *
8*ec779b8eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker  *
10*ec779b8eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker  * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker  */
16*ec779b8eSAndroid Build Coastguard Worker 
17*ec779b8eSAndroid Build Coastguard Worker #include <mediautils/MethodStatistics.h>
18*ec779b8eSAndroid Build Coastguard Worker 
19*ec779b8eSAndroid Build Coastguard Worker namespace android::mediautils {
20*ec779b8eSAndroid Build Coastguard Worker 
21*ec779b8eSAndroid Build Coastguard Worker // Repository for MethodStatistics Objects
22*ec779b8eSAndroid Build Coastguard Worker 
23*ec779b8eSAndroid Build Coastguard Worker // It's important to have the HAL class name defined with suffix "Hidl/Aidl" because
24*ec779b8eSAndroid Build Coastguard Worker // TimerThread::isRequestFromHal use this string to match binder call to/from hal.
25*ec779b8eSAndroid Build Coastguard Worker std::shared_ptr<std::vector<std::string>>
getStatisticsClassesForModule(std::string_view moduleName)26*ec779b8eSAndroid Build Coastguard Worker getStatisticsClassesForModule(std::string_view moduleName) {
27*ec779b8eSAndroid Build Coastguard Worker     static const std::map<std::string, std::shared_ptr<std::vector<std::string>>,
28*ec779b8eSAndroid Build Coastguard Worker             std::less<> /* transparent comparator */> m {
29*ec779b8eSAndroid Build Coastguard Worker         {
30*ec779b8eSAndroid Build Coastguard Worker             METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL,
31*ec779b8eSAndroid Build Coastguard Worker             std::shared_ptr<std::vector<std::string>>(
32*ec779b8eSAndroid Build Coastguard Worker                 new std::vector<std::string>{
33*ec779b8eSAndroid Build Coastguard Worker                 "DeviceHalHidl",
34*ec779b8eSAndroid Build Coastguard Worker                 "EffectHalHidl",
35*ec779b8eSAndroid Build Coastguard Worker                 "StreamInHalHidl",
36*ec779b8eSAndroid Build Coastguard Worker                 "StreamOutHalHidl",
37*ec779b8eSAndroid Build Coastguard Worker               })
38*ec779b8eSAndroid Build Coastguard Worker         },
39*ec779b8eSAndroid Build Coastguard Worker         {
40*ec779b8eSAndroid Build Coastguard Worker             METHOD_STATISTICS_MODULE_NAME_AUDIO_AIDL,
41*ec779b8eSAndroid Build Coastguard Worker             std::shared_ptr<std::vector<std::string>>(
42*ec779b8eSAndroid Build Coastguard Worker                 new std::vector<std::string>{
43*ec779b8eSAndroid Build Coastguard Worker                 "DeviceHalAidl",
44*ec779b8eSAndroid Build Coastguard Worker                 "EffectHalAidl",
45*ec779b8eSAndroid Build Coastguard Worker                 "StreamHalAidl",
46*ec779b8eSAndroid Build Coastguard Worker               })
47*ec779b8eSAndroid Build Coastguard Worker         },
48*ec779b8eSAndroid Build Coastguard Worker     };
49*ec779b8eSAndroid Build Coastguard Worker     auto it = m.find(moduleName);
50*ec779b8eSAndroid Build Coastguard Worker     if (it == m.end()) return {};
51*ec779b8eSAndroid Build Coastguard Worker     return it->second;
52*ec779b8eSAndroid Build Coastguard Worker }
53*ec779b8eSAndroid Build Coastguard Worker 
addClassesToMap(const std::shared_ptr<std::vector<std::string>> & classNames,std::map<std::string,std::shared_ptr<MethodStatistics<std::string>>,std::less<>> & map)54*ec779b8eSAndroid Build Coastguard Worker static void addClassesToMap(const std::shared_ptr<std::vector<std::string>> &classNames,
55*ec779b8eSAndroid Build Coastguard Worker         std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>,
56*ec779b8eSAndroid Build Coastguard Worker                 std::less<> /* transparent comparator */> &map) {
57*ec779b8eSAndroid Build Coastguard Worker     if (classNames) {
58*ec779b8eSAndroid Build Coastguard Worker         for (const auto& className : *classNames) {
59*ec779b8eSAndroid Build Coastguard Worker             map.emplace(className, std::make_shared<MethodStatistics<std::string>>());
60*ec779b8eSAndroid Build Coastguard Worker         }
61*ec779b8eSAndroid Build Coastguard Worker     }
62*ec779b8eSAndroid Build Coastguard Worker }
63*ec779b8eSAndroid Build Coastguard Worker 
64*ec779b8eSAndroid Build Coastguard Worker // singleton statistics for DeviceHalHidl StreamOutHalHidl StreamInHalHidl
65*ec779b8eSAndroid Build Coastguard Worker std::shared_ptr<MethodStatistics<std::string>>
getStatisticsForClass(std::string_view className)66*ec779b8eSAndroid Build Coastguard Worker getStatisticsForClass(std::string_view className) {
67*ec779b8eSAndroid Build Coastguard Worker     static const std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>,
68*ec779b8eSAndroid Build Coastguard Worker             std::less<> /* transparent comparator */> m =
69*ec779b8eSAndroid Build Coastguard Worker         // copy elided initialization of map m.
70*ec779b8eSAndroid Build Coastguard Worker         [](){
71*ec779b8eSAndroid Build Coastguard Worker             std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<>> m;
72*ec779b8eSAndroid Build Coastguard Worker             addClassesToMap(
73*ec779b8eSAndroid Build Coastguard Worker                     getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL),
74*ec779b8eSAndroid Build Coastguard Worker                     m);
75*ec779b8eSAndroid Build Coastguard Worker             addClassesToMap(
76*ec779b8eSAndroid Build Coastguard Worker                     getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_AIDL),
77*ec779b8eSAndroid Build Coastguard Worker                     m);
78*ec779b8eSAndroid Build Coastguard Worker             return m;
79*ec779b8eSAndroid Build Coastguard Worker         }();
80*ec779b8eSAndroid Build Coastguard Worker 
81*ec779b8eSAndroid Build Coastguard Worker     auto it = m.find(className);
82*ec779b8eSAndroid Build Coastguard Worker     if (it == m.end()) return {};
83*ec779b8eSAndroid Build Coastguard Worker     return it->second;
84*ec779b8eSAndroid Build Coastguard Worker }
85*ec779b8eSAndroid Build Coastguard Worker 
86*ec779b8eSAndroid Build Coastguard Worker } // android::mediautils
87