Loading media/utils/MethodStatistics.cpp +9 −6 Original line number Diff line number Diff line Loading @@ -22,7 +22,8 @@ namespace android::mediautils { std::shared_ptr<std::vector<std::string>> getStatisticsClassesForModule(std::string_view moduleName) { static const std::map<std::string, std::shared_ptr<std::vector<std::string>>> m { static const std::map<std::string, std::shared_ptr<std::vector<std::string>>, std::less<> /* transparent comparator */> m { { METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL, std::shared_ptr<std::vector<std::string>>( Loading @@ -34,13 +35,14 @@ getStatisticsClassesForModule(std::string_view moduleName) { }) }, }; auto it = m.find({moduleName.begin(), moduleName.end()}); auto it = m.find(moduleName); if (it == m.end()) return {}; return it->second; } static void addClassesToMap(const std::shared_ptr<std::vector<std::string>> &classNames, std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>> &map) { std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<> /* transparent comparator */> &map) { if (classNames) { for (const auto& className : *classNames) { map.emplace(className, std::make_shared<MethodStatistics<std::string>>()); Loading @@ -51,17 +53,18 @@ static void addClassesToMap(const std::shared_ptr<std::vector<std::string>> &cla // singleton statistics for DeviceHalHidl StreamOutHalHidl StreamInHalHidl std::shared_ptr<MethodStatistics<std::string>> getStatisticsForClass(std::string_view className) { static const std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>> m = static const std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<> /* transparent comparator */> m = // copy elided initialization of map m. [](){ std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>> m; std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<>> m; addClassesToMap( getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL), m); return m; }(); auto it = m.find({className.begin(), className.end()}); auto it = m.find(className); if (it == m.end()) return {}; return it->second; } Loading media/utils/include/mediautils/MethodStatistics.h +16 −7 Original line number Diff line number Diff line Loading @@ -55,15 +55,23 @@ public: /** * Adds a method event, typically execution time in ms. */ void event(Code code, FloatType executeMs) { template <typename C> void event(C&& code, FloatType executeMs) { std::lock_guard lg(mLock); mStatisticsMap[code].add(executeMs); auto it = mStatisticsMap.lower_bound(code); if (it != mStatisticsMap.end() && it->first == code) { it->second.add(executeMs); } else { // StatsType ctor takes an optional array of data for initialization. FloatType dataArray[1] = { executeMs }; mStatisticsMap.emplace_hint(it, std::forward<C>(code), dataArray); } } /** * Returns the name for the method code. */ std::string getMethodForCode(Code code) const { std::string getMethodForCode(const Code& code) const { auto it = mMethodMap.find(code); return it == mMethodMap.end() ? std::to_string((int)code) : it->second; } Loading @@ -71,7 +79,7 @@ public: /** * Returns the number of times the method was invoked by event(). */ size_t getMethodCount(Code code) const { size_t getMethodCount(const Code& code) const { std::lock_guard lg(mLock); auto it = mStatisticsMap.find(code); return it == mStatisticsMap.end() ? 0 : it->second.getN(); Loading @@ -80,7 +88,7 @@ public: /** * Returns the statistics object for the method. */ StatsType getStatistics(Code code) const { StatsType getStatistics(const Code& code) const { std::lock_guard lg(mLock); auto it = mStatisticsMap.find(code); return it == mStatisticsMap.end() ? StatsType{} : it->second; Loading @@ -107,9 +115,10 @@ public: } private: const std::map<Code, std::string> mMethodMap; // Note: we use a transparent comparator std::less<> for heterogeneous key lookup. const std::map<Code, std::string, std::less<>> mMethodMap; mutable std::mutex mLock; std::map<Code, StatsType> mStatisticsMap GUARDED_BY(mLock); std::map<Code, StatsType, std::less<>> mStatisticsMap GUARDED_BY(mLock); }; // Managed Statistics support. Loading Loading
media/utils/MethodStatistics.cpp +9 −6 Original line number Diff line number Diff line Loading @@ -22,7 +22,8 @@ namespace android::mediautils { std::shared_ptr<std::vector<std::string>> getStatisticsClassesForModule(std::string_view moduleName) { static const std::map<std::string, std::shared_ptr<std::vector<std::string>>> m { static const std::map<std::string, std::shared_ptr<std::vector<std::string>>, std::less<> /* transparent comparator */> m { { METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL, std::shared_ptr<std::vector<std::string>>( Loading @@ -34,13 +35,14 @@ getStatisticsClassesForModule(std::string_view moduleName) { }) }, }; auto it = m.find({moduleName.begin(), moduleName.end()}); auto it = m.find(moduleName); if (it == m.end()) return {}; return it->second; } static void addClassesToMap(const std::shared_ptr<std::vector<std::string>> &classNames, std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>> &map) { std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<> /* transparent comparator */> &map) { if (classNames) { for (const auto& className : *classNames) { map.emplace(className, std::make_shared<MethodStatistics<std::string>>()); Loading @@ -51,17 +53,18 @@ static void addClassesToMap(const std::shared_ptr<std::vector<std::string>> &cla // singleton statistics for DeviceHalHidl StreamOutHalHidl StreamInHalHidl std::shared_ptr<MethodStatistics<std::string>> getStatisticsForClass(std::string_view className) { static const std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>> m = static const std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<> /* transparent comparator */> m = // copy elided initialization of map m. [](){ std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>> m; std::map<std::string, std::shared_ptr<MethodStatistics<std::string>>, std::less<>> m; addClassesToMap( getStatisticsClassesForModule(METHOD_STATISTICS_MODULE_NAME_AUDIO_HIDL), m); return m; }(); auto it = m.find({className.begin(), className.end()}); auto it = m.find(className); if (it == m.end()) return {}; return it->second; } Loading
media/utils/include/mediautils/MethodStatistics.h +16 −7 Original line number Diff line number Diff line Loading @@ -55,15 +55,23 @@ public: /** * Adds a method event, typically execution time in ms. */ void event(Code code, FloatType executeMs) { template <typename C> void event(C&& code, FloatType executeMs) { std::lock_guard lg(mLock); mStatisticsMap[code].add(executeMs); auto it = mStatisticsMap.lower_bound(code); if (it != mStatisticsMap.end() && it->first == code) { it->second.add(executeMs); } else { // StatsType ctor takes an optional array of data for initialization. FloatType dataArray[1] = { executeMs }; mStatisticsMap.emplace_hint(it, std::forward<C>(code), dataArray); } } /** * Returns the name for the method code. */ std::string getMethodForCode(Code code) const { std::string getMethodForCode(const Code& code) const { auto it = mMethodMap.find(code); return it == mMethodMap.end() ? std::to_string((int)code) : it->second; } Loading @@ -71,7 +79,7 @@ public: /** * Returns the number of times the method was invoked by event(). */ size_t getMethodCount(Code code) const { size_t getMethodCount(const Code& code) const { std::lock_guard lg(mLock); auto it = mStatisticsMap.find(code); return it == mStatisticsMap.end() ? 0 : it->second.getN(); Loading @@ -80,7 +88,7 @@ public: /** * Returns the statistics object for the method. */ StatsType getStatistics(Code code) const { StatsType getStatistics(const Code& code) const { std::lock_guard lg(mLock); auto it = mStatisticsMap.find(code); return it == mStatisticsMap.end() ? StatsType{} : it->second; Loading @@ -107,9 +115,10 @@ public: } private: const std::map<Code, std::string> mMethodMap; // Note: we use a transparent comparator std::less<> for heterogeneous key lookup. const std::map<Code, std::string, std::less<>> mMethodMap; mutable std::mutex mLock; std::map<Code, StatsType> mStatisticsMap GUARDED_BY(mLock); std::map<Code, StatsType, std::less<>> mStatisticsMap GUARDED_BY(mLock); }; // Managed Statistics support. Loading