blob: 93fe0ec70e54a3d7efea14c2e00caf7ebb5df472 [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{
Ray Essick2e9c63b2017-03-29 15:16:44 -070088 if (mKey == NULL)
89 return true;
Ray Essickde000852017-04-13 10:16:25 -070090 AString itemKey = item.getKey();
91 if (strcmp(mKey, itemKey.c_str()) != 0) {
Ray Essick2e9c63b2017-03-29 15:16:44 -070092 return false;
93 }
Ray Essick2e9c63b2017-03-29 15:16:44 -070094 return true;
95}
96
97AString MetricsSummarizer::dumpSummary(int &slot)
98{
99 return dumpSummary(slot, NULL);
100}
101
102AString MetricsSummarizer::dumpSummary(int &slot, const char *only)
103{
104 AString value = "";
105
106 List<MediaAnalyticsItem *>::iterator it = mSummaries->begin();
107 if (it != mSummaries->end()) {
108 char buf[16]; // enough for "#####: "
109 for (; it != mSummaries->end(); it++) {
110 if (only != NULL && strcmp(only, (*it)->getKey().c_str()) != 0) {
111 continue;
112 }
113 AString entry = (*it)->toString();
114 snprintf(buf, sizeof(buf), "%5d: ", slot);
115 value.append(buf);
116 value.append(entry.c_str());
117 value.append("\n");
118 slot++;
119 }
120 }
121 return value;
122}
123
124void MetricsSummarizer::setIgnorables(const char **ignorables) {
125 mIgnorables = ignorables;
126}
127
128const char **MetricsSummarizer::getIgnorables() {
129 return mIgnorables;
130}
131
132void MetricsSummarizer::handleRecord(MediaAnalyticsItem *item) {
133
134 ALOGV("MetricsSummarizer::handleRecord() for %s",
135 item == NULL ? "<nothing>" : item->toString().c_str());
136
137 if (item == NULL) {
138 return;
139 }
140
141 List<MediaAnalyticsItem *>::iterator it = mSummaries->begin();
142 for (; it != mSummaries->end(); it++) {
143 bool good = sameAttributes((*it), item, getIgnorables());
Ray Essick96eaaac2017-08-31 11:45:05 -0700144 ALOGV("Match against %s says %d", (*it)->toString().c_str(), good);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700145 if (good)
146 break;
147 }
148 if (it == mSummaries->end()) {
149 ALOGV("save new record");
Ray Essick96eaaac2017-08-31 11:45:05 -0700150 MediaAnalyticsItem *nitem = item->dup();
151 if (nitem == NULL) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700152 ALOGE("unable to save MediaMetrics record");
153 }
Ray Essick96eaaac2017-08-31 11:45:05 -0700154 sortProps(nitem);
155 nitem->setInt32("aggregated",1);
156 mergeRecord(*nitem, *item);
157 mSummaries->push_back(nitem);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700158 } else {
159 ALOGV("increment existing record");
Ray Essick96eaaac2017-08-31 11:45:05 -0700160 (*it)->addInt32("aggregated",1);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700161 mergeRecord(*(*it), *item);
162 }
163}
164
165void MetricsSummarizer::mergeRecord(MediaAnalyticsItem &/*have*/, MediaAnalyticsItem &/*item*/) {
166 // default is no further massaging.
167 ALOGV("MetricsSummarizer::mergeRecord() [default]");
168 return;
169}
170
Ray Essick96eaaac2017-08-31 11:45:05 -0700171// keep some stats for things: sums, counts, standard deviation
172// the integer version -- all of these pieces are in 64 bits
173void MetricsSummarizer::minMaxVar64(MediaAnalyticsItem &summation, const char *key, int64_t value) {
174 if (key == NULL)
175 return;
176 int len = strlen(key) + 32;
177 char *tmpKey = (char *)malloc(len);
178
179 if (tmpKey == NULL) {
180 return;
181 }
182
183 // N - count of samples
184 snprintf(tmpKey, len, "%s.n", key);
185 summation.addInt64(tmpKey, 1);
186
187 // zero - count of samples that are zero
188 if (value == 0) {
189 snprintf(tmpKey, len, "%s.zero", key);
190 int64_t zero = 0;
191 (void) summation.getInt64(tmpKey,&zero);
192 zero++;
193 summation.setInt64(tmpKey, zero);
194 }
195
196 // min
197 snprintf(tmpKey, len, "%s.min", key);
198 int64_t min = value;
199 if (summation.getInt64(tmpKey,&min)) {
200 if (min > value) {
201 summation.setInt64(tmpKey, value);
202 }
203 } else {
204 summation.setInt64(tmpKey, value);
205 }
206
207 // max
208 snprintf(tmpKey, len, "%s.max", key);
209 int64_t max = value;
210 if (summation.getInt64(tmpKey,&max)) {
211 if (max < value) {
212 summation.setInt64(tmpKey, value);
213 }
214 } else {
215 summation.setInt64(tmpKey, value);
216 }
217
218 // components for mean, stddev;
219 // stddev = sqrt(1/4*(sumx2 - (2*sumx*sumx/n) + ((sumx/n)^2)))
220 // sum x
221 snprintf(tmpKey, len, "%s.sumX", key);
222 summation.addInt64(tmpKey, value);
223 // sum x^2
224 snprintf(tmpKey, len, "%s.sumX2", key);
225 summation.addInt64(tmpKey, value*value);
226
227
228 // last thing we do -- remove the base key from the summation
229 // record so we won't get confused about it having both individual
230 // and summary information in there.
231 summation.removeProp(key);
232
233 free(tmpKey);
234}
235
Ray Essick2e9c63b2017-03-29 15:16:44 -0700236
237//
238// Comparators
239//
240
241// testing that all of 'single' is in 'summ'
242// and that the values match.
243// 'summ' may have extra fields.
244// 'ignorable' is a set of things that we don't worry about matching up
245// (usually time- or count-based values we'll sum elsewhere)
246bool MetricsSummarizer::sameAttributes(MediaAnalyticsItem *summ, MediaAnalyticsItem *single, const char **ignorable) {
247
248 if (single == NULL || summ == NULL) {
249 return false;
250 }
251 ALOGV("MetricsSummarizer::sameAttributes(): summ %s", summ->toString().c_str());
252 ALOGV("MetricsSummarizer::sameAttributes(): single %s", single->toString().c_str());
253
Ray Essick96eaaac2017-08-31 11:45:05 -0700254 // keep different sources/users separate
255 if (single->mUid != summ->mUid) {
256 return false;
257 }
258
Ray Essick2e9c63b2017-03-29 15:16:44 -0700259 // this can be made better.
260 for(size_t i=0;i<single->mPropCount;i++) {
261 MediaAnalyticsItem::Prop *prop1 = &(single->mProps[i]);
262 const char *attrName = prop1->mName;
Ray Essick2e9c63b2017-03-29 15:16:44 -0700263
264 // is it something we should ignore
265 if (ignorable != NULL) {
266 const char **ig = ignorable;
Ray Essick96eaaac2017-08-31 11:45:05 -0700267 for (;*ig; ig++) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700268 if (strcmp(*ig, attrName) == 0) {
269 break;
270 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700271 }
272 if (*ig) {
273 ALOGV("we don't mind that it has attr '%s'", attrName);
274 continue;
275 }
276 }
277
278 MediaAnalyticsItem::Prop *prop2 = summ->findProp(attrName);
279 if (prop2 == NULL) {
280 ALOGV("summ doesn't have this attr");
281 return false;
282 }
283 if (prop1->mType != prop2->mType) {
284 ALOGV("mismatched attr types");
285 return false;
286 }
287 switch (prop1->mType) {
288 case MediaAnalyticsItem::kTypeInt32:
Ray Essick96eaaac2017-08-31 11:45:05 -0700289 if (prop1->u.int32Value != prop2->u.int32Value) {
290 ALOGV("mismatch values");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700291 return false;
Ray Essick96eaaac2017-08-31 11:45:05 -0700292 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700293 break;
294 case MediaAnalyticsItem::kTypeInt64:
Ray Essick96eaaac2017-08-31 11:45:05 -0700295 if (prop1->u.int64Value != prop2->u.int64Value) {
296 ALOGV("mismatch values");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700297 return false;
Ray Essick96eaaac2017-08-31 11:45:05 -0700298 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700299 break;
300 case MediaAnalyticsItem::kTypeDouble:
301 // XXX: watch out for floating point comparisons!
Ray Essick96eaaac2017-08-31 11:45:05 -0700302 if (prop1->u.doubleValue != prop2->u.doubleValue) {
303 ALOGV("mismatch values");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700304 return false;
Ray Essick96eaaac2017-08-31 11:45:05 -0700305 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700306 break;
307 case MediaAnalyticsItem::kTypeCString:
Ray Essick96eaaac2017-08-31 11:45:05 -0700308 if (strcmp(prop1->u.CStringValue, prop2->u.CStringValue) != 0) {
309 ALOGV("mismatch values");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700310 return false;
Ray Essick96eaaac2017-08-31 11:45:05 -0700311 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700312 break;
313 case MediaAnalyticsItem::kTypeRate:
Ray Essick96eaaac2017-08-31 11:45:05 -0700314 if (prop1->u.rate.count != prop2->u.rate.count) {
315 ALOGV("mismatch values");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700316 return false;
Ray Essick96eaaac2017-08-31 11:45:05 -0700317 }
318 if (prop1->u.rate.duration != prop2->u.rate.duration) {
319 ALOGV("mismatch values");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700320 return false;
Ray Essick96eaaac2017-08-31 11:45:05 -0700321 }
Ray Essick2e9c63b2017-03-29 15:16:44 -0700322 break;
323 default:
Ray Essick96eaaac2017-08-31 11:45:05 -0700324 ALOGV("mismatch values in default type");
Ray Essick2e9c63b2017-03-29 15:16:44 -0700325 return false;
326 }
327 }
328
329 return true;
330}
331
Ray Essick2e9c63b2017-03-29 15:16:44 -0700332
333int MetricsSummarizer::PropSorter(const void *a, const void *b) {
334 MediaAnalyticsItem::Prop *ai = (MediaAnalyticsItem::Prop *)a;
335 MediaAnalyticsItem::Prop *bi = (MediaAnalyticsItem::Prop *)b;
336 return strcmp(ai->mName, bi->mName);
337}
338
339// we sort in the summaries so that it looks pretty in the dumpsys
340void MetricsSummarizer::sortProps(MediaAnalyticsItem *item) {
341 if (item->mPropCount != 0) {
Ray Essick2e9c63b2017-03-29 15:16:44 -0700342 qsort(item->mProps, item->mPropCount,
343 sizeof(MediaAnalyticsItem::Prop), MetricsSummarizer::PropSorter);
Ray Essick2e9c63b2017-03-29 15:16:44 -0700344 }
345}
346
347} // namespace android