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"
28 #include <audio_utils/clock.h>
29 #include <audio_utils/power.h>
30 #include <audio_utils/PowerLog.h>
34 // TODO move to separate file
35 template <typename T, size_t N>
36 constexpr size_t array_size(const T(&)[N])
41 PowerLog::PowerLog(uint32_t sampleRate,
42 uint32_t channelCount,
43 audio_format_t format,
45 size_t framesPerEntry)
50 , mConsecutiveZeroes(0)
51 , mSampleRate(sampleRate)
52 , mChannelCount(channelCount)
54 , mFramesPerEntry(framesPerEntry)
57 (void)mSampleRate; // currently unused, for future use
58 LOG_ALWAYS_FATAL_IF(!audio_utils_is_compute_power_format_supported(format),
59 "unsupported format: %#x", format);
62 void PowerLog::log(const void *buffer, size_t frames, int64_t nowNs)
64 std::lock_guard<std::mutex> guard(mLock);
66 const size_t bytes_per_sample = audio_bytes_per_sample(mFormat);
68 // check partial computation
69 size_t required = mFramesPerEntry - mCurrentFrames;
70 size_t process = std::min(required, frames);
72 if (mCurrentTime == 0) {
76 audio_utils_compute_energy_mono(buffer, mFormat, process * mChannelCount);
77 mCurrentFrames += process;
79 ALOGV("nowNs:%lld, required:%zu, process:%zu, mCurrentEnergy:%f, mCurrentFrames:%zu",
80 (long long)nowNs, required, process, mCurrentEnergy, mCurrentFrames);
81 if (process < required) {
85 // We store the data as normalized energy per sample. The energy sequence is
86 // zero terminated. Consecutive zeroes are ignored.
87 if (mCurrentEnergy == 0.f) {
88 if (mConsecutiveZeroes++ == 0) {
89 mEntries[mIdx++] = std::make_pair(nowNs, 0.f);
90 // zero terminate the signal sequence.
93 mConsecutiveZeroes = 0;
94 mEntries[mIdx++] = std::make_pair(mCurrentTime, mCurrentEnergy);
95 ALOGV("writing %lld %f", (long long)mCurrentTime, mCurrentEnergy);
97 if (mIdx >= mEntries.size()) {
98 mIdx -= mEntries.size();
104 buffer = (const uint8_t *)buffer + mCurrentFrames * mChannelCount * bytes_per_sample;
108 std::string PowerLog::dumpToString(const char *prefix, size_t lines, int64_t limitNs) const
110 std::lock_guard<std::mutex> guard(mLock);
112 const size_t maxColumns = 10;
113 const size_t numberOfEntries = mEntries.size();
114 if (lines == 0) lines = SIZE_MAX;
116 // compute where to start logging
124 ssize_t offset; // TODO doesn't dump if # entries exceeds SSIZE_MAX
125 for (offset = 0; offset < (ssize_t)numberOfEntries && count < lines; ++offset) {
126 const size_t idx = (mIdx + numberOfEntries - offset - 1) % numberOfEntries; // reverse direction
127 const int64_t time = mEntries[idx].first;
128 const float energy = mEntries[idx].second;
130 if (state == AT_END) {
132 ALOGV("two zeroes detected");
133 break; // normally single zero terminated - two zeroes means no more data.
136 } else { // IN_SIGNAL
146 if (column == 0 && time < limitNs) {
150 if (++column == maxColumns) {
152 // TODO ideally we would peek the previous entry to see if it is 0
153 // to ensure we properly put in a starting signal bracket.
154 // We don't do that because it would complicate the logic here.
161 // We accumulate the log info into a string, and write to the fd once.
162 std::stringstream ss;
163 ss << std::fixed << std::setprecision(1);
164 // ss << std::scientific;
166 ss << prefix << "Signal power history: (none)\n";
168 ss << prefix << "Signal power history:\n";
173 float cumulative = 0.f;
174 for (; offset >= 0; --offset) {
175 const size_t idx = (mIdx + numberOfEntries - offset - 1) % numberOfEntries;
176 const int64_t time = mEntries[idx].first;
177 const float energy = mEntries[idx].second;
181 ss << " ] sum(" << audio_utils_power_from_energy(cumulative) << ")";
189 // print time if at start of column
193 ss << prefix << " " << audio_utils_time_string_from_ns(time).time
194 << (start ? ": [ ": ": ");
200 if (++column >= maxColumns) {
204 cumulative += energy;
205 // convert energy to power and print
207 audio_utils_power_from_energy(energy / (mChannelCount * mFramesPerEntry));
208 ss << std::setw(6) << power;
209 ALOGV("state: %d %lld %f", state, (long long)time, power);
216 status_t PowerLog::dump(int fd, const char *prefix, size_t lines, int64_t limitNs) const
218 // Since dumpToString and write are thread safe, this function
219 // is conceptually thread-safe but simultaneous calls to dump
220 // by different threads to the same file descriptor may not write
221 // the two logs in time order.
222 const std::string s = dumpToString(prefix, lines, limitNs);
223 if (s.size() > 0 && write(fd, s.c_str(), s.size()) < 0) {
229 } // namespace android
231 using namespace android;
233 power_log_t *power_log_create(uint32_t sample_rate,
234 uint32_t channel_count, audio_format_t format, size_t entries, size_t frames_per_entry)
236 if (!audio_utils_is_compute_power_format_supported(format)) {
239 return reinterpret_cast<power_log_t *>
241 PowerLog(sample_rate, channel_count, format, entries, frames_per_entry));
244 void power_log_log(power_log_t *power_log,
245 const void *buffer, size_t frames, int64_t now_ns)
247 if (power_log == nullptr) {
250 reinterpret_cast<PowerLog *>(power_log)->log(buffer, frames, now_ns);
254 power_log_t *power_log, int fd, const char *prefix, size_t lines, int64_t limit_ns)
256 if (power_log == nullptr) {
259 return reinterpret_cast<PowerLog *>(power_log)->dump(fd, prefix, lines, limit_ns);
262 void power_log_destroy(power_log_t *power_log)
264 delete reinterpret_cast<PowerLog *>(power_log);