Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1 | /* |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 3 | * |
| 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 Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 23 | #include <stdint.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 24 | #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> |
| 32 | |
| 33 | #include <cutils/atomic.h> |
| 34 | #include <cutils/properties.h> // for property_get |
| 35 | |
| 36 | #include <utils/misc.h> |
| 37 | |
| 38 | #include <binder/IPCThreadState.h> |
| 39 | #include <binder/IServiceManager.h> |
| 40 | #include <binder/MemoryHeapBase.h> |
| 41 | #include <binder/MemoryBase.h> |
| 42 | #include <gui/Surface.h> |
| 43 | #include <utils/Errors.h> // for status_t |
| 44 | #include <utils/List.h> |
| 45 | #include <utils/String8.h> |
| 46 | #include <utils/SystemClock.h> |
| 47 | #include <utils/Timers.h> |
| 48 | #include <utils/Vector.h> |
| 49 | |
| 50 | #include <media/AudioPolicyHelper.h> |
| 51 | #include <media/IMediaHTTPService.h> |
| 52 | #include <media/IRemoteDisplay.h> |
| 53 | #include <media/IRemoteDisplayClient.h> |
| 54 | #include <media/MediaPlayerInterface.h> |
| 55 | #include <media/mediarecorder.h> |
| 56 | #include <media/MediaMetadataRetrieverInterface.h> |
| 57 | #include <media/Metadata.h> |
| 58 | #include <media/AudioTrack.h> |
| 59 | #include <media/MemoryLeakTrackUtil.h> |
| 60 | #include <media/stagefright/MediaCodecList.h> |
| 61 | #include <media/stagefright/MediaErrors.h> |
| 62 | #include <media/stagefright/Utils.h> |
| 63 | #include <media/stagefright/foundation/ADebug.h> |
| 64 | #include <media/stagefright/foundation/ALooperRoster.h> |
| 65 | #include <mediautils/BatteryNotifier.h> |
| 66 | |
| 67 | //#include <memunreachable/memunreachable.h> |
| 68 | #include <system/audio.h> |
| 69 | |
| 70 | #include <private/android_filesystem_config.h> |
| 71 | |
| 72 | #include "MediaAnalyticsService.h" |
| 73 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 74 | #include "MetricsSummarizer.h" |
| 75 | #include "MetricsSummarizerCodec.h" |
| 76 | #include "MetricsSummarizerExtractor.h" |
| 77 | #include "MetricsSummarizerPlayer.h" |
| 78 | #include "MetricsSummarizerRecorder.h" |
| 79 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 80 | |
| 81 | namespace android { |
| 82 | |
| 83 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 84 | |
| 85 | // summarized records |
| 86 | // up to 48 sets, each covering an hour -- at least 2 days of coverage |
| 87 | // (will be longer if there are hours without any media action) |
| 88 | static const nsecs_t kNewSetIntervalNs = 3600*(1000*1000*1000ll); |
| 89 | static const int kMaxRecordSets = 48; |
| 90 | // individual records kept in memory |
| 91 | static const int kMaxRecords = 100; |
| 92 | |
| 93 | |
| 94 | static const char *kServiceName = "media.metrics"; |
| 95 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 96 | |
| 97 | //using android::status_t; |
| 98 | //using android::OK; |
| 99 | //using android::BAD_VALUE; |
| 100 | //using android::NOT_ENOUGH_DATA; |
| 101 | //using android::Parcel; |
| 102 | |
| 103 | |
| 104 | void MediaAnalyticsService::instantiate() { |
| 105 | defaultServiceManager()->addService( |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 106 | String16(kServiceName), new MediaAnalyticsService()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 109 | // handle sets of summarizers |
| 110 | MediaAnalyticsService::SummarizerSet::SummarizerSet() { |
| 111 | mSummarizers = new List<MetricsSummarizer *>(); |
| 112 | } |
| 113 | MediaAnalyticsService::SummarizerSet::~SummarizerSet() { |
| 114 | // empty the list |
| 115 | List<MetricsSummarizer *> *l = mSummarizers; |
| 116 | while (l->size() > 0) { |
| 117 | MetricsSummarizer *summarizer = *(l->begin()); |
| 118 | l->erase(l->begin()); |
| 119 | delete summarizer; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void MediaAnalyticsService::newSummarizerSet() { |
| 124 | ALOGD("MediaAnalyticsService::newSummarizerSet"); |
| 125 | MediaAnalyticsService::SummarizerSet *set = new MediaAnalyticsService::SummarizerSet(); |
| 126 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 127 | set->setStarted(now); |
| 128 | |
| 129 | set->appendSummarizer(new MetricsSummarizerExtractor("extractor")); |
| 130 | set->appendSummarizer(new MetricsSummarizerCodec("codec")); |
| 131 | set->appendSummarizer(new MetricsSummarizerPlayer("nuplayer")); |
| 132 | set->appendSummarizer(new MetricsSummarizerRecorder("recorder")); |
| 133 | |
| 134 | // ALWAYS at the end, since it catches everything |
| 135 | set->appendSummarizer(new MetricsSummarizer(NULL)); |
| 136 | |
| 137 | // inject this set at the BACK of the list. |
| 138 | mSummarizerSets->push_back(set); |
| 139 | mCurrentSet = set; |
| 140 | |
| 141 | // limit the # that we have |
| 142 | if (mMaxRecordSets > 0) { |
| 143 | List<SummarizerSet *> *l = mSummarizerSets; |
| 144 | while (l->size() > (size_t) mMaxRecordSets) { |
| 145 | ALOGD("Deleting oldest record set...."); |
| 146 | MediaAnalyticsService::SummarizerSet *oset = *(l->begin()); |
| 147 | l->erase(l->begin()); |
| 148 | delete oset; |
| 149 | mSetsDiscarded++; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 154 | MediaAnalyticsService::MediaAnalyticsService() |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 155 | : mMaxRecords(kMaxRecords), |
| 156 | mMaxRecordSets(kMaxRecordSets), |
| 157 | mNewSetInterval(kNewSetIntervalNs) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 158 | |
| 159 | ALOGD("MediaAnalyticsService created"); |
| 160 | // clear our queues |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 161 | mOpen = new List<MediaAnalyticsItem *>(); |
| 162 | mFinalized = new List<MediaAnalyticsItem *>(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 163 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 164 | mSummarizerSets = new List<MediaAnalyticsService::SummarizerSet *>(); |
| 165 | newSummarizerSet(); |
| 166 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 167 | mItemsSubmitted = 0; |
| 168 | mItemsFinalized = 0; |
| 169 | mItemsDiscarded = 0; |
| 170 | |
| 171 | mLastSessionID = 0; |
| 172 | // recover any persistency we set up |
| 173 | // etc |
| 174 | } |
| 175 | |
| 176 | MediaAnalyticsService::~MediaAnalyticsService() { |
| 177 | ALOGD("MediaAnalyticsService destroyed"); |
| 178 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 179 | // clean out mOpen and mFinalized |
| 180 | delete mOpen; |
| 181 | mOpen = NULL; |
| 182 | delete mFinalized; |
| 183 | mFinalized = NULL; |
| 184 | |
| 185 | // XXX: clean out the summaries |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | |
| 189 | MediaAnalyticsItem::SessionID_t MediaAnalyticsService::generateUniqueSessionID() { |
| 190 | // generate a new sessionid |
| 191 | |
| 192 | Mutex::Autolock _l(mLock_ids); |
| 193 | return (++mLastSessionID); |
| 194 | } |
| 195 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 196 | // caller surrenders ownership of 'item' |
| 197 | MediaAnalyticsItem::SessionID_t MediaAnalyticsService::submit(MediaAnalyticsItem *item, bool forcenew) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 198 | |
| 199 | MediaAnalyticsItem::SessionID_t id = MediaAnalyticsItem::SessionIDInvalid; |
| 200 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 201 | // we control these, generally not trusting user input |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 202 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 203 | item->setTimestamp(now); |
| 204 | int pid = IPCThreadState::self()->getCallingPid(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 205 | int uid = IPCThreadState::self()->getCallingUid(); |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 206 | |
| 207 | int uid_given = item->getUid(); |
| 208 | int pid_given = item->getPid(); |
| 209 | |
| 210 | // although we do make exceptions for particular client uids |
| 211 | // that we know we trust. |
| 212 | // |
| 213 | bool isTrusted = false; |
| 214 | |
| 215 | switch (uid) { |
| 216 | case AID_MEDIA: |
| 217 | case AID_MEDIA_CODEC: |
| 218 | case AID_MEDIA_EX: |
| 219 | case AID_MEDIA_DRM: |
| 220 | // trusted source, only override default values |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 221 | isTrusted = true; |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 222 | if (uid_given == (-1)) { |
| 223 | item->setUid(uid); |
| 224 | } |
| 225 | if (pid_given == (-1)) { |
| 226 | item->setPid(pid); |
| 227 | } |
| 228 | break; |
| 229 | default: |
| 230 | isTrusted = false; |
| 231 | item->setPid(pid); |
| 232 | item->setUid(uid); |
| 233 | break; |
| 234 | } |
| 235 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 236 | |
| 237 | mItemsSubmitted++; |
| 238 | |
| 239 | // validate the record; we discard if we don't like it |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 240 | if (contentValid(item, isTrusted) == false) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 241 | delete item; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 242 | return MediaAnalyticsItem::SessionIDInvalid; |
| 243 | } |
| 244 | |
| 245 | |
| 246 | // if we have a sesisonid in the new record, look to make |
| 247 | // sure it doesn't appear in the finalized list. |
| 248 | // XXX: this is for security / DOS prevention. |
| 249 | // may also require that we persist the unique sessionIDs |
| 250 | // across boots [instead of within a single boot] |
| 251 | |
| 252 | |
| 253 | // match this new record up against records in the open |
| 254 | // list... |
| 255 | // if there's a match, merge them together |
| 256 | // deal with moving the old / merged record into the finalized que |
| 257 | |
| 258 | bool finalizing = item->getFinalized(); |
| 259 | |
| 260 | // if finalizing, we'll remove it |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 261 | MediaAnalyticsItem *oitem = findItem(mOpen, item, finalizing | forcenew); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 262 | if (oitem != NULL) { |
| 263 | if (forcenew) { |
| 264 | // old one gets finalized, then we insert the new one |
| 265 | // so we'll have 2 records at the end of this. |
| 266 | // but don't finalize an empty record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 267 | if (oitem->count() == 0) { |
| 268 | // we're responsible for disposing of the dead record |
| 269 | delete oitem; |
| 270 | oitem = NULL; |
| 271 | } else { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 272 | oitem->setFinalized(true); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 273 | summarize(oitem); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 274 | saveItem(mFinalized, oitem, 0); |
| 275 | } |
| 276 | // new record could itself be marked finalized... |
| 277 | if (finalizing) { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 278 | summarize(item); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 279 | saveItem(mFinalized, item, 0); |
| 280 | mItemsFinalized++; |
| 281 | } else { |
| 282 | saveItem(mOpen, item, 1); |
| 283 | } |
| 284 | id = item->getSessionID(); |
| 285 | } else { |
| 286 | // combine the records, send it to finalized if appropriate |
| 287 | oitem->merge(item); |
| 288 | if (finalizing) { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 289 | summarize(oitem); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 290 | saveItem(mFinalized, oitem, 0); |
| 291 | mItemsFinalized++; |
| 292 | } |
| 293 | id = oitem->getSessionID(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 294 | |
| 295 | // we're responsible for disposing of the dead record |
| 296 | delete item; |
| 297 | item = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 298 | } |
| 299 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 300 | // nothing to merge, save the new record |
| 301 | id = item->getSessionID(); |
| 302 | if (finalizing) { |
| 303 | if (item->count() == 0) { |
| 304 | // drop empty records |
| 305 | delete item; |
| 306 | item = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 307 | } else { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 308 | summarize(item); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 309 | saveItem(mFinalized, item, 0); |
| 310 | mItemsFinalized++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 311 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 312 | } else { |
| 313 | saveItem(mOpen, item, 1); |
| 314 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 315 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 316 | return id; |
| 317 | } |
| 318 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 319 | status_t MediaAnalyticsService::dump(int fd, const Vector<String16>& args) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 320 | { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 321 | const size_t SIZE = 512; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 322 | char buffer[SIZE]; |
| 323 | String8 result; |
| 324 | |
| 325 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 326 | snprintf(buffer, SIZE, "Permission Denial: " |
| 327 | "can't dump MediaAnalyticsService from pid=%d, uid=%d\n", |
| 328 | IPCThreadState::self()->getCallingPid(), |
| 329 | IPCThreadState::self()->getCallingUid()); |
| 330 | result.append(buffer); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 331 | write(fd, result.string(), result.size()); |
| 332 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 333 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 334 | |
| 335 | // crack any parameters |
| 336 | bool clear = false; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 337 | bool summary = false; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 338 | nsecs_t ts_since = 0; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 339 | String16 summaryOption("-summary"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 340 | String16 clearOption("-clear"); |
| 341 | String16 sinceOption("-since"); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 342 | String16 helpOption("-help"); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 343 | String16 onlyOption("-only"); |
| 344 | const char *only = NULL; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 345 | int n = args.size(); |
| 346 | for (int i = 0; i < n; i++) { |
| 347 | String8 myarg(args[i]); |
| 348 | if (args[i] == clearOption) { |
| 349 | clear = true; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 350 | } else if (args[i] == summaryOption) { |
| 351 | summary = true; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 352 | } else if (args[i] == sinceOption) { |
| 353 | i++; |
| 354 | if (i < n) { |
| 355 | String8 value(args[i]); |
| 356 | char *endp; |
| 357 | const char *p = value.string(); |
| 358 | ts_since = strtoll(p, &endp, 10); |
| 359 | if (endp == p || *endp != '\0') { |
| 360 | ts_since = 0; |
| 361 | } |
| 362 | } else { |
| 363 | ts_since = 0; |
| 364 | } |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 365 | // command line is milliseconds; internal units are nano-seconds |
| 366 | ts_since *= 1000*1000; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 367 | } else if (args[i] == onlyOption) { |
| 368 | i++; |
| 369 | if (i < n) { |
| 370 | String8 value(args[i]); |
| 371 | const char *p = value.string(); |
| 372 | char *q = strdup(p); |
| 373 | if (q != NULL) { |
| 374 | if (only != NULL) { |
| 375 | free((void*)only); |
| 376 | } |
| 377 | only = q; |
| 378 | } |
| 379 | } |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 380 | } else if (args[i] == helpOption) { |
| 381 | result.append("Recognized parameters:\n"); |
| 382 | result.append("-help this help message\n"); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 383 | result.append("-summary show summary info\n"); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 384 | result.append("-clear clears out saved records\n"); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 385 | result.append("-only X process records for component X\n"); |
| 386 | result.append("-since X include records since X\n"); |
| 387 | result.append(" (X is milliseconds since the UNIX epoch)\n"); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 388 | write(fd, result.string(), result.size()); |
| 389 | return NO_ERROR; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | Mutex::Autolock _l(mLock); |
| 394 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 395 | // we ALWAYS dump this piece |
| 396 | snprintf(buffer, SIZE, "Dump of the %s process:\n", kServiceName); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 397 | result.append(buffer); |
| 398 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 399 | dumpHeaders(result, ts_since); |
| 400 | |
| 401 | // only want 1, to avoid confusing folks that parse the output |
| 402 | if (summary) { |
| 403 | dumpSummaries(result, ts_since, only); |
| 404 | } else { |
| 405 | dumpRecent(result, ts_since, only); |
| 406 | } |
Yunlian Jiang | 8caa24e | 2017-06-06 16:18:31 -0700 | [diff] [blame^] | 407 | free((void*)only); |
| 408 | only=NULL; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 409 | |
| 410 | |
| 411 | if (clear) { |
| 412 | // remove everything from the finalized queue |
| 413 | while (mFinalized->size() > 0) { |
| 414 | MediaAnalyticsItem * oitem = *(mFinalized->begin()); |
| 415 | mFinalized->erase(mFinalized->begin()); |
| 416 | delete oitem; |
| 417 | mItemsDiscarded++; |
| 418 | } |
| 419 | |
| 420 | // shall we clear the summary data too? |
| 421 | |
| 422 | } |
| 423 | |
| 424 | write(fd, result.string(), result.size()); |
| 425 | return NO_ERROR; |
| 426 | } |
| 427 | |
| 428 | // dump headers |
| 429 | void MediaAnalyticsService::dumpHeaders(String8 &result, nsecs_t ts_since) { |
| 430 | const size_t SIZE = 512; |
| 431 | char buffer[SIZE]; |
| 432 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 433 | int enabled = MediaAnalyticsItem::isEnabled(); |
| 434 | if (enabled) { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 435 | snprintf(buffer, SIZE, "Metrics gathering: enabled\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 436 | } else { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 437 | snprintf(buffer, SIZE, "Metrics gathering: DISABLED via property\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 438 | } |
| 439 | result.append(buffer); |
| 440 | |
| 441 | snprintf(buffer, SIZE, |
| 442 | "Since Boot: Submissions: %" PRId64 |
| 443 | " Finalizations: %" PRId64 |
| 444 | " Discarded: %" PRId64 "\n", |
| 445 | mItemsSubmitted, mItemsFinalized, mItemsDiscarded); |
| 446 | result.append(buffer); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 447 | snprintf(buffer, SIZE, |
| 448 | "Summary Sets Discarded: %" PRId64 "\n", mSetsDiscarded); |
| 449 | result.append(buffer); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 450 | if (ts_since != 0) { |
| 451 | snprintf(buffer, SIZE, |
| 452 | "Dumping Queue entries more recent than: %" PRId64 "\n", |
| 453 | (int64_t) ts_since); |
| 454 | result.append(buffer); |
| 455 | } |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | // dump summary info |
| 459 | void MediaAnalyticsService::dumpSummaries(String8 &result, nsecs_t ts_since, const char *only) { |
| 460 | const size_t SIZE = 512; |
| 461 | char buffer[SIZE]; |
| 462 | int slot = 0; |
| 463 | |
| 464 | snprintf(buffer, SIZE, "\nSummarized Metrics:\n"); |
| 465 | result.append(buffer); |
| 466 | |
| 467 | // have each of the distillers dump records |
| 468 | if (mSummarizerSets != NULL) { |
| 469 | List<SummarizerSet *>::iterator itSet = mSummarizerSets->begin(); |
| 470 | for (; itSet != mSummarizerSets->end(); itSet++) { |
| 471 | nsecs_t when = (*itSet)->getStarted(); |
| 472 | if (when < ts_since) { |
| 473 | continue; |
| 474 | } |
| 475 | List<MetricsSummarizer *> *list = (*itSet)->getSummarizers(); |
| 476 | List<MetricsSummarizer *>::iterator it = list->begin(); |
| 477 | for (; it != list->end(); it++) { |
| 478 | if (only != NULL && strcmp(only, (*it)->getKey()) != 0) { |
| 479 | ALOGV("Told to omit '%s'", (*it)->getKey()); |
| 480 | } |
| 481 | AString distilled = (*it)->dumpSummary(slot, only); |
| 482 | result.append(distilled.c_str()); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // the recent, detailed queues |
| 489 | void MediaAnalyticsService::dumpRecent(String8 &result, nsecs_t ts_since, const char * only) { |
| 490 | const size_t SIZE = 512; |
| 491 | char buffer[SIZE]; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 492 | |
| 493 | // show the recently recorded records |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 494 | snprintf(buffer, sizeof(buffer), "\nFinalized Metrics (oldest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 495 | result.append(buffer); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 496 | result.append(this->dumpQueue(mFinalized, ts_since, only)); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 497 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 498 | snprintf(buffer, sizeof(buffer), "\nIn-Progress Metrics (newest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 499 | result.append(buffer); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 500 | result.append(this->dumpQueue(mOpen, ts_since, only)); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 501 | |
| 502 | // show who is connected and injecting records? |
| 503 | // talk about # records fed to the 'readers' |
| 504 | // talk about # records we discarded, perhaps "discarded w/o reading" too |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 505 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 506 | // caller has locked mLock... |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 507 | String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList) { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 508 | return dumpQueue(theList, (nsecs_t) 0, NULL); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 509 | } |
| 510 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 511 | String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList, nsecs_t ts_since, const char * only) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 512 | String8 result; |
| 513 | int slot = 0; |
| 514 | |
| 515 | if (theList->empty()) { |
| 516 | result.append("empty\n"); |
| 517 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 518 | List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
| 519 | for (; it != theList->end(); it++) { |
| 520 | nsecs_t when = (*it)->getTimestamp(); |
| 521 | if (when < ts_since) { |
| 522 | continue; |
| 523 | } |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 524 | if (only != NULL && |
| 525 | strcmp(only, (*it)->getKey().c_str()) != 0) { |
| 526 | ALOGV("Omit '%s', it's not '%s'", (*it)->getKey().c_str(), only); |
| 527 | continue; |
| 528 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 529 | AString entry = (*it)->toString(); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 530 | result.appendFormat("%5d: %s\n", slot, entry.c_str()); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 531 | slot++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | return result; |
| 536 | } |
| 537 | |
| 538 | // |
| 539 | // Our Cheap in-core, non-persistent records management. |
| 540 | // XXX: rewrite this to manage persistence, etc. |
| 541 | |
| 542 | // insert appropriately into queue |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 543 | void MediaAnalyticsService::saveItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem * item, int front) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 544 | |
| 545 | Mutex::Autolock _l(mLock); |
| 546 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 547 | // adding at back of queue (fifo order) |
| 548 | if (front) { |
| 549 | l->push_front(item); |
| 550 | } else { |
| 551 | l->push_back(item); |
| 552 | } |
| 553 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 554 | // keep removing old records the front until we're in-bounds |
| 555 | if (mMaxRecords > 0) { |
| 556 | while (l->size() > (size_t) mMaxRecords) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 557 | MediaAnalyticsItem * oitem = *(l->begin()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 558 | l->erase(l->begin()); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 559 | delete oitem; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 560 | mItemsDiscarded++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // are they alike enough that nitem can be folded into oitem? |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 566 | static bool compatibleItems(MediaAnalyticsItem * oitem, MediaAnalyticsItem * nitem) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 567 | |
| 568 | if (0) { |
| 569 | ALOGD("Compare: o %s n %s", |
| 570 | oitem->toString().c_str(), nitem->toString().c_str()); |
| 571 | } |
| 572 | |
| 573 | // general safety |
| 574 | if (nitem->getUid() != oitem->getUid()) { |
| 575 | return false; |
| 576 | } |
| 577 | if (nitem->getPid() != oitem->getPid()) { |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | // key -- needs to match |
| 582 | if (nitem->getKey() == oitem->getKey()) { |
| 583 | // still in the game. |
| 584 | } else { |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | // session id -- empty field in new is allowed |
| 589 | MediaAnalyticsItem::SessionID_t osession = oitem->getSessionID(); |
| 590 | MediaAnalyticsItem::SessionID_t nsession = nitem->getSessionID(); |
| 591 | if (nsession != osession) { |
| 592 | // incoming '0' matches value in osession |
| 593 | if (nsession != 0) { |
| 594 | return false; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | // find the incomplete record that this will overlay |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 602 | MediaAnalyticsItem *MediaAnalyticsService::findItem(List<MediaAnalyticsItem*> *theList, MediaAnalyticsItem *nitem, bool removeit) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 603 | if (nitem == NULL) { |
| 604 | return NULL; |
| 605 | } |
| 606 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 607 | MediaAnalyticsItem *item = NULL; |
| 608 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 609 | Mutex::Autolock _l(mLock); |
| 610 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 611 | for (List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 612 | it != theList->end(); it++) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 613 | MediaAnalyticsItem *tmp = (*it); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 614 | |
| 615 | if (!compatibleItems(tmp, nitem)) { |
| 616 | continue; |
| 617 | } |
| 618 | |
| 619 | // we match! this is the one I want. |
| 620 | if (removeit) { |
| 621 | theList->erase(it); |
| 622 | } |
| 623 | item = tmp; |
| 624 | break; |
| 625 | } |
| 626 | return item; |
| 627 | } |
| 628 | |
| 629 | |
| 630 | // delete the indicated record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 631 | void MediaAnalyticsService::deleteItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem *item) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 632 | |
| 633 | Mutex::Autolock _l(mLock); |
| 634 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 635 | for (List<MediaAnalyticsItem *>::iterator it = l->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 636 | it != l->end(); it++) { |
| 637 | if ((*it)->getSessionID() != item->getSessionID()) |
| 638 | continue; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 639 | delete *it; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 640 | l->erase(it); |
| 641 | break; |
| 642 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 645 | static AString allowedKeys[] = |
| 646 | { |
| 647 | "codec", |
| 648 | "extractor" |
| 649 | }; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 650 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 651 | static const int nAllowedKeys = sizeof(allowedKeys) / sizeof(allowedKeys[0]); |
| 652 | |
| 653 | // are the contents good |
| 654 | bool MediaAnalyticsService::contentValid(MediaAnalyticsItem *item, bool isTrusted) { |
| 655 | |
| 656 | // untrusted uids can only send us a limited set of keys |
| 657 | if (isTrusted == false) { |
| 658 | // restrict to a specific set of keys |
| 659 | AString key = item->getKey(); |
| 660 | |
| 661 | size_t i; |
| 662 | for(i = 0; i < nAllowedKeys; i++) { |
| 663 | if (key == allowedKeys[i]) { |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | if (i == nAllowedKeys) { |
| 668 | ALOGD("Ignoring (key): %s", item->toString().c_str()); |
| 669 | return false; |
| 670 | } |
| 671 | } |
| 672 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 673 | // internal consistency |
| 674 | |
| 675 | return true; |
| 676 | } |
| 677 | |
| 678 | // are we rate limited, normally false |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 679 | bool MediaAnalyticsService::rateLimited(MediaAnalyticsItem *) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 680 | |
| 681 | return false; |
| 682 | } |
| 683 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 684 | // insert into the appropriate summarizer. |
| 685 | // we make our own copy to save/summarize |
| 686 | void MediaAnalyticsService::summarize(MediaAnalyticsItem *item) { |
| 687 | |
| 688 | ALOGV("MediaAnalyticsService::summarize()"); |
| 689 | |
| 690 | if (item == NULL) { |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 695 | if (mCurrentSet == NULL |
| 696 | || (mCurrentSet->getStarted() + mNewSetInterval < now)) { |
| 697 | newSummarizerSet(); |
| 698 | } |
| 699 | |
| 700 | if (mCurrentSet == NULL) { |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | List<MetricsSummarizer *> *summarizers = mCurrentSet->getSummarizers(); |
| 705 | List<MetricsSummarizer *>::iterator it = summarizers->begin(); |
| 706 | for (; it != summarizers->end(); it++) { |
| 707 | if ((*it)->isMine(*item)) { |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | if (it == summarizers->end()) { |
| 712 | ALOGD("no handler for type %s", item->getKey().c_str()); |
| 713 | return; // no handler |
| 714 | } |
| 715 | |
| 716 | // invoke the summarizer. summarizer will make whatever copies |
| 717 | // it wants; the caller retains ownership of item. |
| 718 | |
| 719 | (*it)->handleRecord(item); |
| 720 | |
| 721 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 722 | |
| 723 | } // namespace android |