blob: d76bc2c0ee47bb0771e2af5a08b57dc6d6b26ada [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
Ray Essick2e9c63b2017-03-29 15:16:44 -07002 * Copyright (C) 2017 The Android Open Source Project
Ray Essick3938dc62016-11-01 08:56:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ray Essick3938dc62016-11-01 08:56:56 -070017//#define LOG_NDEBUG 0
Ray Essickf27e9872019-12-07 06:28:46 -080018#define LOG_TAG "MediaMetricsService"
Ray Essick3938dc62016-11-01 08:56:56 -070019#include <utils/Log.h>
20
Ray Essick40e8e5e2019-12-05 20:19:40 -080021#include "MediaMetricsService.h"
Ray Essick3938dc62016-11-01 08:56:56 -070022
Andy Hung17dbaf22019-10-11 14:06:31 -070023#include <pwd.h> //getpwuid
24
Andy Hung17dbaf22019-10-11 14:06:31 -070025#include <android/content/pm/IPackageManagerNative.h> // package info
Andy Hung55aaf522019-12-03 15:07:51 -080026#include <audio_utils/clock.h> // clock conversions
Andy Hung17dbaf22019-10-11 14:06:31 -070027#include <binder/IPCThreadState.h> // get calling uid
28#include <cutils/properties.h> // for property_get
Andy Hung9099a1a2020-04-04 14:23:36 -070029#include <mediautils/MemoryLeakTrackUtil.h>
30#include <memunreachable/memunreachable.h>
Andy Hung17dbaf22019-10-11 14:06:31 -070031#include <private/android_filesystem_config.h> // UID
32
Ray Essick3938dc62016-11-01 08:56:56 -070033namespace android {
34
Andy Hung1efc9c62019-12-03 13:43:33 -080035using namespace mediametrics;
36
Ray Essickf65f4212017-08-31 11:41:19 -070037// individual records kept in memory: age or count
Ray Essick72a436b2018-06-14 15:08:13 -070038// age: <= 28 hours (1 1/6 days)
Ray Essickf65f4212017-08-31 11:41:19 -070039// count: hard limit of # records
40// (0 for either of these disables that threshold)
Ray Essick72a436b2018-06-14 15:08:13 -070041//
Andy Hung17dbaf22019-10-11 14:06:31 -070042static constexpr nsecs_t kMaxRecordAgeNs = 28 * 3600 * NANOS_PER_SECOND;
Ray Essick23f4d6c2019-06-20 10:16:37 -070043// 2019/6: average daily per device is currently 375-ish;
44// setting this to 2000 is large enough to catch most devices
45// we'll lose some data on very very media-active devices, but only for
46// the gms collection; statsd will have already covered those for us.
47// This also retains enough information to help with bugreports
Andy Hung17dbaf22019-10-11 14:06:31 -070048static constexpr size_t kMaxRecords = 2000;
Ray Essick72a436b2018-06-14 15:08:13 -070049
50// max we expire in a single call, to constrain how long we hold the
51// mutex, which also constrains how long a client might wait.
Andy Hung17dbaf22019-10-11 14:06:31 -070052static constexpr size_t kMaxExpiredAtOnce = 50;
Ray Essick72a436b2018-06-14 15:08:13 -070053
54// TODO: need to look at tuning kMaxRecords and friends for low-memory devices
Ray Essick2e9c63b2017-03-29 15:16:44 -070055
Andy Hung55aaf522019-12-03 15:07:51 -080056/* static */
Ray Essickf27e9872019-12-07 06:28:46 -080057nsecs_t MediaMetricsService::roundTime(nsecs_t timeNs)
Andy Hung55aaf522019-12-03 15:07:51 -080058{
59 return (timeNs + NANOS_PER_SECOND / 2) / NANOS_PER_SECOND * NANOS_PER_SECOND;
60}
61
Andy Hunga85efab2019-12-23 11:41:29 -080062/* static */
63bool MediaMetricsService::useUidForPackage(
64 const std::string& package, const std::string& installer)
65{
66 if (strchr(package.c_str(), '.') == NULL) {
67 return false; // not of form 'com.whatever...'; assume internal and ok
68 } else if (strncmp(package.c_str(), "android.", 8) == 0) {
69 return false; // android.* packages are assumed fine
70 } else if (strncmp(installer.c_str(), "com.android.", 12) == 0) {
71 return false; // from play store
72 } else if (strncmp(installer.c_str(), "com.google.", 11) == 0) {
73 return false; // some google source
74 } else if (strcmp(installer.c_str(), "preload") == 0) {
75 return false; // preloads
76 } else {
77 return true; // we're not sure where it came from, use uid only.
78 }
79}
80
Ray Essickf27e9872019-12-07 06:28:46 -080081MediaMetricsService::MediaMetricsService()
Ray Essick2e9c63b2017-03-29 15:16:44 -070082 : mMaxRecords(kMaxRecords),
Ray Essickf65f4212017-08-31 11:41:19 -070083 mMaxRecordAgeNs(kMaxRecordAgeNs),
Andy Hung3b4c1f02020-01-23 18:58:32 -080084 mMaxRecordsExpiredAtOnce(kMaxExpiredAtOnce)
Andy Hung17dbaf22019-10-11 14:06:31 -070085{
86 ALOGD("%s", __func__);
Ray Essick3938dc62016-11-01 08:56:56 -070087}
88
Ray Essickf27e9872019-12-07 06:28:46 -080089MediaMetricsService::~MediaMetricsService()
Andy Hung17dbaf22019-10-11 14:06:31 -070090{
91 ALOGD("%s", __func__);
92 // the class destructor clears anyhow, but we enforce clearing items first.
93 mItemsDiscarded += mItems.size();
94 mItems.clear();
Ray Essick3938dc62016-11-01 08:56:56 -070095}
96
Ray Essickf27e9872019-12-07 06:28:46 -080097status_t MediaMetricsService::submitInternal(mediametrics::Item *item, bool release)
Ray Essick92d23b42018-01-29 12:10:30 -080098{
Andy Hung55aaf522019-12-03 15:07:51 -080099 // calling PID is 0 for one-way calls.
100 const pid_t pid = IPCThreadState::self()->getCallingPid();
101 const pid_t pid_given = item->getPid();
102 const uid_t uid = IPCThreadState::self()->getCallingUid();
103 const uid_t uid_given = item->getUid();
Ray Essickd38e1742017-01-23 15:17:06 -0800104
Andy Hung55aaf522019-12-03 15:07:51 -0800105 //ALOGD("%s: caller pid=%d uid=%d, item pid=%d uid=%d", __func__,
106 // (int)pid, (int)uid, (int) pid_given, (int)uid_given);
Ray Essickd38e1742017-01-23 15:17:06 -0800107
Andy Hung17dbaf22019-10-11 14:06:31 -0700108 bool isTrusted;
109 switch (uid) {
Andy Hung55aaf522019-12-03 15:07:51 -0800110 case AID_AUDIOSERVER:
111 case AID_BLUETOOTH:
112 case AID_CAMERA:
Andy Hung17dbaf22019-10-11 14:06:31 -0700113 case AID_DRM:
114 case AID_MEDIA:
115 case AID_MEDIA_CODEC:
116 case AID_MEDIA_EX:
117 case AID_MEDIA_DRM:
Andy Hung5d3f2d12020-03-04 19:55:03 -0800118 // case AID_SHELL: // DEBUG ONLY - used for mediametrics_tests to add new keys
Andy Hung55aaf522019-12-03 15:07:51 -0800119 case AID_SYSTEM:
Andy Hung17dbaf22019-10-11 14:06:31 -0700120 // trusted source, only override default values
121 isTrusted = true;
Andy Hung55aaf522019-12-03 15:07:51 -0800122 if (uid_given == (uid_t)-1) {
Ray Essickd38e1742017-01-23 15:17:06 -0800123 item->setUid(uid);
Andy Hung17dbaf22019-10-11 14:06:31 -0700124 }
Andy Hung55aaf522019-12-03 15:07:51 -0800125 if (pid_given == (pid_t)-1) {
126 item->setPid(pid); // if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700127 }
128 break;
129 default:
130 isTrusted = false;
Andy Hung55aaf522019-12-03 15:07:51 -0800131 item->setPid(pid); // always use calling pid, if one-way then this is 0.
Andy Hung17dbaf22019-10-11 14:06:31 -0700132 item->setUid(uid);
133 break;
Ray Essickd38e1742017-01-23 15:17:06 -0800134 }
135
Andy Hunga85efab2019-12-23 11:41:29 -0800136 // Overwrite package name and version if the caller was untrusted or empty
137 if (!isTrusted || item->getPkgName().empty()) {
138 const uid_t uid = item->getUid();
139 mediautils::UidInfo::Info info = mUidInfo.getInfo(uid);
140 if (useUidForPackage(info.package, info.installer)) {
141 // remove uid information of unknown installed packages.
142 // TODO: perhaps this can be done just before uploading to Westworld.
143 item->setPkgName(std::to_string(uid));
144 item->setPkgVersionCode(0);
145 } else {
146 item->setPkgName(info.package);
147 item->setPkgVersionCode(info.versionCode);
148 }
Adam Stone21c72122017-09-05 19:02:06 -0700149 }
150
Andy Hung5d3f2d12020-03-04 19:55:03 -0800151 ALOGV("%s: isTrusted:%d given uid %d; sanitized uid: %d sanitized pkg: %s "
Andy Hung17dbaf22019-10-11 14:06:31 -0700152 "sanitized pkg version: %lld",
153 __func__,
Andy Hung5d3f2d12020-03-04 19:55:03 -0800154 (int)isTrusted,
Adam Stone21c72122017-09-05 19:02:06 -0700155 uid_given, item->getUid(),
156 item->getPkgName().c_str(),
Andy Hung17dbaf22019-10-11 14:06:31 -0700157 (long long)item->getPkgVersionCode());
Ray Essick3938dc62016-11-01 08:56:56 -0700158
159 mItemsSubmitted++;
160
161 // validate the record; we discard if we don't like it
Andy Hung17dbaf22019-10-11 14:06:31 -0700162 if (isContentValid(item, isTrusted) == false) {
Andy Hunga87e69c2019-10-18 10:07:40 -0700163 if (release) delete item;
164 return PERMISSION_DENIED;
Ray Essick3938dc62016-11-01 08:56:56 -0700165 }
166
Ray Essick92d23b42018-01-29 12:10:30 -0800167 // XXX: if we have a sessionid in the new record, look to make
Ray Essick3938dc62016-11-01 08:56:56 -0700168 // sure it doesn't appear in the finalized list.
Ray Essick3938dc62016-11-01 08:56:56 -0700169
Ray Essick92d23b42018-01-29 12:10:30 -0800170 if (item->count() == 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700171 ALOGV("%s: dropping empty record...", __func__);
Andy Hunga87e69c2019-10-18 10:07:40 -0700172 if (release) delete item;
173 return BAD_VALUE;
Ray Essick3938dc62016-11-01 08:56:56 -0700174 }
Ray Essick92d23b42018-01-29 12:10:30 -0800175
Andy Hung55aaf522019-12-03 15:07:51 -0800176 if (!isTrusted || item->getTimestamp() == 0) {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800177 // Westworld logs two times for events: ElapsedRealTimeNs (BOOTTIME) and
178 // WallClockTimeNs (REALTIME), but currently logs REALTIME to cloud.
Andy Hung55aaf522019-12-03 15:07:51 -0800179 //
Andy Hung3b4c1f02020-01-23 18:58:32 -0800180 // For consistency and correlation with other logging mechanisms
181 // we use REALTIME here.
182 const int64_t now = systemTime(SYSTEM_TIME_REALTIME);
Andy Hung55aaf522019-12-03 15:07:51 -0800183 item->setTimestamp(now);
184 }
185
Andy Hung82a074b2019-12-03 14:16:45 -0800186 // now attach either the item or its dup to a const shared pointer
Ray Essickf27e9872019-12-07 06:28:46 -0800187 std::shared_ptr<const mediametrics::Item> sitem(release ? item : item->dup());
Ray Essick6ce27e52019-02-15 10:58:05 -0800188
Andy Hung06f3aba2019-12-03 16:36:42 -0800189 (void)mAudioAnalytics.submit(sitem, isTrusted);
190
Ray Essickf27e9872019-12-07 06:28:46 -0800191 extern bool dump2Statsd(const std::shared_ptr<const mediametrics::Item>& item);
Andy Hung82a074b2019-12-03 14:16:45 -0800192 (void)dump2Statsd(sitem); // failure should be logged in function.
193 saveItem(sitem);
Andy Hunga87e69c2019-10-18 10:07:40 -0700194 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700195}
196
Ray Essickf27e9872019-12-07 06:28:46 -0800197status_t MediaMetricsService::dump(int fd, const Vector<String16>& args)
Ray Essick3938dc62016-11-01 08:56:56 -0700198{
Ray Essick3938dc62016-11-01 08:56:56 -0700199 String8 result;
200
201 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700202 result.appendFormat("Permission Denial: "
Ray Essickf27e9872019-12-07 06:28:46 -0800203 "can't dump MediaMetricsService from pid=%d, uid=%d\n",
Ray Essick3938dc62016-11-01 08:56:56 -0700204 IPCThreadState::self()->getCallingPid(),
205 IPCThreadState::self()->getCallingUid());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800206 write(fd, result.string(), result.size());
207 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700208 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800209
Andy Hung9099a1a2020-04-04 14:23:36 -0700210 static const String16 heapOption("--heap");
211 static const String16 unreachableOption("--unreachable");
212 bool heap = false;
213 bool unreachable = false;
214
Andy Hung333bb3d2020-01-31 20:17:48 -0800215 const String16 protoOption("--proto");
216 const String16 clearOption("--clear");
Ray Essickf65f4212017-08-31 11:41:19 -0700217 bool clear = false;
Andy Hung333bb3d2020-01-31 20:17:48 -0800218 const String16 sinceOption("--since");
Ray Essickf65f4212017-08-31 11:41:19 -0700219 nsecs_t ts_since = 0;
Andy Hung333bb3d2020-01-31 20:17:48 -0800220 const String16 helpOption("--help");
221 const String16 onlyOption("--only");
Ray Essick783bd0d2018-01-11 11:10:35 -0800222 std::string only;
Andy Hung17dbaf22019-10-11 14:06:31 -0700223 const int n = args.size();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800224 for (int i = 0; i < n; i++) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800225 if (args[i] == clearOption) {
226 clear = true;
Andy Hung9099a1a2020-04-04 14:23:36 -0700227 } else if (args[i] == heapOption) {
228 heap = true;
Ray Essickf65f4212017-08-31 11:41:19 -0700229 } else if (args[i] == protoOption) {
230 i++;
231 if (i < n) {
Andy Hung3b4c1f02020-01-23 18:58:32 -0800232 // ignore
Ray Essick583a23a2017-11-27 12:49:57 -0800233 } else {
234 result.append("missing value for -proto\n\n");
Ray Essickf65f4212017-08-31 11:41:19 -0700235 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800236 } else if (args[i] == sinceOption) {
237 i++;
238 if (i < n) {
239 String8 value(args[i]);
240 char *endp;
241 const char *p = value.string();
242 ts_since = strtoll(p, &endp, 10);
243 if (endp == p || *endp != '\0') {
244 ts_since = 0;
245 }
246 } else {
247 ts_since = 0;
248 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800249 // command line is milliseconds; internal units are nano-seconds
Andy Hung17dbaf22019-10-11 14:06:31 -0700250 ts_since *= NANOS_PER_MILLISECOND;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700251 } else if (args[i] == onlyOption) {
252 i++;
253 if (i < n) {
254 String8 value(args[i]);
Ray Essickf65f4212017-08-31 11:41:19 -0700255 only = value.string();
Ray Essick2e9c63b2017-03-29 15:16:44 -0700256 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800257 } else if (args[i] == helpOption) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700258 // TODO: consider function area dumping.
259 // dumpsys media.metrics audiotrack,codec
260 // or dumpsys media.metrics audiotrack codec
261
Ray Essick35ad27f2017-01-30 14:04:11 -0800262 result.append("Recognized parameters:\n");
Andy Hung9099a1a2020-04-04 14:23:36 -0700263 result.append("--heap heap usage (top 100)\n");
Andy Hung333bb3d2020-01-31 20:17:48 -0800264 result.append("--help this help message\n");
265 result.append("--proto # dump using protocol #");
266 result.append("--clear clears out saved records\n");
267 result.append("--only X process records for component X\n");
268 result.append("--since X include records since X\n");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700269 result.append(" (X is milliseconds since the UNIX epoch)\n");
Andy Hung9099a1a2020-04-04 14:23:36 -0700270 result.append("--unreachable unreachable memory (leaks)\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800271 write(fd, result.string(), result.size());
272 return NO_ERROR;
Andy Hung9099a1a2020-04-04 14:23:36 -0700273 } else if (args[i] == unreachableOption) {
274 unreachable = true;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800275 }
276 }
277
Andy Hung17dbaf22019-10-11 14:06:31 -0700278 {
279 std::lock_guard _l(mLock);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800280
Andy Hung17dbaf22019-10-11 14:06:31 -0700281 result.appendFormat("Dump of the %s process:\n", kServiceName);
Andy Hung3b4c1f02020-01-23 18:58:32 -0800282 dumpHeaders_l(result, ts_since);
283 dumpRecent_l(result, ts_since, only.c_str());
Ray Essick583a23a2017-11-27 12:49:57 -0800284
Andy Hung17dbaf22019-10-11 14:06:31 -0700285 if (clear) {
286 mItemsDiscarded += mItems.size();
287 mItems.clear();
288 // shall we clear the summary data too?
Ray Essick2e9c63b2017-03-29 15:16:44 -0700289 }
Andy Hung06f3aba2019-12-03 16:36:42 -0800290 // TODO: maybe consider a better way of dumping audio analytics info.
291 constexpr int32_t linesToDump = 1000;
Andy Hung0f7ad8c2020-01-03 13:24:34 -0800292 auto [ dumpString, lines ] = mAudioAnalytics.dump(linesToDump);
293 result.append(dumpString.c_str());
294 if (lines == linesToDump) {
295 result.append("-- some lines may be truncated --\n");
296 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700297 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700298 write(fd, result.string(), result.size());
Andy Hung9099a1a2020-04-04 14:23:36 -0700299
300 // Check heap and unreachable memory outside of lock.
301 if (heap) {
302 dprintf(fd, "\nDumping heap:\n");
303 std::string s = dumpMemoryAddresses(100 /* limit */);
304 write(fd, s.c_str(), s.size());
305 }
306 if (unreachable) {
307 dprintf(fd, "\nDumping unreachable memory:\n");
308 // TODO - should limit be an argument parameter?
309 std::string s = GetUnreachableMemoryString(true /* contents */, 100 /* limit */);
310 write(fd, s.c_str(), s.size());
311 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700312 return NO_ERROR;
313}
314
315// dump headers
Andy Hung3b4c1f02020-01-23 18:58:32 -0800316void MediaMetricsService::dumpHeaders_l(String8 &result, nsecs_t ts_since)
Ray Essick92d23b42018-01-29 12:10:30 -0800317{
Ray Essickf27e9872019-12-07 06:28:46 -0800318 if (mediametrics::Item::isEnabled()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700319 result.append("Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800320 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700321 result.append("Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800322 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700323 result.appendFormat(
324 "Since Boot: Submissions: %lld Accepted: %lld\n",
325 (long long)mItemsSubmitted.load(), (long long)mItemsFinalized);
326 result.appendFormat(
327 "Records Discarded: %lld (by Count: %lld by Expiration: %lld)\n",
328 (long long)mItemsDiscarded, (long long)mItemsDiscardedCount,
329 (long long)mItemsDiscardedExpire);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800330 if (ts_since != 0) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700331 result.appendFormat(
332 "Emitting Queue entries more recent than: %lld\n",
333 (long long)ts_since);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800334 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700335}
336
Ray Essickf27e9872019-12-07 06:28:46 -0800337void MediaMetricsService::dumpRecent_l(
Andy Hung3b4c1f02020-01-23 18:58:32 -0800338 String8 &result, nsecs_t ts_since, const char * only)
Ray Essick92d23b42018-01-29 12:10:30 -0800339{
Andy Hung17dbaf22019-10-11 14:06:31 -0700340 if (only != nullptr && *only == '\0') {
341 only = nullptr;
Ray Essickf65f4212017-08-31 11:41:19 -0700342 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700343 result.append("\nFinalized Metrics (oldest first):\n");
Andy Hung3b4c1f02020-01-23 18:58:32 -0800344 dumpQueue_l(result, ts_since, only);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800345
346 // show who is connected and injecting records?
347 // talk about # records fed to the 'readers'
348 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700349}
Ray Essick92d23b42018-01-29 12:10:30 -0800350
Andy Hung3b4c1f02020-01-23 18:58:32 -0800351void MediaMetricsService::dumpQueue_l(String8 &result) {
352 dumpQueue_l(result, (nsecs_t) 0, nullptr /* only */);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800353}
354
Ray Essickf27e9872019-12-07 06:28:46 -0800355void MediaMetricsService::dumpQueue_l(
Andy Hung3b4c1f02020-01-23 18:58:32 -0800356 String8 &result, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700357 int slot = 0;
358
Ray Essick92d23b42018-01-29 12:10:30 -0800359 if (mItems.empty()) {
Andy Hung17dbaf22019-10-11 14:06:31 -0700360 result.append("empty\n");
Ray Essick3938dc62016-11-01 08:56:56 -0700361 } else {
Andy Hung17dbaf22019-10-11 14:06:31 -0700362 for (const auto &item : mItems) {
363 nsecs_t when = item->getTimestamp();
Ray Essickb5fac8e2016-12-12 11:33:56 -0800364 if (when < ts_since) {
365 continue;
366 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700367 // TODO: Only should be a set<string>
368 if (only != nullptr &&
369 item->getKey() /* std::string */ != only) {
370 ALOGV("%s: omit '%s', it's not '%s'",
371 __func__, item->getKey().c_str(), only);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700372 continue;
373 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700374 result.appendFormat("%5d: %s\n",
Andy Hung3b4c1f02020-01-23 18:58:32 -0800375 slot, item->toString().c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800376 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700377 }
378 }
Ray Essick3938dc62016-11-01 08:56:56 -0700379}
380
381//
382// Our Cheap in-core, non-persistent records management.
Ray Essick3938dc62016-11-01 08:56:56 -0700383
Ray Essick72a436b2018-06-14 15:08:13 -0700384// if item != NULL, it's the item we just inserted
385// true == more items eligible to be recovered
Ray Essickf27e9872019-12-07 06:28:46 -0800386bool MediaMetricsService::expirations_l(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick92d23b42018-01-29 12:10:30 -0800387{
Ray Essick72a436b2018-06-14 15:08:13 -0700388 bool more = false;
Ray Essicke5db6db2017-11-10 15:54:32 -0800389
Andy Hung17dbaf22019-10-11 14:06:31 -0700390 // check queue size
391 size_t overlimit = 0;
392 if (mMaxRecords > 0 && mItems.size() > mMaxRecords) {
393 overlimit = mItems.size() - mMaxRecords;
394 if (overlimit > mMaxRecordsExpiredAtOnce) {
395 more = true;
396 overlimit = mMaxRecordsExpiredAtOnce;
Ray Essickf65f4212017-08-31 11:41:19 -0700397 }
398 }
399
Andy Hung17dbaf22019-10-11 14:06:31 -0700400 // check queue times
401 size_t expired = 0;
402 if (!more && mMaxRecordAgeNs > 0) {
403 const nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
404 // we check one at a time, skip search would be more efficient.
405 size_t i = overlimit;
406 for (; i < mItems.size(); ++i) {
407 auto &oitem = mItems[i];
Ray Essickf65f4212017-08-31 11:41:19 -0700408 nsecs_t when = oitem->getTimestamp();
Andy Hung82a074b2019-12-03 14:16:45 -0800409 if (oitem.get() == item.get()) {
Ray Essicke5db6db2017-11-10 15:54:32 -0800410 break;
411 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700412 if (now > when && (now - when) <= mMaxRecordAgeNs) {
Andy Hunged416da2020-03-05 18:42:55 -0800413 break; // Note SYSTEM_TIME_REALTIME may not be monotonic.
Ray Essickf65f4212017-08-31 11:41:19 -0700414 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700415 if (i >= mMaxRecordsExpiredAtOnce) {
Ray Essick72a436b2018-06-14 15:08:13 -0700416 // this represents "one too many"; tell caller there are
417 // more to be reclaimed.
418 more = true;
419 break;
420 }
Ray Essick3938dc62016-11-01 08:56:56 -0700421 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700422 expired = i - overlimit;
Ray Essick3938dc62016-11-01 08:56:56 -0700423 }
Ray Essick72a436b2018-06-14 15:08:13 -0700424
Andy Hung17dbaf22019-10-11 14:06:31 -0700425 if (const size_t toErase = overlimit + expired;
426 toErase > 0) {
427 mItemsDiscardedCount += overlimit;
428 mItemsDiscardedExpire += expired;
429 mItemsDiscarded += toErase;
430 mItems.erase(mItems.begin(), mItems.begin() + toErase); // erase from front
431 }
Ray Essick72a436b2018-06-14 15:08:13 -0700432 return more;
433}
434
Ray Essickf27e9872019-12-07 06:28:46 -0800435void MediaMetricsService::processExpirations()
Ray Essick72a436b2018-06-14 15:08:13 -0700436{
437 bool more;
438 do {
439 sleep(1);
Andy Hung17dbaf22019-10-11 14:06:31 -0700440 std::lock_guard _l(mLock);
441 more = expirations_l(nullptr);
Ray Essick72a436b2018-06-14 15:08:13 -0700442 } while (more);
Ray Essick72a436b2018-06-14 15:08:13 -0700443}
444
Ray Essickf27e9872019-12-07 06:28:46 -0800445void MediaMetricsService::saveItem(const std::shared_ptr<const mediametrics::Item>& item)
Ray Essick72a436b2018-06-14 15:08:13 -0700446{
Andy Hung17dbaf22019-10-11 14:06:31 -0700447 std::lock_guard _l(mLock);
448 // we assume the items are roughly in time order.
449 mItems.emplace_back(item);
450 ++mItemsFinalized;
451 if (expirations_l(item)
452 && (!mExpireFuture.valid()
453 || mExpireFuture.wait_for(std::chrono::seconds(0)) == std::future_status::ready)) {
454 mExpireFuture = std::async(std::launch::async, [this] { processExpirations(); });
Ray Essick72a436b2018-06-14 15:08:13 -0700455 }
Ray Essick3938dc62016-11-01 08:56:56 -0700456}
457
Andy Hung17dbaf22019-10-11 14:06:31 -0700458/* static */
Ray Essickf27e9872019-12-07 06:28:46 -0800459bool MediaMetricsService::isContentValid(const mediametrics::Item *item, bool isTrusted)
Ray Essickd38e1742017-01-23 15:17:06 -0800460{
Andy Hung17dbaf22019-10-11 14:06:31 -0700461 if (isTrusted) return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800462 // untrusted uids can only send us a limited set of keys
Andy Hung17dbaf22019-10-11 14:06:31 -0700463 const std::string &key = item->getKey();
Andy Hung06f3aba2019-12-03 16:36:42 -0800464 if (startsWith(key, "audio.")) return true;
Edwin Wong8d188352020-02-11 11:59:34 -0800465 if (startsWith(key, "drm.vendor.")) return true;
466 // the list of allowedKey uses statsd_handlers
467 // in iface_statsd.cpp as reference
468 // drmmanager is from a trusted uid, therefore not needed here
Andy Hung17dbaf22019-10-11 14:06:31 -0700469 for (const char *allowedKey : {
Andy Hung06f3aba2019-12-03 16:36:42 -0800470 // legacy audio
Andy Hung17dbaf22019-10-11 14:06:31 -0700471 "audiopolicy",
472 "audiorecord",
473 "audiothread",
474 "audiotrack",
Andy Hung06f3aba2019-12-03 16:36:42 -0800475 // other media
Andy Hung17dbaf22019-10-11 14:06:31 -0700476 "codec",
477 "extractor",
Edwin Wong8d188352020-02-11 11:59:34 -0800478 "mediadrm",
Andy Hung17dbaf22019-10-11 14:06:31 -0700479 "nuplayer",
480 }) {
481 if (key == allowedKey) {
482 return true;
Ray Essickd38e1742017-01-23 15:17:06 -0800483 }
484 }
Andy Hung17dbaf22019-10-11 14:06:31 -0700485 ALOGD("%s: invalid key: %s", __func__, item->toString().c_str());
Ray Essick3938dc62016-11-01 08:56:56 -0700486 return false;
487}
488
Andy Hung17dbaf22019-10-11 14:06:31 -0700489// are we rate limited, normally false
Ray Essickf27e9872019-12-07 06:28:46 -0800490bool MediaMetricsService::isRateLimited(mediametrics::Item *) const
Andy Hung17dbaf22019-10-11 14:06:31 -0700491{
492 return false;
493}
494
Ray Essick3938dc62016-11-01 08:56:56 -0700495} // namespace android