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

Commit 5be90c89 authored by Andy Hung's avatar Andy Hung
Browse files

MediaMetrics: Report last statsd atoms pushed

Report all pushed statsd atoms (previously only audio
analytics atoms were listed). Move the statsd atom info
out of AudioAnalytics to the main dump.

Report last 4 atoms sent, 30 shown on dumpsys --all.

Test: adb shell dumpsys media.metrics
Bug: 184263266
Change-Id: I2abc2fe7a0ec42fcf20f6b9fca490c6396c5272e
parent 504f1761
Loading
Loading
Loading
Loading
+16 −22
Original line number Diff line number Diff line
@@ -207,8 +207,10 @@ std::pair<int, std::string> sendToStatsd(const char * const (& fields)[N], Types
    return { result, ss.str() };
}

AudioAnalytics::AudioAnalytics()
AudioAnalytics::AudioAnalytics(const std::shared_ptr<StatsdLog>& statsdLog)
    : mDeliverStatistics(property_get_bool(PROP_AUDIO_ANALYTICS_CLOUD_ENABLED, true))
    , mStatsdLog(statsdLog)
    , mAudioPowerUsage(this, statsdLog)
{
    SetMinimumLogSeverity(android::base::DEBUG); // for LOG().
    ALOGD("%s", __func__);
@@ -407,20 +409,6 @@ std::pair<std::string, int32_t> AudioAnalytics::dump(
        ll -= l;
    }

    if (ll > 0) {
        // Print the statsd atoms we sent out.
        const std::string statsd = mStatsdLog.dumpToString("  " /* prefix */, ll - 1);
        const size_t n = std::count(statsd.begin(), statsd.end(), '\n') + 1; // we control this.
        if ((size_t)ll >= n) {
            if (n == 1) {
                ss << "Statsd atoms: empty or truncated\n";
            } else {
                ss << "Statsd atoms:\n" << statsd;
            }
            ll -= (int32_t)n;
        }
    }

    if (ll > 0 && prefix == nullptr) {
        auto [s, l] = mAudioPowerUsage.dump(ll);
        ss << s;
@@ -602,7 +590,8 @@ void AudioAnalytics::DeviceUse::endAudioIntervalGroup(
                    , logSessionIdForStats.c_str()
                    );
            ALOGV("%s: statsd %s", __func__, str.c_str());
            mAudioAnalytics.mStatsdLog.log("%s", str.c_str());
            mAudioAnalytics.mStatsdLog->log(
                    android::util::MEDIAMETRICS_AUDIORECORDDEVICEUSAGE_REPORTED, str);
        }
    } break;
    case THREAD: {
@@ -650,7 +639,8 @@ void AudioAnalytics::DeviceUse::endAudioIntervalGroup(
                , ENUM_EXTRACT(typeForStats)
            );
            ALOGV("%s: statsd %s", __func__, str.c_str());
            mAudioAnalytics.mStatsdLog.log("%s", str.c_str());
            mAudioAnalytics.mStatsdLog->log(
                    android::util::MEDIAMETRICS_AUDIOTHREADDEVICEUSAGE_REPORTED, str);
        }
    } break;
    case TRACK: {
@@ -770,7 +760,8 @@ void AudioAnalytics::DeviceUse::endAudioIntervalGroup(
                    , logSessionIdForStats.c_str()
                    );
            ALOGV("%s: statsd %s", __func__, str.c_str());
            mAudioAnalytics.mStatsdLog.log("%s", str.c_str());
            mAudioAnalytics.mStatsdLog->log(
                    android::util::MEDIAMETRICS_AUDIOTRACKDEVICEUSAGE_REPORTED, str);
        }
        } break;
    }
@@ -846,7 +837,8 @@ void AudioAnalytics::DeviceConnection::createPatch(
                    , /* connection_count */ 1
                    );
            ALOGV("%s: statsd %s", __func__, str.c_str());
            mAudioAnalytics.mStatsdLog.log("%s", str.c_str());
            mAudioAnalytics.mStatsdLog->log(
                    android::util::MEDIAMETRICS_AUDIODEVICECONNECTION_REPORTED, str);
        }
    }
}
@@ -899,7 +891,8 @@ void AudioAnalytics::DeviceConnection::expire() {
                    , /* connection_count */ 1
                    );
            ALOGV("%s: statsd %s", __func__, str.c_str());
            mAudioAnalytics.mStatsdLog.log("%s", str.c_str());
            mAudioAnalytics.mStatsdLog->log(
                    android::util::MEDIAMETRICS_AUDIODEVICECONNECTION_REPORTED, str);
        }
        return;
    }
