blob: a6fefd2404deb52c3ac6e43a23d89cb2650214ac [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
29#include <private/android_filesystem_config.h> // UID
30
Ray Essick3938dc62016-11-01 08:56:56 -070031namespace android {
32
Andy Hung1efc9c62019-12-03 13:43:33 -080033using namespace mediametrics;
34
Ray Essickf65f4212017-08-31 11:41:19 -070035// individual records kept in memory: age or count
Ray Essick72a436b2018-06-14 15:08:13 -070036// age: <= 28 hours (1 1/6 days)
Ray Essickf65f4212017-08-31 11:41:19 -070037// count: hard limit of # records
38// (0 for either of these disables that threshold)
Ray Essick72a436b2018-06-14 15:08:13 -070039//
Andy Hung17dbaf22019-10-11 14:06:31 -070040static constexpr nsecs_t kMaxRecordAgeNs = 28 * 3600 * NANOS_PER_SECOND;
Ray Essick23f4d6c2019-06-20 10:16:37 -070041// 2019/6: average daily per device is currently 375-ish;
42// setting this to 2000 is large enough to catch most devices
43// we'll lose some data on very very media-active devices, but only for
44// the gms collection; statsd will have already covered those for us.
45// This also retains enough information to help with bugreports
Andy Hung17dbaf22019-10-11 14:06:31 -070046static constexpr size_t kMaxRecords = 2000;
Ray Essick72a436b2018-06-14 15:08:13 -070047
48// max we expire in a single call, to constrain how long we hold the
49// mutex, which also constrains how long a client might wait.
Andy Hung17dbaf22019-10-11 14:06:31 -070050static constexpr size_t kMaxExpiredAtOnce = 50;
Ray Essick72a436b2018-06-14 15:08:13 -070051
52// TODO: need to look at tuning kMaxRecords and friends for low-memory devices
Ray Essick2e9c63b2017-03-29 15:16:44 -070053
Andy Hung55aaf522019-12-03 15:07:51 -080054/* static */
Ray Essickf27e9872019-12-07 06:28:46 -080055nsecs_t MediaMetricsService::roundTime(nsecs_t timeNs)
Andy Hung55aaf522019-12-03 15:07:51 -080056{
57 return (timeNs + NANOS_PER_SECOND / 2) / NANOS_PER_SECOND * NANOS_PER_SECOND;
58}
59
Andy Hunga85efab2019-12-23 11:41:29 -080060/* static */
61bool MediaMetricsService::useUidForPackage(
62 const std::string& package, const std::string& installer)
63{
64 if (strchr(package.c_str(), '.') == NULL) {
65 return false; // not of form 'com.whatever...'; assume internal and ok
66 } else if (strncmp(package.c_str(), "android.", 8) == 0) {
67 return false; // android.* packages are assumed fine
68 } else if (strncmp(installer.c_str(), "com.android.", 12) == 0) {
69 return false; // from play store
70 } else if (strncmp(installer.c_str(), "com.google.", 11) == 0) {
71 return false; // some google source
72 } else if (strcmp(installer.c_str(), "preload") == 0) {
73 return false; // preloads
74 } else {
75 return true; // we're not sure where it came from, use uid only.
76 }
77}
78
Ray Essickf27e9872019-12-07 06:28:46 -080079MediaMetricsService::MediaMetricsService()
Ray Essick2e9c63b2017-03-29 15:16:44 -070080 : mMaxRecords(kMaxRecords),
Ray Essickf65f4212017-08-31 11:41:19 -070081 mMaxRecordAgeNs(kMaxRecordAgeNs),
Andy Hung3b4c1f02020-01-23 18:58:32 -080082 mMaxRecordsExpiredAtOnce(kMaxExpiredAtOnce)
Andy Hung17dbaf22019-10-11 14:06:31 -070083{
84 ALOGD("%s", __func__);
Ray Essick3938dc62016-11-01 08:56:56 -070085}
86
Ray Essickf27e9872019-12-07 06:28:46 -080087MediaMetricsService::~MediaMetricsService()
Andy Hung17dbaf22019-10-11 14:06:31 -070088{
89 ALOGD("%s", __func__);
90 // the class destructor clears anyhow, but we enforce clearing items first.
91 mItemsDiscarded += mItems.size();
92 mItems.clear();
Ray Essick3938dc62016-11-01 08:56:56 -070093}
94
Ray Essickf27e9872019-12-07 06:28:46 -080095status_t MediaMetricsService::submitInternal(mediametrics::Item *item, bool release)
Ray Essick92d23b42018-01-29 12:10:30 -080096{
Andy Hung55aaf522019-12-03 15:07:51 -080097 // calling PID is 0 for one-way calls.
98 const pid_t pid = IPCThreadState::self()->getCallingPid();
99 const pid_t pid_given = item->getPid();
100 const uid_t uid = IPCThreadState::self()->getCallingUid();
101 const uid_t uid_given = item->getUid();
Ray Essickd38e1742017-01-23 15:17:06 -0800102
Andy Hung55aaf522019-12-03 15:07:51 -0800103 //ALOGD("%s: caller pid=%d uid=%d, item pid=%d uid=%d", __func__,
104 // (int)pid, (int)uid, (int) pid_given, (int)uid_given);
Ray Essickd38e1742017-01-23 15:17:06 -0800105
Andy Hung17dbaf22019-10-11 14:06:31 -0700106 bool isTrusted;
107 switch (uid) {
Andy Hung55aaf522019-12-03 15:07:51 -0800108 case AID_AUDIOSERVER:
109 case AID_BLUETOOTH:
110 case AID_CAMERA:
Andy Hung17dbaf22019-10-11 14:06:31 -0700111 case AID_DRM:
112 case AID_MEDIA:
113 case AID_MEDIA_CODEC:
114 case AID_MEDIA_EX:
115 case AID_MEDIA_DRM:
Andy Hung5d3f2d12020-03-04 19:55:03 -0800116 // case AID_SHELL: // DEBUG ONLY - used for mediametrics_tests to add new keys
Andy Hung55aaf522019-12-03 15:07:51 -0800117 case AID_SYSTEM:
Andy Hung17dbaf22019-10-11 14:06:31 -0700118 // trusted source, only override default values
119 isTrusted = true;
Andy Hung55aaf522019-12-03 15:07:51 -0800120 if (uid_given == (uid_t)-1) {
Ray Essickd38e1742017-01-23 15:17:06 -0800121 item->setUid(uid);
Andy Hung17dbaf22019-10-11 14:06:31 -0700122 }
Andy Hung55aaf522019-12-03 15:07:51 -0800123 if (pid_given == (pid_t)-1) {
124 item->setPid(pid); // if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700125 }
126 break;
127 default:
128 isTrusted = false;
Andy Hung55aaf522019-12-03 15:07:51 -0800129 item->setPid(pid); // always use calling pid, if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700130 item->setUid(uid);
131 break;
Ray Essickd38e1742017-01-23 15:17:06 -0800132 }
133
Andy Hunga85efab2019-12-23 11:41:29 -0800134 // Overwrite package name and version if the caller was untrusted or empty
135 if (!isTrusted || item->getPkgName().empty()) {
136 const uid_t uid = item->getUid();
137 mediautils::UidInfo::Info info = mUidInfo.getInfo(uid);
138 if (useUidForPackage(info.package, info.installer)) {
139 // remove uid information of unknown installed packages.
140 // TODO: perhaps this can be done just before uploading to Westworld.
141 item->setPkgName(std::to_string(uid));
142 item->setPkgVersionCode(0);
143 } else {
144 item->setPkgName(info.package);
145 item->setPkgVersionCode(info.versionCode);
146 }
Adam Stone21c72122017-09-05 19:02:06 -0700147 }
148
Andy Hung5d3f2d12020-03-04 19:55:03 -0800149 ALOGV("%s: isTrusted:%d given uid %d; sanitized uid: %d sanitized pkg: %s "
Andy Hung17dbaf22019-10-11 14:06:31 -0700150 "sanitized pkg version: %lld",
151 __func__,
Andy Hung5d3f2d12020-03-04 19:55:03 -0800152 (int)isTrusted,
Adam Stone21c72122017-09-05 19:02:06 -0700153 uid_given, item->getUid(),
154 item->getPkgName().c_str(),
Andy Hung17dbaf22019-10-11 14:06:31 -0700155 (long long)item->getPkgVersionCode());
Ray Essick3938dc62016-11-01 08:56:56 -0700156
157 mItemsSubmitted++;
158
159 // validate the record; we discard if we don't like it
Andy Hung17dbaf22019-10-11 14:06:31 -0700160 if (isContentValid(item, isTrusted) == false) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700161 if (release) delete item;
162 return PERMISSION_DENIED;
Ray Essick3938dc62016-11-01 08:56:56 -0700163 }
164
Ray Essick92d23b42018-01-29 12:10:30 -0800165 // XXX: if we have a sessionid in the new record, look to make
Ray Essick3938dc62016-11-01 08:56:56 -0700166 // sure it doesn't appear in the finalized list.
Ray Essick3938dc62016-11-01 08:56:56 -0700167
Ray Essick92d23b42018-01-29 12:10:30 -0800168 if (item->count() == 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700169 ALOGV("%s: dropping empty record...", __func__);
Andy Hunga87e69c2019-10-18 10:07:40 -0700170 if (release) delete item;
171 return BAD_VALUE;
Ray Essick3938dc62016-11-01 08:56:56 -0700172 }
Ray Essick92d23b42018-01-29 12:10:30 -0800173
Andy Hung55aaf522019-12-03 15:07:51 -0800174 if (!isTrusted || item->getTimestamp() == 0) {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800175 // Westworld logs two times for events: ElapsedRealTimeNs (BOOTTIME) and
176 // WallClockTimeNs (REALTIME), but currently logs REALTIME to cloud.
Andy Hung55aaf522019-12-03 15:07:51 -0800177 //
Andy Hung3b4c1f02020-01-23 18:58:32 -0800178 // For consistency and correlation with other logging mechanisms
179 // we use REALTIME here.
180 const int64_t now = systemTime(SYSTEM_TIME_REALTIME);
Andy Hung55aaf522019-12-03 15:07:51 -0800181 item->setTimestamp(now);
182 }
183
Andy Hung82a074b2019-12-03 14:16:45 -0800184 // now attach either the item or its dup to a const shared pointer
Ray Essickf27e9872019-12-07 06:28:46 -0800185 std::shared_ptr<const mediametrics::Item> sitem(release ? item : item->dup());
Ray Essick6ce27e52019-02-15 10:58:05 -0800186
Andy Hung06f3aba2019-12-03 16:36:42 -0800187 (void)mAudioAnalytics.submit(sitem, isTrusted);
188
Ray Essickf27e9872019-12-07 06:28:46 -0800189 extern bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item);
Andy Hung82a074b2019-12-03 14:16:45 -0800190 (void)dump2Statsd(sitem); // failure should be logged in function.
191 saveItem(sitem);
Andy Hunga87e69c2019-10-18 10:07:40 -0700192 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700193}
194
Ray Essickf27e9872019-12-07 06:28:46 -0800195status_t MediaMetricsService::dump(int fd, const Vector<String16>& args)
Ray Essick3938dc62016-11-01 08:56:56 -0700196{
Ray Essick3938dc62016-11-01 08:56:56 -0700197 String8 result;
198
199 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700200 result.appendFormat("Permission Denial: "
Ray Essickf27e9872019-12-07 06:28:46 -0800201 "can't dump MediaMetricsService from pid=%d, uid=%d\n",
Ray Essick3938dc62016-11-01 08:56:56 -0700202 IPCThreadState::self()->getCallingPid(),
203 IPCThreadState::self()->getCallingUid());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800204 write(fd, result.string(), result.size());
205 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700206 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800207
208 // crack any parameters
Andy Hung333bb3d2020-01-31 20:17:48 -0800209 const String16 protoOption("--proto");
210 const String16 clearOption("--clear");
Ray Essickf65f4212017-08-31 11:41:19 -0700211 bool clear = false;
Andy Hung333bb3d2020-01-31 20:17:48 -0800212 const String16 sinceOption("--since");
Ray Essickf65f4212017-08-31 11:41:19 -0700213 nsecs_t ts_since = 0;
Andy Hung333bb3d2020-01-31 20:17:48 -0800214 const String16 helpOption("--help");
215 const String16 onlyOption("--only");
Ray Essick783bd0d2018-01-11 11:10:35 -0800216 std::string only;
Andy Hung17dbaf22019-10-11 14:06:31 -0700217 const int n = args.size();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800218 for (int i = 0; i < n; i++) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800219 if (args[i] == clearOption) {
220 clear = true;
Ray Essickf65f4212017-08-31 11:41:19 -0700221 } else if (args[i] == protoOption) {
222 i++;
223 if (i < n) {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800224 // ignore
Ray Essick583a23a2017-11-27 12:49:57 -0800225 } else {
226 result.append("missing value for -proto\n\n");
Ray Essickf65f4212017-08-31 11:41:19 -0700227 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800228 } else if (args[i] == sinceOption) {
229 i++;
230 if (i < n) {
231 String8 value(args[i]);
232 char *endp;
233 const char *p = value.string();
234 ts_since = strtoll(p, &endp, 10);
235 if (endp == p || *endp != '\0') {
236 ts_since = 0;
237 }
238 } else {
239 ts_since = 0;
240 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800241 // command line is milliseconds; internal units are nano-seconds
Andy Hung17dbaf22019-10-11 14:06:31 -0700242 ts_since *= NANOS_PER_MILLISECOND;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700243 } else if (args[i] == onlyOption) {
244 i++;
245 if (i < n) {
246 String8 value(args[i]);
Ray Essickf65f4212017-08-31 11:41:19 -0700247 only = value.string();
Ray Essick2e9c63b2017-03-29 15:16:44 -0700248 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800249 } else if (args[i] == helpOption) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700250 // TODO: consider function area dumping.
251 // dumpsys media.metrics audiotrack,codec
252 // or dumpsys media.metrics audiotrack codec
253
Ray Essick35ad27f2017-01-30 14:04:11 -0800254 result.append("Recognized parameters:\n");
Andy Hung333bb3d2020-01-31 20:17:48 -0800255 result.append("--help this help message\n");
256 result.append("--proto # dump using protocol #");
257 result.append("--clear clears out saved records\n");
258 result.append("--only X process records for component X\n");
259 result.append("--since X include records since X\n");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700260 result.append(" (X is milliseconds since the UNIX epoch)\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800261 write(fd, result.string(), result.size());
262 return NO_ERROR;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800263 }
264 }
265
Andy Hung17dbaf22019-10-11 14:06:31 -0700266 {
267 std::lock_guard _l(mLock);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800268
Andy Hung17dbaf22019-10-11 14:06:31 -0700269 result.appendFormat("Dump of the %s process:\n", kServiceName);
Andy Hung3b4c1f02020-01-23 18:58:32 -0800270 dumpHeaders_l(result, ts_since);
271 dumpRecent_l(result, ts_since, only.c_str());
Ray Essick583a23a2017-11-27 12:49:57 -0800272
Andy Hung17dbaf22019-10-11 14:06:31 -0700273 if (clear) {
274 mItemsDiscarded += mItems.size();
275 mItems.clear();
276 // shall we clear the summary data too?
Ray Essick2e9c63b2017-03-29 15:16:44 -0700277 }
Andy Hung06f3aba2019-12-03 16:36:42 -0800278 // TODO: maybe consider a better way of dumping audio analytics info.
279 constexpr int32_t linesToDump = 1000;
Andy Hung0f7ad8c2020-01-03 13:24:34 -0800280 auto [ dumpString, lines ] = mAudioAnalytics.dump(linesToDump);
281 result.append(dumpString.c_str());
282 if (lines == linesToDump) {
283 result.append("-- some lines may be truncated --\n");
284 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700285 }
286
287 write(fd, result.string(), result.size());
288 return NO_ERROR;
289}
290
291// dump headers
Andy Hung3b4c1f02020-01-23 18:58:32 -0800292void MediaMetricsService::dumpHeaders_l(String8 &result, nsecs_t ts_since)
Ray Essick92d23b42018-01-29 12:10:30 -0800293{
Ray Essickf27e9872019-12-07 06:28:46 -0800294 if (mediametrics::Item::isEnabled()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700295 result.append("Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800296 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700297 result.append("Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800298 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700299 result.appendFormat(
300 "Since Boot: Submissions: %lld Accepted: %lld\n",
301 (long long)mItemsSubmitted.load(), (long long)mItemsFinalized);
302 result.appendFormat(
303 "Records Discarded: %lld (by Count: %lld by Expiration: %lld)\n",
304 (long long)mItemsDiscarded, (long long)mItemsDiscardedCount,
305 (long long)mItemsDiscardedExpire);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800306 if (ts_since != 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700307 result.appendFormat(
308 "Emitting Queue entries more recent than: %lld\n",
309 (long long)ts_since);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800310 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700311}
312
Ray Essickf27e9872019-12-07 06:28:46 -0800313void MediaMetricsService::dumpRecent_l(
Andy Hung3b4c1f02020-01-23 18:58:32 -0800314 String8 &result, nsecs_t ts_since, const char * only)
Ray Essick92d23b42018-01-29 12:10:30 -0800315{
Andy Hung17dbaf22019-10-11 14:06:31 -0700316 if (only != nullptr && *only == '\0') {
317 only = nullptr;
Ray Essickf65f4212017-08-31 11:41:19 -0700318 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700319 result.append("\nFinalized Metrics (oldest first):\n");
Andy Hung3b4c1f02020-01-23 18:58:32 -0800320 dumpQueue_l(result, ts_since, only);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800321
322 // show who is connected and injecting records?
323 // talk about # records fed to the 'readers'
324 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700325}
Ray Essick92d23b42018-01-29 12:10:30 -0800326
Andy Hung3b4c1f02020-01-23 18:58:32 -0800327void MediaMetricsService::dumpQueue_l(String8 &result) {
328 dumpQueue_l(result, (nsecs_t) 0, nullptr /* only */);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800329}
330
Ray Essickf27e9872019-12-07 06:28:46 -0800331void MediaMetricsService::dumpQueue_l(
Andy Hung3b4c1f02020-01-23 18:58:32 -0800332 String8 &result, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700333 int slot = 0;
334
Ray Essick92d23b42018-01-29 12:10:30 -0800335 if (mItems.empty()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700336 result.append("empty\n");
Ray Essick3938dc62016-11-01 08:56:56 -0700337 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700338 for (const auto &item : mItems) {
339 nsecs_t when = item->getTimestamp();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800340 if (when < ts_since) {
341 continue;
342 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700343 // TODO: Only should be a set<string>
344 if (only != nullptr &&
345 item->getKey() /* std::string */ != only) {
346 ALOGV("%s: omit '%s', it's not '%s'",
347 __func__, item->getKey().c_str(), only);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700348 continue;
349 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700350 result.appendFormat("%5d: %s\n",
Andy Hung3b4c1f02020-01-23 18:58:32 -0800351 slot, item->toString().c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800352 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700353 }
354 }
Ray Essick3938dc62016-11-01 08:56:56 -0700355}
356
357//
358// Our Cheap in-core, non-persistent records management.
Ray Essick3938dc62016-11-01 08:56:56 -0700359
Ray Essick72a436b2018-06-14 15:08:13 -0700360// if item != NULL, it's the item we just inserted
361// true == more items eligible to be recovered
Ray Essickf27e9872019-12-07 06:28:46 -0800362bool MediaMetricsService::expirations_l(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick92d23b42018-01-29 12:10:30 -0800363{
Ray Essick72a436b2018-06-14 15:08:13 -0700364 bool more = false;
Ray Essicke5db6db2017-11-10 15:54:32 -0800365
Andy Hung17dbaf22019-10-11 14:06:31 -0700366 // check queue size
367 size_t overlimit = 0;
368 if (mMaxRecords > 0 && mItems.size() > mMaxRecords) {
369 overlimit = mItems.size() - mMaxRecords;
370 if (overlimit > mMaxRecordsExpiredAtOnce) {
371 more = true;
372 overlimit = mMaxRecordsExpiredAtOnce;
Ray Essickf65f4212017-08-31 11:41:19 -0700373 }
374 }
375
Andy Hung17dbaf22019-10-11 14:06:31 -0700376 // check queue times
377 size_t expired = 0;
378 if (!more && mMaxRecordAgeNs > 0) {
379 const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
380 // we check one at a time, skip search would be more efficient.
381 size_t i = overlimit;
382 for (; i < mItems.size(); ++i) {
383 auto &oitem = mItems[i];
Ray Essickf65f4212017-08-31 11:41:19 -0700384 nsecs_t when = oitem->getTimestamp();
Andy Hung82a074b2019-12-03 14:16:45 -0800385 if (oitem.get() == item.get()) {
Ray Essicke5db6db2017-11-10 15:54:32 -0800386 break;
387 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700388 if (now > when && (now - when) <= mMaxRecordAgeNs) {
Andy Hunged416da2020-03-05 18:42:55 -0800389 break; // Note SYSTEM_TIME_REALTIME may not be monotonic.
Ray Essickf65f4212017-08-31 11:41:19 -0700390 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700391 if (i >= mMaxRecordsExpiredAtOnce) {
Ray Essick72a436b2018-06-14 15:08:13 -0700392 // this represents "one too many"; tell caller there are
393 // more to be reclaimed.
394 more = true;
395 break;
396 }
Ray Essick3938dc62016-11-01 08:56:56 -0700397 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700398 expired = i - overlimit;
Ray Essick3938dc62016-11-01 08:56:56 -0700399 }
Ray Essick72a436b2018-06-14 15:08:13 -0700400
Andy Hung17dbaf22019-10-11 14:06:31 -0700401 if (const size_t toErase = overlimit + expired;
402 toErase > 0) {
403 mItemsDiscardedCount += overlimit;
404 mItemsDiscardedExpire += expired;
405 mItemsDiscarded += toErase;
406 mItems.erase(mItems.begin(), mItems.begin() + toErase); // erase from front
407 }
Ray Essick72a436b2018-06-14 15:08:13 -0700408 return more;
409}
410
Ray Essickf27e9872019-12-07 06:28:46 -0800411void MediaMetricsService::processExpirations()
Ray Essick72a436b2018-06-14 15:08:13 -0700412{
413 bool more;
414 do {
415 sleep(1);
Andy Hung17dbaf22019-10-11 14:06:31 -0700416 std::lock_guard _l(mLock);
417 more = expirations_l(nullptr);
Ray Essick72a436b2018-06-14 15:08:13 -0700418 } while (more);
Ray Essick72a436b2018-06-14 15:08:13 -0700419}
420
Ray Essickf27e9872019-12-07 06:28:46 -0800421void MediaMetricsService::saveItem(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick72a436b2018-06-14 15:08:13 -0700422{
Andy Hung17dbaf22019-10-11 14:06:31 -0700423 std::lock_guard _l(mLock);
424 // we assume the items are roughly in time order.
425 mItems.emplace_back(item);
426 ++mItemsFinalized;
427 if (expirations_l(item)
428 && (!mExpireFuture.valid()
429 || mExpireFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)) {
430 mExpireFuture = std::async(std::launch::async, [this] { processExpirations(); });
Ray Essick72a436b2018-06-14 15:08:13 -0700431 }
Ray Essick3938dc62016-11-01 08:56:56 -0700432}
433
Andy Hung17dbaf22019-10-11 14:06:31 -0700434/* static */
Ray Essickf27e9872019-12-07 06:28:46 -0800435bool MediaMetricsService::isContentValid(const mediametrics::Item *item, bool isTrusted)
Ray Essickd38e1742017-01-23 15:17:06 -0800436{
Andy Hung17dbaf22019-10-11 14:06:31 -0700437 if (isTrusted) return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800438 // untrusted uids can only send us a limited set of keys
Andy Hung17dbaf22019-10-11 14:06:31 -0700439 const std::string &key = item->getKey();
Andy Hung06f3aba2019-12-03 16:36:42 -0800440 if (startsWith(key, "audio.")) return true;
Edwin Wong8d188352020-02-11 11:59:34 -0800441 if (startsWith(key, "drm.vendor.")) return true;
442 // the list of allowedKey uses statsd_handlers
443 // in iface_statsd.cpp as reference
444 // drmmanager is from a trusted uid, therefore not needed here
Andy Hung17dbaf22019-10-11 14:06:31 -0700445 for (const char *allowedKey : {
Andy Hung06f3aba2019-12-03 16:36:42 -0800446 // legacy audio
Andy Hung17dbaf22019-10-11 14:06:31 -0700447 "audiopolicy",
448 "audiorecord",
449 "audiothread",
450 "audiotrack",
Andy Hung06f3aba2019-12-03 16:36:42 -0800451 // other media
Andy Hung17dbaf22019-10-11 14:06:31 -0700452 "codec",
453 "extractor",
Edwin Wong8d188352020-02-11 11:59:34 -0800454 "mediadrm",
Andy Hung17dbaf22019-10-11 14:06:31 -0700455 "nuplayer",
456 }) {
457 if (key == allowedKey) {
458 return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800459 }
460 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700461 ALOGD("%s: invalid key: %s", __func__, item->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -0700462 return false;
463}
464
Andy Hung17dbaf22019-10-11 14:06:31 -0700465// are we rate limited, normally false
Ray Essickf27e9872019-12-07 06:28:46 -0800466bool MediaMetricsService::isRateLimited(mediametrics::Item *) const
Andy Hung17dbaf22019-10-11 14:06:31 -0700467{
468 return false;
469}
470
Ray Essick3938dc62016-11-01 08:56:56 -0700471} // namespace android