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 | |
| 18 | #ifndef ANDROID_MEDIAANALYTICSSERVICE_H |
| 19 | #define ANDROID_MEDIAANALYTICSSERVICE_H |
| 20 | |
| 21 | #include <arpa/inet.h> |
| 22 | |
| 23 | #include <utils/threads.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/KeyedVector.h> |
| 26 | #include <utils/String8.h> |
| 27 | #include <utils/List.h> |
| 28 | |
| 29 | #include <media/IMediaAnalyticsService.h> |
| 30 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 31 | #include "MetricsSummarizer.h" |
| 32 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | class MediaAnalyticsService : public BnMediaAnalyticsService |
| 37 | { |
| 38 | |
| 39 | public: |
| 40 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 41 | // on this side, caller surrenders ownership |
| 42 | virtual int64_t submit(MediaAnalyticsItem *item, bool forcenew); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 43 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 44 | static void instantiate(); |
| 45 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 46 | |
| 47 | MediaAnalyticsService(); |
| 48 | virtual ~MediaAnalyticsService(); |
| 49 | |
| 50 | private: |
| 51 | MediaAnalyticsItem::SessionID_t generateUniqueSessionID(); |
| 52 | |
| 53 | // statistics about our analytics |
| 54 | int64_t mItemsSubmitted; |
| 55 | int64_t mItemsFinalized; |
| 56 | int64_t mItemsDiscarded; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame^] | 57 | int64_t mItemsDiscardedExpire; |
| 58 | int64_t mItemsDiscardedCount; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 59 | int64_t mSetsDiscarded; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 60 | MediaAnalyticsItem::SessionID_t mLastSessionID; |
| 61 | |
| 62 | // partitioned a bit so we don't over serialize |
| 63 | mutable Mutex mLock; |
| 64 | mutable Mutex mLock_ids; |
| 65 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame^] | 66 | // limit how many records we'll retain |
| 67 | // by count (in each queue (open, finalized)) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 68 | int32_t mMaxRecords; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame^] | 69 | // by time (none older than this long agan |
| 70 | nsecs_t mMaxRecordAgeNs; |
| 71 | // |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 72 | // # of sets of summaries |
| 73 | int32_t mMaxRecordSets; |
| 74 | // nsecs until we start a new record set |
| 75 | nsecs_t mNewSetInterval; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 76 | |
| 77 | // input validation after arrival from client |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 78 | bool contentValid(MediaAnalyticsItem *item, bool isTrusted); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 79 | bool rateLimited(MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 80 | |
| 81 | // the ones that are still open |
| 82 | // (newest at front) since we keep looking for them |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 83 | List<MediaAnalyticsItem *> *mOpen; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 84 | // the ones we've finalized |
| 85 | // (oldest at front) so it prints nicely for dumpsys |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 86 | List<MediaAnalyticsItem *> *mFinalized; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 87 | // searching within these queues: queue, key |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 88 | MediaAnalyticsItem *findItem(List<MediaAnalyticsItem *> *, |
| 89 | MediaAnalyticsItem *, bool removeit); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 90 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 91 | // summarizers |
| 92 | void summarize(MediaAnalyticsItem *item); |
| 93 | class SummarizerSet { |
| 94 | nsecs_t mStarted; |
| 95 | List<MetricsSummarizer *> *mSummarizers; |
| 96 | |
| 97 | public: |
| 98 | void appendSummarizer(MetricsSummarizer *s) { |
| 99 | if (s) { |
| 100 | mSummarizers->push_back(s); |
| 101 | } |
| 102 | }; |
| 103 | nsecs_t getStarted() { return mStarted;} |
| 104 | void setStarted(nsecs_t started) {mStarted = started;} |
| 105 | List<MetricsSummarizer *> *getSummarizers() { return mSummarizers;} |
| 106 | |
| 107 | SummarizerSet(); |
| 108 | ~SummarizerSet(); |
| 109 | }; |
| 110 | void newSummarizerSet(); |
| 111 | List<SummarizerSet *> *mSummarizerSets; |
| 112 | SummarizerSet *mCurrentSet; |
| 113 | List<MetricsSummarizer *> *getFirstSet() { |
| 114 | List<SummarizerSet *>::iterator first = mSummarizerSets->begin(); |
| 115 | if (first != mSummarizerSets->end()) { |
| 116 | return (*first)->getSummarizers(); |
| 117 | } |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 121 | void saveItem(MediaAnalyticsItem); |
| 122 | void saveItem(List<MediaAnalyticsItem *> *, MediaAnalyticsItem *, int); |
| 123 | void deleteItem(List<MediaAnalyticsItem *> *, MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 124 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 125 | // support for generating output |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame^] | 126 | int mDumpProto; |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 127 | String8 dumpQueue(List<MediaAnalyticsItem*> *); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 128 | String8 dumpQueue(List<MediaAnalyticsItem*> *, nsecs_t, const char *only); |
| 129 | |
| 130 | void dumpHeaders(String8 &result, nsecs_t ts_since); |
| 131 | void dumpSummaries(String8 &result, nsecs_t ts_since, const char * only); |
| 132 | void dumpRecent(String8 &result, nsecs_t ts_since, const char * only); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 133 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame^] | 134 | // mapping uids to package names |
| 135 | struct UidToPkgMap { |
| 136 | uid_t uid; |
| 137 | AString pkg; |
| 138 | }; |
| 139 | |
| 140 | KeyedVector<uid_t,AString> mPkgMappings; |
| 141 | AString getPkgName(uid_t uid, bool addIfMissing); |
| 142 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | // ---------------------------------------------------------------------------- |
| 146 | |
| 147 | }; // namespace android |
| 148 | |
| 149 | #endif // ANDROID_MEDIAANALYTICSSERVICE_H |