@@ -925,7 +918,8 @@ void AudioAnalytics::DeviceConnection::expire() {
                , /* connection_count */ 1
                );
        ALOGV("%s: statsd %s", __func__, str.c_str());
        mAudioAnalytics.mStatsdLog.log("%s", str.c_str());
        mAudioAnalytics.mStatsdLog->log(
                android::util::MEDIAMETRICS_AUDIODEVICECONNECTION_REPORTED, str);
    }
}

@@ -1065,7 +1059,7 @@ void AudioAnalytics::AAudioStreamInfo::endAAudioStream(
        ss << " " << fieldsStr;
        std::string str = ss.str();
        ALOGV("%s: statsd %s", __func__, str.c_str());
        mAudioAnalytics.mStatsdLog.log("%s", str.c_str());
        mAudioAnalytics.mStatsdLog->log(android::util::MEDIAMETRICS_AAUDIOSTREAM_REPORTED, str);
    }
}

+4 −5
Original line number Diff line number Diff line
@@ -17,10 +17,10 @@
#pragma once

#include <android-base/thread_annotations.h>
#include <audio_utils/SimpleLog.h>
#include "AnalyticsActions.h"
#include "AnalyticsState.h"
#include "AudioPowerUsage.h"
#include "StatsdLog.h"
#include "TimedAction.h"
#include "Wrap.h"

@@ -32,7 +32,7 @@ class AudioAnalytics
    friend AudioPowerUsage;

public:
    AudioAnalytics();
    explicit AudioAnalytics(const std::shared_ptr<StatsdLog>& statsdLog);
    ~AudioAnalytics();

    /**
@@ -122,8 +122,7 @@ private:
    SharedPtrWrap<AnalyticsState> mPreviousAnalyticsState;

    TimedAction mTimedAction; // locked internally

    SimpleLog mStatsdLog{16 /* log lines */}; // locked internally
    const std::shared_ptr<StatsdLog> mStatsdLog; // locked internally, ok for multiple threads.

    // DeviceUse is a nested class which handles audio device usage accounting.
    // We define this class at the end to ensure prior variables all properly constructed.
@@ -212,7 +211,7 @@ private:
        AudioAnalytics &mAudioAnalytics;
    } mAAudioStreamInfo{*this};

    AudioPowerUsage mAudioPowerUsage{this};
    AudioPowerUsage mAudioPowerUsage;
};

} // namespace android::mediametrics
+23 −9
Original line number Diff line number Diff line
@@ -127,14 +127,13 @@ int32_t AudioPowerUsage::deviceFromStringPairs(const std::string& device_strings
    return deviceMask;
}

