MediaAnalyticsService.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef ANDROID_MEDIAANALYTICSSERVICE_H
  17. #define ANDROID_MEDIAANALYTICSSERVICE_H
  18. #include <arpa/inet.h>
  19. #include <utils/threads.h>
  20. #include <utils/Errors.h>
  21. #include <utils/KeyedVector.h>
  22. #include <utils/String8.h>
  23. #include <utils/List.h>
  24. #include <future>
  25. #include <media/IMediaAnalyticsService.h>
  26. namespace android {
  27. class MediaAnalyticsService : public BnMediaAnalyticsService
  28. {
  29. public:
  30. // on this side, caller surrenders ownership
  31. virtual int64_t submit(MediaAnalyticsItem *item, bool forcenew);
  32. static void instantiate();
  33. virtual status_t dump(int fd, const Vector<String16>& args);
  34. MediaAnalyticsService();
  35. virtual ~MediaAnalyticsService();
  36. bool processExpirations();
  37. private:
  38. MediaAnalyticsItem::SessionID_t generateUniqueSessionID();
  39. // statistics about our analytics
  40. int64_t mItemsSubmitted;
  41. int64_t mItemsFinalized;
  42. int64_t mItemsDiscarded;
  43. int64_t mItemsDiscardedExpire;
  44. int64_t mItemsDiscardedCount;
  45. MediaAnalyticsItem::SessionID_t mLastSessionID;
  46. // partitioned a bit so we don't over serialize
  47. mutable Mutex mLock;
  48. mutable Mutex mLock_ids;
  49. mutable Mutex mLock_mappings;
  50. // limit how many records we'll retain
  51. // by count (in each queue (open, finalized))
  52. int32_t mMaxRecords;
  53. // by time (none older than this long agan
  54. nsecs_t mMaxRecordAgeNs;
  55. // max to expire per expirations_l() invocation
  56. int32_t mMaxRecordsExpiredAtOnce;
  57. //
  58. // # of sets of summaries
  59. int32_t mMaxRecordSets;
  60. // nsecs until we start a new record set
  61. nsecs_t mNewSetInterval;
  62. // input validation after arrival from client
  63. bool contentValid(MediaAnalyticsItem *item, bool isTrusted);
  64. bool rateLimited(MediaAnalyticsItem *);
  65. // (oldest at front) so it prints nicely for dumpsys
  66. List<MediaAnalyticsItem *> mItems;
  67. void saveItem(MediaAnalyticsItem *);
  68. bool expirations_l(MediaAnalyticsItem *);
  69. std::future<bool> mExpireFuture;
  70. // support for generating output
  71. int mDumpProto;
  72. int mDumpProtoDefault;
  73. String8 dumpQueue();
  74. String8 dumpQueue(nsecs_t, const char *only);
  75. void dumpHeaders(String8 &result, nsecs_t ts_since);
  76. void dumpSummaries(String8 &result, nsecs_t ts_since, const char * only);
  77. void dumpRecent(String8 &result, nsecs_t ts_since, const char * only);
  78. // mapping uids to package names
  79. struct UidToPkgMap {
  80. uid_t uid;
  81. std::string pkg;
  82. std::string installer;
  83. int64_t versionCode;
  84. nsecs_t expiration;
  85. };
  86. KeyedVector<uid_t,struct UidToPkgMap> mPkgMappings;
  87. void setPkgInfo(MediaAnalyticsItem *item, uid_t uid, bool setName, bool setVersion);
  88. };
  89. // hook to send things off to the statsd subsystem
  90. extern bool dump2Statsd(MediaAnalyticsItem *item);
  91. // ----------------------------------------------------------------------------
  92. }; // namespace android
  93. #endif // ANDROID_MEDIAANALYTICSSERVICE_H