blob: fc8f594b6d9875a396d4d4eb2ce9edbefef2d351 [file] [log] [blame]
Ray Essick2e9c63b2017-03-29 15:16:44 -07001/*
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#define LOG_TAG "MetricsSummarizer"
18#include <utils/Log.h>
19
20#include <stdlib.h>
21#include <stdint.h>
22#include <inttypes.h>
23
24#include <utils/threads.h>
25#include <utils/Errors.h>
26#include <utils/KeyedVector.h>
27#include <utils/String8.h>
28#include <utils/List.h>
29
30#include <media/IMediaAnalyticsService.h>
31
32#include "MetricsSummarizer.h"
33
34
35namespace android {
36
37#define DEBUG_SORT 0
38#define DEBUG_QUEUE 0
39
40
41MetricsSummarizer::MetricsSummarizer(const char *key)
42 : mIgnorables(NULL)
43{
44 ALOGV("MetricsSummarizer::MetricsSummarizer");
45
46 if (key == NULL) {
47 mKey = key;
48 } else {
49 mKey = strdup(key);
50 }
51
52 mSummaries = new List<MediaAnalyticsItem *>();
53}
54
55MetricsSummarizer::~MetricsSummarizer()
56{
57 ALOGV("MetricsSummarizer::~MetricsSummarizer");
58 if (mKey) {
59 free((void *)mKey);
60 mKey = NULL;
61 }
62
63 // clear the list of items we have saved
64 while (mSummaries->size() > 0) {
65 MediaAnalyticsItem * oitem = *(mSummaries->begin());
66 if (DEBUG_QUEUE) {
67 ALOGD("zap old record: key %s sessionID %" PRId64 " ts %" PRId64 "",
68 oitem->getKey().c_str(), oitem->getSessionID(),
69 oitem->getTimestamp());
70 }
71 mSummaries->erase(mSummaries->begin());
72 delete oitem;
73 }
74}
75
76// so we know what summarizer we were using
77const char *MetricsSummarizer::getKey() {
78 const char *value = mKey;
79 if (value == NULL) {
80 value = "unknown";
81 }
82 return value;
83}
84
85// should the record be given to this summarizer
86bool MetricsSummarizer::isMine(MediaAnalyticsItem &item)
87{
88 const char *incoming = item.getKey().c_str();
89 if (incoming == NULL) {
90 incoming = "unspecified";
91 }
92 if (mKey == NULL)
93 return true;
94 if (strcmp(mKey, incoming) != 0) {
95 return false;
96 }
97 // since nothing failed....
98 return true;
99}
100
101AString MetricsSummarizer::dumpSummary(int &slot)
102{
103 return dumpSummary(slot, NULL);
104}
105
106AString MetricsSummarizer::dumpSummary(int &slot, const char *only)
107{
108 AString value = "";
109
110 List<MediaAnalyticsItem *>::iterator it = mSummaries->begin();
111 if (it != mSummaries->end()) {
112 char buf[16]; // enough for "#####: "
113 for (; it != mSummaries->end(); it++) {
114 if (only != NULL && strcmp(only, (*it)->getKey().c_str()) != 0) {
115 continue;
116 }
117 AString entry = (*it)->toString();
118 snprintf(buf, sizeof(buf), "%5d: ", slot);
119 value.append(buf);
120 value.append(entry.c_str());
121 value.append("\n");
122 slot++;
123 }
124 }
125 return value;
126}
127
128void MetricsSummarizer::setIgnorables(const char **ignorables) {
129 mIgnorables = ignorables;
130}
131
132const char **MetricsSummarizer::getIgnorables() {
133 return mIgnorables;
134}
135
136void MetricsSummarizer::handleRecord(MediaAnalyticsItem *item) {
137
138 ALOGV("MetricsSummarizer::handleRecord() for %s",
139 item == NULL ? "<nothing>" : item->toString().c_str());
140
141 if (item == NULL) {
142 return;
143 }
144
145 List<MediaAnalyticsItem *>::iterator it = mSummaries->begin();
146 for (; it != mSummaries->end(); it++) {
147 bool good = sameAttributes((*it), item, getIgnorables());
148 ALOGV("Match against %s says %d",
149 (*it)->toString().c_str(), good);
150 if (good)
151 break;
152 }
153 if (it == mSummaries->end()) {
154 ALOGV("save new record");
155 item = item->dup();
156 if (item == NULL) {
157 ALOGE("unable to save MediaMetrics record");
158 }
159 sortProps(item);
160 item->setInt32("count",1);
161 mSummaries->push_back(item);
162 } else {
163 ALOGV("increment existing record");
164 (*it)->addInt32("count",1);
165 mergeRecord(*(*it), *item);
166 }
167}
168
169void MetricsSummarizer::mergeRecord(MediaAnalyticsItem &/*have*/, MediaAnalyticsItem &/*item*/) {
170 // default is no further massaging.
171 ALOGV("MetricsSummarizer::mergeRecord() [default]");
172 return;
173}
174
175
176//
177// Comparators
178//
179
180// testing that all of 'single' is in 'summ'
181// and that the values match.
182// 'summ' may have extra fields.
183// 'ignorable' is a set of things that we don't worry about matching up
184// (usually time- or count-based values we'll sum elsewhere)
185bool MetricsSummarizer::sameAttributes(MediaAnalyticsItem *summ, MediaAnalyticsItem *single, const char **ignorable) {
186
187 if (single == NULL || summ == NULL) {
188 return false;
189 }
190 ALOGV("MetricsSummarizer::sameAttributes(): summ %s", summ->toString().c_str());
191 ALOGV("MetricsSummarizer::sameAttributes(): single %s", single->toString().c_str());
192
193 // this can be made better.
194 for(size_t i=0;i<single->mPropCount;i++) {
195 MediaAnalyticsItem::Prop *prop1 = &(single->mProps[i]);
196 const char *attrName = prop1->mName;
197 ALOGV("compare on attr '%s'", attrName);
198
199 // is it something we should ignore
200 if (ignorable != NULL) {
201 const char **ig = ignorable;
202 while (*ig) {
203 if (strcmp(*ig, attrName) == 0) {
204 break;
205 }
206 ig++;
207 }
208 if (*ig) {
209 ALOGV("we don't mind that it has attr '%s'", attrName);
210 continue;
211 }
212 }
213
214 MediaAnalyticsItem::Prop *prop2 = summ->findProp(attrName);
215 if (prop2 == NULL) {
216 ALOGV("summ doesn't have this attr");
217 return false;
218 }
219 if (prop1->mType != prop2->mType) {
220 ALOGV("mismatched attr types");
221 return false;
222 }
223 switch (prop1->mType) {
224 case MediaAnalyticsItem::kTypeInt32:
225 if (prop1->u.int32Value != prop2->u.int32Value)
226 return false;
227 break;
228 case MediaAnalyticsItem::kTypeInt64:
229 if (prop1->u.int64Value != prop2->u.int64Value)
230 return false;
231 break;
232 case MediaAnalyticsItem::kTypeDouble:
233 // XXX: watch out for floating point comparisons!
234 if (prop1->u.doubleValue != prop2->u.doubleValue)
235 return false;
236 break;
237 case MediaAnalyticsItem::kTypeCString:
238 if (strcmp(prop1->u.CStringValue, prop2->u.CStringValue) != 0)
239 return false;
240 break;
241 case MediaAnalyticsItem::kTypeRate:
242 if (prop1->u.rate.count != prop2->u.rate.count)
243 return false;
244 if (prop1->u.rate.duration != prop2->u.rate.duration)
245 return false;
246 break;
247 default:
248 return false;
249 }
250 }
251
252 return true;
253}
254
255bool MetricsSummarizer::sameAttributesId(MediaAnalyticsItem *summ, MediaAnalyticsItem *single, const char **ignorable) {
256
257 // verify same user
258 if (summ->mPid != single->mPid)
259 return false;
260
261 // and finally do the more expensive validation of the attributes
262 return sameAttributes(summ, single, ignorable);
263}
264
265int MetricsSummarizer::PropSorter(const void *a, const void *b) {
266 MediaAnalyticsItem::Prop *ai = (MediaAnalyticsItem::Prop *)a;
267 MediaAnalyticsItem::Prop *bi = (MediaAnalyticsItem::Prop *)b;
268 return strcmp(ai->mName, bi->mName);
269}
270
271// we sort in the summaries so that it looks pretty in the dumpsys
272void MetricsSummarizer::sortProps(MediaAnalyticsItem *item) {
273 if (item->mPropCount != 0) {
274 if (DEBUG_SORT) {
275 ALOGD("sortProps(pre): %s", item->toString().c_str());
276 }
277 qsort(item->mProps, item->mPropCount,
278 sizeof(MediaAnalyticsItem::Prop), MetricsSummarizer::PropSorter);
279 if (DEBUG_SORT) {
280 ALOGD("sortProps(pst): %s", item->toString().c_str());
281 }
282 }
283}
284
285} // namespace android