blob: b5bdd6f62646702d4290dd22f468092fda0311b7 [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;
294 result.append(mAudioAnalytics.dump(linesToDump).first.c_str());
Ray Essick2e9c63b2017-03-29 15:16:44 -0700295 }
296
297 write(fd, result.string(), result.size());
298 return NO_ERROR;
299}
300
301// dump headers
Ray Essickf27e9872019-12-07 06:28:46 -0800302void MediaMetricsService::dumpHeaders_l(String8 &result, int dumpProto, nsecs_t ts_since)
Ray Essick92d23b42018-01-29 12:10:30 -0800303{
Andy Hung17dbaf22019-10-11 14:06:31 -0700304 result.appendFormat("Protocol Version: %d\n", dumpProto);
Ray Essickf27e9872019-12-07 06:28:46 -0800305 if (mediametrics::Item::isEnabled()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700306 result.append("Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800307 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700308 result.append("Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800309 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700310 result.appendFormat(
311 "Since Boot: Submissions: %lld Accepted: %lld\n",
312 (long long)mItemsSubmitted.load(), (long long)mItemsFinalized);
313 result.appendFormat(
314 "Records Discarded: %lld (by Count: %lld by Expiration: %lld)\n",
315 (long long)mItemsDiscarded, (long long)mItemsDiscardedCount,
316 (long long)mItemsDiscardedExpire);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800317 if (ts_since != 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700318 result.appendFormat(
319 "Emitting Queue entries more recent than: %lld\n",
320 (long long)ts_since);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800321 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700322}
323
Ray Essickf27e9872019-12-07 06:28:46 -0800324void MediaMetricsService::dumpRecent_l(
Andy Hung17dbaf22019-10-11 14:06:31 -0700325 String8 &result, int dumpProto, nsecs_t ts_since, const char * only)
Ray Essick92d23b42018-01-29 12:10:30 -0800326{
Andy Hung17dbaf22019-10-11 14:06:31 -0700327 if (only != nullptr && *only == '\0') {
328 only = nullptr;
Ray Essickf65f4212017-08-31 11:41:19 -0700329 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700330 result.append("\nFinalized Metrics (oldest first):\n");
331 dumpQueue_l(result, dumpProto, ts_since, only);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800332
333 // show who is connected and injecting records?
334 // talk about # records fed to the 'readers'
335 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700336}
Ray Essick92d23b42018-01-29 12:10:30 -0800337
Ray Essickf27e9872019-12-07 06:28:46 -0800338void MediaMetricsService::dumpQueue_l(String8 &result, int dumpProto) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700339 dumpQueue_l(result, dumpProto, (nsecs_t) 0, nullptr /* only */);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800340}
341
Ray Essickf27e9872019-12-07 06:28:46 -0800342void MediaMetricsService::dumpQueue_l(
Andy Hung17dbaf22019-10-11 14:06:31 -0700343 String8 &result, int dumpProto, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700344 int slot = 0;
345
Ray Essick92d23b42018-01-29 12:10:30 -0800346 if (mItems.empty()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700347 result.append("empty\n");
Ray Essick3938dc62016-11-01 08:56:56 -0700348 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700349 for (const auto &item : mItems) {
350 nsecs_t when = item->getTimestamp();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800351 if (when < ts_since) {
352 continue;
353 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700354 // TODO: Only should be a set<string>
355 if (only != nullptr &&
356 item->getKey() /* std::string */ != only) {
357 ALOGV("%s: omit '%s', it's not '%s'",
358 __func__, item->getKey().c_str(), only);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700359 continue;
360 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700361 result.appendFormat("%5d: %s\n",
362 slot, item->toString(dumpProto).c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800363 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700364 }
365 }
Ray Essick3938dc62016-11-01 08:56:56 -0700366}
367
368//
369// Our Cheap in-core, non-persistent records management.
Ray Essick3938dc62016-11-01 08:56:56 -0700370
Ray Essick72a436b2018-06-14 15:08:13 -0700371// if item != NULL, it's the item we just inserted
372// true == more items eligible to be recovered
Ray Essickf27e9872019-12-07 06:28:46 -0800373bool MediaMetricsService::expirations_l(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick92d23b42018-01-29 12:10:30 -0800374{
Ray Essick72a436b2018-06-14 15:08:13 -0700375 bool more = false;
Ray Essicke5db6db2017-11-10 15:54:32 -0800376
Andy Hung17dbaf22019-10-11 14:06:31 -0700377 // check queue size
378 size_t overlimit = 0;
379 if (mMaxRecords > 0 && mItems.size() > mMaxRecords) {
380 overlimit = mItems.size() - mMaxRecords;
381 if (overlimit > mMaxRecordsExpiredAtOnce) {
382 more = true;
383 overlimit = mMaxRecordsExpiredAtOnce;
Ray Essickf65f4212017-08-31 11:41:19 -0700384 }
385 }
386
Andy Hung17dbaf22019-10-11 14:06:31 -0700387 // check queue times
388 size_t expired = 0;
389 if (!more && mMaxRecordAgeNs > 0) {
390 const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
391 // we check one at a time, skip search would be more efficient.
392 size_t i = overlimit;
393 for (; i < mItems.size(); ++i) {
394 auto &oitem = mItems[i];
Ray Essickf65f4212017-08-31 11:41:19 -0700395 nsecs_t when = oitem->getTimestamp();
Andy Hung82a074b2019-12-03 14:16:45 -0800396 if (oitem.get() == item.get()) {
Ray Essicke5db6db2017-11-10 15:54:32 -0800397 break;
398 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700399 if (now > when && (now - when) <= mMaxRecordAgeNs) {
400 break; // TODO: if we use BOOTTIME, should be monotonic.
Ray Essickf65f4212017-08-31 11:41:19 -0700401 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700402 if (i >= mMaxRecordsExpiredAtOnce) {
Ray Essick72a436b2018-06-14 15:08:13 -0700403 // this represents "one too many"; tell caller there are
404 // more to be reclaimed.
405 more = true;
406 break;
407 }
Ray Essick3938dc62016-11-01 08:56:56 -0700408 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700409 expired = i - overlimit;
Ray Essick3938dc62016-11-01 08:56:56 -0700410 }
Ray Essick72a436b2018-06-14 15:08:13 -0700411
Andy Hung17dbaf22019-10-11 14:06:31 -0700412 if (const size_t toErase = overlimit + expired;
413 toErase > 0) {
414 mItemsDiscardedCount += overlimit;
415 mItemsDiscardedExpire += expired;
416 mItemsDiscarded += toErase;
417 mItems.erase(mItems.begin(), mItems.begin() + toErase); // erase from front
418 }
Ray Essick72a436b2018-06-14 15:08:13 -0700419 return more;
420}
421
Ray Essickf27e9872019-12-07 06:28:46 -0800422void MediaMetricsService::processExpirations()
Ray Essick72a436b2018-06-14 15:08:13 -0700423{
424 bool more;
425 do {
426 sleep(1);
Andy Hung17dbaf22019-10-11 14:06:31 -0700427 std::lock_guard _l(mLock);
428 more = expirations_l(nullptr);
Ray Essick72a436b2018-06-14 15:08:13 -0700429 } while (more);
Ray Essick72a436b2018-06-14 15:08:13 -0700430}
431
Ray Essickf27e9872019-12-07 06:28:46 -0800432void MediaMetricsService::saveItem(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick72a436b2018-06-14 15:08:13 -0700433{
Andy Hung17dbaf22019-10-11 14:06:31 -0700434 std::lock_guard _l(mLock);
435 // we assume the items are roughly in time order.
436 mItems.emplace_back(item);
437 ++mItemsFinalized;
438 if (expirations_l(item)
439 && (!mExpireFuture.valid()
440 || mExpireFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)) {
441 mExpireFuture = std::async(std::launch::async, [this] { processExpirations(); });
Ray Essick72a436b2018-06-14 15:08:13 -0700442 }
Ray Essick3938dc62016-11-01 08:56:56 -0700443}
444
Andy Hung17dbaf22019-10-11 14:06:31 -0700445/* static */
Ray Essickf27e9872019-12-07 06:28:46 -0800446bool MediaMetricsService::isContentValid(const mediametrics::Item *item, bool isTrusted)
Ray Essickd38e1742017-01-23 15:17:06 -0800447{
Andy Hung17dbaf22019-10-11 14:06:31 -0700448 if (isTrusted) return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800449 // untrusted uids can only send us a limited set of keys
Andy Hung17dbaf22019-10-11 14:06:31 -0700450 const std::string &key = item->getKey();
Andy Hung06f3aba2019-12-03 16:36:42 -0800451 if (startsWith(key, "audio.")) return true;
Andy Hung17dbaf22019-10-11 14:06:31 -0700452 for (const char *allowedKey : {
Andy Hung06f3aba2019-12-03 16:36:42 -0800453 // legacy audio
Andy Hung17dbaf22019-10-11 14:06:31 -0700454 "audiopolicy",
455 "audiorecord",
456 "audiothread",
457 "audiotrack",
Andy Hung06f3aba2019-12-03 16:36:42 -0800458 // other media
Andy Hung17dbaf22019-10-11 14:06:31 -0700459 "codec",
460 "extractor",
461 "nuplayer",
462 }) {
463 if (key == allowedKey) {
464 return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800465 }
466 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700467 ALOGD("%s: invalid key: %s", __func__, item->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -0700468 return false;
469}
470
Andy Hung17dbaf22019-10-11 14:06:31 -0700471// are we rate limited, normally false
Ray Essickf27e9872019-12-07 06:28:46 -0800472bool MediaMetricsService::isRateLimited(mediametrics::Item *) const
Andy Hung17dbaf22019-10-11 14:06:31 -0700473{
474 return false;
475}
476
Ray Essick3938dc62016-11-01 08:56:56 -0700477} // namespace android