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

Commit 6af9330e authored by Girish Shetty's avatar Girish Shetty Committed by Gerrit Code Review
Browse files

Merge changes I0e5b59b9,I6a37e397 into main

* changes:
  resourcemanager: add resource metrics to dumpsys
  libstagefright: record failed codec initialization
parents 02e64af2 dc187558
Loading
Loading
Loading
Loading
+9 −0
Original line number Original line Diff line number Diff line
@@ -3975,6 +3975,15 @@ void MediaCodec::onMessageReceived(const sp<AMessage> &msg) {
                    switch (mState) {
                    switch (mState) {
                        case INITIALIZING:
                        case INITIALIZING:
                        {
                        {
                            // Resource error during INITIALIZING state needs to be logged
                            // through metrics, to be able to track such occurrences.
                            if (isResourceError(err)) {
                                mediametrics_setInt32(mMetricsHandle, kCodecError, err);
                                mediametrics_setCString(mMetricsHandle, kCodecErrorState,
                                                        stateString(mState).c_str());
                                flushMediametrics();
                                initMediametrics();
                            }
                            setState(UNINITIALIZED);
                            setState(UNINITIALIZED);
                            break;
                            break;
                        }
                        }
+146 −27
Original line number Original line Diff line number Diff line
@@ -109,23 +109,17 @@ static CodecBucket getCodecBucket(bool isEncoder, MediaResourceSubType codecType
    return CodecBucketUnspecified;
    return CodecBucketUnspecified;
}
}


static bool getLogMessage(int hwCount, int swCount, std::stringstream& logMsg) {
static std::string getLogMessage(const std::string& firstKey, const long& firstValue,
    bool update = false;
                                 const std::string& secondKey, const long& secondValue) {
    logMsg.clear();


    if (hwCount > 0) {
    std::stringstream logMsg;
        logMsg << " HW: " << hwCount;
    if (firstValue > 0) {
        update = true;
        logMsg << firstKey << firstValue;
    }
    if (swCount > 0) {
        logMsg << " SW: " << swCount;
        update = true;
    }
    }

    if (secondValue > 0) {
    if (update) {
        logMsg << secondKey << secondValue;
        logMsg << " ] ";
    }
    }
    return update;
    return logMsg.str();
}
}


ResourceManagerMetrics::ResourceManagerMetrics(const sp<ProcessInfoInterface>& processInfo) {
ResourceManagerMetrics::ResourceManagerMetrics(const sp<ProcessInfoInterface>& processInfo) {
@@ -364,6 +358,15 @@ void ResourceManagerMetrics::onProcessTerminated(int32_t pid, uid_t uid) {
    std::scoped_lock lock(mLock);
    std::scoped_lock lock(mLock);
    // post MediaCodecConcurrentUsageReported for this terminated pid.
    // post MediaCodecConcurrentUsageReported for this terminated pid.
    pushConcurrentUsageReport(pid, uid);
    pushConcurrentUsageReport(pid, uid);
    // Remove all the metrics associated with this process.
    std::map<int32_t, ConcurrentCodecs>::iterator it1 = mProcessConcurrentCodecsMap.find(pid);
    if (it1 != mProcessConcurrentCodecsMap.end()) {
        mProcessConcurrentCodecsMap.erase(it1);
    }
    std::map<int32_t, PixelCount>::iterator it2 = mProcessPixelsMap.find(pid);
    if (it2 != mProcessPixelsMap.end()) {
        mProcessPixelsMap.erase(it2);
    }
}
}


void ResourceManagerMetrics::pushConcurrentUsageReport(int32_t pid, uid_t uid) {
void ResourceManagerMetrics::pushConcurrentUsageReport(int32_t pid, uid_t uid) {
@@ -400,24 +403,30 @@ void ResourceManagerMetrics::pushConcurrentUsageReport(int32_t pid, uid_t uid) {


    std::stringstream peakCodecLog;
    std::stringstream peakCodecLog;
    peakCodecLog << "Peak { ";
    peakCodecLog << "Peak { ";
    std::stringstream logMsg;
    std::string logMsg;
    if (getLogMessage(peakHwAudioEncoderCount, peakSwAudioEncoderCount, logMsg)) {
    logMsg = getLogMessage(" HW: ", peakHwAudioEncoderCount, " SW: ", peakSwAudioEncoderCount);
        peakCodecLog << "AudioEnc[" << logMsg.str();
    if (!logMsg.empty()) {
        peakCodecLog << "AudioEnc[ " << logMsg << " ] ";
    }
    }
    if (getLogMessage(peakHwAudioDecoderCount, peakSwAudioDecoderCount, logMsg)) {
    logMsg = getLogMessage(" HW: ", peakHwAudioDecoderCount, " SW: ", peakSwAudioDecoderCount);
        peakCodecLog << "AudioDec[" << logMsg.str();
    if (!logMsg.empty()) {
        peakCodecLog << "AudioDec[" << logMsg << " ] ";
    }
    }
    if (getLogMessage(peakHwVideoEncoderCount, peakSwVideoEncoderCount, logMsg)) {
    logMsg = getLogMessage(" HW: ", peakHwVideoEncoderCount, " SW: ", peakSwVideoEncoderCount);
        peakCodecLog << "VideoEnc[" << logMsg.str();
    if (!logMsg.empty()) {
        peakCodecLog << "VideoEnc[" << logMsg << " ] ";
    }
    }
    if (getLogMessage(peakHwVideoDecoderCount, peakSwVideoDecoderCount, logMsg)) {
    logMsg = getLogMessage(" HW: ", peakHwVideoDecoderCount, " SW: ", peakSwVideoDecoderCount);
        peakCodecLog << "VideoDec[" << logMsg.str();
    if (!logMsg.empty()) {
        peakCodecLog << "VideoDec[" << logMsg << " ] ";
    }
    }
    if (getLogMessage(peakHwImageEncoderCount, peakSwImageEncoderCount, logMsg)) {
    logMsg = getLogMessage(" HW: ", peakHwImageEncoderCount, " SW: ", peakSwImageEncoderCount);
        peakCodecLog << "ImageEnc[" << logMsg.str();
    if (!logMsg.empty()) {
        peakCodecLog << "ImageEnc[" << logMsg << " ] ";
    }
    }
    if (getLogMessage(peakHwImageDecoderCount, peakSwImageDecoderCount, logMsg)) {
    logMsg = getLogMessage(" HW: ", peakHwImageDecoderCount, " SW: ", peakSwImageDecoderCount);
        peakCodecLog << "ImageDec[" << logMsg.str();
    if (!logMsg.empty()) {
        peakCodecLog << "ImageDec[" << logMsg << " ] ";
    }
    }
    peakCodecLog << "}";
    peakCodecLog << "}";


@@ -705,4 +714,114 @@ long ResourceManagerMetrics::getCurrentConcurrentPixelCount(int pid) const {
    return 0;
    return 0;
}
}


static std::string getConcurrentInstanceCount(const std::map<std::string, int>& resourceMap) {
    if (resourceMap.empty()) {
        return "";
    }
    std::stringstream concurrentInstanceInfo;
    for (const auto& [name, count] : resourceMap) {
        if (count > 0) {
            concurrentInstanceInfo << "      Name: " << name << " Instances: " << count << "\n";
        }
    }

    std::string info = concurrentInstanceInfo.str();
    if (info.empty()) {
        return "";
    }
    return "    Current Concurrent Codec Instances:\n" + info;
}

static std::string getAppsPixelCount(const std::map<int32_t, PixelCount>& pixelMap) {
    if (pixelMap.empty()) {
        return "";
    }
    std::stringstream pixelInfo;
    for (const auto& [pid, pixelCount] : pixelMap) {
        std::string logMsg = getLogMessage(" Current Pixels: ", pixelCount.mCurrent,
                                           " Peak Pixels: ", pixelCount.mPeak);
        if (!logMsg.empty()) {
            pixelInfo  << "      PID[" << pid << "]: {" << logMsg << " }\n";
        }
    }

    return "    Applications Pixel Usage:\n" + pixelInfo.str();
}

static std::string getCodecUsageMetrics(const ConcurrentCodecsMap& codecsMap) {
    int peakHwAudioEncoderCount = codecsMap[HwAudioEncoder];
    int peakHwAudioDecoderCount = codecsMap[HwAudioDecoder];
    int peakHwVideoEncoderCount = codecsMap[HwVideoEncoder];
    int peakHwVideoDecoderCount = codecsMap[HwVideoDecoder];
    int peakHwImageEncoderCount = codecsMap[HwImageEncoder];
    int peakHwImageDecoderCount = codecsMap[HwImageDecoder];
    int peakSwAudioEncoderCount = codecsMap[SwAudioEncoder];
    int peakSwAudioDecoderCount = codecsMap[SwAudioDecoder];
    int peakSwVideoEncoderCount = codecsMap[SwVideoEncoder];
    int peakSwVideoDecoderCount = codecsMap[SwVideoDecoder];
    int peakSwImageEncoderCount = codecsMap[SwImageEncoder];
    int peakSwImageDecoderCount = codecsMap[SwImageDecoder];
    std::stringstream usageMetrics;
    std::string logMsg;
    logMsg = getLogMessage(" HW: ", peakHwAudioEncoderCount, " SW: ", peakSwAudioEncoderCount);
    if (!logMsg.empty()) {
        usageMetrics << "AudioEnc[" << logMsg << " ] ";
    }
    logMsg = getLogMessage(" HW: ", peakHwAudioDecoderCount, " SW: ", peakSwAudioDecoderCount);
    if (!logMsg.empty()) {
        usageMetrics << "AudioDec[" << logMsg << " ] ";
    }
    logMsg = getLogMessage(" HW: ", peakHwVideoEncoderCount, " SW: ", peakSwVideoEncoderCount);
    if (!logMsg.empty()) {
        usageMetrics << "VideoEnc[" << logMsg << " ] ";
    }
    logMsg = getLogMessage(" HW: ", peakHwVideoDecoderCount, " SW: ", peakSwVideoDecoderCount);
    if (!logMsg.empty()) {
        usageMetrics << "VideoDec[" << logMsg << " ] ";
    }
    logMsg = getLogMessage(" HW: ", peakHwImageEncoderCount, " SW: ", peakSwImageEncoderCount);
    if (!logMsg.empty()) {
        usageMetrics << "ImageEnc[" << logMsg << " ] ";
    }
    logMsg = getLogMessage(" HW: ", peakHwImageDecoderCount, " SW: ", peakSwImageDecoderCount);
    if (!logMsg.empty()) {
        usageMetrics << "ImageDec[" << logMsg << " ] ";
    }

    return usageMetrics.str();
}

static std::string getAppsCodecUsageMetrics(
        const std::map<int32_t, ConcurrentCodecs>& processCodecsMap) {
    if (processCodecsMap.empty()) {
        return "";
    }
    std::stringstream codecUsage;
    std::string info;
    for (const auto& [pid, codecMap] : processCodecsMap) {
        codecUsage << "      PID[" << pid << "]: ";
        info = getCodecUsageMetrics(codecMap.mCurrent);
        if (!info.empty()) {
            codecUsage << "Current Codec Usage: { " << info << "} ";
        }
        info = getCodecUsageMetrics(codecMap.mPeak);
        if (!info.empty()) {
            codecUsage << "Peak Codec Usage: { " << info << "}";
        }
        codecUsage << "\n";
    }

    return "    Applications Codec Usage:\n" + codecUsage.str();
}


std::string ResourceManagerMetrics::dump() const {
    std::string metricsLog("  Metrics logs:\n");
    metricsLog += getConcurrentInstanceCount(mConcurrentResourceCountMap);
    metricsLog += getAppsPixelCount(mProcessPixelsMap);
    metricsLog += getAppsCodecUsageMetrics(mProcessConcurrentCodecsMap);

    return std::move(metricsLog);
}

} // namespace android
} // namespace android
+5 −2
Original line number Original line Diff line number Diff line
@@ -171,6 +171,9 @@ public:
    // Get the current concurrent pixel count (associated with the video codecs) for the process.
    // Get the current concurrent pixel count (associated with the video codecs) for the process.
    long getCurrentConcurrentPixelCount(int pid) const;
    long getCurrentConcurrentPixelCount(int pid) const;


    // retrieves metrics log.
    std::string dump() const;

private:
private:
    ResourceManagerMetrics(const ResourceManagerMetrics&) = delete;
    ResourceManagerMetrics(const ResourceManagerMetrics&) = delete;
    ResourceManagerMetrics(ResourceManagerMetrics&&) = delete;
    ResourceManagerMetrics(ResourceManagerMetrics&&) = delete;
@@ -204,9 +207,9 @@ private:
    // Map of resources (name) and number of concurrent instances
    // Map of resources (name) and number of concurrent instances
    std::map<std::string, int> mConcurrentResourceCountMap;
    std::map<std::string, int> mConcurrentResourceCountMap;


    // Map of concurrent codes by CodecBucket across the system.
    // Map of concurrent codecs by CodecBucket across the system.
    ConcurrentCodecsMap mConcurrentCodecsMap;
    ConcurrentCodecsMap mConcurrentCodecsMap;
    // Map of concurrent and peak codes by CodecBucket for each process/application.
    // Map of concurrent and peak codecs by CodecBucket for each process/application.
    std::map<int32_t, ConcurrentCodecs> mProcessConcurrentCodecsMap;
    std::map<int32_t, ConcurrentCodecs> mProcessConcurrentCodecsMap;


    // Uid Observer to monitor the application termination.
    // Uid Observer to monitor the application termination.
+13 −1
Original line number Original line Diff line number Diff line
@@ -108,10 +108,17 @@ binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint
        serviceLog = mServiceLog->toString("    " /* linePrefix */);
        serviceLog = mServiceLog->toString("    " /* linePrefix */);
    }
    }


    // Get all the resource (and overload pid) logs
    // Get all the resource (and overload pid) log.
    std::string resourceLog;
    std::string resourceLog;
    getResourceDump(resourceLog);
    getResourceDump(resourceLog);


    // Get all the metrics log.
    std::string metricsLog;
    {
        std::scoped_lock lock{mLock};
        metricsLog = mResourceManagerMetrics->dump();
    }

    const size_t SIZE = 256;
    const size_t SIZE = 256;
    char buffer[SIZE];
    char buffer[SIZE];
    snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
    snprintf(buffer, SIZE, "ResourceManagerService: %p\n", this);
@@ -123,11 +130,16 @@ binder_status_t ResourceManagerService::dump(int fd, const char** /*args*/, uint
            supportsSecureWithNonSecureCodec);
            supportsSecureWithNonSecureCodec);
    result.append(buffer);
    result.append(buffer);


    // Add resource log.
    result.append(resourceLog.c_str());
    result.append(resourceLog.c_str());


    // Add service log.
    result.append("  Events logs (most recent at top):\n");
    result.append("  Events logs (most recent at top):\n");
    result.append(serviceLog);
    result.append(serviceLog);


    // Add metrics log.
    result.append(metricsLog.c_str());

    write(fd, result.c_str(), result.size());
    write(fd, result.c_str(), result.size());
    return OK;
    return OK;
}
}