blob: 96a33092304432d403aa0d65a20d48b0145819b4 [file] [log] [blame]
Andy Hungc2b11cb2020-04-22 09:04:01 -07001/*
2 * Copyright (C) 2020 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#ifndef ANDROID_AUDIO_TRACKMETRICS_H
18#define ANDROID_AUDIO_TRACKMETRICS_H
19
Kunal Malhotra3be68902023-02-28 22:03:15 +000020#include <binder/IActivityManager.h>
21#include <binder/IPCThreadState.h>
22#include <binder/IServiceManager.h>
Andy Hungc2b11cb2020-04-22 09:04:01 -070023#include <mutex>
24
25namespace android {
26
27/**
28 * TrackMetrics handles the AudioFlinger track metrics.
29 *
30 * We aggregate metrics for a particular device for proper analysis.
31 * This includes power, performance, and usage metrics.
32 *
33 * This class is thread-safe with a lock for safety. There is no risk of deadlock
34 * as this class only executes external one-way calls in Mediametrics and does not
35 * call any other AudioFlinger class.
36 *
37 * Terminology:
38 * An AudioInterval is a contiguous playback segment.
39 * An AudioIntervalGroup is a group of continuous playback segments on the same device.
40 *
41 * We currently deliver metrics based on an AudioIntervalGroup.
42 */
43class TrackMetrics final {
Kunal Malhotra3be68902023-02-28 22:03:15 +000044
45
Andy Hungc2b11cb2020-04-22 09:04:01 -070046public:
Kunal Malhotra3be68902023-02-28 22:03:15 +000047 TrackMetrics(std::string metricsId, bool isOut, int clientUid)
Andy Hungc2b11cb2020-04-22 09:04:01 -070048 : mMetricsId(std::move(metricsId))
49 , mIsOut(isOut)
Kunal Malhotra3be68902023-02-28 22:03:15 +000050 , mUid(clientUid)
Andy Hungc2b11cb2020-04-22 09:04:01 -070051 {} // we don't log a constructor item, we wait for more info in logConstructor().
52
53 ~TrackMetrics() {
54 logEndInterval();
55 std::lock_guard l(mLock);
56 deliverCumulativeMetrics(AMEDIAMETRICS_PROP_EVENT_VALUE_ENDAUDIOINTERVALGROUP);
57 // we don't log a destructor item here.
58 }
59
60 // Called under the following circumstances
61 // 1) when we are added to the Thread
62 // 2) when we have a createPatch in the Thread.
63 void logBeginInterval(const std::string& devices) {
64 std::lock_guard l(mLock);
65 if (mDevices != devices) {
66 deliverCumulativeMetrics(AMEDIAMETRICS_PROP_EVENT_VALUE_ENDAUDIOINTERVALGROUP);
67 mDevices = devices;
68 resetIntervalGroupMetrics();
69 deliverDeviceMetrics(
70 AMEDIAMETRICS_PROP_EVENT_VALUE_BEGINAUDIOINTERVALGROUP, devices.c_str());
71 }
72 ++mIntervalCount;
Kunal Malhotra3be68902023-02-28 22:03:15 +000073 const auto& mActivityManager = getActivityManager();
74 if (mActivityManager) {
Kunal Malhotraae6d1602023-03-30 00:17:25 +000075 if (mIsOut) {
76 mActivityManager->logFgsApiBegin(AUDIO_API,
77 mUid,
78 IPCThreadState::self() -> getCallingPid());
79 } else {
80 mActivityManager->logFgsApiBegin(MICROPHONE_API,
81 mUid,
82 IPCThreadState::self() -> getCallingPid());
83 }
Kunal Malhotra3be68902023-02-28 22:03:15 +000084 }
Andy Hungc2b11cb2020-04-22 09:04:01 -070085 }
86
Andy Hung5837c7f2021-02-25 10:48:24 -080087 void logConstructor(pid_t creatorPid, uid_t creatorUid, int32_t internalTrackId,
Andy Hunga629bd12020-06-05 16:03:53 -070088 const std::string& traits = {},
Andy Hungea840382020-05-05 21:50:17 -070089 audio_stream_type_t streamType = AUDIO_STREAM_DEFAULT) const {
Andy Hungc2b11cb2020-04-22 09:04:01 -070090 // Once this item is logged by the server, the client can add properties.
91 // no lock required, all local or const variables.
Andy Hungea840382020-05-05 21:50:17 -070092 mediametrics::LogItem item(mMetricsId);
93 item.setPid(creatorPid)
Andy Hungc2b11cb2020-04-22 09:04:01 -070094 .setUid(creatorUid)
95 .set(AMEDIAMETRICS_PROP_ALLOWUID, (int32_t)creatorUid)
96 .set(AMEDIAMETRICS_PROP_EVENT,
Andy Hunga629bd12020-06-05 16:03:53 -070097 AMEDIAMETRICS_PROP_PREFIX_SERVER AMEDIAMETRICS_PROP_EVENT_VALUE_CTOR)
Andy Hung5837c7f2021-02-25 10:48:24 -080098 .set(AMEDIAMETRICS_PROP_INTERNALTRACKID, internalTrackId)
Andy Hunga629bd12020-06-05 16:03:53 -070099 .set(AMEDIAMETRICS_PROP_TRAITS, traits);
Andy Hungea840382020-05-05 21:50:17 -0700100 // log streamType from the service, since client doesn't know chosen streamType.
101 if (streamType != AUDIO_STREAM_DEFAULT) {
102 item.set(AMEDIAMETRICS_PROP_STREAMTYPE, toString(streamType).c_str());
103 }
104 item.record();
Andy Hungc2b11cb2020-04-22 09:04:01 -0700105 }
106
107 // Called when we are removed from the Thread.
108 void logEndInterval() {
109 std::lock_guard l(mLock);
Andy Hunga81a4b42022-05-19 19:24:51 -0700110 if (mLastVolumeChangeTimeNs != 0) {
111 logVolume_l(mVolume); // flush out the last volume.
112 mLastVolumeChangeTimeNs = 0;
Andy Hungc2b11cb2020-04-22 09:04:01 -0700113 }
Kunal Malhotra3be68902023-02-28 22:03:15 +0000114 const auto& mActivityManager = getActivityManager();
115 if (mActivityManager) {
Kunal Malhotraae6d1602023-03-30 00:17:25 +0000116 if (mIsOut) {
117 mActivityManager->logFgsApiEnd(AUDIO_API,
118 mUid,
119 IPCThreadState::self() -> getCallingPid());
120 } else {
121 mActivityManager->logFgsApiEnd(MICROPHONE_API,
122 mUid,
123 IPCThreadState::self() -> getCallingPid());
124 }
Kunal Malhotra3be68902023-02-28 22:03:15 +0000125 }
Andy Hungc2b11cb2020-04-22 09:04:01 -0700126 }
127
128 void logInvalidate() const {
129 // no lock required, all local or const variables.
130 mediametrics::LogItem(mMetricsId)
131 .set(AMEDIAMETRICS_PROP_EVENT,
132 AMEDIAMETRICS_PROP_EVENT_VALUE_INVALIDATE)
133 .record();
134 }
135
136 void logLatencyAndStartup(double latencyMs, double startupMs) {
137 mediametrics::LogItem(mMetricsId)
138 .set(AMEDIAMETRICS_PROP_LATENCYMS, latencyMs)
139 .set(AMEDIAMETRICS_PROP_STARTUPMS, startupMs)
140 .record();
141 std::lock_guard l(mLock);
142 mDeviceLatencyMs.add(latencyMs);
143 mDeviceStartupMs.add(startupMs);
144 }
145
Robert Lee36ac0412022-01-13 03:05:41 +0000146 void updateMinMaxVolume(int64_t durationNs, double deviceVolume) {
147 if (deviceVolume > mMaxVolume) {
148 mMaxVolume = deviceVolume;
149 mMaxVolumeDurationNs = durationNs;
150 } else if (deviceVolume == mMaxVolume) {
151 mMaxVolumeDurationNs += durationNs;
152 }
153 if (deviceVolume < mMinVolume) {
154 mMinVolume = deviceVolume;
155 mMinVolumeDurationNs = durationNs;
156 } else if (deviceVolume == mMinVolume) {
157 mMinVolumeDurationNs += durationNs;
158 }
159 }
160
Andy Hungc2b11cb2020-04-22 09:04:01 -0700161 // may be called multiple times during an interval
162 void logVolume(float volume) {
Andy Hungc2b11cb2020-04-22 09:04:01 -0700163 std::lock_guard l(mLock);
Andy Hunga81a4b42022-05-19 19:24:51 -0700164 logVolume_l(volume);
Andy Hungc2b11cb2020-04-22 09:04:01 -0700165 }
166
167 // Use absolute numbers returned by AudioTrackShared.
168 void logUnderruns(size_t count, size_t frames) {
169 std::lock_guard l(mLock);
170 mUnderrunCount = count;
171 mUnderrunFrames = frames;
172 // Consider delivering a message here (also be aware of excessive spam).
173 }
174
175private:
Andy Hunga81a4b42022-05-19 19:24:51 -0700176
Andy Hungc2b11cb2020-04-22 09:04:01 -0700177 // no lock required - all arguments and constants.
178 void deliverDeviceMetrics(const char *eventName, const char *devices) const {
179 mediametrics::LogItem(mMetricsId)
180 .set(AMEDIAMETRICS_PROP_EVENT, eventName)
181 .set(mIsOut ? AMEDIAMETRICS_PROP_OUTPUTDEVICES
182 : AMEDIAMETRICS_PROP_INPUTDEVICES, devices)
183 .record();
184 }
185
Andy Hunga81a4b42022-05-19 19:24:51 -0700186 void logVolume_l(float volume) REQUIRES(mLock) {
187 const int64_t timeNs = systemTime();
188 const int64_t durationNs = mLastVolumeChangeTimeNs == 0
189 ? 0 : timeNs - mLastVolumeChangeTimeNs;
190 if (durationNs > 0) {
191 // See West's algorithm for weighted averages
192 // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
193 mDeviceVolume += (mVolume - mDeviceVolume) * durationNs
194 / (durationNs + mDeviceTimeNs);
195 mDeviceTimeNs += durationNs;
196 mCumulativeTimeNs += durationNs;
197 }
198 updateMinMaxVolume(durationNs, mVolume); // always update.
199 mVolume = volume;
200 mLastVolumeChangeTimeNs = timeNs;
201 }
202
Andy Hungc2b11cb2020-04-22 09:04:01 -0700203 void deliverCumulativeMetrics(const char *eventName) const REQUIRES(mLock) {
204 if (mIntervalCount > 0) {
205 mediametrics::LogItem item(mMetricsId);
206 item.set(AMEDIAMETRICS_PROP_CUMULATIVETIMENS, mCumulativeTimeNs)
207 .set(AMEDIAMETRICS_PROP_DEVICETIMENS, mDeviceTimeNs)
208 .set(AMEDIAMETRICS_PROP_EVENT, eventName)
209 .set(AMEDIAMETRICS_PROP_INTERVALCOUNT, (int32_t)mIntervalCount);
210 if (mIsOut) {
Robert Lee36ac0412022-01-13 03:05:41 +0000211 item.set(AMEDIAMETRICS_PROP_DEVICEVOLUME, mDeviceVolume)
212 .set(AMEDIAMETRICS_PROP_DEVICEMAXVOLUMEDURATIONNS, mMaxVolumeDurationNs)
213 .set(AMEDIAMETRICS_PROP_DEVICEMAXVOLUME, mMaxVolume)
214 .set(AMEDIAMETRICS_PROP_DEVICEMINVOLUMEDURATIONNS, mMinVolumeDurationNs)
215 .set(AMEDIAMETRICS_PROP_DEVICEMINVOLUME, mMinVolume);
Andy Hungc2b11cb2020-04-22 09:04:01 -0700216 }
217 if (mDeviceLatencyMs.getN() > 0) {
218 item.set(AMEDIAMETRICS_PROP_DEVICELATENCYMS, mDeviceLatencyMs.getMean())
219 .set(AMEDIAMETRICS_PROP_DEVICESTARTUPMS, mDeviceStartupMs.getMean());
220 }
221 if (mUnderrunCount > 0) {
222 item.set(AMEDIAMETRICS_PROP_UNDERRUN,
223 (int32_t)(mUnderrunCount - mUnderrunCountSinceIntervalGroup))
224 .set(AMEDIAMETRICS_PROP_UNDERRUNFRAMES,
225 (int64_t)(mUnderrunFrames - mUnderrunFramesSinceIntervalGroup));
226 }
227 item.record();
228 }
229 }
230
231 void resetIntervalGroupMetrics() REQUIRES(mLock) {
232 // mDevices is not reset by resetIntervalGroupMetrics.
233
234 mIntervalCount = 0;
Andy Hungc2b11cb2020-04-22 09:04:01 -0700235 // mCumulativeTimeNs is not reset by resetIntervalGroupMetrics.
236 mDeviceTimeNs = 0;
237
238 mVolume = 0.f;
239 mDeviceVolume = 0.f;
Andy Hunga81a4b42022-05-19 19:24:51 -0700240 mLastVolumeChangeTimeNs = 0; // last time volume logged, cleared on endInterval
Robert Lee36ac0412022-01-13 03:05:41 +0000241 mMinVolume = AMEDIAMETRICS_INITIAL_MIN_VOLUME;
242 mMaxVolume = AMEDIAMETRICS_INITIAL_MAX_VOLUME;
243 mMinVolumeDurationNs = 0;
244 mMaxVolumeDurationNs = 0;
Andy Hungc2b11cb2020-04-22 09:04:01 -0700245
246 mDeviceLatencyMs.reset();
247 mDeviceStartupMs.reset();
248
249 mUnderrunCountSinceIntervalGroup = mUnderrunCount;
250 mUnderrunFramesSinceIntervalGroup = mUnderrunFrames;
251 // do not reset mUnderrunCount - it keeps continuously running for tracks.
252 }
253
Kunal Malhotra3be68902023-02-28 22:03:15 +0000254 // Meyer's singleton is thread-safe.
255 static const sp<IActivityManager>& getActivityManager() {
256 static const auto activityManager = []() -> sp<IActivityManager> {
257 const sp<IServiceManager> sm(defaultServiceManager());
258 if (sm != nullptr) {
259 return interface_cast<IActivityManager>(sm->checkService(String16("activity")));
260 }
261 return nullptr;
262 }();
263 return activityManager;
264 }
265
Andy Hungc2b11cb2020-04-22 09:04:01 -0700266 const std::string mMetricsId;
267 const bool mIsOut; // if true, than a playback track, otherwise used for record.
268
Kunal Malhotra3be68902023-02-28 22:03:15 +0000269 static constexpr int AUDIO_API = 5;
Kunal Malhotraae6d1602023-03-30 00:17:25 +0000270 static constexpr int MICROPHONE_API = 6;
Kunal Malhotra3be68902023-02-28 22:03:15 +0000271 const int mUid;
272
Andy Hungc2b11cb2020-04-22 09:04:01 -0700273 mutable std::mutex mLock;
274
275 // Devices in the interval group.
276 std::string mDevices GUARDED_BY(mLock);
277
278 // Number of intervals and playing time
279 int32_t mIntervalCount GUARDED_BY(mLock) = 0;
Andy Hunga81a4b42022-05-19 19:24:51 -0700280 int64_t mCumulativeTimeNs GUARDED_BY(mLock) = 0; // total time.
281 int64_t mDeviceTimeNs GUARDED_BY(mLock) = 0; // time on device.
Andy Hungc2b11cb2020-04-22 09:04:01 -0700282
283 // Average volume
Andy Hunga81a4b42022-05-19 19:24:51 -0700284 double mVolume GUARDED_BY(mLock) = 0.f; // last set volume.
285 double mDeviceVolume GUARDED_BY(mLock) = 0.f; // running average volume.
Andy Hungc2b11cb2020-04-22 09:04:01 -0700286 int64_t mLastVolumeChangeTimeNs GUARDED_BY(mLock) = 0;
287
Robert Lee36ac0412022-01-13 03:05:41 +0000288 // Min/Max volume
289 double mMinVolume GUARDED_BY(mLock) = AMEDIAMETRICS_INITIAL_MIN_VOLUME;
290 double mMaxVolume GUARDED_BY(mLock) = AMEDIAMETRICS_INITIAL_MAX_VOLUME;
291 int64_t mMinVolumeDurationNs GUARDED_BY(mLock) = 0;
292 int64_t mMaxVolumeDurationNs GUARDED_BY(mLock) = 0;
293
Andy Hungc2b11cb2020-04-22 09:04:01 -0700294 // latency and startup for each interval.
295 audio_utils::Statistics<double> mDeviceLatencyMs GUARDED_BY(mLock);
296 audio_utils::Statistics<double> mDeviceStartupMs GUARDED_BY(mLock);
297
298 // underrun count and frames
299 int64_t mUnderrunCount GUARDED_BY(mLock) = 0;
300 int64_t mUnderrunFrames GUARDED_BY(mLock) = 0;
301 int64_t mUnderrunCountSinceIntervalGroup GUARDED_BY(mLock) = 0;
302 int64_t mUnderrunFramesSinceIntervalGroup GUARDED_BY(mLock) = 0;
303};
304
305} // namespace android
306
307#endif // ANDROID_AUDIO_TRACKMETRICS_H