blob: 3b95f7a757d49cebfa3f39533049589102f26feb [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),
Ray Essick72a436b2018-06-14 15:08:13 -070082 mMaxRecordsExpiredAtOnce(kMaxExpiredAtOnce),
Ray Essickf27e9872019-12-07 06:28:46 -080083 mDumpProtoDefault(mediametrics::Item::PROTO_V1)
Andy Hung17dbaf22019-10-11 14:06:31 -070084{
85 ALOGD("%s", __func__);
Ray Essick3938dc62016-11-01 08:56:56 -070086}
87
Ray Essickf27e9872019-12-07 06:28:46 -080088MediaMetricsService::~MediaMetricsService()
Andy Hung17dbaf22019-10-11 14:06:31 -070089{
90 ALOGD("%s", __func__);
91 // the class destructor clears anyhow, but we enforce clearing items first.
92 mItemsDiscarded += mItems.size();
93 mItems.clear();
Ray Essick3938dc62016-11-01 08:56:56 -070094}
95
Ray Essickf27e9872019-12-07 06:28:46 -080096status_t MediaMetricsService::submitInternal(mediametrics::Item *item, bool release)
Ray Essick92d23b42018-01-29 12:10:30 -080097{
Andy Hung55aaf522019-12-03 15:07:51 -080098 // calling PID is 0 for one-way calls.
99 const pid_t pid = IPCThreadState::self()->getCallingPid();
100 const pid_t pid_given = item->getPid();
101 const uid_t uid = IPCThreadState::self()->getCallingUid();
102 const uid_t uid_given = item->getUid();
Ray Essickd38e1742017-01-23 15:17:06 -0800103
Andy Hung55aaf522019-12-03 15:07:51 -0800104 //ALOGD("%s: caller pid=%d uid=%d, item pid=%d uid=%d", __func__,
105 // (int)pid, (int)uid, (int) pid_given, (int)uid_given);
Ray Essickd38e1742017-01-23 15:17:06 -0800106
Andy Hung17dbaf22019-10-11 14:06:31 -0700107 bool isTrusted;
108 switch (uid) {
Andy Hung55aaf522019-12-03 15:07:51 -0800109 case AID_AUDIOSERVER:
110 case AID_BLUETOOTH:
111 case AID_CAMERA:
Andy Hung17dbaf22019-10-11 14:06:31 -0700112 case AID_DRM:
113 case AID_MEDIA:
114 case AID_MEDIA_CODEC:
115 case AID_MEDIA_EX:
116 case AID_MEDIA_DRM:
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 Hung17dbaf22019-10-11 14:06:31 -0700149 ALOGV("%s: given uid %d; sanitized uid: %d sanitized pkg: %s "
150 "sanitized pkg version: %lld",
151 __func__,
Adam Stone21c72122017-09-05 19:02:06 -0700152 uid_given, item->getUid(),
153 item->getPkgName().c_str(),
Andy Hung17dbaf22019-10-11 14:06:31 -0700154 (long long)item->getPkgVersionCode());
Ray Essick3938dc62016-11-01 08:56:56 -0700155
156 mItemsSubmitted++;
157
158 // validate the record; we discard if we don't like it
Andy Hung17dbaf22019-10-11 14:06:31 -0700159 if (isContentValid(item, isTrusted) == false) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700160 if (release) delete item;
161 return PERMISSION_DENIED;
Ray Essick3938dc62016-11-01 08:56:56 -0700162 }
163
Ray Essick92d23b42018-01-29 12:10:30 -0800164 // XXX: if we have a sessionid in the new record, look to make
Ray Essick3938dc62016-11-01 08:56:56 -0700165 // sure it doesn't appear in the finalized list.
Ray Essick3938dc62016-11-01 08:56:56 -0700166
Ray Essick92d23b42018-01-29 12:10:30 -0800167 if (item->count() == 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700168 ALOGV("%s: dropping empty record...", __func__);
Andy Hunga87e69c2019-10-18 10:07:40 -0700169 if (release) delete item;
170 return BAD_VALUE;
Ray Essick3938dc62016-11-01 08:56:56 -0700171 }
Ray Essick92d23b42018-01-29 12:10:30 -0800172
Andy Hung55aaf522019-12-03 15:07:51 -0800173 if (!isTrusted || item->getTimestamp() == 0) {
174 // WestWorld logs two times for events: ElapsedRealTimeNs (BOOTTIME) and
175 // WallClockTimeNs (REALTIME). The new audio keys use BOOTTIME.
176 //
177 // TODO: Reevaluate time base with other teams.
178 const bool useBootTime = startsWith(item->getKey(), "audio.");
179 const int64_t now = systemTime(useBootTime ? SYSTEM_TIME_BOOTTIME : SYSTEM_TIME_REALTIME);
180 item->setTimestamp(now);
181 }
182
Andy Hung82a074b2019-12-03 14:16:45 -0800183 // now attach either the item or its dup to a const shared pointer
Ray Essickf27e9872019-12-07 06:28:46 -0800184 std::shared_ptr<const mediametrics::Item> sitem(release ? item : item->dup());
Ray Essick6ce27e52019-02-15 10:58:05 -0800185
Andy Hung06f3aba2019-12-03 16:36:42 -0800186 (void)mAudioAnalytics.submit(sitem, isTrusted);
187
Ray Essickf27e9872019-12-07 06:28:46 -0800188 extern bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item);
Andy Hung82a074b2019-12-03 14:16:45 -0800189 (void)dump2Statsd(sitem); // failure should be logged in function.
190 saveItem(sitem);
Andy Hunga87e69c2019-10-18 10:07:40 -0700191 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700192}
193
Ray Essickf27e9872019-12-07 06:28:46 -0800194status_t MediaMetricsService::dump(int fd, const Vector<String16>& args)
Ray Essick3938dc62016-11-01 08:56:56 -0700195{
Ray Essick3938dc62016-11-01 08:56:56 -0700196 String8 result;
197
198 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700199 result.appendFormat("Permission Denial: "
Ray Essickf27e9872019-12-07 06:28:46 -0800200 "can't dump MediaMetricsService from pid=%d, uid=%d\n",
Ray Essick3938dc62016-11-01 08:56:56 -0700201 IPCThreadState::self()->getCallingPid(),
202 IPCThreadState::self()->getCallingUid());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800203 write(fd, result.string(), result.size());
204 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700205 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800206
207 // crack any parameters
Andy Hung17dbaf22019-10-11 14:06:31 -0700208 const String16 protoOption("-proto");
Ray Essick583a23a2017-11-27 12:49:57 -0800209 int chosenProto = mDumpProtoDefault;
Andy Hung17dbaf22019-10-11 14:06:31 -0700210 const String16 clearOption("-clear");
Ray Essickf65f4212017-08-31 11:41:19 -0700211 bool clear = false;
Andy Hung17dbaf22019-10-11 14:06:31 -0700212 const String16 sinceOption("-since");
Ray Essickf65f4212017-08-31 11:41:19 -0700213 nsecs_t ts_since = 0;
Andy Hung17dbaf22019-10-11 14:06:31 -0700214 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) {
224 String8 value(args[i]);
Ray Essickf27e9872019-12-07 06:28:46 -0800225 int proto = mediametrics::Item::PROTO_V0;
Ray Essickf65f4212017-08-31 11:41:19 -0700226 char *endp;
227 const char *p = value.string();
228 proto = strtol(p, &endp, 10);
229 if (endp != p || *endp == '\0') {
Ray Essickf27e9872019-12-07 06:28:46 -0800230 if (proto < mediametrics::Item::PROTO_FIRST) {
231 proto = mediametrics::Item::PROTO_FIRST;
232 } else if (proto > mediametrics::Item::PROTO_LAST) {
233 proto = mediametrics::Item::PROTO_LAST;
Ray Essickf65f4212017-08-31 11:41:19 -0700234 }
Ray Essick583a23a2017-11-27 12:49:57 -0800235 chosenProto = proto;
236 } else {
237 result.append("unable to parse value for -proto\n\n");
Ray Essickf65f4212017-08-31 11:41:19 -0700238 }
Ray Essick583a23a2017-11-27 12:49:57 -0800239 } else {
240 result.append("missing value for -proto\n\n");
Ray Essickf65f4212017-08-31 11:41:19 -0700241 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800242 } else if (args[i] == sinceOption) {
243 i++;
244 if (i < n) {
245 String8 value(args[i]);
246 char *endp;
247 const char *p = value.string();
248 ts_since = strtoll(p, &endp, 10);
249 if (endp == p || *endp != '\0') {
250 ts_since = 0;
251 }
252 } else {
253 ts_since = 0;
254 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800255 // command line is milliseconds; internal units are nano-seconds
Andy Hung17dbaf22019-10-11 14:06:31 -0700256 ts_since *= NANOS_PER_MILLISECOND;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700257 } else if (args[i] == onlyOption) {
258 i++;
259 if (i < n) {
260 String8 value(args[i]);
Ray Essickf65f4212017-08-31 11:41:19 -0700261 only = value.string();
Ray Essick2e9c63b2017-03-29 15:16:44 -0700262 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800263 } else if (args[i] == helpOption) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700264 // TODO: consider function area dumping.
265 // dumpsys media.metrics audiotrack,codec
266 // or dumpsys media.metrics audiotrack codec
267
Ray Essick35ad27f2017-01-30 14:04:11 -0800268 result.append("Recognized parameters:\n");
269 result.append("-help this help message\n");
Ray Essick583a23a2017-11-27 12:49:57 -0800270 result.append("-proto # dump using protocol #");
Ray Essick35ad27f2017-01-30 14:04:11 -0800271 result.append("-clear clears out saved records\n");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700272 result.append("-only X process records for component X\n");
273 result.append("-since X include records since X\n");
274 result.append(" (X is milliseconds since the UNIX epoch)\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800275 write(fd, result.string(), result.size());
276 return NO_ERROR;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800277 }
278 }
279
Andy Hung17dbaf22019-10-11 14:06:31 -0700280 {
281 std::lock_guard _l(mLock);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800282
Andy Hung17dbaf22019-10-11 14:06:31 -0700283 result.appendFormat("Dump of the %s process:\n", kServiceName);
284 dumpHeaders_l(result, chosenProto, ts_since);
285 dumpRecent_l(result, chosenProto, ts_since, only.c_str());
Ray Essick583a23a2017-11-27 12:49:57 -0800286
Andy Hung17dbaf22019-10-11 14:06:31 -0700287 if (clear) {
288 mItemsDiscarded += mItems.size();
289 mItems.clear();
290 // shall we clear the summary data too?
Ray Essick2e9c63b2017-03-29 15:16:44 -0700291 }
Andy Hung06f3aba2019-12-03 16:36:42 -0800292 // TODO: maybe consider a better way of dumping audio analytics info.
293 constexpr int32_t linesToDump = 1000;
Andy Hung0f7ad8c2020-01-03 13:24:34 -0800294 auto [ dumpString, lines ] = mAudioAnalytics.dump(linesToDump);
295 result.append(dumpString.c_str());
296 if (lines == linesToDump) {
297 result.append("-- some lines may be truncated --\n");
298 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700299 }
300
301 write(fd, result.string(), result.size());
302 return NO_ERROR;
303}
304
305// dump headers
Ray Essickf27e9872019-12-07 06:28:46 -0800306void MediaMetricsService::dumpHeaders_l(String8 &result, int dumpProto, nsecs_t ts_since)
Ray Essick92d23b42018-01-29 12:10:30 -0800307{
Andy Hung17dbaf22019-10-11 14:06:31 -0700308 result.appendFormat("Protocol Version: %d\n", dumpProto);
Ray Essickf27e9872019-12-07 06:28:46 -0800309 if (mediametrics::Item::isEnabled()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700310 result.append("Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800311 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700312 result.append("Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800313 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700314 result.appendFormat(
315 "Since Boot: Submissions: %lld Accepted: %lld\n",
316 (long long)mItemsSubmitted.load(), (long long)mItemsFinalized);
317 result.appendFormat(
318 "Records Discarded: %lld (by Count: %lld by Expiration: %lld)\n",
319 (long long)mItemsDiscarded, (long long)mItemsDiscardedCount,
320 (long long)mItemsDiscardedExpire);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800321 if (ts_since != 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700322 result.appendFormat(
323 "Emitting Queue entries more recent than: %lld\n",
324 (long long)ts_since);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800325 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700326}
327
Ray Essickf27e9872019-12-07 06:28:46 -0800328void MediaMetricsService::dumpRecent_l(
Andy Hung17dbaf22019-10-11 14:06:31 -0700329 String8 &result, int dumpProto, nsecs_t ts_since, const char * only)
Ray Essick92d23b42018-01-29 12:10:30 -0800330{
Andy Hung17dbaf22019-10-11 14:06:31 -0700331 if (only != nullptr && *only == '\0') {
332 only = nullptr;
Ray Essickf65f4212017-08-31 11:41:19 -0700333 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700334 result.append("\nFinalized Metrics (oldest first):\n");
335 dumpQueue_l(result, dumpProto, ts_since, only);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800336
337 // show who is connected and injecting records?
338 // talk about # records fed to the 'readers'
339 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700340}
Ray Essick92d23b42018-01-29 12:10:30 -0800341
Ray Essickf27e9872019-12-07 06:28:46 -0800342void MediaMetricsService::dumpQueue_l(String8 &result, int dumpProto) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700343 dumpQueue_l(result, dumpProto, (nsecs_t) 0, nullptr /* only */);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800344}
345
Ray Essickf27e9872019-12-07 06:28:46 -0800346void MediaMetricsService::dumpQueue_l(
Andy Hung17dbaf22019-10-11 14:06:31 -0700347 String8 &result, int dumpProto, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700348 int slot = 0;
349
Ray Essick92d23b42018-01-29 12:10:30 -0800350 if (mItems.empty()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700351 result.append("empty\n");
Ray Essick3938dc62016-11-01 08:56:56 -0700352 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700353 for (const auto &item : mItems) {
354 nsecs_t when = item->getTimestamp();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800355 if (when < ts_since) {
356 continue;
357 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700358 // TODO: Only should be a set<string>
359 if (only != nullptr &&
360 item->getKey() /* std::string */ != only) {
361 ALOGV("%s: omit '%s', it's not '%s'",
362 __func__, item->getKey().c_str(), only);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700363 continue;
364 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700365 result.appendFormat("%5d: %s\n",
366 slot, item->toString(dumpProto).c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800367 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700368 }
369 }
Ray Essick3938dc62016-11-01 08:56:56 -0700370}
371
372//
373// Our Cheap in-core, non-persistent records management.
Ray Essick3938dc62016-11-01 08:56:56 -0700374
Ray Essick72a436b2018-06-14 15:08:13 -0700375// if item != NULL, it's the item we just inserted
376// true == more items eligible to be recovered
Ray Essickf27e9872019-12-07 06:28:46 -0800377bool MediaMetricsService::expirations_l(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick92d23b42018-01-29 12:10:30 -0800378{
Ray Essick72a436b2018-06-14 15:08:13 -0700379 bool more = false;
Ray Essicke5db6db2017-11-10 15:54:32 -0800380
Andy Hung17dbaf22019-10-11 14:06:31 -0700381 // check queue size
382 size_t overlimit = 0;
383 if (mMaxRecords > 0 && mItems.size() > mMaxRecords) {
384 overlimit = mItems.size() - mMaxRecords;
385 if (overlimit > mMaxRecordsExpiredAtOnce) {
386 more = true;
387 overlimit = mMaxRecordsExpiredAtOnce;
Ray Essickf65f4212017-08-31 11:41:19 -0700388 }
389 }
390
Andy Hung17dbaf22019-10-11 14:06:31 -0700391 // check queue times
392 size_t expired = 0;
393 if (!more && mMaxRecordAgeNs > 0) {
394 const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
395 // we check one at a time, skip search would be more efficient.
396 size_t i = overlimit;
397 for (; i < mItems.size(); ++i) {
398 auto &oitem = mItems[i];
Ray Essickf65f4212017-08-31 11:41:19 -0700399 nsecs_t when = oitem->getTimestamp();
Andy Hung82a074b2019-12-03 14:16:45 -0800400 if (oitem.get() == item.get()) {
Ray Essicke5db6db2017-11-10 15:54:32 -0800401 break;
402 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700403 if (now > when && (now - when) <= mMaxRecordAgeNs) {
404 break; // TODO: if we use BOOTTIME, should be monotonic.
Ray Essickf65f4212017-08-31 11:41:19 -0700405 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700406 if (i >= mMaxRecordsExpiredAtOnce) {
Ray Essick72a436b2018-06-14 15:08:13 -0700407 // this represents "one too many"; tell caller there are
408 // more to be reclaimed.
409 more = true;
410 break;
411 }
Ray Essick3938dc62016-11-01 08:56:56 -0700412 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700413 expired = i - overlimit;
Ray Essick3938dc62016-11-01 08:56:56 -0700414 }
Ray Essick72a436b2018-06-14 15:08:13 -0700415
Andy Hung17dbaf22019-10-11 14:06:31 -0700416 if (const size_t toErase = overlimit + expired;
417 toErase > 0) {
418 mItemsDiscardedCount += overlimit;
419 mItemsDiscardedExpire += expired;
420 mItemsDiscarded += toErase;
421 mItems.erase(mItems.begin(), mItems.begin() + toErase); // erase from front
422 }
Ray Essick72a436b2018-06-14 15:08:13 -0700423 return more;
424}
425
Ray Essickf27e9872019-12-07 06:28:46 -0800426void MediaMetricsService::processExpirations()
Ray Essick72a436b2018-06-14 15:08:13 -0700427{
428 bool more;
429 do {
430 sleep(1);
Andy Hung17dbaf22019-10-11 14:06:31 -0700431 std::lock_guard _l(mLock);
432 more = expirations_l(nullptr);
Ray Essick72a436b2018-06-14 15:08:13 -0700433 } while (more);
Ray Essick72a436b2018-06-14 15:08:13 -0700434}
435
Ray Essickf27e9872019-12-07 06:28:46 -0800436void MediaMetricsService::saveItem(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick72a436b2018-06-14 15:08:13 -0700437{
Andy Hung17dbaf22019-10-11 14:06:31 -0700438 std::lock_guard _l(mLock);
439 // we assume the items are roughly in time order.
440 mItems.emplace_back(item);
441 ++mItemsFinalized;
442 if (expirations_l(item)
443 && (!mExpireFuture.valid()
444 || mExpireFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)) {
445 mExpireFuture = std::async(std::launch::async, [this] { processExpirations(); });
Ray Essick72a436b2018-06-14 15:08:13 -0700446 }
Ray Essick3938dc62016-11-01 08:56:56 -0700447}
448
Andy Hung17dbaf22019-10-11 14:06:31 -0700449/* static */
Ray Essickf27e9872019-12-07 06:28:46 -0800450bool MediaMetricsService::isContentValid(const mediametrics::Item *item, bool isTrusted)
Ray Essickd38e1742017-01-23 15:17:06 -0800451{
Andy Hung17dbaf22019-10-11 14:06:31 -0700452 if (isTrusted) return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800453 // untrusted uids can only send us a limited set of keys
Andy Hung17dbaf22019-10-11 14:06:31 -0700454 const std::string &key = item->getKey();
Andy Hung06f3aba2019-12-03 16:36:42 -0800455 if (startsWith(key, "audio.")) return true;
Andy Hung17dbaf22019-10-11 14:06:31 -0700456 for (const char *allowedKey : {
Andy Hung06f3aba2019-12-03 16:36:42 -0800457 // legacy audio
Andy Hung17dbaf22019-10-11 14:06:31 -0700458 "audiopolicy",
459 "audiorecord",
460 "audiothread",
461 "audiotrack",
Andy Hung06f3aba2019-12-03 16:36:42 -0800462 // other media
Andy Hung17dbaf22019-10-11 14:06:31 -0700463 "codec",
464 "extractor",
465 "nuplayer",
466 }) {
467 if (key == allowedKey) {
468 return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800469 }
470 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700471 ALOGD("%s: invalid key: %s", __func__, item->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -0700472 return false;
473}
474
Andy Hung17dbaf22019-10-11 14:06:31 -0700475// are we rate limited, normally false
Ray Essickf27e9872019-12-07 06:28:46 -0800476bool MediaMetricsService::isRateLimited(mediametrics::Item *) const
Andy Hung17dbaf22019-10-11 14:06:31 -0700477{
478 return false;
479}
480
Ray Essick3938dc62016-11-01 08:56:56 -0700481} // namespace android