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 | |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 25 | // IMediaMetricsService must include Vector, String16, Errors |
Andy Hung | f7c1410 | 2020-04-18 14:54:08 -0700 | [diff] [blame] | 26 | #include <android-base/thread_annotations.h> |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 27 | #include <android/media/BnMediaMetricsService.h> |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 28 | #include <mediautils/ServiceUtilities.h> |
Robert Shih | 2e15aed | 2021-03-16 18:30:35 -0700 | [diff] [blame] | 29 | #include <stats_pull_atom_callback.h> |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 30 | #include <utils/String8.h> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 31 | |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 32 | #include "AudioAnalytics.h" |
| 33 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 36 | class MediaMetricsService : public media::BnMediaMetricsService |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 37 | { |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 38 | public: |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 39 | MediaMetricsService(); |
| 40 | ~MediaMetricsService() override; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 41 | |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 42 | // AIDL interface |
| 43 | binder::Status submitBuffer(const std::vector<uint8_t>& buffer) override { |
| 44 | status_t status = submitBuffer((char *)buffer.data(), buffer.size()); |
| 45 | return binder::Status::fromStatusT(status); |
| 46 | } |
| 47 | |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 48 | /** |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 49 | * Submits the indicated record to the mediaanalytics service. |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 50 | * |
| 51 | * \param item the item to submit. |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 52 | * \return status failure, which is negative on binder transaction failure. |
| 53 | * As the transaction is one-way, remote failures will not be reported. |
Andy Hung | c89c8dc | 2019-10-16 17:48:21 -0700 | [diff] [blame] | 54 | */ |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 55 | status_t submit(mediametrics::Item *item) { |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 56 | return submitInternal(item, false /* release */); |
| 57 | } |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 58 | |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 59 | status_t submitBuffer(const char *buffer, size_t length) { |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 60 | mediametrics::Item *item = new mediametrics::Item(); |
Andy Hung | 1efc9c6 | 2019-12-03 13:43:33 -0800 | [diff] [blame] | 61 | return item->readFromByteString(buffer, length) |
| 62 | ?: submitInternal(item, true /* release */); |
| 63 | } |
| 64 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 65 | status_t dump(int fd, const Vector<String16>& args) override; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 66 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 67 | static constexpr const char * const kServiceName = "media.metrics"; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 68 | |
Andy Hung | 55aaf52 | 2019-12-03 15:07:51 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Rounds time to the nearest second. |
| 71 | */ |
| 72 | static nsecs_t roundTime(nsecs_t timeNs); |
| 73 | |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 74 | /** |
Muhammad Qureshi | 087b37c | 2020-06-16 16:37:36 -0700 | [diff] [blame] | 75 | * Returns true if we should use uid for package name when uploading to statsd. |
Andy Hung | a85efab | 2019-12-23 11:41:29 -0800 | [diff] [blame] | 76 | */ |
| 77 | static bool useUidForPackage(const std::string& package, const std::string& installer); |
| 78 | |
Andy Hung | ce9b663 | 2020-04-28 20:15:17 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Returns a std::pair of packageName and versionCode for a given uid. |
| 81 | * |
| 82 | * The value is sanitized - i.e. if the result is not approved to send, |
| 83 | * we use the uid as a string and a version code of 0. |
| 84 | */ |
| 85 | static std::pair<std::string, int64_t> getSanitizedPackageNameAndVersionCode(uid_t uid); |
| 86 | |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 87 | protected: |
| 88 | |
| 89 | // Internal call where release is true if ownership of item is transferred |
| 90 | // to the service (that is, the service will eventually delete the item). |
Andy Hung | 49ca44e | 2020-11-10 22:14:58 -0800 | [diff] [blame] | 91 | status_t submitInternal(mediametrics::Item *item, bool release); |
Andy Hung | a87e69c | 2019-10-18 10:07:40 -0700 | [diff] [blame] | 92 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 93 | private: |
| 94 | void processExpirations(); |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 95 | // input validation after arrival from client |
Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 96 | static bool isContentValid(const mediametrics::Item *item, bool isTrusted); |
| 97 | bool isRateLimited(mediametrics::Item *) const; |
| 98 | void saveItem(const std::shared_ptr<const mediametrics::Item>& item); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 99 | |
Andy Hung | f7c1410 | 2020-04-18 14:54:08 -0700 | [diff] [blame] | 100 | bool expirations(const std::shared_ptr<const mediametrics::Item>& item) REQUIRES(mLock); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 101 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 102 | // support for generating output |
Andy Hung | 54c73ce | 2021-03-30 14:25:54 -0700 | [diff] [blame] | 103 | std::string dumpQueue(int64_t sinceNs, const char* prefix) REQUIRES(mLock); |
| 104 | std::string dumpHeaders(int64_t sinceNs, const char* prefix) REQUIRES(mLock); |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 105 | |
Robert Shih | 2e15aed | 2021-03-16 18:30:35 -0700 | [diff] [blame] | 106 | // support statsd pushed atoms |
| 107 | static bool isPullable(const std::string &key); |
| 108 | static std::string atomTagToKey(int32_t atomTag); |
| 109 | static AStatsManager_PullAtomCallbackReturn pullAtomCallback( |
| 110 | int32_t atomTag, AStatsEventList* data, void* cookie); |
| 111 | AStatsManager_PullAtomCallbackReturn pullItems(int32_t atomTag, AStatsEventList* data); |
| 112 | void registerStatsdCallbacksIfNeeded(); |
| 113 | std::atomic_flag mStatsdRegistered = ATOMIC_FLAG_INIT; |
| 114 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 115 | // The following variables accessed without mLock |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 116 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 117 | // limit how many records we'll retain |
| 118 | // by count (in each queue (open, finalized)) |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 119 | const size_t mMaxRecords; |
| 120 | // by time (none older than this) |
| 121 | const nsecs_t mMaxRecordAgeNs; |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame] | 122 | // max to expire per expirations_l() invocation |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 123 | const size_t mMaxRecordsExpiredAtOnce; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 124 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 125 | std::atomic<int64_t> mItemsSubmitted{}; // accessed outside of lock. |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 126 | |
Andy Hung | 5be90c8 | 2021-03-30 14:30:20 -0700 | [diff] [blame] | 127 | // mStatsdLog is locked internally (thread-safe) and shows the last atoms logged |
| 128 | static constexpr size_t STATSD_LOG_LINES_MAX = 30; // recent log lines to keep |
| 129 | static constexpr size_t STATSD_LOG_LINES_DUMP = 4; // normal amount of lines to dump |
| 130 | const std::shared_ptr<mediametrics::StatsdLog> mStatsdLog{ |
| 131 | std::make_shared<mediametrics::StatsdLog>(STATSD_LOG_LINES_MAX)}; |
| 132 | |
| 133 | // mAudioAnalytics is locked internally. |
| 134 | mediametrics::AudioAnalytics mAudioAnalytics{mStatsdLog}; |
Andy Hung | 06f3aba | 2019-12-03 16:36:42 -0800 | [diff] [blame] | 135 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 136 | std::mutex mLock; |
| 137 | // statistics about our analytics |
Andy Hung | f7c1410 | 2020-04-18 14:54:08 -0700 | [diff] [blame] | 138 | int64_t mItemsFinalized GUARDED_BY(mLock) = 0; |
| 139 | int64_t mItemsDiscarded GUARDED_BY(mLock) = 0; |
| 140 | int64_t mItemsDiscardedExpire GUARDED_BY(mLock) = 0; |
| 141 | int64_t mItemsDiscardedCount GUARDED_BY(mLock) = 0; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 142 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 143 | // If we have a worker thread to garbage collect |
Andy Hung | f7c1410 | 2020-04-18 14:54:08 -0700 | [diff] [blame] | 144 | std::future<void> mExpireFuture GUARDED_BY(mLock); |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 145 | |
| 146 | // Our item queue, generally (oldest at front) |
| 147 | // TODO: Make separate class, use segmented queue, write lock only end. |
| 148 | // Note: Another analytics module might have ownership of an item longer than the log. |
Andy Hung | f7c1410 | 2020-04-18 14:54:08 -0700 | [diff] [blame] | 149 | std::deque<std::shared_ptr<const mediametrics::Item>> mItems GUARDED_BY(mLock); |
Robert Shih | 2e15aed | 2021-03-16 18:30:35 -0700 | [diff] [blame] | 150 | |
| 151 | // Queues per item key, pending to be pulled by statsd. |
| 152 | // Use weak_ptr such that a pullable item can still expire. |
| 153 | using ItemKey = std::string; |
| 154 | using WeakItemQueue = std::deque<std::weak_ptr<const mediametrics::Item>>; |
| 155 | std::unordered_map<ItemKey, WeakItemQueue> mPullableItems GUARDED_BY(mLock); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Andy Hung | 17dbaf2 | 2019-10-11 14:06:31 -0700 | [diff] [blame] | 158 | } // namespace android |