From 3213c5e868e93241ac6af84c10f32d1f9e251392 Mon Sep 17 00:00:00 2001 From: Andy Hung Date: Thu, 23 Mar 2017 16:09:11 -0700 Subject: [PATCH] PowerLog: Add dump prefix to powerlog Test: unit test and dumpsys media.audio_flinger Bug: 30572472 Change-Id: I7d53b399cbaa22195b386422efe789250624bb3a (cherry picked from commit 70f10246d5436875a22650a53bf33b3241157a31) --- audio_utils/PowerLog.cpp | 22 ++++++++++++---------- audio_utils/include/audio_utils/PowerLog.h | 12 +++++++----- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/audio_utils/PowerLog.cpp b/audio_utils/PowerLog.cpp index 64e3c650..c4ccbc4f 100644 --- a/audio_utils/PowerLog.cpp +++ b/audio_utils/PowerLog.cpp @@ -104,7 +104,7 @@ void PowerLog::log(const void *buffer, size_t frames, int64_t nowNs) } } -std::string PowerLog::dumpToString(size_t lines, int64_t limitNs) const +std::string PowerLog::dumpToString(const char *prefix, size_t lines, int64_t limitNs) const { std::lock_guard guard(mLock); @@ -142,7 +142,7 @@ std::string PowerLog::dumpToString(size_t lines, int64_t limitNs) const continue; } } - if (column == 0 && time <= limitNs) { + if (column == 0 && time < limitNs) { break; } ++nonzeros; @@ -162,9 +162,9 @@ std::string PowerLog::dumpToString(size_t lines, int64_t limitNs) const ss << std::fixed << std::setprecision(1); // ss << std::scientific; if (nonzeros == 0) { - ss << " Signal power history: (none)\n"; + ss << prefix << "Signal power history: (none)\n"; } else { - ss << " Signal power history:\n"; + ss << prefix << "Signal power history:\n"; size_t column = 0; bool first = true; @@ -191,7 +191,7 @@ std::string PowerLog::dumpToString(size_t lines, int64_t limitNs) const if (!first) { ss << "\n"; } - ss << timeinfo << (start ? ": [ ": ": "); + ss << prefix << " " << timeinfo << (start ? ": [ ": ": "); first = false; start = false; } else { @@ -213,13 +213,13 @@ std::string PowerLog::dumpToString(size_t lines, int64_t limitNs) const return ss.str(); } -status_t PowerLog::dump(int fd, size_t lines, int64_t limitNs) const +status_t PowerLog::dump(int fd, const char *prefix, size_t lines, int64_t limitNs) const { // Since dumpToString and write are thread safe, this function // is conceptually thread-safe but simultaneous calls to dump // by different threads to the same file descriptor may not write // the two logs in time order. - const std::string s = dumpToString(lines, limitNs); + const std::string s = dumpToString(prefix, lines, limitNs); if (s.size() > 0 && write(fd, s.c_str(), s.size()) < 0) { return -errno; } @@ -237,7 +237,8 @@ power_log_t *power_log_create(uint32_t sample_rate, return nullptr; } return reinterpret_cast - (new PowerLog(sample_rate, channel_count, format, entries, frames_per_entry)); + (new(std::nothrow) + PowerLog(sample_rate, channel_count, format, entries, frames_per_entry)); } void power_log_log(power_log_t *power_log, @@ -249,12 +250,13 @@ void power_log_log(power_log_t *power_log, reinterpret_cast(power_log)->log(buffer, frames, now_ns); } -int power_log_dump(power_log_t *power_log, int fd, size_t lines, int64_t limit_ns) +int power_log_dump( + power_log_t *power_log, int fd, const char *prefix, size_t lines, int64_t limit_ns) { if (power_log == nullptr) { return BAD_VALUE; } - return reinterpret_cast(power_log)->dump(fd, lines, limit_ns); + return reinterpret_cast(power_log)->dump(fd, prefix, lines, limit_ns); } void power_log_destroy(power_log_t *power_log) diff --git a/audio_utils/include/audio_utils/PowerLog.h b/audio_utils/include/audio_utils/PowerLog.h index 497bb3c9..1f5a8b7c 100644 --- a/audio_utils/include/audio_utils/PowerLog.h +++ b/audio_utils/include/audio_utils/PowerLog.h @@ -21,7 +21,7 @@ #include #include - +#include #include namespace android { @@ -51,7 +51,7 @@ public: * \param entries total number of energy entries "bins" to use. * \param framesPerEntry total number of audio frames used in each entry. */ - explicit PowerLog(uint32_t sampleRate, + PowerLog(uint32_t sampleRate, uint32_t channelCount, audio_format_t format, size_t entries, @@ -73,7 +73,8 @@ public: * \param limitNs limit dump to data more recent than limitNs (0 disables). * \return the std::string for the log. */ - std::string dumpToString(size_t lines = 0, int64_t limitNs = 0) const; + std::string dumpToString( + const char *prefix = "", size_t lines = 0, int64_t limitNs = 0) const; /** * \brief Dumps the log to a raw file descriptor. @@ -84,7 +85,7 @@ public: * \return * NO_ERROR on success or a negative number (-errno) on failure of write(). */ - status_t dump(int fd, size_t lines = 0, int64_t limitNs = 0) const; + status_t dump(int fd, const char *prefix = "", size_t lines = 0, int64_t limitNs = 0) const; private: mutable std::mutex mLock; // monitor mutex @@ -148,7 +149,8 @@ void power_log_log(power_log_t *power_log, const void *buffer, size_t frames, in * NO_ERROR on success or a negative number (-errno) on failure of write(). * if power_log is NULL, BAD_VALUE is returned. */ -int power_log_dump(power_log_t *power_log, int fd, size_t lines, int64_t limit_ns); +int power_log_dump( + power_log_t *power_log, int fd, const char *prefix, size_t lines, int64_t limit_ns); /** * \brief Destroys the power log object. -- 2.11.0