Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 75e59f6c authored by Andy Hung's avatar Andy Hung Committed by Automerger Merge Worker
Browse files

Merge "MethodStatistics: Use const references in API" into tm-qpr-dev am: 2587c053

parents f8862a1e 2587c053
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -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>>(
@@ -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>>());
@@ -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;
}
+16 −7
Original line number Diff line number Diff line
@@ -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;
    }
@@ -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();
@@ -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;
@@ -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.