blob: 83992aa98ea6844d77614f196596b4bc287785f6 [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
17// Proxy for media player implementations
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "MediaAnalyticsService"
21#include <utils/Log.h>
22
Ray Essick2e9c63b2017-03-29 15:16:44 -070023#include <stdint.h>
Ray Essick3938dc62016-11-01 08:56:56 -070024#include <inttypes.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <dirent.h>
29#include <unistd.h>
30
31#include <string.h>
Ray Essickf65f4212017-08-31 11:41:19 -070032#include <pwd.h>
Ray Essick3938dc62016-11-01 08:56:56 -070033
34#include <cutils/atomic.h>
35#include <cutils/properties.h> // for property_get
36
37#include <utils/misc.h>
38
Ray Essickf65f4212017-08-31 11:41:19 -070039#include <android/content/pm/IPackageManagerNative.h>
40
Ray Essick3938dc62016-11-01 08:56:56 -070041#include <binder/IPCThreadState.h>
42#include <binder/IServiceManager.h>
43#include <binder/MemoryHeapBase.h>
44#include <binder/MemoryBase.h>
45#include <gui/Surface.h>
46#include <utils/Errors.h> // for status_t
47#include <utils/List.h>
48#include <utils/String8.h>
49#include <utils/SystemClock.h>
50#include <utils/Timers.h>
51#include <utils/Vector.h>
52
53#include <media/AudioPolicyHelper.h>
54#include <media/IMediaHTTPService.h>
55#include <media/IRemoteDisplay.h>
56#include <media/IRemoteDisplayClient.h>
57#include <media/MediaPlayerInterface.h>
58#include <media/mediarecorder.h>
59#include <media/MediaMetadataRetrieverInterface.h>
60#include <media/Metadata.h>
61#include <media/AudioTrack.h>
62#include <media/MemoryLeakTrackUtil.h>
63#include <media/stagefright/MediaCodecList.h>
64#include <media/stagefright/MediaErrors.h>
65#include <media/stagefright/Utils.h>
66#include <media/stagefright/foundation/ADebug.h>
67#include <media/stagefright/foundation/ALooperRoster.h>
68#include <mediautils/BatteryNotifier.h>
69
70//#include <memunreachable/memunreachable.h>
71#include <system/audio.h>
72
73#include <private/android_filesystem_config.h>
74
75#include "MediaAnalyticsService.h"
76
Ray Essick2e9c63b2017-03-29 15:16:44 -070077#include "MetricsSummarizer.h"
78#include "MetricsSummarizerCodec.h"
79#include "MetricsSummarizerExtractor.h"
80#include "MetricsSummarizerPlayer.h"
81#include "MetricsSummarizerRecorder.h"
82
Ray Essick3938dc62016-11-01 08:56:56 -070083
84namespace android {
85
Ray Essickf65f4212017-08-31 11:41:19 -070086 using namespace android::base;
87 using namespace android::content::pm;
88
Ray Essick3938dc62016-11-01 08:56:56 -070089
Ray Essick2e9c63b2017-03-29 15:16:44 -070090
91// summarized records
Ray Essickf65f4212017-08-31 11:41:19 -070092// up to 36 sets, each covering an hour -- so at least 1.5 days
Ray Essick2e9c63b2017-03-29 15:16:44 -070093// (will be longer if there are hours without any media action)
94static const nsecs_t kNewSetIntervalNs = 3600*(1000*1000*1000ll);
Ray Essickf65f4212017-08-31 11:41:19 -070095static const int kMaxRecordSets = 36;
Ray Essick2e9c63b2017-03-29 15:16:44 -070096
Ray Essickf65f4212017-08-31 11:41:19 -070097// individual records kept in memory: age or count
98// age: <= 36 hours (1.5 days)
99// count: hard limit of # records
100// (0 for either of these disables that threshold)
101static const nsecs_t kMaxRecordAgeNs = 36 * 3600 * (1000*1000*1000ll);
102static const int kMaxRecords = 0;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700103
104static const char *kServiceName = "media.metrics";
105
Ray Essick3938dc62016-11-01 08:56:56 -0700106void MediaAnalyticsService::instantiate() {
107 defaultServiceManager()->addService(
Ray Essick2e9c63b2017-03-29 15:16:44 -0700108 String16(kServiceName), new MediaAnalyticsService());
Ray Essick3938dc62016-11-01 08:56:56 -0700109}
110
Ray Essick2e9c63b2017-03-29 15:16:44 -0700111// handle sets of summarizers
112MediaAnalyticsService::SummarizerSet::SummarizerSet() {
113 mSummarizers = new List<MetricsSummarizer *>();
114}
Ray Essickf65f4212017-08-31 11:41:19 -0700115
Ray Essick2e9c63b2017-03-29 15:16:44 -0700116MediaAnalyticsService::SummarizerSet::~SummarizerSet() {
117 // empty the list
118 List<MetricsSummarizer *> *l = mSummarizers;
119 while (l->size() > 0) {
120 MetricsSummarizer *summarizer = *(l->begin());
121 l->erase(l->begin());
122 delete summarizer;
123 }
124}
125
126void MediaAnalyticsService::newSummarizerSet() {
127 ALOGD("MediaAnalyticsService::newSummarizerSet");
128 MediaAnalyticsService::SummarizerSet *set = new MediaAnalyticsService::SummarizerSet();
129 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
130 set->setStarted(now);
131
132 set->appendSummarizer(new MetricsSummarizerExtractor("extractor"));
133 set->appendSummarizer(new MetricsSummarizerCodec("codec"));
134 set->appendSummarizer(new MetricsSummarizerPlayer("nuplayer"));
135 set->appendSummarizer(new MetricsSummarizerRecorder("recorder"));
136
137 // ALWAYS at the end, since it catches everything
138 set->appendSummarizer(new MetricsSummarizer(NULL));
139
140 // inject this set at the BACK of the list.
141 mSummarizerSets->push_back(set);
142 mCurrentSet = set;
143
144 // limit the # that we have
145 if (mMaxRecordSets > 0) {
146 List<SummarizerSet *> *l = mSummarizerSets;
147 while (l->size() > (size_t) mMaxRecordSets) {
148 ALOGD("Deleting oldest record set....");
149 MediaAnalyticsService::SummarizerSet *oset = *(l->begin());
150 l->erase(l->begin());
151 delete oset;
152 mSetsDiscarded++;
153 }
154 }
155}
156
Ray Essick3938dc62016-11-01 08:56:56 -0700157MediaAnalyticsService::MediaAnalyticsService()
Ray Essick2e9c63b2017-03-29 15:16:44 -0700158 : mMaxRecords(kMaxRecords),
Ray Essickf65f4212017-08-31 11:41:19 -0700159 mMaxRecordAgeNs(kMaxRecordAgeNs),
Ray Essick2e9c63b2017-03-29 15:16:44 -0700160 mMaxRecordSets(kMaxRecordSets),
Ray Essickf65f4212017-08-31 11:41:19 -0700161 mNewSetInterval(kNewSetIntervalNs),
162 mDumpProto(MediaAnalyticsItem::PROTO_V0) {
Ray Essick3938dc62016-11-01 08:56:56 -0700163
164 ALOGD("MediaAnalyticsService created");
165 // clear our queues
Ray Essickb5fac8e2016-12-12 11:33:56 -0800166 mOpen = new List<MediaAnalyticsItem *>();
167 mFinalized = new List<MediaAnalyticsItem *>();
Ray Essick3938dc62016-11-01 08:56:56 -0700168
Ray Essick2e9c63b2017-03-29 15:16:44 -0700169 mSummarizerSets = new List<MediaAnalyticsService::SummarizerSet *>();
170 newSummarizerSet();
171
Ray Essick3938dc62016-11-01 08:56:56 -0700172 mItemsSubmitted = 0;
173 mItemsFinalized = 0;
174 mItemsDiscarded = 0;
Ray Essickf65f4212017-08-31 11:41:19 -0700175 mItemsDiscardedExpire = 0;
176 mItemsDiscardedCount = 0;
Ray Essick3938dc62016-11-01 08:56:56 -0700177
178 mLastSessionID = 0;
179 // recover any persistency we set up
180 // etc
181}
182
183MediaAnalyticsService::~MediaAnalyticsService() {
184 ALOGD("MediaAnalyticsService destroyed");
185
Ray Essick2e9c63b2017-03-29 15:16:44 -0700186 // clean out mOpen and mFinalized
Ray Essickf65f4212017-08-31 11:41:19 -0700187 while (mOpen->size() > 0) {
188 MediaAnalyticsItem * oitem = *(mOpen->begin());
189 mOpen->erase(mOpen->begin());
190 delete oitem;
191 mItemsDiscarded++;
192 mItemsDiscardedCount++;
193 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700194 delete mOpen;
195 mOpen = NULL;
Ray Essickf65f4212017-08-31 11:41:19 -0700196
197 while (mFinalized->size() > 0) {
198 MediaAnalyticsItem * oitem = *(mFinalized->begin());
199 mFinalized->erase(mFinalized->begin());
200 delete oitem;
201 mItemsDiscarded++;
202 mItemsDiscardedCount++;
203 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700204 delete mFinalized;
205 mFinalized = NULL;
206
207 // XXX: clean out the summaries
Ray Essick3938dc62016-11-01 08:56:56 -0700208}
209
210
211MediaAnalyticsItem::SessionID_t MediaAnalyticsService::generateUniqueSessionID() {
212 // generate a new sessionid
213
214 Mutex::Autolock _l(mLock_ids);
215 return (++mLastSessionID);
216}
217
Ray Essickb5fac8e2016-12-12 11:33:56 -0800218// caller surrenders ownership of 'item'
219MediaAnalyticsItem::SessionID_t MediaAnalyticsService::submit(MediaAnalyticsItem *item, bool forcenew) {
Ray Essick3938dc62016-11-01 08:56:56 -0700220
221 MediaAnalyticsItem::SessionID_t id = MediaAnalyticsItem::SessionIDInvalid;
222
Ray Essickd38e1742017-01-23 15:17:06 -0800223 // we control these, generally not trusting user input
Ray Essick3938dc62016-11-01 08:56:56 -0700224 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
Ray Essick721b7a02017-09-11 09:33:56 -0700225 // round nsecs to seconds
226 now = ((now + 500000000) / 1000000000) * 1000000000;
Ray Essick3938dc62016-11-01 08:56:56 -0700227 item->setTimestamp(now);
228 int pid = IPCThreadState::self()->getCallingPid();
Ray Essick3938dc62016-11-01 08:56:56 -0700229 int uid = IPCThreadState::self()->getCallingUid();
Ray Essickd38e1742017-01-23 15:17:06 -0800230
231 int uid_given = item->getUid();
232 int pid_given = item->getPid();
233
234 // although we do make exceptions for particular client uids
235 // that we know we trust.
236 //
237 bool isTrusted = false;
238
Ray Essickf65f4212017-08-31 11:41:19 -0700239 ALOGV("caller has uid=%d, embedded uid=%d", uid, uid_given);
240
Ray Essickd38e1742017-01-23 15:17:06 -0800241 switch (uid) {
242 case AID_MEDIA:
243 case AID_MEDIA_CODEC:
244 case AID_MEDIA_EX:
245 case AID_MEDIA_DRM:
246 // trusted source, only override default values
Ray Essickf65f4212017-08-31 11:41:19 -0700247 isTrusted = true;
Ray Essickd38e1742017-01-23 15:17:06 -0800248 if (uid_given == (-1)) {
249 item->setUid(uid);
250 }
251 if (pid_given == (-1)) {
252 item->setPid(pid);
253 }
254 break;
255 default:
256 isTrusted = false;
257 item->setPid(pid);
258 item->setUid(uid);
259 break;
260 }
261
Ray Essickfa149562017-09-19 09:27:31 -0700262
Adam Stone21c72122017-09-05 19:02:06 -0700263 // Overwrite package name and version if the caller was untrusted.
264 if (!isTrusted) {
Ray Essickfa149562017-09-19 09:27:31 -0700265 setPkgInfo(item, item->getUid(), true, true);
Adam Stone21c72122017-09-05 19:02:06 -0700266 } else if (item->getPkgName().empty()) {
Ray Essickfa149562017-09-19 09:27:31 -0700267 // empty, so fill out both parts
268 setPkgInfo(item, item->getUid(), true, true);
269 } else {
270 // trusted, provided a package, do nothing
Adam Stone21c72122017-09-05 19:02:06 -0700271 }
272
273 ALOGV("given uid %d; sanitized uid: %d sanitized pkg: %s "
274 "sanitized pkg version: %d",
275 uid_given, item->getUid(),
276 item->getPkgName().c_str(),
277 item->getPkgVersionCode());
Ray Essick3938dc62016-11-01 08:56:56 -0700278
279 mItemsSubmitted++;
280
281 // validate the record; we discard if we don't like it
Ray Essickd38e1742017-01-23 15:17:06 -0800282 if (contentValid(item, isTrusted) == false) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800283 delete item;
Ray Essick3938dc62016-11-01 08:56:56 -0700284 return MediaAnalyticsItem::SessionIDInvalid;
285 }
286
287
288 // if we have a sesisonid in the new record, look to make
289 // sure it doesn't appear in the finalized list.
290 // XXX: this is for security / DOS prevention.
291 // may also require that we persist the unique sessionIDs
292 // across boots [instead of within a single boot]
293
294
295 // match this new record up against records in the open
296 // list...
297 // if there's a match, merge them together
298 // deal with moving the old / merged record into the finalized que
299
300 bool finalizing = item->getFinalized();
301
Ray Essick9bb7e3b2017-10-23 13:01:48 -0700302 Mutex::Autolock _l(mLock);
303
Ray Essick3938dc62016-11-01 08:56:56 -0700304 // if finalizing, we'll remove it
Ray Essickb5fac8e2016-12-12 11:33:56 -0800305 MediaAnalyticsItem *oitem = findItem(mOpen, item, finalizing | forcenew);
Ray Essick3938dc62016-11-01 08:56:56 -0700306 if (oitem != NULL) {
307 if (forcenew) {
308 // old one gets finalized, then we insert the new one
309 // so we'll have 2 records at the end of this.
310 // but don't finalize an empty record
Ray Essickb5fac8e2016-12-12 11:33:56 -0800311 if (oitem->count() == 0) {
312 // we're responsible for disposing of the dead record
313 delete oitem;
314 oitem = NULL;
315 } else {
Ray Essick3938dc62016-11-01 08:56:56 -0700316 oitem->setFinalized(true);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700317 summarize(oitem);
Ray Essick3938dc62016-11-01 08:56:56 -0700318 saveItem(mFinalized, oitem, 0);
319 }
320 // new record could itself be marked finalized...
Ray Essicke5db6db2017-11-10 15:54:32 -0800321 id = item->getSessionID();
Ray Essick3938dc62016-11-01 08:56:56 -0700322 if (finalizing) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700323 summarize(item);
Ray Essick3938dc62016-11-01 08:56:56 -0700324 saveItem(mFinalized, item, 0);
325 mItemsFinalized++;
326 } else {
327 saveItem(mOpen, item, 1);
328 }
Ray Essick3938dc62016-11-01 08:56:56 -0700329 } else {
330 // combine the records, send it to finalized if appropriate
331 oitem->merge(item);
Ray Essicke5db6db2017-11-10 15:54:32 -0800332 id = oitem->getSessionID();
Ray Essick3938dc62016-11-01 08:56:56 -0700333 if (finalizing) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700334 summarize(oitem);
Ray Essick3938dc62016-11-01 08:56:56 -0700335 saveItem(mFinalized, oitem, 0);
336 mItemsFinalized++;
337 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800338
339 // we're responsible for disposing of the dead record
340 delete item;
341 item = NULL;
Ray Essick3938dc62016-11-01 08:56:56 -0700342 }
343 } else {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800344 // nothing to merge, save the new record
345 id = item->getSessionID();
346 if (finalizing) {
347 if (item->count() == 0) {
348 // drop empty records
349 delete item;
350 item = NULL;
Ray Essick3938dc62016-11-01 08:56:56 -0700351 } else {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700352 summarize(item);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800353 saveItem(mFinalized, item, 0);
354 mItemsFinalized++;
Ray Essick3938dc62016-11-01 08:56:56 -0700355 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800356 } else {
357 saveItem(mOpen, item, 1);
358 }
Ray Essick3938dc62016-11-01 08:56:56 -0700359 }
Ray Essick3938dc62016-11-01 08:56:56 -0700360 return id;
361}
362
Ray Essickf65f4212017-08-31 11:41:19 -0700363
Ray Essickb5fac8e2016-12-12 11:33:56 -0800364status_t MediaAnalyticsService::dump(int fd, const Vector<String16>& args)
Ray Essick3938dc62016-11-01 08:56:56 -0700365{
Ray Essickb5fac8e2016-12-12 11:33:56 -0800366 const size_t SIZE = 512;
Ray Essick3938dc62016-11-01 08:56:56 -0700367 char buffer[SIZE];
368 String8 result;
369
370 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
371 snprintf(buffer, SIZE, "Permission Denial: "
372 "can't dump MediaAnalyticsService from pid=%d, uid=%d\n",
373 IPCThreadState::self()->getCallingPid(),
374 IPCThreadState::self()->getCallingUid());
375 result.append(buffer);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800376 write(fd, result.string(), result.size());
377 return NO_ERROR;
Ray Essick3938dc62016-11-01 08:56:56 -0700378 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800379
380 // crack any parameters
Ray Essick2e9c63b2017-03-29 15:16:44 -0700381 String16 summaryOption("-summary");
Ray Essickf65f4212017-08-31 11:41:19 -0700382 bool summary = false;
383 String16 protoOption("-proto");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800384 String16 clearOption("-clear");
Ray Essickf65f4212017-08-31 11:41:19 -0700385 bool clear = false;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800386 String16 sinceOption("-since");
Ray Essickf65f4212017-08-31 11:41:19 -0700387 nsecs_t ts_since = 0;
Ray Essick35ad27f2017-01-30 14:04:11 -0800388 String16 helpOption("-help");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700389 String16 onlyOption("-only");
Ray Essickf65f4212017-08-31 11:41:19 -0700390 AString only;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800391 int n = args.size();
Ray Essickf65f4212017-08-31 11:41:19 -0700392
Ray Essickb5fac8e2016-12-12 11:33:56 -0800393 for (int i = 0; i < n; i++) {
394 String8 myarg(args[i]);
395 if (args[i] == clearOption) {
396 clear = true;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700397 } else if (args[i] == summaryOption) {
398 summary = true;
Ray Essickf65f4212017-08-31 11:41:19 -0700399 } else if (args[i] == protoOption) {
400 i++;
401 if (i < n) {
402 String8 value(args[i]);
403 int proto = MediaAnalyticsItem::PROTO_V0; // default to original
404 char *endp;
405 const char *p = value.string();
406 proto = strtol(p, &endp, 10);
407 if (endp != p || *endp == '\0') {
408 if (proto < MediaAnalyticsItem::PROTO_FIRST) {
409 proto = MediaAnalyticsItem::PROTO_FIRST;
410 } else if (proto > MediaAnalyticsItem::PROTO_LAST) {
411 proto = MediaAnalyticsItem::PROTO_LAST;
412 }
413 mDumpProto = proto;
414 }
415 }
Ray Essickb5fac8e2016-12-12 11:33:56 -0800416 } else if (args[i] == sinceOption) {
417 i++;
418 if (i < n) {
419 String8 value(args[i]);
420 char *endp;
421 const char *p = value.string();
422 ts_since = strtoll(p, &endp, 10);
423 if (endp == p || *endp != '\0') {
424 ts_since = 0;
425 }
426 } else {
427 ts_since = 0;
428 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800429 // command line is milliseconds; internal units are nano-seconds
430 ts_since *= 1000*1000;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700431 } else if (args[i] == onlyOption) {
432 i++;
433 if (i < n) {
434 String8 value(args[i]);
Ray Essickf65f4212017-08-31 11:41:19 -0700435 only = value.string();
Ray Essick2e9c63b2017-03-29 15:16:44 -0700436 }
Ray Essick35ad27f2017-01-30 14:04:11 -0800437 } else if (args[i] == helpOption) {
438 result.append("Recognized parameters:\n");
439 result.append("-help this help message\n");
Ray Essickf65f4212017-08-31 11:41:19 -0700440 result.append("-proto X dump using protocol X (defaults to 1)");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700441 result.append("-summary show summary info\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800442 result.append("-clear clears out saved records\n");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700443 result.append("-only X process records for component X\n");
444 result.append("-since X include records since X\n");
445 result.append(" (X is milliseconds since the UNIX epoch)\n");
Ray Essick35ad27f2017-01-30 14:04:11 -0800446 write(fd, result.string(), result.size());
447 return NO_ERROR;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800448 }
449 }
450
451 Mutex::Autolock _l(mLock);
452
Ray Essick2e9c63b2017-03-29 15:16:44 -0700453 // we ALWAYS dump this piece
454 snprintf(buffer, SIZE, "Dump of the %s process:\n", kServiceName);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800455 result.append(buffer);
456
Ray Essick2e9c63b2017-03-29 15:16:44 -0700457 dumpHeaders(result, ts_since);
458
Ray Essickf65f4212017-08-31 11:41:19 -0700459 // want exactly 1, to avoid confusing folks that parse the output
Ray Essick2e9c63b2017-03-29 15:16:44 -0700460 if (summary) {
Ray Essickf65f4212017-08-31 11:41:19 -0700461 dumpSummaries(result, ts_since, only.c_str());
Ray Essick2e9c63b2017-03-29 15:16:44 -0700462 } else {
Ray Essickf65f4212017-08-31 11:41:19 -0700463 dumpRecent(result, ts_since, only.c_str());
Ray Essick2e9c63b2017-03-29 15:16:44 -0700464 }
465
466
467 if (clear) {
468 // remove everything from the finalized queue
469 while (mFinalized->size() > 0) {
470 MediaAnalyticsItem * oitem = *(mFinalized->begin());
471 mFinalized->erase(mFinalized->begin());
472 delete oitem;
473 mItemsDiscarded++;
474 }
475
476 // shall we clear the summary data too?
477
478 }
479
480 write(fd, result.string(), result.size());
481 return NO_ERROR;
482}
483
484// dump headers
485void MediaAnalyticsService::dumpHeaders(String8 &result, nsecs_t ts_since) {
486 const size_t SIZE = 512;
487 char buffer[SIZE];
488
Ray Essickf65f4212017-08-31 11:41:19 -0700489 snprintf(buffer, SIZE, "Protocol Version: %d\n", mDumpProto);
490 result.append(buffer);
491
Ray Essickb5fac8e2016-12-12 11:33:56 -0800492 int enabled = MediaAnalyticsItem::isEnabled();
493 if (enabled) {
Ray Essickd38e1742017-01-23 15:17:06 -0800494 snprintf(buffer, SIZE, "Metrics gathering: enabled\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800495 } else {
Ray Essickd38e1742017-01-23 15:17:06 -0800496 snprintf(buffer, SIZE, "Metrics gathering: DISABLED via property\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800497 }
498 result.append(buffer);
499
500 snprintf(buffer, SIZE,
Ray Essickf65f4212017-08-31 11:41:19 -0700501 "Since Boot: Submissions: %8" PRId64
502 " Finalizations: %8" PRId64 "\n",
503 mItemsSubmitted, mItemsFinalized);
504 result.append(buffer);
505 snprintf(buffer, SIZE,
506 "Records Discarded: %8" PRId64
507 " (by Count: %" PRId64 " by Expiration: %" PRId64 ")\n",
508 mItemsDiscarded, mItemsDiscardedCount, mItemsDiscardedExpire);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800509 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700510 snprintf(buffer, SIZE,
511 "Summary Sets Discarded: %" PRId64 "\n", mSetsDiscarded);
512 result.append(buffer);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800513 if (ts_since != 0) {
514 snprintf(buffer, SIZE,
515 "Dumping Queue entries more recent than: %" PRId64 "\n",
516 (int64_t) ts_since);
517 result.append(buffer);
518 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700519}
520
521// dump summary info
522void MediaAnalyticsService::dumpSummaries(String8 &result, nsecs_t ts_since, const char *only) {
523 const size_t SIZE = 512;
524 char buffer[SIZE];
525 int slot = 0;
526
527 snprintf(buffer, SIZE, "\nSummarized Metrics:\n");
528 result.append(buffer);
529
Ray Essickf65f4212017-08-31 11:41:19 -0700530 if (only != NULL && *only == '\0') {
531 only = NULL;
532 }
533
Ray Essick2e9c63b2017-03-29 15:16:44 -0700534 // have each of the distillers dump records
535 if (mSummarizerSets != NULL) {
536 List<SummarizerSet *>::iterator itSet = mSummarizerSets->begin();
537 for (; itSet != mSummarizerSets->end(); itSet++) {
538 nsecs_t when = (*itSet)->getStarted();
539 if (when < ts_since) {
540 continue;
541 }
542 List<MetricsSummarizer *> *list = (*itSet)->getSummarizers();
543 List<MetricsSummarizer *>::iterator it = list->begin();
544 for (; it != list->end(); it++) {
545 if (only != NULL && strcmp(only, (*it)->getKey()) != 0) {
546 ALOGV("Told to omit '%s'", (*it)->getKey());
547 }
548 AString distilled = (*it)->dumpSummary(slot, only);
549 result.append(distilled.c_str());
550 }
551 }
552 }
553}
554
555// the recent, detailed queues
556void MediaAnalyticsService::dumpRecent(String8 &result, nsecs_t ts_since, const char * only) {
557 const size_t SIZE = 512;
558 char buffer[SIZE];
Ray Essickb5fac8e2016-12-12 11:33:56 -0800559
Ray Essickf65f4212017-08-31 11:41:19 -0700560 if (only != NULL && *only == '\0') {
561 only = NULL;
562 }
563
Ray Essickb5fac8e2016-12-12 11:33:56 -0800564 // show the recently recorded records
Ray Essickd38e1742017-01-23 15:17:06 -0800565 snprintf(buffer, sizeof(buffer), "\nFinalized Metrics (oldest first):\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800566 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700567 result.append(this->dumpQueue(mFinalized, ts_since, only));
Ray Essickb5fac8e2016-12-12 11:33:56 -0800568
Ray Essickd38e1742017-01-23 15:17:06 -0800569 snprintf(buffer, sizeof(buffer), "\nIn-Progress Metrics (newest first):\n");
Ray Essickb5fac8e2016-12-12 11:33:56 -0800570 result.append(buffer);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700571 result.append(this->dumpQueue(mOpen, ts_since, only));
Ray Essickb5fac8e2016-12-12 11:33:56 -0800572
573 // show who is connected and injecting records?
574 // talk about # records fed to the 'readers'
575 // talk about # records we discarded, perhaps "discarded w/o reading" too
Ray Essick3938dc62016-11-01 08:56:56 -0700576}
Ray Essick3938dc62016-11-01 08:56:56 -0700577// caller has locked mLock...
Ray Essickb5fac8e2016-12-12 11:33:56 -0800578String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700579 return dumpQueue(theList, (nsecs_t) 0, NULL);
Ray Essickb5fac8e2016-12-12 11:33:56 -0800580}
581
Ray Essick2e9c63b2017-03-29 15:16:44 -0700582String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList, nsecs_t ts_since, const char * only) {
Ray Essick3938dc62016-11-01 08:56:56 -0700583 String8 result;
584 int slot = 0;
585
586 if (theList->empty()) {
587 result.append("empty\n");
588 } else {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800589 List<MediaAnalyticsItem *>::iterator it = theList->begin();
590 for (; it != theList->end(); it++) {
591 nsecs_t when = (*it)->getTimestamp();
592 if (when < ts_since) {
593 continue;
594 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700595 if (only != NULL &&
596 strcmp(only, (*it)->getKey().c_str()) != 0) {
597 ALOGV("Omit '%s', it's not '%s'", (*it)->getKey().c_str(), only);
598 continue;
599 }
Ray Essickf65f4212017-08-31 11:41:19 -0700600 AString entry = (*it)->toString(mDumpProto);
Ray Essick35ad27f2017-01-30 14:04:11 -0800601 result.appendFormat("%5d: %s\n", slot, entry.c_str());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800602 slot++;
Ray Essick3938dc62016-11-01 08:56:56 -0700603 }
604 }
605
606 return result;
607}
608
609//
610// Our Cheap in-core, non-persistent records management.
611// XXX: rewrite this to manage persistence, etc.
612
613// insert appropriately into queue
Ray Essick9bb7e3b2017-10-23 13:01:48 -0700614// caller should hold mLock
Ray Essickb5fac8e2016-12-12 11:33:56 -0800615void MediaAnalyticsService::saveItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem * item, int front) {
Ray Essick3938dc62016-11-01 08:56:56 -0700616
Ray Essick3938dc62016-11-01 08:56:56 -0700617 if (front) {
Ray Essicke5db6db2017-11-10 15:54:32 -0800618 // for non-finalized stuff, since we expect to reference it again soon,
619 // make it quicker to find (nearer the front of our list)
Ray Essick3938dc62016-11-01 08:56:56 -0700620 l->push_front(item);
621 } else {
Ray Essicke5db6db2017-11-10 15:54:32 -0800622 // for finalized records, which we want to dump 'in sequence order'
Ray Essick3938dc62016-11-01 08:56:56 -0700623 l->push_back(item);
624 }
625
Ray Essicke5db6db2017-11-10 15:54:32 -0800626 // our reclaim process is for oldest-first queues
627 if (front) {
628 return;
629 }
630
631
Ray Essickf65f4212017-08-31 11:41:19 -0700632 // keep removing old records the front until we're in-bounds (count)
Ray Essick3938dc62016-11-01 08:56:56 -0700633 if (mMaxRecords > 0) {
634 while (l->size() > (size_t) mMaxRecords) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800635 MediaAnalyticsItem * oitem = *(l->begin());
Ray Essicke5db6db2017-11-10 15:54:32 -0800636 if (oitem == item) {
637 break;
638 }
Ray Essick3938dc62016-11-01 08:56:56 -0700639 l->erase(l->begin());
Ray Essickb5fac8e2016-12-12 11:33:56 -0800640 delete oitem;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800641 mItemsDiscarded++;
Ray Essickf65f4212017-08-31 11:41:19 -0700642 mItemsDiscardedCount++;
643 }
644 }
645
646 // keep removing old records the front until we're in-bounds (count)
647 if (mMaxRecordAgeNs > 0) {
648 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
649 while (l->size() > 0) {
650 MediaAnalyticsItem * oitem = *(l->begin());
651 nsecs_t when = oitem->getTimestamp();
Ray Essicke5db6db2017-11-10 15:54:32 -0800652 if (oitem == item) {
653 break;
654 }
Ray Essickf65f4212017-08-31 11:41:19 -0700655 // careful about timejumps too
656 if ((now > when) && (now-when) <= mMaxRecordAgeNs) {
657 // this (and the rest) are recent enough to keep
658 break;
659 }
660 l->erase(l->begin());
661 delete oitem;
662 mItemsDiscarded++;
663 mItemsDiscardedExpire++;
Ray Essick3938dc62016-11-01 08:56:56 -0700664 }
665 }
Ray Essick3938dc62016-11-01 08:56:56 -0700666}
667
668// are they alike enough that nitem can be folded into oitem?
Ray Essickb5fac8e2016-12-12 11:33:56 -0800669static bool compatibleItems(MediaAnalyticsItem * oitem, MediaAnalyticsItem * nitem) {
Ray Essick3938dc62016-11-01 08:56:56 -0700670
Ray Essick3938dc62016-11-01 08:56:56 -0700671 // general safety
672 if (nitem->getUid() != oitem->getUid()) {
673 return false;
674 }
675 if (nitem->getPid() != oitem->getPid()) {
676 return false;
677 }
678
679 // key -- needs to match
680 if (nitem->getKey() == oitem->getKey()) {
681 // still in the game.
682 } else {
683 return false;
684 }
685
686 // session id -- empty field in new is allowed
687 MediaAnalyticsItem::SessionID_t osession = oitem->getSessionID();
688 MediaAnalyticsItem::SessionID_t nsession = nitem->getSessionID();
689 if (nsession != osession) {
690 // incoming '0' matches value in osession
691 if (nsession != 0) {
692 return false;
693 }
694 }
695
696 return true;
697}
698
699// find the incomplete record that this will overlay
Ray Essick9bb7e3b2017-10-23 13:01:48 -0700700// caller should hold mLock
Ray Essickb5fac8e2016-12-12 11:33:56 -0800701MediaAnalyticsItem *MediaAnalyticsService::findItem(List<MediaAnalyticsItem*> *theList, MediaAnalyticsItem *nitem, bool removeit) {
Ray Essick3938dc62016-11-01 08:56:56 -0700702 if (nitem == NULL) {
703 return NULL;
704 }
705
Ray Essickb5fac8e2016-12-12 11:33:56 -0800706 MediaAnalyticsItem *item = NULL;
707
Ray Essickb5fac8e2016-12-12 11:33:56 -0800708 for (List<MediaAnalyticsItem *>::iterator it = theList->begin();
Ray Essick3938dc62016-11-01 08:56:56 -0700709 it != theList->end(); it++) {
Ray Essickb5fac8e2016-12-12 11:33:56 -0800710 MediaAnalyticsItem *tmp = (*it);
Ray Essick3938dc62016-11-01 08:56:56 -0700711
712 if (!compatibleItems(tmp, nitem)) {
713 continue;
714 }
715
716 // we match! this is the one I want.
717 if (removeit) {
718 theList->erase(it);
719 }
720 item = tmp;
721 break;
722 }
723 return item;
724}
725
726
727// delete the indicated record
Ray Essick9bb7e3b2017-10-23 13:01:48 -0700728// caller should hold mLock
Ray Essickb5fac8e2016-12-12 11:33:56 -0800729void MediaAnalyticsService::deleteItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem *item) {
Ray Essick3938dc62016-11-01 08:56:56 -0700730
Ray Essickb5fac8e2016-12-12 11:33:56 -0800731 for (List<MediaAnalyticsItem *>::iterator it = l->begin();
Ray Essick3938dc62016-11-01 08:56:56 -0700732 it != l->end(); it++) {
733 if ((*it)->getSessionID() != item->getSessionID())
734 continue;
Ray Essickb5fac8e2016-12-12 11:33:56 -0800735 delete *it;
Ray Essick3938dc62016-11-01 08:56:56 -0700736 l->erase(it);
737 break;
738 }
Ray Essick3938dc62016-11-01 08:56:56 -0700739}
740
Ray Essickd38e1742017-01-23 15:17:06 -0800741static AString allowedKeys[] =
742{
743 "codec",
744 "extractor"
745};
Ray Essick3938dc62016-11-01 08:56:56 -0700746
Ray Essickd38e1742017-01-23 15:17:06 -0800747static const int nAllowedKeys = sizeof(allowedKeys) / sizeof(allowedKeys[0]);
748
749// are the contents good
750bool MediaAnalyticsService::contentValid(MediaAnalyticsItem *item, bool isTrusted) {
751
752 // untrusted uids can only send us a limited set of keys
753 if (isTrusted == false) {
754 // restrict to a specific set of keys
755 AString key = item->getKey();
756
757 size_t i;
758 for(i = 0; i < nAllowedKeys; i++) {
759 if (key == allowedKeys[i]) {
760 break;
761 }
762 }
763 if (i == nAllowedKeys) {
764 ALOGD("Ignoring (key): %s", item->toString().c_str());
765 return false;
766 }
767 }
768
Ray Essick3938dc62016-11-01 08:56:56 -0700769 // internal consistency
770
771 return true;
772}
773
774// are we rate limited, normally false
Ray Essickb5fac8e2016-12-12 11:33:56 -0800775bool MediaAnalyticsService::rateLimited(MediaAnalyticsItem *) {
Ray Essick3938dc62016-11-01 08:56:56 -0700776
777 return false;
778}
779
Ray Essick2e9c63b2017-03-29 15:16:44 -0700780// insert into the appropriate summarizer.
781// we make our own copy to save/summarize
782void MediaAnalyticsService::summarize(MediaAnalyticsItem *item) {
783
784 ALOGV("MediaAnalyticsService::summarize()");
785
786 if (item == NULL) {
787 return;
788 }
789
790 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
791 if (mCurrentSet == NULL
792 || (mCurrentSet->getStarted() + mNewSetInterval < now)) {
793 newSummarizerSet();
794 }
795
796 if (mCurrentSet == NULL) {
797 return;
798 }
799
800 List<MetricsSummarizer *> *summarizers = mCurrentSet->getSummarizers();
801 List<MetricsSummarizer *>::iterator it = summarizers->begin();
802 for (; it != summarizers->end(); it++) {
803 if ((*it)->isMine(*item)) {
804 break;
805 }
806 }
807 if (it == summarizers->end()) {
808 ALOGD("no handler for type %s", item->getKey().c_str());
809 return; // no handler
810 }
811
812 // invoke the summarizer. summarizer will make whatever copies
813 // it wants; the caller retains ownership of item.
814
815 (*it)->handleRecord(item);
816
817}
Ray Essick3938dc62016-11-01 08:56:56 -0700818
Ray Essickfa149562017-09-19 09:27:31 -0700819// how long we hold package info before we re-fetch it
820#define PKG_EXPIRATION_NS (30*60*1000000000ll) // 30 minutes, in nsecs
Ray Essickf65f4212017-08-31 11:41:19 -0700821
822// give me the package name, perhaps going to find it
Ray Essickfa149562017-09-19 09:27:31 -0700823void MediaAnalyticsService::setPkgInfo(MediaAnalyticsItem *item, uid_t uid, bool setName, bool setVersion) {
824 ALOGV("asking for packagename to go with uid=%d", uid);
825
826 if (!setName && !setVersion) {
827 // setting nothing? strange
828 return;
829 }
830
831 nsecs_t now = systemTime(SYSTEM_TIME_REALTIME);
832 struct UidToPkgMap mapping;
833 mapping.uid = (-1);
834
Ray Essickf65f4212017-08-31 11:41:19 -0700835 ssize_t i = mPkgMappings.indexOfKey(uid);
836 if (i >= 0) {
Ray Essickfa149562017-09-19 09:27:31 -0700837 mapping = mPkgMappings.valueAt(i);
838 ALOGV("Expiration? uid %d expiration %" PRId64 " now %" PRId64,
839 uid, mapping.expiration, now);
840 if (mapping.expiration < now) {
841 // purge our current entry and re-query
842 ALOGV("entry for uid %d expired, now= %" PRId64 "", uid, now);
843 mPkgMappings.removeItemsAt(i, 1);
844 // could cheat and use a goto back to the top of the routine.
845 // a good compiler should recognize the local tail recursion...
846 return setPkgInfo(item, uid, setName, setVersion);
Ray Essickf65f4212017-08-31 11:41:19 -0700847 }
Ray Essickfa149562017-09-19 09:27:31 -0700848 } else {
849 AString pkg;
850 std::string installer = "";
851 int32_t versionCode = 0;
Ray Essickf65f4212017-08-31 11:41:19 -0700852
Ray Essickfa149562017-09-19 09:27:31 -0700853 struct passwd *pw = getpwuid(uid);
854 if (pw) {
855 pkg = pw->pw_name;
856 }
Ray Essickf65f4212017-08-31 11:41:19 -0700857
Ray Essickfa149562017-09-19 09:27:31 -0700858 // find the proper value -- should we cache this binder??
Ray Essickf65f4212017-08-31 11:41:19 -0700859
Ray Essickfa149562017-09-19 09:27:31 -0700860 sp<IBinder> binder = NULL;
861 sp<IServiceManager> sm = defaultServiceManager();
862 if (sm == NULL) {
863 ALOGE("defaultServiceManager failed");
Ray Essickf65f4212017-08-31 11:41:19 -0700864 } else {
Ray Essickfa149562017-09-19 09:27:31 -0700865 binder = sm->getService(String16("package_native"));
866 if (binder == NULL) {
867 ALOGE("getService package_native failed");
868 }
869 }
870
871 if (binder != NULL) {
872 sp<IPackageManagerNative> package_mgr = interface_cast<IPackageManagerNative>(binder);
873 binder::Status status;
874
875 std::vector<int> uids;
876 std::vector<std::string> names;
877
878 uids.push_back(uid);
879
880 status = package_mgr->getNamesForUids(uids, &names);
881 if (!status.isOk()) {
882 ALOGE("package_native::getNamesForUids failed: %s",
883 status.exceptionMessage().c_str());
884 } else {
885 if (!names[0].empty()) {
886 pkg = names[0].c_str();
887 }
888 }
889
890 // strip any leading "shared:" strings that came back
891 if (pkg.startsWith("shared:")) {
892 pkg.erase(0, 7);
893 }
894
895 // determine how pkg was installed and the versionCode
896 //
897 if (pkg.empty()) {
898 // no name for us to manage
899 } else if (strchr(pkg.c_str(), '.') == NULL) {
900 // not of form 'com.whatever...'; assume internal and ok
901 } else if (strncmp(pkg.c_str(), "android.", 8) == 0) {
902 // android.* packages are assumed fine
903 } else {
904 String16 pkgName16(pkg.c_str());
905 status = package_mgr->getInstallerForPackage(pkgName16, &installer);
906 if (!status.isOk()) {
907 ALOGE("package_native::getInstallerForPackage failed: %s",
908 status.exceptionMessage().c_str());
909 }
910
911 // skip if we didn't get an installer
912 if (status.isOk()) {
913 status = package_mgr->getVersionCodeForPackage(pkgName16, &versionCode);
914 if (!status.isOk()) {
915 ALOGE("package_native::getVersionCodeForPackage failed: %s",
916 status.exceptionMessage().c_str());
917 }
918 }
919
920
921 ALOGV("package '%s' installed by '%s' versioncode %d / %08x",
922 pkg.c_str(), installer.c_str(), versionCode, versionCode);
923
924 if (strncmp(installer.c_str(), "com.android.", 12) == 0) {
925 // from play store, we keep info
926 } else if (strncmp(installer.c_str(), "com.google.", 11) == 0) {
927 // some google source, we keep info
928 } else if (strcmp(installer.c_str(), "preload") == 0) {
929 // preloads, we keep the info
930 } else if (installer.c_str()[0] == '\0') {
931 // sideload (no installer); do not report
932 pkg = "";
933 versionCode = 0;
934 } else {
935 // unknown installer; do not report
936 pkg = "";
937 versionCode = 0;
938 }
939 }
940 }
941
942 // add it to the map, to save a subsequent lookup
943 if (!pkg.empty()) {
944 Mutex::Autolock _l(mLock_mappings);
945 ALOGV("Adding uid %d pkg '%s'", uid, pkg.c_str());
946 ssize_t i = mPkgMappings.indexOfKey(uid);
947 if (i < 0) {
948 mapping.uid = uid;
949 mapping.pkg = pkg;
950 mapping.installer = installer.c_str();
951 mapping.versionCode = versionCode;
952 mapping.expiration = now + PKG_EXPIRATION_NS;
953 ALOGV("expiration for uid %d set to %" PRId64 "", uid, mapping.expiration);
954
955 mPkgMappings.add(uid, mapping);
Ray Essickf65f4212017-08-31 11:41:19 -0700956 }
957 }
958 }
959
Ray Essickfa149562017-09-19 09:27:31 -0700960 if (mapping.uid != (uid_t)(-1)) {
961 if (setName) {
962 item->setPkgName(mapping.pkg);
963 }
964 if (setVersion) {
965 item->setPkgVersionCode(mapping.versionCode);
Ray Essickf65f4212017-08-31 11:41:19 -0700966 }
967 }
Ray Essickf65f4212017-08-31 11:41:19 -0700968}
969
Ray Essick3938dc62016-11-01 08:56:56 -0700970} // namespace android