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 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 17 | #pragma once |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 18 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 19 | #include <atomic> |
| 20 | #include <deque> |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame] | 21 | #include <future> |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 22 | #include <mutex> |
| 23 | #include <unordered_map> |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame] | 24 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 25 | // IMediaAnalyticsService must include Vector, String16, Errors |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 26 | #include <media/IMediaAnalyticsService.h> |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 27 | #include <utils/String8.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 28 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | class MediaAnalyticsService : public BnMediaAnalyticsService |
| 32 | { |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 33 | public: |
| 34 | MediaAnalyticsService(); |
| 35 | ~MediaAnalyticsService() override; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 36 | |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 37 | /** |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 38 | * Submits the indicated record to the mediaanalytics service. |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 39 | * |
| 40 | * \param item the item to submit. |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 41 | * \return status failure, which is negative on binder transaction failure. |
| 42 | * As the transaction is one-way, remote failures will not be reported. |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 43 | */ |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 44 | status_t submit(MediaAnalyticsItem *item) override { |
| 45 | return submitInternal(item, false /* release */); |
| 46 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 47 | |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame^] | 48 | status_t submitBuffer(const char *buffer, size_t length) override { |
| 49 | MediaAnalyticsItem *item = new MediaAnalyticsItem(); |
| 50 | return item->readFromByteString(buffer, length) |
| 51 | ?: submitInternal(item, true /* release */); |
| 52 | } |
| 53 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 54 | status_t dump(int fd, const Vector<String16>& args) override; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 55 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 56 | static constexpr const char * const kServiceName = "media.metrics"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 57 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 58 | protected: |
| 59 | |
| 60 | // Internal call where release is true if ownership of item is transferred |
| 61 | // to the service (that is, the service will eventually delete the item). |
| 62 | status_t submitInternal(MediaAnalyticsItem *item, bool release) override; |
| 63 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 64 | private: |
| 65 | void processExpirations(); |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 66 | // input validation after arrival from client |
| 67 | static bool isContentValid(const MediaAnalyticsItem *item, bool isTrusted); |
| 68 | bool isRateLimited(MediaAnalyticsItem *) const; |
| 69 | void saveItem(MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 70 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 71 | // The following methods are GUARDED_BY(mLock) |
| 72 | bool expirations_l(MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 73 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 74 | // support for generating output |
| 75 | void dumpQueue_l(String8 &result, int dumpProto); |
| 76 | void dumpQueue_l(String8 &result, int dumpProto, nsecs_t, const char *only); |
| 77 | void dumpHeaders_l(String8 &result, int dumpProto, nsecs_t ts_since); |
| 78 | void dumpSummaries_l(String8 &result, int dumpProto, nsecs_t ts_since, const char * only); |
| 79 | void dumpRecent_l(String8 &result, int dumpProto, nsecs_t ts_since, const char * only); |
| 80 | |
| 81 | // The following variables accessed without mLock |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 82 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 83 | // limit how many records we'll retain |
| 84 | // by count (in each queue (open, finalized)) |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 85 | const size_t mMaxRecords; |
| 86 | // by time (none older than this) |
| 87 | const nsecs_t mMaxRecordAgeNs; |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame] | 88 | // max to expire per expirations_l() invocation |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 89 | const size_t mMaxRecordsExpiredAtOnce; |
| 90 | const int mDumpProtoDefault; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 91 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 92 | class UidInfo { |
| 93 | public: |
| 94 | void setPkgInfo(MediaAnalyticsItem *item, uid_t uid, bool setName, bool setVersion); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 95 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 96 | private: |
| 97 | std::mutex mUidInfoLock; |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame] | 98 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 99 | struct UidToPkgInfo { |
| 100 | uid_t uid = -1; |
| 101 | std::string pkg; |
| 102 | std::string installer; |
| 103 | int64_t versionCode = 0; |
| 104 | nsecs_t expiration = 0; // TODO: remove expiration. |
| 105 | }; |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 106 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 107 | // TODO: use concurrent hashmap with striped lock. |
| 108 | std::unordered_map<uid_t, struct UidToPkgInfo> mPkgMappings; // GUARDED_BY(mUidInfoLock) |
| 109 | } mUidInfo; // mUidInfo can be accessed without lock (locked internally) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 110 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 111 | std::atomic<int64_t> mItemsSubmitted{}; // accessed outside of lock. |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 112 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 113 | std::mutex mLock; |
| 114 | // statistics about our analytics |
| 115 | int64_t mItemsFinalized = 0; // GUARDED_BY(mLock) |
| 116 | int64_t mItemsDiscarded = 0; // GUARDED_BY(mLock) |
| 117 | int64_t mItemsDiscardedExpire = 0; // GUARDED_BY(mLock) |
| 118 | int64_t mItemsDiscardedCount = 0; // GUARDED_BY(mLock) |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 119 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 120 | // If we have a worker thread to garbage collect |
| 121 | std::future<void> mExpireFuture; // GUARDED_BY(mLock) |
| 122 | |
| 123 | // Our item queue, generally (oldest at front) |
| 124 | // TODO: Make separate class, use segmented queue, write lock only end. |
| 125 | // Note: Another analytics module might have ownership of an item longer than the log. |
| 126 | std::deque<std::shared_ptr<const MediaAnalyticsItem>> mItems; // GUARDED_BY(mLock) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 129 | } // namespace android |