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> |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 32 | #include <pwd.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 33 | |
| 34 | #include <cutils/atomic.h> |
| 35 | #include <cutils/properties.h> // for property_get |
| 36 | |
| 37 | #include <utils/misc.h> |
| 38 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 39 | #include <android/content/pm/IPackageManagerNative.h> |
| 40 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 41 | #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 Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 77 | #include "MetricsSummarizer.h" |
| 78 | #include "MetricsSummarizerCodec.h" |
| 79 | #include "MetricsSummarizerExtractor.h" |
| 80 | #include "MetricsSummarizerPlayer.h" |
| 81 | #include "MetricsSummarizerRecorder.h" |
| 82 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 83 | |
| 84 | namespace android { |
| 85 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 86 | using namespace android::base; |
| 87 | using namespace android::content::pm; |
| 88 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 89 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 90 | |
| 91 | // summarized records |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 92 | // up to 36 sets, each covering an hour -- so at least 1.5 days |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 93 | // (will be longer if there are hours without any media action) |
| 94 | static const nsecs_t kNewSetIntervalNs = 3600*(1000*1000*1000ll); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 95 | static const int kMaxRecordSets = 36; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 96 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 97 | // 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) |
| 101 | static const nsecs_t kMaxRecordAgeNs = 36 * 3600 * (1000*1000*1000ll); |
| 102 | static const int kMaxRecords = 0; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 103 | |
| 104 | static const char *kServiceName = "media.metrics"; |
| 105 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 106 | void MediaAnalyticsService::instantiate() { |
| 107 | defaultServiceManager()->addService( |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 108 | String16(kServiceName), new MediaAnalyticsService()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 111 | // handle sets of summarizers |
| 112 | MediaAnalyticsService::SummarizerSet::SummarizerSet() { |
| 113 | mSummarizers = new List<MetricsSummarizer *>(); |
| 114 | } |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 115 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 116 | MediaAnalyticsService::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 | |
| 126 | void 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 Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 157 | MediaAnalyticsService::MediaAnalyticsService() |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 158 | : mMaxRecords(kMaxRecords), |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 159 | mMaxRecordAgeNs(kMaxRecordAgeNs), |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 160 | mMaxRecordSets(kMaxRecordSets), |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 161 | mNewSetInterval(kNewSetIntervalNs), |
| 162 | mDumpProto(MediaAnalyticsItem::PROTO_V0) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 163 | |
| 164 | ALOGD("MediaAnalyticsService created"); |
| 165 | // clear our queues |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 166 | mOpen = new List<MediaAnalyticsItem *>(); |
| 167 | mFinalized = new List<MediaAnalyticsItem *>(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 168 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 169 | mSummarizerSets = new List<MediaAnalyticsService::SummarizerSet *>(); |
| 170 | newSummarizerSet(); |
| 171 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 172 | mItemsSubmitted = 0; |
| 173 | mItemsFinalized = 0; |
| 174 | mItemsDiscarded = 0; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 175 | mItemsDiscardedExpire = 0; |
| 176 | mItemsDiscardedCount = 0; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 177 | |
| 178 | mLastSessionID = 0; |
| 179 | // recover any persistency we set up |
| 180 | // etc |
| 181 | } |
| 182 | |
| 183 | MediaAnalyticsService::~MediaAnalyticsService() { |
| 184 | ALOGD("MediaAnalyticsService destroyed"); |
| 185 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 186 | // clean out mOpen and mFinalized |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 187 | while (mOpen->size() > 0) { |
| 188 | MediaAnalyticsItem * oitem = *(mOpen->begin()); |
| 189 | mOpen->erase(mOpen->begin()); |
| 190 | delete oitem; |
| 191 | mItemsDiscarded++; |
| 192 | mItemsDiscardedCount++; |
| 193 | } |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 194 | delete mOpen; |
| 195 | mOpen = NULL; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 196 | |
| 197 | while (mFinalized->size() > 0) { |
| 198 | MediaAnalyticsItem * oitem = *(mFinalized->begin()); |
| 199 | mFinalized->erase(mFinalized->begin()); |
| 200 | delete oitem; |
| 201 | mItemsDiscarded++; |
| 202 | mItemsDiscardedCount++; |
| 203 | } |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 204 | delete mFinalized; |
| 205 | mFinalized = NULL; |
| 206 | |
| 207 | // XXX: clean out the summaries |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | |
| 211 | MediaAnalyticsItem::SessionID_t MediaAnalyticsService::generateUniqueSessionID() { |
| 212 | // generate a new sessionid |
| 213 | |
| 214 | Mutex::Autolock _l(mLock_ids); |
| 215 | return (++mLastSessionID); |
| 216 | } |
| 217 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 218 | // caller surrenders ownership of 'item' |
| 219 | MediaAnalyticsItem::SessionID_t MediaAnalyticsService::submit(MediaAnalyticsItem *item, bool forcenew) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 220 | |
| 221 | MediaAnalyticsItem::SessionID_t id = MediaAnalyticsItem::SessionIDInvalid; |
| 222 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 223 | // we control these, generally not trusting user input |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 224 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 225 | item->setTimestamp(now); |
| 226 | int pid = IPCThreadState::self()->getCallingPid(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 227 | int uid = IPCThreadState::self()->getCallingUid(); |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 228 | |
| 229 | int uid_given = item->getUid(); |
| 230 | int pid_given = item->getPid(); |
| 231 | |
| 232 | // although we do make exceptions for particular client uids |
| 233 | // that we know we trust. |
| 234 | // |
| 235 | bool isTrusted = false; |
| 236 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 237 | ALOGV("caller has uid=%d, embedded uid=%d", uid, uid_given); |
| 238 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 239 | switch (uid) { |
| 240 | case AID_MEDIA: |
| 241 | case AID_MEDIA_CODEC: |
| 242 | case AID_MEDIA_EX: |
| 243 | case AID_MEDIA_DRM: |
| 244 | // trusted source, only override default values |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 245 | isTrusted = true; |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 246 | if (uid_given == (-1)) { |
| 247 | item->setUid(uid); |
| 248 | } |
| 249 | if (pid_given == (-1)) { |
| 250 | item->setPid(pid); |
| 251 | } |
| 252 | break; |
| 253 | default: |
| 254 | isTrusted = false; |
| 255 | item->setPid(pid); |
| 256 | item->setUid(uid); |
| 257 | break; |
| 258 | } |
| 259 | |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 260 | |
Adam Stone | 21c7212 | 2017-09-05 19:02:06 -0700 | [diff] [blame] | 261 | // Overwrite package name and version if the caller was untrusted. |
| 262 | if (!isTrusted) { |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 263 | setPkgInfo(item, item->getUid(), true, true); |
Adam Stone | 21c7212 | 2017-09-05 19:02:06 -0700 | [diff] [blame] | 264 | } else if (item->getPkgName().empty()) { |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 265 | // empty, so fill out both parts |
| 266 | setPkgInfo(item, item->getUid(), true, true); |
| 267 | } else { |
| 268 | // trusted, provided a package, do nothing |
Adam Stone | 21c7212 | 2017-09-05 19:02:06 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | ALOGV("given uid %d; sanitized uid: %d sanitized pkg: %s " |
| 272 | "sanitized pkg version: %d", |
| 273 | uid_given, item->getUid(), |
| 274 | item->getPkgName().c_str(), |
| 275 | item->getPkgVersionCode()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 276 | |
| 277 | mItemsSubmitted++; |
| 278 | |
| 279 | // validate the record; we discard if we don't like it |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 280 | if (contentValid(item, isTrusted) == false) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 281 | delete item; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 282 | return MediaAnalyticsItem::SessionIDInvalid; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | // if we have a sesisonid in the new record, look to make |
| 287 | // sure it doesn't appear in the finalized list. |
| 288 | // XXX: this is for security / DOS prevention. |
| 289 | // may also require that we persist the unique sessionIDs |
| 290 | // across boots [instead of within a single boot] |
| 291 | |
| 292 | |
| 293 | // match this new record up against records in the open |
| 294 | // list... |
| 295 | // if there's a match, merge them together |
| 296 | // deal with moving the old / merged record into the finalized que |
| 297 | |
| 298 | bool finalizing = item->getFinalized(); |
| 299 | |
| 300 | // if finalizing, we'll remove it |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 301 | MediaAnalyticsItem *oitem = findItem(mOpen, item, finalizing | forcenew); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 302 | if (oitem != NULL) { |
| 303 | if (forcenew) { |
| 304 | // old one gets finalized, then we insert the new one |
| 305 | // so we'll have 2 records at the end of this. |
| 306 | // but don't finalize an empty record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 307 | if (oitem->count() == 0) { |
| 308 | // we're responsible for disposing of the dead record |
| 309 | delete oitem; |
| 310 | oitem = NULL; |
| 311 | } else { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 312 | oitem->setFinalized(true); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 313 | summarize(oitem); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 314 | saveItem(mFinalized, oitem, 0); |
| 315 | } |
| 316 | // new record could itself be marked finalized... |
| 317 | if (finalizing) { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 318 | summarize(item); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 319 | saveItem(mFinalized, item, 0); |
| 320 | mItemsFinalized++; |
| 321 | } else { |
| 322 | saveItem(mOpen, item, 1); |
| 323 | } |
| 324 | id = item->getSessionID(); |
| 325 | } else { |
| 326 | // combine the records, send it to finalized if appropriate |
| 327 | oitem->merge(item); |
| 328 | if (finalizing) { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 329 | summarize(oitem); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 330 | saveItem(mFinalized, oitem, 0); |
| 331 | mItemsFinalized++; |
| 332 | } |
| 333 | id = oitem->getSessionID(); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 334 | |
| 335 | // we're responsible for disposing of the dead record |
| 336 | delete item; |
| 337 | item = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 338 | } |
| 339 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 340 | // nothing to merge, save the new record |
| 341 | id = item->getSessionID(); |
| 342 | if (finalizing) { |
| 343 | if (item->count() == 0) { |
| 344 | // drop empty records |
| 345 | delete item; |
| 346 | item = NULL; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 347 | } else { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 348 | summarize(item); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 349 | saveItem(mFinalized, item, 0); |
| 350 | mItemsFinalized++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 351 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 352 | } else { |
| 353 | saveItem(mOpen, item, 1); |
| 354 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 355 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 356 | return id; |
| 357 | } |
| 358 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 359 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 360 | status_t MediaAnalyticsService::dump(int fd, const Vector<String16>& args) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 361 | { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 362 | const size_t SIZE = 512; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 363 | char buffer[SIZE]; |
| 364 | String8 result; |
| 365 | |
| 366 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 367 | snprintf(buffer, SIZE, "Permission Denial: " |
| 368 | "can't dump MediaAnalyticsService from pid=%d, uid=%d\n", |
| 369 | IPCThreadState::self()->getCallingPid(), |
| 370 | IPCThreadState::self()->getCallingUid()); |
| 371 | result.append(buffer); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 372 | write(fd, result.string(), result.size()); |
| 373 | return NO_ERROR; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 374 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 375 | |
| 376 | // crack any parameters |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 377 | String16 summaryOption("-summary"); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 378 | bool summary = false; |
| 379 | String16 protoOption("-proto"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 380 | String16 clearOption("-clear"); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 381 | bool clear = false; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 382 | String16 sinceOption("-since"); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 383 | nsecs_t ts_since = 0; |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 384 | String16 helpOption("-help"); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 385 | String16 onlyOption("-only"); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 386 | AString only; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 387 | int n = args.size(); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 388 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 389 | for (int i = 0; i < n; i++) { |
| 390 | String8 myarg(args[i]); |
| 391 | if (args[i] == clearOption) { |
| 392 | clear = true; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 393 | } else if (args[i] == summaryOption) { |
| 394 | summary = true; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 395 | } else if (args[i] == protoOption) { |
| 396 | i++; |
| 397 | if (i < n) { |
| 398 | String8 value(args[i]); |
| 399 | int proto = MediaAnalyticsItem::PROTO_V0; // default to original |
| 400 | char *endp; |
| 401 | const char *p = value.string(); |
| 402 | proto = strtol(p, &endp, 10); |
| 403 | if (endp != p || *endp == '\0') { |
| 404 | if (proto < MediaAnalyticsItem::PROTO_FIRST) { |
| 405 | proto = MediaAnalyticsItem::PROTO_FIRST; |
| 406 | } else if (proto > MediaAnalyticsItem::PROTO_LAST) { |
| 407 | proto = MediaAnalyticsItem::PROTO_LAST; |
| 408 | } |
| 409 | mDumpProto = proto; |
| 410 | } |
| 411 | } |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 412 | } else if (args[i] == sinceOption) { |
| 413 | i++; |
| 414 | if (i < n) { |
| 415 | String8 value(args[i]); |
| 416 | char *endp; |
| 417 | const char *p = value.string(); |
| 418 | ts_since = strtoll(p, &endp, 10); |
| 419 | if (endp == p || *endp != '\0') { |
| 420 | ts_since = 0; |
| 421 | } |
| 422 | } else { |
| 423 | ts_since = 0; |
| 424 | } |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 425 | // command line is milliseconds; internal units are nano-seconds |
| 426 | ts_since *= 1000*1000; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 427 | } else if (args[i] == onlyOption) { |
| 428 | i++; |
| 429 | if (i < n) { |
| 430 | String8 value(args[i]); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 431 | only = value.string(); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 432 | } |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 433 | } else if (args[i] == helpOption) { |
| 434 | result.append("Recognized parameters:\n"); |
| 435 | result.append("-help this help message\n"); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 436 | result.append("-proto X dump using protocol X (defaults to 1)"); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 437 | result.append("-summary show summary info\n"); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 438 | result.append("-clear clears out saved records\n"); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 439 | result.append("-only X process records for component X\n"); |
| 440 | result.append("-since X include records since X\n"); |
| 441 | result.append(" (X is milliseconds since the UNIX epoch)\n"); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 442 | write(fd, result.string(), result.size()); |
| 443 | return NO_ERROR; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | Mutex::Autolock _l(mLock); |
| 448 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 449 | // we ALWAYS dump this piece |
| 450 | snprintf(buffer, SIZE, "Dump of the %s process:\n", kServiceName); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 451 | result.append(buffer); |
| 452 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 453 | dumpHeaders(result, ts_since); |
| 454 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 455 | // want exactly 1, to avoid confusing folks that parse the output |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 456 | if (summary) { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 457 | dumpSummaries(result, ts_since, only.c_str()); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 458 | } else { |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 459 | dumpRecent(result, ts_since, only.c_str()); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | |
| 463 | if (clear) { |
| 464 | // remove everything from the finalized queue |
| 465 | while (mFinalized->size() > 0) { |
| 466 | MediaAnalyticsItem * oitem = *(mFinalized->begin()); |
| 467 | mFinalized->erase(mFinalized->begin()); |
| 468 | delete oitem; |
| 469 | mItemsDiscarded++; |
| 470 | } |
| 471 | |
| 472 | // shall we clear the summary data too? |
| 473 | |
| 474 | } |
| 475 | |
| 476 | write(fd, result.string(), result.size()); |
| 477 | return NO_ERROR; |
| 478 | } |
| 479 | |
| 480 | // dump headers |
| 481 | void MediaAnalyticsService::dumpHeaders(String8 &result, nsecs_t ts_since) { |
| 482 | const size_t SIZE = 512; |
| 483 | char buffer[SIZE]; |
| 484 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 485 | snprintf(buffer, SIZE, "Protocol Version: %d\n", mDumpProto); |
| 486 | result.append(buffer); |
| 487 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 488 | int enabled = MediaAnalyticsItem::isEnabled(); |
| 489 | if (enabled) { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 490 | snprintf(buffer, SIZE, "Metrics gathering: enabled\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 491 | } else { |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 492 | snprintf(buffer, SIZE, "Metrics gathering: DISABLED via property\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 493 | } |
| 494 | result.append(buffer); |
| 495 | |
| 496 | snprintf(buffer, SIZE, |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 497 | "Since Boot: Submissions: %8" PRId64 |
| 498 | " Finalizations: %8" PRId64 "\n", |
| 499 | mItemsSubmitted, mItemsFinalized); |
| 500 | result.append(buffer); |
| 501 | snprintf(buffer, SIZE, |
| 502 | "Records Discarded: %8" PRId64 |
| 503 | " (by Count: %" PRId64 " by Expiration: %" PRId64 ")\n", |
| 504 | mItemsDiscarded, mItemsDiscardedCount, mItemsDiscardedExpire); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 505 | result.append(buffer); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 506 | snprintf(buffer, SIZE, |
| 507 | "Summary Sets Discarded: %" PRId64 "\n", mSetsDiscarded); |
| 508 | result.append(buffer); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 509 | if (ts_since != 0) { |
| 510 | snprintf(buffer, SIZE, |
| 511 | "Dumping Queue entries more recent than: %" PRId64 "\n", |
| 512 | (int64_t) ts_since); |
| 513 | result.append(buffer); |
| 514 | } |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // dump summary info |
| 518 | void MediaAnalyticsService::dumpSummaries(String8 &result, nsecs_t ts_since, const char *only) { |
| 519 | const size_t SIZE = 512; |
| 520 | char buffer[SIZE]; |
| 521 | int slot = 0; |
| 522 | |
| 523 | snprintf(buffer, SIZE, "\nSummarized Metrics:\n"); |
| 524 | result.append(buffer); |
| 525 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 526 | if (only != NULL && *only == '\0') { |
| 527 | only = NULL; |
| 528 | } |
| 529 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 530 | // have each of the distillers dump records |
| 531 | if (mSummarizerSets != NULL) { |
| 532 | List<SummarizerSet *>::iterator itSet = mSummarizerSets->begin(); |
| 533 | for (; itSet != mSummarizerSets->end(); itSet++) { |
| 534 | nsecs_t when = (*itSet)->getStarted(); |
| 535 | if (when < ts_since) { |
| 536 | continue; |
| 537 | } |
| 538 | List<MetricsSummarizer *> *list = (*itSet)->getSummarizers(); |
| 539 | List<MetricsSummarizer *>::iterator it = list->begin(); |
| 540 | for (; it != list->end(); it++) { |
| 541 | if (only != NULL && strcmp(only, (*it)->getKey()) != 0) { |
| 542 | ALOGV("Told to omit '%s'", (*it)->getKey()); |
| 543 | } |
| 544 | AString distilled = (*it)->dumpSummary(slot, only); |
| 545 | result.append(distilled.c_str()); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | // the recent, detailed queues |
| 552 | void MediaAnalyticsService::dumpRecent(String8 &result, nsecs_t ts_since, const char * only) { |
| 553 | const size_t SIZE = 512; |
| 554 | char buffer[SIZE]; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 555 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 556 | if (only != NULL && *only == '\0') { |
| 557 | only = NULL; |
| 558 | } |
| 559 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 560 | // show the recently recorded records |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 561 | snprintf(buffer, sizeof(buffer), "\nFinalized Metrics (oldest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 562 | result.append(buffer); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 563 | result.append(this->dumpQueue(mFinalized, ts_since, only)); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 564 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 565 | snprintf(buffer, sizeof(buffer), "\nIn-Progress Metrics (newest first):\n"); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 566 | result.append(buffer); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 567 | result.append(this->dumpQueue(mOpen, ts_since, only)); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 568 | |
| 569 | // show who is connected and injecting records? |
| 570 | // talk about # records fed to the 'readers' |
| 571 | // talk about # records we discarded, perhaps "discarded w/o reading" too |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 572 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 573 | // caller has locked mLock... |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 574 | String8 MediaAnalyticsService::dumpQueue(List<MediaAnalyticsItem *> *theList) { |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 575 | return dumpQueue(theList, (nsecs_t) 0, NULL); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 578 | 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] | 579 | String8 result; |
| 580 | int slot = 0; |
| 581 | |
| 582 | if (theList->empty()) { |
| 583 | result.append("empty\n"); |
| 584 | } else { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 585 | List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
| 586 | for (; it != theList->end(); it++) { |
| 587 | nsecs_t when = (*it)->getTimestamp(); |
| 588 | if (when < ts_since) { |
| 589 | continue; |
| 590 | } |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 591 | if (only != NULL && |
| 592 | strcmp(only, (*it)->getKey().c_str()) != 0) { |
| 593 | ALOGV("Omit '%s', it's not '%s'", (*it)->getKey().c_str(), only); |
| 594 | continue; |
| 595 | } |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 596 | AString entry = (*it)->toString(mDumpProto); |
Ray Essick | 35ad27f | 2017-01-30 14:04:11 -0800 | [diff] [blame] | 597 | result.appendFormat("%5d: %s\n", slot, entry.c_str()); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 598 | slot++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
| 602 | return result; |
| 603 | } |
| 604 | |
| 605 | // |
| 606 | // Our Cheap in-core, non-persistent records management. |
| 607 | // XXX: rewrite this to manage persistence, etc. |
| 608 | |
| 609 | // insert appropriately into queue |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 610 | void MediaAnalyticsService::saveItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem * item, int front) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 611 | |
| 612 | Mutex::Autolock _l(mLock); |
| 613 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 614 | // adding at back of queue (fifo order) |
| 615 | if (front) { |
| 616 | l->push_front(item); |
| 617 | } else { |
| 618 | l->push_back(item); |
| 619 | } |
| 620 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 621 | // keep removing old records the front until we're in-bounds (count) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 622 | if (mMaxRecords > 0) { |
| 623 | while (l->size() > (size_t) mMaxRecords) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 624 | MediaAnalyticsItem * oitem = *(l->begin()); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 625 | l->erase(l->begin()); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 626 | delete oitem; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 627 | mItemsDiscarded++; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 628 | mItemsDiscardedCount++; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // keep removing old records the front until we're in-bounds (count) |
| 633 | if (mMaxRecordAgeNs > 0) { |
| 634 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 635 | while (l->size() > 0) { |
| 636 | MediaAnalyticsItem * oitem = *(l->begin()); |
| 637 | nsecs_t when = oitem->getTimestamp(); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 638 | // careful about timejumps too |
| 639 | if ((now > when) && (now-when) <= mMaxRecordAgeNs) { |
| 640 | // this (and the rest) are recent enough to keep |
| 641 | break; |
| 642 | } |
| 643 | l->erase(l->begin()); |
| 644 | delete oitem; |
| 645 | mItemsDiscarded++; |
| 646 | mItemsDiscardedExpire++; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | // are they alike enough that nitem can be folded into oitem? |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 652 | static bool compatibleItems(MediaAnalyticsItem * oitem, MediaAnalyticsItem * nitem) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 653 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 654 | // general safety |
| 655 | if (nitem->getUid() != oitem->getUid()) { |
| 656 | return false; |
| 657 | } |
| 658 | if (nitem->getPid() != oitem->getPid()) { |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | // key -- needs to match |
| 663 | if (nitem->getKey() == oitem->getKey()) { |
| 664 | // still in the game. |
| 665 | } else { |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | // session id -- empty field in new is allowed |
| 670 | MediaAnalyticsItem::SessionID_t osession = oitem->getSessionID(); |
| 671 | MediaAnalyticsItem::SessionID_t nsession = nitem->getSessionID(); |
| 672 | if (nsession != osession) { |
| 673 | // incoming '0' matches value in osession |
| 674 | if (nsession != 0) { |
| 675 | return false; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | return true; |
| 680 | } |
| 681 | |
| 682 | // find the incomplete record that this will overlay |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 683 | MediaAnalyticsItem *MediaAnalyticsService::findItem(List<MediaAnalyticsItem*> *theList, MediaAnalyticsItem *nitem, bool removeit) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 684 | if (nitem == NULL) { |
| 685 | return NULL; |
| 686 | } |
| 687 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 688 | MediaAnalyticsItem *item = NULL; |
| 689 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 690 | Mutex::Autolock _l(mLock); |
| 691 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 692 | for (List<MediaAnalyticsItem *>::iterator it = theList->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 693 | it != theList->end(); it++) { |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 694 | MediaAnalyticsItem *tmp = (*it); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 695 | |
| 696 | if (!compatibleItems(tmp, nitem)) { |
| 697 | continue; |
| 698 | } |
| 699 | |
| 700 | // we match! this is the one I want. |
| 701 | if (removeit) { |
| 702 | theList->erase(it); |
| 703 | } |
| 704 | item = tmp; |
| 705 | break; |
| 706 | } |
| 707 | return item; |
| 708 | } |
| 709 | |
| 710 | |
| 711 | // delete the indicated record |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 712 | void MediaAnalyticsService::deleteItem(List<MediaAnalyticsItem *> *l, MediaAnalyticsItem *item) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 713 | |
| 714 | Mutex::Autolock _l(mLock); |
| 715 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 716 | for (List<MediaAnalyticsItem *>::iterator it = l->begin(); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 717 | it != l->end(); it++) { |
| 718 | if ((*it)->getSessionID() != item->getSessionID()) |
| 719 | continue; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 720 | delete *it; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 721 | l->erase(it); |
| 722 | break; |
| 723 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 726 | static AString allowedKeys[] = |
| 727 | { |
| 728 | "codec", |
| 729 | "extractor" |
| 730 | }; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 731 | |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 732 | static const int nAllowedKeys = sizeof(allowedKeys) / sizeof(allowedKeys[0]); |
| 733 | |
| 734 | // are the contents good |
| 735 | bool MediaAnalyticsService::contentValid(MediaAnalyticsItem *item, bool isTrusted) { |
| 736 | |
| 737 | // untrusted uids can only send us a limited set of keys |
| 738 | if (isTrusted == false) { |
| 739 | // restrict to a specific set of keys |
| 740 | AString key = item->getKey(); |
| 741 | |
| 742 | size_t i; |
| 743 | for(i = 0; i < nAllowedKeys; i++) { |
| 744 | if (key == allowedKeys[i]) { |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | if (i == nAllowedKeys) { |
| 749 | ALOGD("Ignoring (key): %s", item->toString().c_str()); |
| 750 | return false; |
| 751 | } |
| 752 | } |
| 753 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 754 | // internal consistency |
| 755 | |
| 756 | return true; |
| 757 | } |
| 758 | |
| 759 | // are we rate limited, normally false |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 760 | bool MediaAnalyticsService::rateLimited(MediaAnalyticsItem *) { |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 761 | |
| 762 | return false; |
| 763 | } |
| 764 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 765 | // insert into the appropriate summarizer. |
| 766 | // we make our own copy to save/summarize |
| 767 | void MediaAnalyticsService::summarize(MediaAnalyticsItem *item) { |
| 768 | |
| 769 | ALOGV("MediaAnalyticsService::summarize()"); |
| 770 | |
| 771 | if (item == NULL) { |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 776 | if (mCurrentSet == NULL |
| 777 | || (mCurrentSet->getStarted() + mNewSetInterval < now)) { |
| 778 | newSummarizerSet(); |
| 779 | } |
| 780 | |
| 781 | if (mCurrentSet == NULL) { |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | List<MetricsSummarizer *> *summarizers = mCurrentSet->getSummarizers(); |
| 786 | List<MetricsSummarizer *>::iterator it = summarizers->begin(); |
| 787 | for (; it != summarizers->end(); it++) { |
| 788 | if ((*it)->isMine(*item)) { |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | if (it == summarizers->end()) { |
| 793 | ALOGD("no handler for type %s", item->getKey().c_str()); |
| 794 | return; // no handler |
| 795 | } |
| 796 | |
| 797 | // invoke the summarizer. summarizer will make whatever copies |
| 798 | // it wants; the caller retains ownership of item. |
| 799 | |
| 800 | (*it)->handleRecord(item); |
| 801 | |
| 802 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 803 | |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 804 | // how long we hold package info before we re-fetch it |
| 805 | #define PKG_EXPIRATION_NS (30*60*1000000000ll) // 30 minutes, in nsecs |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 806 | |
| 807 | // give me the package name, perhaps going to find it |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 808 | void MediaAnalyticsService::setPkgInfo(MediaAnalyticsItem *item, uid_t uid, bool setName, bool setVersion) { |
| 809 | ALOGV("asking for packagename to go with uid=%d", uid); |
| 810 | |
| 811 | if (!setName && !setVersion) { |
| 812 | // setting nothing? strange |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | nsecs_t now = systemTime(SYSTEM_TIME_REALTIME); |
| 817 | struct UidToPkgMap mapping; |
| 818 | mapping.uid = (-1); |
| 819 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 820 | ssize_t i = mPkgMappings.indexOfKey(uid); |
| 821 | if (i >= 0) { |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 822 | mapping = mPkgMappings.valueAt(i); |
| 823 | ALOGV("Expiration? uid %d expiration %" PRId64 " now %" PRId64, |
| 824 | uid, mapping.expiration, now); |
| 825 | if (mapping.expiration < now) { |
| 826 | // purge our current entry and re-query |
| 827 | ALOGV("entry for uid %d expired, now= %" PRId64 "", uid, now); |
| 828 | mPkgMappings.removeItemsAt(i, 1); |
| 829 | // could cheat and use a goto back to the top of the routine. |
| 830 | // a good compiler should recognize the local tail recursion... |
| 831 | return setPkgInfo(item, uid, setName, setVersion); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 832 | } |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 833 | } else { |
| 834 | AString pkg; |
| 835 | std::string installer = ""; |
| 836 | int32_t versionCode = 0; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 837 | |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 838 | struct passwd *pw = getpwuid(uid); |
| 839 | if (pw) { |
| 840 | pkg = pw->pw_name; |
| 841 | } |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 842 | |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 843 | // find the proper value -- should we cache this binder?? |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 844 | |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 845 | sp<IBinder> binder = NULL; |
| 846 | sp<IServiceManager> sm = defaultServiceManager(); |
| 847 | if (sm == NULL) { |
| 848 | ALOGE("defaultServiceManager failed"); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 849 | } else { |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 850 | binder = sm->getService(String16("package_native")); |
| 851 | if (binder == NULL) { |
| 852 | ALOGE("getService package_native failed"); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | if (binder != NULL) { |
| 857 | sp<IPackageManagerNative> package_mgr = interface_cast<IPackageManagerNative>(binder); |
| 858 | binder::Status status; |
| 859 | |
| 860 | std::vector<int> uids; |
| 861 | std::vector<std::string> names; |
| 862 | |
| 863 | uids.push_back(uid); |
| 864 | |
| 865 | status = package_mgr->getNamesForUids(uids, &names); |
| 866 | if (!status.isOk()) { |
| 867 | ALOGE("package_native::getNamesForUids failed: %s", |
| 868 | status.exceptionMessage().c_str()); |
| 869 | } else { |
| 870 | if (!names[0].empty()) { |
| 871 | pkg = names[0].c_str(); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // strip any leading "shared:" strings that came back |
| 876 | if (pkg.startsWith("shared:")) { |
| 877 | pkg.erase(0, 7); |
| 878 | } |
| 879 | |
| 880 | // determine how pkg was installed and the versionCode |
| 881 | // |
| 882 | if (pkg.empty()) { |
| 883 | // no name for us to manage |
| 884 | } else if (strchr(pkg.c_str(), '.') == NULL) { |
| 885 | // not of form 'com.whatever...'; assume internal and ok |
| 886 | } else if (strncmp(pkg.c_str(), "android.", 8) == 0) { |
| 887 | // android.* packages are assumed fine |
| 888 | } else { |
| 889 | String16 pkgName16(pkg.c_str()); |
| 890 | status = package_mgr->getInstallerForPackage(pkgName16, &installer); |
| 891 | if (!status.isOk()) { |
| 892 | ALOGE("package_native::getInstallerForPackage failed: %s", |
| 893 | status.exceptionMessage().c_str()); |
| 894 | } |
| 895 | |
| 896 | // skip if we didn't get an installer |
| 897 | if (status.isOk()) { |
| 898 | status = package_mgr->getVersionCodeForPackage(pkgName16, &versionCode); |
| 899 | if (!status.isOk()) { |
| 900 | ALOGE("package_native::getVersionCodeForPackage failed: %s", |
| 901 | status.exceptionMessage().c_str()); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | |
| 906 | ALOGV("package '%s' installed by '%s' versioncode %d / %08x", |
| 907 | pkg.c_str(), installer.c_str(), versionCode, versionCode); |
| 908 | |
| 909 | if (strncmp(installer.c_str(), "com.android.", 12) == 0) { |
| 910 | // from play store, we keep info |
| 911 | } else if (strncmp(installer.c_str(), "com.google.", 11) == 0) { |
| 912 | // some google source, we keep info |
| 913 | } else if (strcmp(installer.c_str(), "preload") == 0) { |
| 914 | // preloads, we keep the info |
| 915 | } else if (installer.c_str()[0] == '\0') { |
| 916 | // sideload (no installer); do not report |
| 917 | pkg = ""; |
| 918 | versionCode = 0; |
| 919 | } else { |
| 920 | // unknown installer; do not report |
| 921 | pkg = ""; |
| 922 | versionCode = 0; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | // add it to the map, to save a subsequent lookup |
| 928 | if (!pkg.empty()) { |
| 929 | Mutex::Autolock _l(mLock_mappings); |
| 930 | ALOGV("Adding uid %d pkg '%s'", uid, pkg.c_str()); |
| 931 | ssize_t i = mPkgMappings.indexOfKey(uid); |
| 932 | if (i < 0) { |
| 933 | mapping.uid = uid; |
| 934 | mapping.pkg = pkg; |
| 935 | mapping.installer = installer.c_str(); |
| 936 | mapping.versionCode = versionCode; |
| 937 | mapping.expiration = now + PKG_EXPIRATION_NS; |
| 938 | ALOGV("expiration for uid %d set to %" PRId64 "", uid, mapping.expiration); |
| 939 | |
| 940 | mPkgMappings.add(uid, mapping); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame^] | 945 | if (mapping.uid != (uid_t)(-1)) { |
| 946 | if (setName) { |
| 947 | item->setPkgName(mapping.pkg); |
| 948 | } |
| 949 | if (setVersion) { |
| 950 | item->setPkgVersionCode(mapping.versionCode); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 951 | } |
| 952 | } |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 955 | } // namespace android |