blob: 811e135c3b3379383a35a0ab35a5571f7f2d0644 [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 Hung55aaf522019-12-03 15:07:51 -0800116 case AID_SYSTEM:
Andy Hung17dbaf22019-10-11 14:06:31 -0700117 // trusted source, only override default values
118 isTrusted = true;
Andy Hung55aaf522019-12-03 15:07:51 -0800119 if (uid_given == (uid_t)-1) {
Ray Essickd38e1742017-01-23 15:17:06 -0800120 item->setUid(uid);
Andy Hung17dbaf22019-10-11 14:06:31 -0700121 }
Andy Hung55aaf522019-12-03 15:07:51 -0800122 if (pid_given == (pid_t)-1) {
123 item->setPid(pid); // if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700124 }
125 break;
126 default:
127 isTrusted = false;
Andy Hung55aaf522019-12-03 15:07:51 -0800128 item->setPid(pid); // always use calling pid, if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700129 item->setUid(uid);
130 break;
Ray Essickd38e1742017-01-23 15:17:06 -0800131 }
132
Andy Hunga85efab2019-12-23 11:41:29 -0800133 // Overwrite package name and version if the caller was untrusted or empty
134 if (!isTrusted || item->getPkgName().empty()) {
135 const uid_t uid = item->getUid();
136 mediautils::UidInfo::Info info = mUidInfo.getInfo(uid);
137 if (useUidForPackage(info.package, info.installer)) {
138 // remove uid information of unknown installed packages.
139 // TODO: perhaps this can be done just before uploading to Westworld.
140 item->setPkgName(std::to_string(uid));
141 item->setPkgVersionCode(0);
142 } else {
143 item->setPkgName(info.package);
144 item->setPkgVersionCode(info.versionCode);
145 }
Adam Stone21c72122017-09-05 19:02:06 -0700146 }
147
Andy Hung17dbaf22019-10-11 14:06:31 -0700148 ALOGV("%s: given uid %d; sanitized uid: %d sanitized pkg: %s "
149 "sanitized pkg version: %lld",
150 __func__,
Adam Stone21c72122017-09-05 19:02:06 -0700151 uid_given, item->getUid(),
152 item->getPkgName().c_str(),
Andy Hung17dbaf22019-10-11 14:06:31 -0700153 (long long)item->getPkgVersionCode());
Ray Essick3938dc62016-11-01 08:56:56 -0700154
155 mItemsSubmitted++;
156
157 // validate the record; we discard if we don't like it
Andy Hung17dbaf22019-10-11 14:06:31 -0700158 if (isContentValid(item, isTrusted) == false) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700159 if (release) delete item;
160 return PERMISSION_DENIED;
Ray Essick3938dc62016-11-01 08:56:56 -0700161 }
162
Ray Essick92d23b42018-01-29 12:10:30 -0800163 // XXX: if we have a sessionid in the new record, look to make
Ray Essick3938dc62016-11-01 08:56:56 -0700164 // sure it doesn't appear in the finalized list.
Ray Essick3938dc62016-11-01 08:56:56 -0700165
Ray Essick92d23b42018-01-29 12:10:30 -0800166 if (item->count() == 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700167 ALOGV("%s: dropping empty record...", __func__);
Andy Hunga87e69c2019-10-18 10:07:40 -0700168 if (release) delete item;
169 return BAD_VALUE;
Ray Essick3938dc62016-11-01 08:56:56 -0700170 }
Ray Essick92d23b42018-01-29 12:10:30 -0800171
Andy Hung55aaf522019-12-03 15:07:51 -0800172 if (!isTrusted || item->getTimestamp() == 0) {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800173 // Westworld logs two times for events: ElapsedRealTimeNs (BOOTTIME) and
174 // WallClockTimeNs (REALTIME), but currently logs REALTIME to cloud.
Andy Hung55aaf522019-12-03 15:07:51 -0800175 //
Andy Hung3b4c1f02020-01-23 18:58:32 -0800176 // For consistency and correlation with other logging mechanisms
177 // we use REALTIME here.
178 const int64_t now = systemTime(SYSTEM_TIME_REALTIME);
Andy Hung55aaf522019-12-03 15:07:51 -0800179 item->setTimestamp(now);
180 }
181
Andy Hung82a074b2019-12-03 14:16:45 -0800182 // now attach either the item or its dup to a const shared pointer
Ray Essickf27e9872019-12-07 06:28:46 -0800183 std::shared_ptr<const mediametrics::Item> sitem(release ? item : item->dup());
Ray Essick6ce27e52019-02-15 10:58:05 -0800184
Andy Hung06f3aba2019-12-03 16:36:42 -0800185 (void)mAudioAnalytics.submit(sitem, isTrusted);
186
Ray Essickf27e9872019-12-07 06:28:46 -0800187 extern bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item);
Andy Hung82a074b2019-12-03 14:16:45 -0800188 (void)dump2Statsd(sitem); // failure should be logged in function.
189 saveItem(sitem);
Andy Hunga87e69c2019-10-18 10:07:40 -0700190 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700191}
192
Ray Essickf27e9872019-12-07 06:28:46 -0800193status_t MediaMetricsService::dump(int fd, const Vector<String16>& args)
Ray Essick3938dc62016-11-01 08:56:56 -0700194{
Ray Essick3938dc62016-11-01 08:56:56 -0700195 String8 result;
196
197 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700198 result.appendFormat("Permission Denial: "
Ray Essickf27e9872019-12-07 06:28:46 -0800199 "can't dump MediaMetricsService from pid=%d, uid=%d\n",
Ray Essick3938dc62016-11-01 08:56:56 -0700200 IPCThreadState::self()->getCallingPid(),
201 IPCThreadState::self()->getCallingUid());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800202 write(fd, result.string(), result.size());
203 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700204 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800205
206 // crack any parameters
Andy Hung333bb3d2020-01-31 20:17:48 -0800207 const String16 protoOption("--proto");
208 const String16 clearOption("--clear");
Ray Essickf65f4212017-08-31 11:41:19 -0700209 bool clear = false;
Andy Hung333bb3d2020-01-31 20:17:48 -0800210 const String16 sinceOption("--since");
Ray Essickf65f4212017-08-31 11:41:19 -0700211 nsecs_t ts_since = 0;
Andy Hung333bb3d2020-01-31 20:17:48 -0800212 const String16 helpOption("--help");
213 const String16 onlyOption("--only");
Ray Essick783bd0d2018-01-11 11:10:35 -0800214 std::string only;
Andy Hung17dbaf22019-10-11 14:06:31 -0700215 const int n = args.size();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800216 for (int i = 0; i < n; i++) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800217 if (args[i] == clearOption) {
218 clear = true;
Ray Essickf65f4212017-08-31 11:41:19 -0700219 } else if (args[i] == protoOption) {
220 i++;
221 if (i < n) {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800222 // ignore
Ray Essick583a23a2017-11-27 12:49:57 -0800223 } else {
224 result.append("missing value for -proto\n\n");
Ray Essickf65f4212017-08-31 11:41:19 -0700225 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800226 } else if (args[i] == sinceOption) {
227 i++;
228 if (i < n) {
229 String8 value(args[i]);
230 char *endp;
231 const char *p = value.string();
232 ts_since = strtoll(p, &endp, 10);
233 if (endp == p || *endp != '\0') {
234 ts_since = 0;
235 }
236 } else {
237 ts_since = 0;
238 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800239 // command line is milliseconds; internal units are nano-seconds
Andy Hung17dbaf22019-10-11 14:06:31 -0700240 ts_since *= NANOS_PER_MILLISECOND;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700241 } else if (args[i] == onlyOption) {
242 i++;
243 if (i < n) {
244 String8 value(args[i]);
Ray Essickf65f4212017-08-31 11:41:19 -0700245 only = value.string();
Ray Essick2e9c63b2017-03-29 15:16:44 -0700246 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800247 } else if (args[i] == helpOption) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700248 // TODO: consider function area dumping.
249 // dumpsys media.metrics audiotrack,codec
250 // or dumpsys media.metrics audiotrack codec
251
Ray Essick35ad27f2017-01-30 14:04:11 -0800252 result.append("Recognized parameters:\n");
Andy Hung333bb3d2020-01-31 20:17:48 -0800253 result.append("--help this help message\n");
254 result.append("--proto # dump using protocol #");
255 result.append("--clear clears out saved records\n");
256 result.append("--only X process records for component X\n");
257 result.append("--since X include records since X\n");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700258 result.append(" (X is milliseconds since the UNIX epoch)\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800259 write(fd, result.string(), result.size());
260 return NO_ERROR;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800261 }
262 }
263
Andy Hung17dbaf22019-10-11 14:06:31 -0700264 {
265 std::lock_guard _l(mLock);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800266
Andy Hung17dbaf22019-10-11 14:06:31 -0700267 result.appendFormat("Dump of the %s process:\n", kServiceName);
Andy Hung3b4c1f02020-01-23 18:58:32 -0800268 dumpHeaders_l(result, ts_since);
269 dumpRecent_l(result, ts_since, only.c_str());
Ray Essick583a23a2017-11-27 12:49:57 -0800270
Andy Hung17dbaf22019-10-11 14:06:31 -0700271 if (clear) {
272 mItemsDiscarded += mItems.size();
273 mItems.clear();
274 // shall we clear the summary data too?
Ray Essick2e9c63b2017-03-29 15:16:44 -0700275 }
Andy Hung06f3aba2019-12-03 16:36:42 -0800276 // TODO: maybe consider a better way of dumping audio analytics info.
277 constexpr int32_t linesToDump = 1000;
Andy Hung0f7ad8c2020-01-03 13:24:34 -0800278 auto [ dumpString, lines ] = mAudioAnalytics.dump(linesToDump);
279 result.append(dumpString.c_str());
280 if (lines == linesToDump) {
281 result.append("-- some lines may be truncated --\n");
282 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700283 }
284
285 write(fd, result.string(), result.size());
286 return NO_ERROR;
287}
288
289// dump headers
Andy Hung3b4c1f02020-01-23 18:58:32 -0800290void MediaMetricsService::dumpHeaders_l(String8 &result, nsecs_t ts_since)
Ray Essick92d23b42018-01-29 12:10:30 -0800291{
Ray Essickf27e9872019-12-07 06:28:46 -0800292 if (mediametrics::Item::isEnabled()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700293 result.append("Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800294 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700295 result.append("Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800296 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700297 result.appendFormat(
298 "Since Boot: Submissions: %lld Accepted: %lld\n",
299 (long long)mItemsSubmitted.load(), (long long)mItemsFinalized);
300 result.appendFormat(
301 "Records Discarded: %lld (by Count: %lld by Expiration: %lld)\n",
302 (long long)mItemsDiscarded, (long long)mItemsDiscardedCount,
303 (long long)mItemsDiscardedExpire);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800304 if (ts_since != 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700305 result.appendFormat(
306 "Emitting Queue entries more recent than: %lld\n",
307 (long long)ts_since);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800308 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700309}
310
Ray Essickf27e9872019-12-07 06:28:46 -0800311void MediaMetricsService::dumpRecent_l(
Andy Hung3b4c1f02020-01-23 18:58:32 -0800312 String8 &result, nsecs_t ts_since, const char * only)
Ray Essick92d23b42018-01-29 12:10:30 -0800313{
Andy Hung17dbaf22019-10-11 14:06:31 -0700314 if (only != nullptr && *only == '\0') {
315 only = nullptr;
Ray Essickf65f4212017-08-31 11:41:19 -0700316 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700317 result.append("\nFinalized Metrics (oldest first):\n");
Andy Hung3b4c1f02020-01-23 18:58:32 -0800318 dumpQueue_l(result, ts_since, only);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800319
320 // show who is connected and injecting records?
321 // talk about # records fed to the 'readers'
322 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700323}
Ray Essick92d23b42018-01-29 12:10:30 -0800324
Andy Hung3b4c1f02020-01-23 18:58:32 -0800325void MediaMetricsService::dumpQueue_l(String8 &result) {
326 dumpQueue_l(result, (nsecs_t) 0, nullptr /* only */);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800327}
328
Ray Essickf27e9872019-12-07 06:28:46 -0800329void MediaMetricsService::dumpQueue_l(
Andy Hung3b4c1f02020-01-23 18:58:32 -0800330 String8 &result, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700331 int slot = 0;
332
Ray Essick92d23b42018-01-29 12:10:30 -0800333 if (mItems.empty()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700334 result.append("empty\n");
Ray Essick3938dc62016-11-01 08:56:56 -0700335 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700336 for (const auto &item : mItems) {
337 nsecs_t when = item->getTimestamp();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800338 if (when < ts_since) {
339 continue;
340 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700341 // TODO: Only should be a set<string>
342 if (only != nullptr &&
343 item->getKey() /* std::string */ != only) {
344 ALOGV("%s: omit '%s', it's not '%s'",
345 __func__, item->getKey().c_str(), only);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700346 continue;
347 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700348 result.appendFormat("%5d: %s\n",
Andy Hung3b4c1f02020-01-23 18:58:32 -0800349 slot, item->toString().c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800350 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700351 }
352 }
Ray Essick3938dc62016-11-01 08:56:56 -0700353}
354
355//
356// Our Cheap in-core, non-persistent records management.
Ray Essick3938dc62016-11-01 08:56:56 -0700357
Ray Essick72a436b2018-06-14 15:08:13 -0700358// if item != NULL, it's the item we just inserted
359// true == more items eligible to be recovered
Ray Essickf27e9872019-12-07 06:28:46 -0800360bool MediaMetricsService::expirations_l(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick92d23b42018-01-29 12:10:30 -0800361{
Ray Essick72a436b2018-06-14 15:08:13 -0700362 bool more = false;
Ray Essicke5db6db2017-11-10 15:54:32 -0800363
Andy Hung17dbaf22019-10-11 14:06:31 -0700364 // check queue size
365 size_t overlimit = 0;
366 if (mMaxRecords > 0 && mItems.size() > mMaxRecords) {
367 overlimit = mItems.size() - mMaxRecords;
368 if (overlimit > mMaxRecordsExpiredAtOnce) {
369 more = true;
370 overlimit = mMaxRecordsExpiredAtOnce;
Ray Essickf65f4212017-08-31 11:41:19 -0700371 }
372 }
373
Andy Hung17dbaf22019-10-11 14:06:31 -0700374 // check queue times
375 size_t expired = 0;
376 if (!more && mMaxRecordAgeNs > 0) {
377 const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
378 // we check one at a time, skip search would be more efficient.
379 size_t i = overlimit;
380 for (; i < mItems.size(); ++i) {
381 auto &oitem = mItems[i];
Ray Essickf65f4212017-08-31 11:41:19 -0700382 nsecs_t when = oitem->getTimestamp();
Andy Hung82a074b2019-12-03 14:16:45 -0800383 if (oitem.get() == item.get()) {
Ray Essicke5db6db2017-11-10 15:54:32 -0800384 break;
385 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700386 if (now > when && (now - when) <= mMaxRecordAgeNs) {
387 break; // TODO: if we use BOOTTIME, should be monotonic.
Ray Essickf65f4212017-08-31 11:41:19 -0700388 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700389 if (i >= mMaxRecordsExpiredAtOnce) {
Ray Essick72a436b2018-06-14 15:08:13 -0700390 // this represents "one too many"; tell caller there are
391 // more to be reclaimed.
392 more = true;
393 break;
394 }
Ray Essick3938dc62016-11-01 08:56:56 -0700395 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700396 expired = i - overlimit;
Ray Essick3938dc62016-11-01 08:56:56 -0700397 }
Ray Essick72a436b2018-06-14 15:08:13 -0700398
Andy Hung17dbaf22019-10-11 14:06:31 -0700399 if (const size_t toErase = overlimit + expired;
400 toErase > 0) {
401 mItemsDiscardedCount += overlimit;
402 mItemsDiscardedExpire += expired;
403 mItemsDiscarded += toErase;
404 mItems.erase(mItems.begin(), mItems.begin() + toErase); // erase from front
405 }
Ray Essick72a436b2018-06-14 15:08:13 -0700406 return more;
407}
408
Ray Essickf27e9872019-12-07 06:28:46 -0800409void MediaMetricsService::processExpirations()
Ray Essick72a436b2018-06-14 15:08:13 -0700410{
411 bool more;
412 do {
413 sleep(1);
Andy Hung17dbaf22019-10-11 14:06:31 -0700414 std::lock_guard _l(mLock);
415 more = expirations_l(nullptr);
Ray Essick72a436b2018-06-14 15:08:13 -0700416 } while (more);
Ray Essick72a436b2018-06-14 15:08:13 -0700417}
418
Ray Essickf27e9872019-12-07 06:28:46 -0800419void MediaMetricsService::saveItem(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick72a436b2018-06-14 15:08:13 -0700420{
Andy Hung17dbaf22019-10-11 14:06:31 -0700421 std::lock_guard _l(mLock);
422 // we assume the items are roughly in time order.
423 mItems.emplace_back(item);
424 ++mItemsFinalized;
425 if (expirations_l(item)
426 && (!mExpireFuture.valid()
427 || mExpireFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)) {
428 mExpireFuture = std::async(std::launch::async, [this] { processExpirations(); });
Ray Essick72a436b2018-06-14 15:08:13 -0700429 }
Ray Essick3938dc62016-11-01 08:56:56 -0700430}
431
Andy Hung17dbaf22019-10-11 14:06:31 -0700432/* static */
Ray Essickf27e9872019-12-07 06:28:46 -0800433bool MediaMetricsService::isContentValid(const mediametrics::Item *item, bool isTrusted)
Ray Essickd38e1742017-01-23 15:17:06 -0800434{
Andy Hung17dbaf22019-10-11 14:06:31 -0700435 if (isTrusted) return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800436 // untrusted uids can only send us a limited set of keys
Andy Hung17dbaf22019-10-11 14:06:31 -0700437 const std::string &key = item->getKey();
Andy Hung06f3aba2019-12-03 16:36:42 -0800438 if (startsWith(key, "audio.")) return true;
Edwin Wong8d188352020-02-11 11:59:34 -0800439 if (startsWith(key, "drm.vendor.")) return true;
440 // the list of allowedKey uses statsd_handlers
441 // in iface_statsd.cpp as reference
442 // drmmanager is from a trusted uid, therefore not needed here
Andy Hung17dbaf22019-10-11 14:06:31 -0700443 for (const char *allowedKey : {
Andy Hung06f3aba2019-12-03 16:36:42 -0800444 // legacy audio
Andy Hung17dbaf22019-10-11 14:06:31 -0700445 "audiopolicy",
446 "audiorecord",
447 "audiothread",
448 "audiotrack",
Andy Hung06f3aba2019-12-03 16:36:42 -0800449 // other media
Andy Hung17dbaf22019-10-11 14:06:31 -0700450 "codec",
451 "extractor",
Edwin Wong8d188352020-02-11 11:59:34 -0800452 "mediadrm",
Andy Hung17dbaf22019-10-11 14:06:31 -0700453 "nuplayer",
454 }) {
455 if (key == allowedKey) {
456 return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800457 }
458 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700459 ALOGD("%s: invalid key: %s", __func__, item->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -0700460 return false;
461}
462
Andy Hung17dbaf22019-10-11 14:06:31 -0700463// are we rate limited, normally false
Ray Essickf27e9872019-12-07 06:28:46 -0800464bool MediaMetricsService::isRateLimited(mediametrics::Item *) const
Andy Hung17dbaf22019-10-11 14:06:31 -0700465{
466 return false;
467}
468
Ray Essick3938dc62016-11-01 08:56:56 -0700469} // namespace android