blob: 01c60c98fb0c9179c079e28c52de97d94b9bf100 [file] [log] [blame]
Andy Hung459a2a32017-03-20 09:24:53 -07001/*
2 * Copyright 2017 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17// #define LOG_NDEBUG 0
18#define LOG_TAG "audio_utils_PowerLog"
19#include <log/log.h>
20
21#include <algorithm>
22#include <iomanip>
23#include <math.h>
24#include <sstream>
25#include <stdint.h>
Tri Voa6bddef2017-06-27 09:54:36 -070026#include <unistd.h>
Eric Tan570b7442018-06-22 09:24:22 -070027#include <vector>
Andy Hung459a2a32017-03-20 09:24:53 -070028
29#include <audio_utils/clock.h>
Eric Tan570b7442018-06-22 09:24:22 -070030#include <audio_utils/LogPlot.h>
Andy Hung459a2a32017-03-20 09:24:53 -070031#include <audio_utils/power.h>
32#include <audio_utils/PowerLog.h>
33
34namespace android {
35
36// TODO move to separate file
37template <typename T, size_t N>
38constexpr size_t array_size(const T(&)[N])
39{
40 return N;
41}
42
43PowerLog::PowerLog(uint32_t sampleRate,
44 uint32_t channelCount,
45 audio_format_t format,
46 size_t entries,
47 size_t framesPerEntry)
48 : mCurrentTime(0)
49 , mCurrentEnergy(0)
50 , mCurrentFrames(0)
51 , mIdx(0)
52 , mConsecutiveZeroes(0)
53 , mSampleRate(sampleRate)
54 , mChannelCount(channelCount)
55 , mFormat(format)
56 , mFramesPerEntry(framesPerEntry)
57 , mEntries(entries)
58{
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);
62}
63
64void PowerLog::log(const void *buffer, size_t frames, int64_t nowNs)
65{
66 std::lock_guard<std::mutex> guard(mLock);
67
68 const size_t bytes_per_sample = audio_bytes_per_sample(mFormat);
69 while (frames > 0) {
70 // check partial computation
71 size_t required = mFramesPerEntry - mCurrentFrames;
72 size_t process = std::min(required, frames);
73
74 if (mCurrentTime == 0) {
75 mCurrentTime = nowNs;
76 }
77 mCurrentEnergy +=
78 audio_utils_compute_energy_mono(buffer, mFormat, process * mChannelCount);
79 mCurrentFrames += process;
80
81 ALOGV("nowNs:%lld, required:%zu, process:%zu, mCurrentEnergy:%f, mCurrentFrames:%zu",
82 (long long)nowNs, required, process, mCurrentEnergy, mCurrentFrames);
83 if (process < required) {
84 return;
85 }
86
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.
93 }
94 } else {
95 mConsecutiveZeroes = 0;
96 mEntries[mIdx++] = std::make_pair(mCurrentTime, mCurrentEnergy);
97 ALOGV("writing %lld %f", (long long)mCurrentTime, mCurrentEnergy);
98 }
99 if (mIdx >= mEntries.size()) {
100 mIdx -= mEntries.size();
101 }
102 mCurrentTime = 0;
103 mCurrentEnergy = 0;
104 mCurrentFrames = 0;
105 frames -= process;
106 buffer = (const uint8_t *)buffer + mCurrentFrames * mChannelCount * bytes_per_sample;
107 }
108}
109
Andy Hung70f10242017-03-23 16:09:11 -0700110std::string PowerLog::dumpToString(const char *prefix, size_t lines, int64_t limitNs) const
Andy Hung459a2a32017-03-20 09:24:53 -0700111{
112 std::lock_guard<std::mutex> guard(mLock);
113
114 const size_t maxColumns = 10;
115 const size_t numberOfEntries = mEntries.size();
116 if (lines == 0) lines = SIZE_MAX;
117
118 // compute where to start logging
119 enum {
120 AT_END,
121 IN_SIGNAL,
122 } state = IN_SIGNAL;
123 size_t count = 1;
124 size_t column = 0;
125 size_t nonzeros = 0;
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;
131
132 if (state == AT_END) {
133 if (energy == 0.f) {
134 ALOGV("two zeroes detected");
135 break; // normally single zero terminated - two zeroes means no more data.
136 }
137 state = IN_SIGNAL;
138 } else { // IN_SIGNAL
139 if (energy == 0.f) {
140 if (column != 0) {
141 column = 0;
142 ++count;
143 }
144 state = AT_END;
145 continue;
146 }
147 }
Andy Hung70f10242017-03-23 16:09:11 -0700148 if (column == 0 && time < limitNs) {
Andy Hung459a2a32017-03-20 09:24:53 -0700149 break;
150 }
151 ++nonzeros;
152 if (++column == maxColumns) {
153 column = 0;
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.
157 ++count;
158 }
159 }
160 if (offset > 0) {
161 --offset;
162 }
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;
167 if (nonzeros == 0) {
Andy Hung70f10242017-03-23 16:09:11 -0700168 ss << prefix << "Signal power history: (none)\n";
Andy Hung459a2a32017-03-20 09:24:53 -0700169 } else {
Eric Tan570b7442018-06-22 09:24:22 -0700170 // First value is power, second value is whether value is start of
171 // a new time stamp.
172 std::vector<std::pair<float, bool>> plotEntries;
Andy Hung70f10242017-03-23 16:09:11 -0700173 ss << prefix << "Signal power history:\n";
Andy Hung459a2a32017-03-20 09:24:53 -0700174
175 size_t column = 0;
176 bool first = true;
177 bool start = false;
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;
183
184 if (energy == 0.f) {
185 if (!first) {
186 ss << " ] sum(" << audio_utils_power_from_energy(cumulative) << ")";
Eric Tan570b7442018-06-22 09:24:22 -0700187 // 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);
192 }
Andy Hung459a2a32017-03-20 09:24:53 -0700193 }
194 cumulative = 0.f;
195 column = 0;
196 start = true;
197 continue;
198 }
199 if (column == 0) {
200 // print time if at start of column
Andy Hung459a2a32017-03-20 09:24:53 -0700201 if (!first) {
202 ss << "\n";
203 }
Andy Hungc1eb1862017-03-23 16:13:53 -0700204 ss << prefix << " " << audio_utils_time_string_from_ns(time).time
205 << (start ? ": [ ": ": ");
Andy Hung459a2a32017-03-20 09:24:53 -0700206 first = false;
207 start = false;
208 } else {
209 ss << " ";
210 }
211 if (++column >= maxColumns) {
212 column = 0;
213 }
214
215 cumulative += energy;
216 // convert energy to power and print
217 const float power =
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);
Eric Tan570b7442018-06-22 09:24:22 -0700221 // 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);
Andy Hung459a2a32017-03-20 09:24:53 -0700224 }
Eric Tan570b7442018-06-22 09:24:22 -0700225 ss << "\n" << audio_utils_log_plot(plotEntries.begin(), plotEntries.end());
Andy Hung459a2a32017-03-20 09:24:53 -0700226 ss << "\n";
227 }
228 return ss.str();
229}
230
Andy Hung70f10242017-03-23 16:09:11 -0700231status_t PowerLog::dump(int fd, const char *prefix, size_t lines, int64_t limitNs) const
Andy Hung459a2a32017-03-20 09:24:53 -0700232{
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.
Andy Hung70f10242017-03-23 16:09:11 -0700237 const std::string s = dumpToString(prefix, lines, limitNs);
Andy Hung459a2a32017-03-20 09:24:53 -0700238 if (s.size() > 0 && write(fd, s.c_str(), s.size()) < 0) {
239 return -errno;
240 }
241 return NO_ERROR;
242}
243
244} // namespace android
245
246using namespace android;
247
248power_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)
250{
251 if (!audio_utils_is_compute_power_format_supported(format)) {
252 return nullptr;
253 }
254 return reinterpret_cast<power_log_t *>
Andy Hung70f10242017-03-23 16:09:11 -0700255 (new(std::nothrow)
256 PowerLog(sample_rate, channel_count, format, entries, frames_per_entry));
Andy Hung459a2a32017-03-20 09:24:53 -0700257}
258
259void power_log_log(power_log_t *power_log,
260 const void *buffer, size_t frames, int64_t now_ns)
261{
262 if (power_log == nullptr) {
263 return;
264 }
265 reinterpret_cast<PowerLog *>(power_log)->log(buffer, frames, now_ns);
266}
267
Andy Hung70f10242017-03-23 16:09:11 -0700268int power_log_dump(
269 power_log_t *power_log, int fd, const char *prefix, size_t lines, int64_t limit_ns)
Andy Hung459a2a32017-03-20 09:24:53 -0700270{
271 if (power_log == nullptr) {
272 return BAD_VALUE;
273 }
Andy Hung70f10242017-03-23 16:09:11 -0700274 return reinterpret_cast<PowerLog *>(power_log)->dump(fd, prefix, lines, limit_ns);
Andy Hung459a2a32017-03-20 09:24:53 -0700275}
276
277void power_log_destroy(power_log_t *power_log)
278{
279 delete reinterpret_cast<PowerLog *>(power_log);
280}