blob: 4b84bea09b45abf90af77f2485b0122a3a202a86 [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
Ray Essick2e9c63b2017-03-29 15:16:44 -07002 * Copyright (C) 2017 The Android Open Source Project
Ray Essick3938dc62016-11-01 08:56:56 -07003 *
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
Ray Essick3938dc62016-11-01 08:56:56 -070017//#define LOG_NDEBUG 0
Ray Essickf27e9872019-12-07 06:28:46 -080018#define LOG_TAG "MediaMetricsService"
Ray Essick3938dc62016-11-01 08:56:56 -070019#include <utils/Log.h>
20
Ray Essick40e8e5e2019-12-05 20:19:40 -080021#include "MediaMetricsService.h"
Ray Essick3938dc62016-11-01 08:56:56 -070022
Andy Hung17dbaf22019-10-11 14:06:31 -070023#include <pwd.h> //getpwuid
24
Andy Hung17dbaf22019-10-11 14:06:31 -070025#include <android/content/pm/IPackageManagerNative.h> // package info
Andy Hung55aaf522019-12-03 15:07:51 -080026#include <audio_utils/clock.h> // clock conversions
Andy Hung17dbaf22019-10-11 14:06:31 -070027#include <binder/IPCThreadState.h> // get calling uid
28#include <cutils/properties.h> // for property_get
Andy Hung9099a1a2020-04-04 14:23:36 -070029#include <mediautils/MemoryLeakTrackUtil.h>
30#include <memunreachable/memunreachable.h>
Andy Hung17dbaf22019-10-11 14:06:31 -070031#include <private/android_filesystem_config.h> // UID
32
Ray Essick3938dc62016-11-01 08:56:56 -070033namespace android {
34
Andy Hung1efc9c62019-12-03 13:43:33 -080035using namespace mediametrics;
36
Ray Essickf65f4212017-08-31 11:41:19 -070037// individual records kept in memory: age or count
Ray Essick72a436b2018-06-14 15:08:13 -070038// age: <= 28 hours (1 1/6 days)
Ray Essickf65f4212017-08-31 11:41:19 -070039// count: hard limit of # records
40// (0 for either of these disables that threshold)
Ray Essick72a436b2018-06-14 15:08:13 -070041//
Andy Hung17dbaf22019-10-11 14:06:31 -070042static constexpr nsecs_t kMaxRecordAgeNs = 28 * 3600 * NANOS_PER_SECOND;
Ray Essick23f4d6c2019-06-20 10:16:37 -070043// 2019/6: average daily per device is currently 375-ish;
44// setting this to 2000 is large enough to catch most devices
45// we'll lose some data on very very media-active devices, but only for
46// the gms collection; statsd will have already covered those for us.
47// This also retains enough information to help with bugreports
Andy Hung17dbaf22019-10-11 14:06:31 -070048static constexpr size_t kMaxRecords = 2000;
Ray Essick72a436b2018-06-14 15:08:13 -070049
50// max we expire in a single call, to constrain how long we hold the
51// mutex, which also constrains how long a client might wait.
Andy Hung17dbaf22019-10-11 14:06:31 -070052static constexpr size_t kMaxExpiredAtOnce = 50;
Ray Essick72a436b2018-06-14 15:08:13 -070053
54// TODO: need to look at tuning kMaxRecords and friends for low-memory devices
Ray Essick2e9c63b2017-03-29 15:16:44 -070055
Andy Hung55aaf522019-12-03 15:07:51 -080056/* static */
Ray Essickf27e9872019-12-07 06:28:46 -080057nsecs_t MediaMetricsService::roundTime(nsecs_t timeNs)
Andy Hung55aaf522019-12-03 15:07:51 -080058{
59 return (timeNs + NANOS_PER_SECOND / 2) / NANOS_PER_SECOND * NANOS_PER_SECOND;
60}
61
Andy Hunga85efab2019-12-23 11:41:29 -080062/* static */
63bool MediaMetricsService::useUidForPackage(
64 const std::string& package, const std::string& installer)
65{
66 if (strchr(package.c_str(), '.') == NULL) {
67 return false; // not of form 'com.whatever...'; assume internal and ok
68 } else if (strncmp(package.c_str(), "android.", 8) == 0) {
69 return false; // android.* packages are assumed fine
70 } else if (strncmp(installer.c_str(), "com.android.", 12) == 0) {
71 return false; // from play store
72 } else if (strncmp(installer.c_str(), "com.google.", 11) == 0) {
73 return false; // some google source
74 } else if (strcmp(installer.c_str(), "preload") == 0) {
75 return false; // preloads
76 } else {
77 return true; // we're not sure where it came from, use uid only.
78 }
79}
80
Ray Essickf27e9872019-12-07 06:28:46 -080081MediaMetricsService::MediaMetricsService()
Ray Essick2e9c63b2017-03-29 15:16:44 -070082 : mMaxRecords(kMaxRecords),
Ray Essickf65f4212017-08-31 11:41:19 -070083 mMaxRecordAgeNs(kMaxRecordAgeNs),
Andy Hung3b4c1f02020-01-23 18:58:32 -080084 mMaxRecordsExpiredAtOnce(kMaxExpiredAtOnce)
Andy Hung17dbaf22019-10-11 14:06:31 -070085{
86 ALOGD("%s", __func__);
Ray Essick3938dc62016-11-01 08:56:56 -070087}
88
Ray Essickf27e9872019-12-07 06:28:46 -080089MediaMetricsService::~MediaMetricsService()
Andy Hung17dbaf22019-10-11 14:06:31 -070090{
91 ALOGD("%s", __func__);
92 // the class destructor clears anyhow, but we enforce clearing items first.
93 mItemsDiscarded += mItems.size();
94 mItems.clear();
Ray Essick3938dc62016-11-01 08:56:56 -070095}
96
Ray Essickf27e9872019-12-07 06:28:46 -080097status_t MediaMetricsService::submitInternal(mediametrics::Item *item, bool release)
Ray Essick92d23b42018-01-29 12:10:30 -080098{
Andy Hung55aaf522019-12-03 15:07:51 -080099 // calling PID is 0 for one-way calls.
100 const pid_t pid = IPCThreadState::self()->getCallingPid();
101 const pid_t pid_given = item->getPid();
102 const uid_t uid = IPCThreadState::self()->getCallingUid();
103 const uid_t uid_given = item->getUid();
Ray Essickd38e1742017-01-23 15:17:06 -0800104
Andy Hung55aaf522019-12-03 15:07:51 -0800105 //ALOGD("%s: caller pid=%d uid=%d, item pid=%d uid=%d", __func__,
106 // (int)pid, (int)uid, (int) pid_given, (int)uid_given);
Ray Essickd38e1742017-01-23 15:17:06 -0800107
Andy Hung17dbaf22019-10-11 14:06:31 -0700108 bool isTrusted;
109 switch (uid) {
Andy Hung55aaf522019-12-03 15:07:51 -0800110 case AID_AUDIOSERVER:
111 case AID_BLUETOOTH:
112 case AID_CAMERA:
Andy Hung17dbaf22019-10-11 14:06:31 -0700113 case AID_DRM:
114 case AID_MEDIA:
115 case AID_MEDIA_CODEC:
116 case AID_MEDIA_EX:
117 case AID_MEDIA_DRM:
Andy Hung5d3f2d12020-03-04 19:55:03 -0800118 // case AID_SHELL: // DEBUG ONLY - used for mediametrics_tests to add new keys
Andy Hung55aaf522019-12-03 15:07:51 -0800119 case AID_SYSTEM:
Andy Hung17dbaf22019-10-11 14:06:31 -0700120 // trusted source, only override default values
121 isTrusted = true;
Andy Hung55aaf522019-12-03 15:07:51 -0800122 if (uid_given == (uid_t)-1) {
Ray Essickd38e1742017-01-23 15:17:06 -0800123 item->setUid(uid);
Andy Hung17dbaf22019-10-11 14:06:31 -0700124 }
Andy Hung55aaf522019-12-03 15:07:51 -0800125 if (pid_given == (pid_t)-1) {
126 item->setPid(pid); // if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700127 }
128 break;
129 default:
130 isTrusted = false;
Andy Hung55aaf522019-12-03 15:07:51 -0800131 item->setPid(pid); // always use calling pid, if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700132 item->setUid(uid);
133 break;
Ray Essickd38e1742017-01-23 15:17:06 -0800134 }
135
Andy Hunga85efab2019-12-23 11:41:29 -0800136 // Overwrite package name and version if the caller was untrusted or empty
137 if (!isTrusted || item->getPkgName().empty()) {
138 const uid_t uid = item->getUid();
139 mediautils::UidInfo::Info info = mUidInfo.getInfo(uid);
140 if (useUidForPackage(info.package, info.installer)) {
141 // remove uid information of unknown installed packages.
142 // TODO: perhaps this can be done just before uploading to Westworld.
143 item->setPkgName(std::to_string(uid));
144 item->setPkgVersionCode(0);
145 } else {
146 item->setPkgName(info.package);
147 item->setPkgVersionCode(info.versionCode);
148 }
Adam Stone21c72122017-09-05 19:02:06 -0700149 }
150
Andy Hung5d3f2d12020-03-04 19:55:03 -0800151 ALOGV("%s: isTrusted:%d given uid %d; sanitized uid: %d sanitized pkg: %s "
Andy Hung17dbaf22019-10-11 14:06:31 -0700152 "sanitized pkg version: %lld",
153 __func__,
Andy Hung5d3f2d12020-03-04 19:55:03 -0800154 (int)isTrusted,
Adam Stone21c72122017-09-05 19:02:06 -0700155 uid_given, item->getUid(),
156 item->getPkgName().c_str(),
Andy Hung17dbaf22019-10-11 14:06:31 -0700157 (long long)item->getPkgVersionCode());
Ray Essick3938dc62016-11-01 08:56:56 -0700158
159 mItemsSubmitted++;
160
161 // validate the record; we discard if we don't like it
Andy Hung17dbaf22019-10-11 14:06:31 -0700162 if (isContentValid(item, isTrusted) == false) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700163 if (release) delete item;
164 return PERMISSION_DENIED;
Ray Essick3938dc62016-11-01 08:56:56 -0700165 }
166
Ray Essick92d23b42018-01-29 12:10:30 -0800167 // XXX: if we have a sessionid in the new record, look to make
Ray Essick3938dc62016-11-01 08:56:56 -0700168 // sure it doesn't appear in the finalized list.
Ray Essick3938dc62016-11-01 08:56:56 -0700169
Ray Essick92d23b42018-01-29 12:10:30 -0800170 if (item->count() == 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700171 ALOGV("%s: dropping empty record...", __func__);
Andy Hunga87e69c2019-10-18 10:07:40 -0700172 if (release) delete item;
173 return BAD_VALUE;
Ray Essick3938dc62016-11-01 08:56:56 -0700174 }
Ray Essick92d23b42018-01-29 12:10:30 -0800175
Andy Hung55aaf522019-12-03 15:07:51 -0800176 if (!isTrusted || item->getTimestamp() == 0) {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800177 // Westworld logs two times for events: ElapsedRealTimeNs (BOOTTIME) and
178 // WallClockTimeNs (REALTIME), but currently logs REALTIME to cloud.
Andy Hung55aaf522019-12-03 15:07:51 -0800179 //
Andy Hung3b4c1f02020-01-23 18:58:32 -0800180 // For consistency and correlation with other logging mechanisms
181 // we use REALTIME here.
182 const int64_t now = systemTime(SYSTEM_TIME_REALTIME);
Andy Hung55aaf522019-12-03 15:07:51 -0800183 item->setTimestamp(now);
184 }
185
Andy Hung82a074b2019-12-03 14:16:45 -0800186 // now attach either the item or its dup to a const shared pointer
Ray Essickf27e9872019-12-07 06:28:46 -0800187 std::shared_ptr<const mediametrics::Item> sitem(release ? item : item->dup());
Ray Essick6ce27e52019-02-15 10:58:05 -0800188
Andy Hung06f3aba2019-12-03 16:36:42 -0800189 (void)mAudioAnalytics.submit(sitem, isTrusted);
190
Ray Essickf27e9872019-12-07 06:28:46 -0800191 extern bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item);
Andy Hung82a074b2019-12-03 14:16:45 -0800192 (void)dump2Statsd(sitem); // failure should be logged in function.
193 saveItem(sitem);
Andy Hunga87e69c2019-10-18 10:07:40 -0700194 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700195}
196
Ray Essickf27e9872019-12-07 06:28:46 -0800197status_t MediaMetricsService::dump(int fd, const Vector<String16>& args)
Ray Essick3938dc62016-11-01 08:56:56 -0700198{
Ray Essick3938dc62016-11-01 08:56:56 -0700199 String8 result;
200
201 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700202 result.appendFormat("Permission Denial: "
Ray Essickf27e9872019-12-07 06:28:46 -0800203 "can't dump MediaMetricsService from pid=%d, uid=%d\n",
Ray Essick3938dc62016-11-01 08:56:56 -0700204 IPCThreadState::self()->getCallingPid(),
205 IPCThreadState::self()->getCallingUid());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800206 write(fd, result.string(), result.size());
207 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700208 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800209
Andy Hung709b91e2020-04-04 14:23:36 -0700210 static const String16 allOption("--all");
211 static const String16 clearOption("--clear");
Andy Hung9099a1a2020-04-04 14:23:36 -0700212 static const String16 heapOption("--heap");
Andy Hung709b91e2020-04-04 14:23:36 -0700213 static const String16 helpOption("--help");
214 static const String16 prefixOption("--prefix");
215 static const String16 sinceOption("--since");
Andy Hung9099a1a2020-04-04 14:23:36 -0700216 static const String16 unreachableOption("--unreachable");
Andy Hung709b91e2020-04-04 14:23:36 -0700217
218 bool all = false;
219 bool clear = false;
Andy Hung9099a1a2020-04-04 14:23:36 -0700220 bool heap = false;
221 bool unreachable = false;
Andy Hung709b91e2020-04-04 14:23:36 -0700222 int64_t sinceNs = 0;
223 std::string prefix;
Andy Hung9099a1a2020-04-04 14:23:36 -0700224
Andy Hung709b91e2020-04-04 14:23:36 -0700225 const size_t n = args.size();
226 for (size_t i = 0; i < n; i++) {
227 if (args[i] == allOption) {
228 all = true;
229 } else if (args[i] == clearOption) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800230 clear = true;
Andy Hung9099a1a2020-04-04 14:23:36 -0700231 } else if (args[i] == heapOption) {
232 heap = true;
Ray Essick35ad27f2017-01-30 14:04:11 -0800233 } else if (args[i] == helpOption) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700234 // TODO: consider function area dumping.
235 // dumpsys media.metrics audiotrack,codec
236 // or dumpsys media.metrics audiotrack codec
237
Ray Essick35ad27f2017-01-30 14:04:11 -0800238 result.append("Recognized parameters:\n");
Andy Hung709b91e2020-04-04 14:23:36 -0700239 result.append("--all show all records\n");
240 result.append("--clear clear out saved records\n");
241 result.append("--heap show heap usage (top 100)\n");
242 result.append("--help display help\n");
243 result.append("--prefix X process records for component X\n");
244 result.append("--since X X < 0: records from -X seconds in the past\n");
245 result.append(" X = 0: ignore\n");
246 result.append(" X > 0: records from X seconds since Unix epoch\n");
247 result.append("--unreachable show unreachable memory (leaks)\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800248 write(fd, result.string(), result.size());
249 return NO_ERROR;
Andy Hung709b91e2020-04-04 14:23:36 -0700250 } else if (args[i] == prefixOption) {
251 ++i;
252 if (i < n) {
253 prefix = String8(args[i]).string();
254 }
255 } else if (args[i] == sinceOption) {
256 ++i;
257 if (i < n) {
258 String8 value(args[i]);
259 char *endp;
260 const char *p = value.string();
261 long long sec = strtoll(p, &endp, 10);
262 if (endp == p || *endp != '\0' || sec == 0) {
263 sinceNs = 0;
264 } else if (sec < 0) {
265 sinceNs = systemTime(SYSTEM_TIME_REALTIME) + sec * NANOS_PER_SECOND;
266 } else {
267 sinceNs = sec * NANOS_PER_SECOND;
268 }
269 }
Andy Hung9099a1a2020-04-04 14:23:36 -0700270 } else if (args[i] == unreachableOption) {
271 unreachable = true;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800272 }
273 }
274
Andy Hung17dbaf22019-10-11 14:06:31 -0700275 {
276 std::lock_guard _l(mLock);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800277
Andy Hung17dbaf22019-10-11 14:06:31 -0700278 if (clear) {
279 mItemsDiscarded += mItems.size();
280 mItems.clear();
Andy Hung709b91e2020-04-04 14:23:36 -0700281 mAudioAnalytics.clear();
282 } else {
283 result.appendFormat("Dump of the %s process:\n", kServiceName);
284 const char *prefixptr = prefix.size() > 0 ? prefix.c_str() : nullptr;
Andy Hungf7c14102020-04-18 14:54:08 -0700285 dumpHeaders(result, sinceNs, prefixptr);
286 dumpQueue(result, sinceNs, prefixptr);
Andy Hung709b91e2020-04-04 14:23:36 -0700287
288 // TODO: maybe consider a better way of dumping audio analytics info.
289 const int32_t linesToDump = all ? INT32_MAX : 1000;
290 auto [ dumpString, lines ] = mAudioAnalytics.dump(linesToDump, sinceNs, prefixptr);
291 result.append(dumpString.c_str());
292 if (lines == linesToDump) {
293 result.append("-- some lines may be truncated --\n");
294 }
Andy Hung0f7ad8c2020-01-03 13:24:34 -0800295 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700296 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700297 write(fd, result.string(), result.size());
Andy Hung9099a1a2020-04-04 14:23:36 -0700298
299 // Check heap and unreachable memory outside of lock.
300 if (heap) {
301 dprintf(fd, "\nDumping heap:\n");
302 std::string s = dumpMemoryAddresses(100 /* limit */);
303 write(fd, s.c_str(), s.size());
304 }
305 if (unreachable) {
306 dprintf(fd, "\nDumping unreachable memory:\n");
307 // TODO - should limit be an argument parameter?
308 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
309 write(fd, s.c_str(), s.size());
310 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700311 return NO_ERROR;
312}
313
314// dump headers
Andy Hungf7c14102020-04-18 14:54:08 -0700315void MediaMetricsService::dumpHeaders(String8 &result, int64_t sinceNs, const char* prefix)
Ray Essick92d23b42018-01-29 12:10:30 -0800316{
Ray Essickf27e9872019-12-07 06:28:46 -0800317 if (mediametrics::Item::isEnabled()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700318 result.append("Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800319 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700320 result.append("Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800321 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700322 result.appendFormat(
323 "Since Boot: Submissions: %lld Accepted: %lld\n",
324 (long long)mItemsSubmitted.load(), (long long)mItemsFinalized);
325 result.appendFormat(
326 "Records Discarded: %lld (by Count: %lld by Expiration: %lld)\n",
327 (long long)mItemsDiscarded, (long long)mItemsDiscardedCount,
328 (long long)mItemsDiscardedExpire);
Andy Hung709b91e2020-04-04 14:23:36 -0700329 if (prefix != nullptr) {
330 result.appendFormat("Restricting to prefix %s", prefix);
331 }
332 if (sinceNs != 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700333 result.appendFormat(
334 "Emitting Queue entries more recent than: %lld\n",
Andy Hung709b91e2020-04-04 14:23:36 -0700335 (long long)sinceNs);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800336 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700337}
338
Andy Hung709b91e2020-04-04 14:23:36 -0700339// TODO: should prefix be a set<string>?
Andy Hungf7c14102020-04-18 14:54:08 -0700340void MediaMetricsService::dumpQueue(String8 &result, int64_t sinceNs, const char* prefix)
Ray Essick92d23b42018-01-29 12:10:30 -0800341{
Ray Essick92d23b42018-01-29 12:10:30 -0800342 if (mItems.empty()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700343 result.append("empty\n");
Andy Hung709b91e2020-04-04 14:23:36 -0700344 return;
345 }
346
347 int slot = 0;
348 for (const auto &item : mItems) { // TODO: consider std::lower_bound() on mItems
349 if (item->getTimestamp() < sinceNs) { // sinceNs == 0 means all items shown
350 continue;
Ray Essick3938dc62016-11-01 08:56:56 -0700351 }
Andy Hung709b91e2020-04-04 14:23:36 -0700352 if (prefix != nullptr && !startsWith(item->getKey(), prefix)) {
353 ALOGV("%s: omit '%s', it's not '%s'",
354 __func__, item->getKey().c_str(), prefix);
355 continue;
356 }
357 result.appendFormat("%5d: %s\n", slot, item->toString().c_str());
358 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700359 }
Ray Essick3938dc62016-11-01 08:56:56 -0700360}
361
362//
363// Our Cheap in-core, non-persistent records management.
Ray Essick3938dc62016-11-01 08:56:56 -0700364
Ray Essick72a436b2018-06-14 15:08:13 -0700365// if item != NULL, it's the item we just inserted
366// true == more items eligible to be recovered
Andy Hungf7c14102020-04-18 14:54:08 -0700367bool MediaMetricsService::expirations(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick92d23b42018-01-29 12:10:30 -0800368{
Ray Essick72a436b2018-06-14 15:08:13 -0700369 bool more = false;
Ray Essicke5db6db2017-11-10 15:54:32 -0800370
Andy Hung17dbaf22019-10-11 14:06:31 -0700371 // check queue size
372 size_t overlimit = 0;
373 if (mMaxRecords > 0 && mItems.size() > mMaxRecords) {
374 overlimit = mItems.size() - mMaxRecords;
375 if (overlimit > mMaxRecordsExpiredAtOnce) {
376 more = true;
377 overlimit = mMaxRecordsExpiredAtOnce;
Ray Essickf65f4212017-08-31 11:41:19 -0700378 }
379 }
380
Andy Hung17dbaf22019-10-11 14:06:31 -0700381 // check queue times
382 size_t expired = 0;
383 if (!more && mMaxRecordAgeNs > 0) {
384 const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
385 // we check one at a time, skip search would be more efficient.
386 size_t i = overlimit;
387 for (; i < mItems.size(); ++i) {
388 auto &oitem = mItems[i];
Ray Essickf65f4212017-08-31 11:41:19 -0700389 nsecs_t when = oitem->getTimestamp();
Andy Hung82a074b2019-12-03 14:16:45 -0800390 if (oitem.get() == item.get()) {
Ray Essicke5db6db2017-11-10 15:54:32 -0800391 break;
392 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700393 if (now > when && (now - when) <= mMaxRecordAgeNs) {
Andy Hunged416da2020-03-05 18:42:55 -0800394 break; // Note SYSTEM_TIME_REALTIME may not be monotonic.
Ray Essickf65f4212017-08-31 11:41:19 -0700395 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700396 if (i >= mMaxRecordsExpiredAtOnce) {
Ray Essick72a436b2018-06-14 15:08:13 -0700397 // this represents "one too many"; tell caller there are
398 // more to be reclaimed.
399 more = true;
400 break;
401 }
Ray Essick3938dc62016-11-01 08:56:56 -0700402 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700403 expired = i - overlimit;
Ray Essick3938dc62016-11-01 08:56:56 -0700404 }
Ray Essick72a436b2018-06-14 15:08:13 -0700405
Andy Hung17dbaf22019-10-11 14:06:31 -0700406 if (const size_t toErase = overlimit + expired;
407 toErase > 0) {
408 mItemsDiscardedCount += overlimit;
409 mItemsDiscardedExpire += expired;
410 mItemsDiscarded += toErase;
411 mItems.erase(mItems.begin(), mItems.begin() + toErase); // erase from front
412 }
Ray Essick72a436b2018-06-14 15:08:13 -0700413 return more;
414}
415
Ray Essickf27e9872019-12-07 06:28:46 -0800416void MediaMetricsService::processExpirations()
Ray Essick72a436b2018-06-14 15:08:13 -0700417{
418 bool more;
419 do {
420 sleep(1);
Andy Hung17dbaf22019-10-11 14:06:31 -0700421 std::lock_guard _l(mLock);
Andy Hungf7c14102020-04-18 14:54:08 -0700422 more = expirations(nullptr);
Ray Essick72a436b2018-06-14 15:08:13 -0700423 } while (more);
Ray Essick72a436b2018-06-14 15:08:13 -0700424}
425
Ray Essickf27e9872019-12-07 06:28:46 -0800426void MediaMetricsService::saveItem(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick72a436b2018-06-14 15:08:13 -0700427{
Andy Hung17dbaf22019-10-11 14:06:31 -0700428 std::lock_guard _l(mLock);
429 // we assume the items are roughly in time order.
430 mItems.emplace_back(item);
431 ++mItemsFinalized;
Andy Hungf7c14102020-04-18 14:54:08 -0700432 if (expirations(item)
Andy Hung17dbaf22019-10-11 14:06:31 -0700433 && (!mExpireFuture.valid()
434 || mExpireFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)) {
435 mExpireFuture = std::async(std::launch::async, [this] { processExpirations(); });
Ray Essick72a436b2018-06-14 15:08:13 -0700436 }
Ray Essick3938dc62016-11-01 08:56:56 -0700437}
438
Andy Hung17dbaf22019-10-11 14:06:31 -0700439/* static */
Ray Essickf27e9872019-12-07 06:28:46 -0800440bool MediaMetricsService::isContentValid(const mediametrics::Item *item, bool isTrusted)
Ray Essickd38e1742017-01-23 15:17:06 -0800441{
Andy Hung17dbaf22019-10-11 14:06:31 -0700442 if (isTrusted) return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800443 // untrusted uids can only send us a limited set of keys
Andy Hung17dbaf22019-10-11 14:06:31 -0700444 const std::string &key = item->getKey();
Andy Hung06f3aba2019-12-03 16:36:42 -0800445 if (startsWith(key, "audio.")) return true;
Edwin Wong8d188352020-02-11 11:59:34 -0800446 if (startsWith(key, "drm.vendor.")) return true;
447 // the list of allowedKey uses statsd_handlers
448 // in iface_statsd.cpp as reference
449 // drmmanager is from a trusted uid, therefore not needed here
Andy Hung17dbaf22019-10-11 14:06:31 -0700450 for (const char *allowedKey : {
Andy Hung06f3aba2019-12-03 16:36:42 -0800451 // legacy audio
Andy Hung17dbaf22019-10-11 14:06:31 -0700452 "audiopolicy",
453 "audiorecord",
454 "audiothread",
455 "audiotrack",
Andy Hung06f3aba2019-12-03 16:36:42 -0800456 // other media
Andy Hung17dbaf22019-10-11 14:06:31 -0700457 "codec",
458 "extractor",
Edwin Wong8d188352020-02-11 11:59:34 -0800459 "mediadrm",
Andy Hung17dbaf22019-10-11 14:06:31 -0700460 "nuplayer",
461 }) {
462 if (key == allowedKey) {
463 return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800464 }
465 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700466 ALOGD("%s: invalid key: %s", __func__, item->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -0700467 return false;
468}
469
Andy Hung17dbaf22019-10-11 14:06:31 -0700470// are we rate limited, normally false
Ray Essickf27e9872019-12-07 06:28:46 -0800471bool MediaMetricsService::isRateLimited(mediametrics::Item *) const
Andy Hung17dbaf22019-10-11 14:06:31 -0700472{
473 return false;
474}
475
Ray Essick3938dc62016-11-01 08:56:56 -0700476} // namespace android