/* static */
void AudioPowerUsage::sendItem(const std::shared_ptr<const mediametrics::Item>& item)
void AudioPowerUsage::sendItem(const std::shared_ptr<const mediametrics::Item>& item) const
{
    int32_t type;
    if (!item->getInt32(AUDIO_POWER_USAGE_PROP_TYPE, &type)) return;

    int32_t device;
    if (!item->getInt32(AUDIO_POWER_USAGE_PROP_DEVICE, &device)) return;
    int32_t audio_device;
    if (!item->getInt32(AUDIO_POWER_USAGE_PROP_DEVICE, &audio_device)) return;

    int64_t duration_ns;
    if (!item->getInt64(AUDIO_POWER_USAGE_PROP_DURATION_NS, &duration_ns)) return;
@@ -142,11 +141,24 @@ void AudioPowerUsage::sendItem(const std::shared_ptr<const mediametrics::Item>&
    double volume;
    if (!item->getDouble(AUDIO_POWER_USAGE_PROP_VOLUME, &volume)) return;

    (void)android::util::stats_write(android::util::AUDIO_POWER_USAGE_DATA_REPORTED,
                                         device,
                                         (int32_t)(duration_ns / NANOS_PER_SECOND),
                                         (float)volume,
    const int32_t duration_secs = (int32_t)(duration_ns / NANOS_PER_SECOND);
    const float average_volume = (float)volume;
    const int result = android::util::stats_write(android::util::AUDIO_POWER_USAGE_DATA_REPORTED,
                                         audio_device,
                                         duration_secs,
                                         average_volume,
                                         type);

    std::stringstream log;
    log << "result:" << result << " {"
            << " mediametrics_audio_power_usage_data_reported:"
            << android::util::AUDIO_POWER_USAGE_DATA_REPORTED
            << " audio_device:" << audio_device
            << " duration_secs:" << duration_secs
            << " average_volume:" << average_volume
            << " type:" << type
            << " }";
    mStatsdLog->log(android::util::AUDIO_POWER_USAGE_DATA_REPORTED, log.str());
}

bool AudioPowerUsage::saveAsItem_l(
@@ -360,8 +372,10 @@ void AudioPowerUsage::checkCreatePatch(const std::shared_ptr<const mediametrics:
    mPrimaryDevice = device;
}

AudioPowerUsage::AudioPowerUsage(AudioAnalytics *audioAnalytics)
AudioPowerUsage::AudioPowerUsage(
        AudioAnalytics *audioAnalytics, const std::shared_ptr<StatsdLog>& statsdLog)
    : mAudioAnalytics(audioAnalytics)
    , mStatsdLog(statsdLog)
    , mDisabled(property_get_bool(PROP_AUDIO_METRICS_DISABLED, AUDIO_METRICS_DISABLED_DEFAULT))
    , mIntervalHours(property_get_int32(PROP_AUDIO_METRICS_INTERVAL_HR, INTERVAL_HR_DEFAULT))
{
+5 −2
Original line number Diff line number Diff line
@@ -22,13 +22,15 @@
#include <mutex>
#include <thread>

#include "StatsdLog.h"

namespace android::mediametrics {

class AudioAnalytics;

class AudioPowerUsage {
public:
    explicit AudioPowerUsage(AudioAnalytics *audioAnalytics);
    AudioPowerUsage(AudioAnalytics *audioAnalytics, const std::shared_ptr<StatsdLog>& statsdLog);
    ~AudioPowerUsage();

    void checkTrackRecord(const std::shared_ptr<const mediametrics::Item>& item, bool isTrack);
@@ -83,12 +85,13 @@ public:
private:
    bool saveAsItem_l(int32_t device, int64_t duration, int32_t type, double average_vol)
         REQUIRES(mLock);
    static void sendItem(const std::shared_ptr<const mediametrics::Item>& item);
    void sendItem(const std::shared_ptr<const mediametrics::Item>& item) const;
    void collect();
    bool saveAsItems_l(int32_t device, int64_t duration, int32_t type, double average_vol)
         REQUIRES(mLock);

    AudioAnalytics * const mAudioAnalytics;
    const std::shared_ptr<StatsdLog> mStatsdLog;  // mStatsdLog is internally locked
    const bool mDisabled;
    const int32_t mIntervalHours;

+6 −1
Original line number Diff line number Diff line
@@ -206,7 +206,7 @@ status_t MediaMetricsService::submitInternal(mediametrics::Item *item, bool rele

    (void)mAudioAnalytics.submit(sitem, isTrusted);

    (void)dump2Statsd(sitem);  // failure should be logged in function.
    (void)dump2Statsd(sitem, mStatsdLog);  // failure should be logged in function.
    saveItem(sitem);
    return NO_ERROR;
}
@@ -308,6 +308,11 @@ status_t MediaMetricsService::dump(int fd, const Vector<String16>& args)
            if (lines == linesToDump) {
                result << "-- some lines may be truncated --\n";
            }

            // Dump the statsd atoms we sent out.
            result << "Statsd atoms:\n"
                   << mStatsdLog->dumpToString("  " /* prefix */,
                           all ? STATSD_LOG_LINES_MAX : STATSD_LOG_LINES_DUMP);
        }
    }
    const std::string str = result.str();
Loading