2 * Copyright 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // #define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_PowerLog"
29 #include <audio_utils/clock.h>
30 #include <audio_utils/LogPlot.h>
31 #include <audio_utils/power.h>
32 #include <audio_utils/PowerLog.h>
36 // TODO move to separate file
37 template <typename T, size_t N>
38 constexpr size_t array_size(const T(&)[N])
43 PowerLog::PowerLog(uint32_t sampleRate,
44 uint32_t channelCount,
45 audio_format_t format,
47 size_t framesPerEntry)
52 , mConsecutiveZeroes(0)
53 , mSampleRate(sampleRate)
54 , mChannelCount(channelCount)
56 , mFramesPerEntry(framesPerEntry)
59 (void)mSampleRate; // currently unused, for future use
60 LOG_ALWAYS_FATAL_IF(!audio_utils_is_compute_power_format_supported(format),
61 "unsupported format: %#x", format);
64 void PowerLog::log(const void *buffer, size_t frames, int64_t nowNs)
66 std::lock_guard<std::mutex> guard(mLock);
68 const size_t bytes_per_sample = audio_bytes_per_sample(mFormat);
70 // check partial computation
71 size_t required = mFramesPerEntry - mCurrentFrames;
72 size_t process = std::min(required, frames);
74 if (mCurrentTime == 0) {
78 audio_utils_compute_energy_mono(buffer, mFormat, process * mChannelCount);
79 mCurrentFrames += process;
81 ALOGV("nowNs:%lld, required:%zu, process:%zu, mCurrentEnergy:%f, mCurrentFrames:%zu",
82 (long long)nowNs, required, process, mCurrentEnergy, mCurrentFrames);
83 if (process < required) {
87 // We store the data as normalized energy per sample. The energy sequence is
88 // zero terminated. Consecutive zeroes are ignored.
89 if (mCurrentEnergy == 0.f) {
90 if (mConsecutiveZeroes++ == 0) {
91 mEntries[mIdx++] = std::make_pair(nowNs, 0.f);
92 // zero terminate the signal sequence.
95 mConsecutiveZeroes = 0;
96 mEntries[mIdx++] = std::make_pair(mCurrentTime, mCurrentEnergy);
97 ALOGV("writing %lld %f", (long long)mCurrentTime, mCurrentEnergy);
99 if (mIdx >= mEntries.size()) {
100 mIdx -= mEntries.size();
106 buffer = (const uint8_t *)buffer + mCurrentFrames * mChannelCount * bytes_per_sample;
110 std::string PowerLog::dumpToString(const char *prefix, size_t lines, int64_t limitNs) const
112 std::lock_guard<std::mutex> guard(mLock);
114 const size_t maxColumns = 10;
115 const size_t numberOfEntries = mEntries.size();
116 if (lines == 0) lines = SIZE_MAX;
118 // compute where to start logging
126 ssize_t offset; // TODO doesn't dump if # entries exceeds SSIZE_MAX
127 for (offset = 0; offset < (ssize_t)numberOfEntries && count < lines; ++offset) {
128 const size_t idx = (mIdx + numberOfEntries - offset - 1) % numberOfEntries; // reverse direction
129 const int64_t time = mEntries[idx].first;
130 const float energy = mEntries[idx].second;
132 if (state == AT_END) {
134 ALOGV("two zeroes detected");
135 break; // normally single zero terminated - two zeroes means no more data.
138 } else { // IN_SIGNAL
148 if (column == 0 && time < limitNs) {
152 if (++column == maxColumns) {
154 // TODO ideally we would peek the previous entry to see if it is 0
155 // to ensure we properly put in a starting signal bracket.
156 // We don't do that because it would complicate the logic here.
163 // We accumulate the log info into a string, and write to the fd once.
164 std::stringstream ss;
165 ss << std::fixed << std::setprecision(1);
166 // ss << std::scientific;
168 ss << prefix << "Signal power history: (none)\n";
170 // First value is power, second value is whether value is start of
172 std::vector<std::pair<float, bool>> plotEntries;
173 ss << prefix << "Signal power history:\n";
178 float cumulative = 0.f;
179 for (; offset >= 0; --offset) {
180 const size_t idx = (mIdx + numberOfEntries - offset - 1) % numberOfEntries;
181 const int64_t time = mEntries[idx].first;
182 const float energy = mEntries[idx].second;
186 ss << " ] sum(" << audio_utils_power_from_energy(cumulative) << ")";
187 // Add an entry to denote the start of a new time stamp series.
188 if (!plotEntries.empty()) {
189 // First value should be between min and max of all graph entries
190 // so that it doesn't mess with y-axis scaling.
191 plotEntries.emplace_back(plotEntries.back().first, true);
200 // print time if at start of column
204 ss << prefix << " " << audio_utils_time_string_from_ns(time).time
205 << (start ? ": [ ": ": ");
211 if (++column >= maxColumns) {
215 cumulative += energy;
216 // convert energy to power and print
218 audio_utils_power_from_energy(energy / (mChannelCount * mFramesPerEntry));
219 ss << std::setw(6) << power;
220 ALOGV("state: %d %lld %f", state, (long long)time, power);
221 // Add an entry to the ASCII art power log graph.
222 // false indicates the value doesn't have a new series time stamp.
223 plotEntries.emplace_back(power, false);
225 ss << "\n" << audio_utils_log_plot(plotEntries.begin(), plotEntries.end());
231 status_t PowerLog::dump(int fd, const char *prefix, size_t lines, int64_t limitNs) const
233 // Since dumpToString and write are thread safe, this function
234 // is conceptually thread-safe but simultaneous calls to dump
235 // by different threads to the same file descriptor may not write
236 // the two logs in time order.
237 const std::string s = dumpToString(prefix, lines, limitNs);
238 if (s.size() > 0 && write(fd, s.c_str(), s.size()) < 0) {
244 } // namespace android
246 using namespace android;
248 power_log_t *power_log_create(uint32_t sample_rate,
249 uint32_t channel_count, audio_format_t format, size_t entries, size_t frames_per_entry)
251 if (!audio_utils_is_compute_power_format_supported(format)) {
254 return reinterpret_cast<power_log_t *>
256 PowerLog(sample_rate, channel_count, format, entries, frames_per_entry));
259 void power_log_log(power_log_t *power_log,
260 const void *buffer, size_t frames, int64_t now_ns)
262 if (power_log == nullptr) {
265 reinterpret_cast<PowerLog *>(power_log)->log(buffer, frames, now_ns);
269 power_log_t *power_log, int fd, const char *prefix, size_t lines, int64_t limit_ns)
271 if (power_log == nullptr) {
274 return reinterpret_cast<PowerLog *>(power_log)->dump(fd, prefix, lines, limit_ns);
277 void power_log_destroy(power_log_t *power_log)
279 delete reinterpret_cast<PowerLog *>(power_log);