1 /* 2 * Copyright (C) 2017 The Android Open Source Project 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 31 #include "MetricsSummarizer.h" 32 33 34 namespace android { 35 36 class MediaAnalyticsService : public BnMediaAnalyticsService 37 { 38 39 public: 40 41 // on this side, caller surrenders ownership 42 virtual int64_t submit(MediaAnalyticsItem *item, bool forcenew); 43 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; 57 int64_t mSetsDiscarded; 58 MediaAnalyticsItem::SessionID_t mLastSessionID; 59 60 // partitioned a bit so we don't over serialize 61 mutable Mutex mLock; 62 mutable Mutex mLock_ids; 63 64 // the most we hold in memory 65 // up to this many in each queue (open, finalized) 66 int32_t mMaxRecords; 67 // # of sets of summaries 68 int32_t mMaxRecordSets; 69 // nsecs until we start a new record set 70 nsecs_t mNewSetInterval; 71 72 // input validation after arrival from client 73 bool contentValid(MediaAnalyticsItem *item, bool isTrusted); 74 bool rateLimited(MediaAnalyticsItem *); 75 76 // the ones that are still open 77 // (newest at front) since we keep looking for them 78 List<MediaAnalyticsItem *> *mOpen; 79 // the ones we've finalized 80 // (oldest at front) so it prints nicely for dumpsys 81 List<MediaAnalyticsItem *> *mFinalized; 82 // searching within these queues: queue, key 83 MediaAnalyticsItem *findItem(List<MediaAnalyticsItem *> *, 84 MediaAnalyticsItem *, bool removeit); 85 86 // summarizers 87 void summarize(MediaAnalyticsItem *item); 88 class SummarizerSet { 89 nsecs_t mStarted; 90 List<MetricsSummarizer *> *mSummarizers; 91 92 public: appendSummarizer(MetricsSummarizer * s)93 void appendSummarizer(MetricsSummarizer *s) { 94 if (s) { 95 mSummarizers->push_back(s); 96 } 97 }; getStarted()98 nsecs_t getStarted() { return mStarted;} setStarted(nsecs_t started)99 void setStarted(nsecs_t started) {mStarted = started;} getSummarizers()100 List<MetricsSummarizer *> *getSummarizers() { return mSummarizers;} 101 102 SummarizerSet(); 103 ~SummarizerSet(); 104 }; 105 void newSummarizerSet(); 106 List<SummarizerSet *> *mSummarizerSets; 107 SummarizerSet *mCurrentSet; getFirstSet()108 List<MetricsSummarizer *> *getFirstSet() { 109 List<SummarizerSet *>::iterator first = mSummarizerSets->begin(); 110 if (first != mSummarizerSets->end()) { 111 return (*first)->getSummarizers(); 112 } 113 return NULL; 114 } 115 116 void saveItem(MediaAnalyticsItem); 117 void saveItem(List<MediaAnalyticsItem *> *, MediaAnalyticsItem *, int); 118 void deleteItem(List<MediaAnalyticsItem *> *, MediaAnalyticsItem *); 119 120 // support for generating output 121 String8 dumpQueue(List<MediaAnalyticsItem*> *); 122 String8 dumpQueue(List<MediaAnalyticsItem*> *, nsecs_t, const char *only); 123 124 void dumpHeaders(String8 &result, nsecs_t ts_since); 125 void dumpSummaries(String8 &result, nsecs_t ts_since, const char * only); 126 void dumpRecent(String8 &result, nsecs_t ts_since, const char * only); 127 128 }; 129 130 // ---------------------------------------------------------------------------- 131 132 }; // namespace android 133 134 #endif // ANDROID_MEDIAANALYTICSSERVICE_H 135