blob: fcce2ff3b36e9336482e66820dee98a13dde187d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "stats_util.h"
#include <log/log_event_list.h>
namespace android {
namespace os {
namespace statsd {
static inline uint32_t get4LE(const char* src) {
return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}
int getTagId(log_msg msg) {
return get4LE(msg.msg());
}
EventMetricData parse(log_msg msg) {
// dump all statsd logs to dropbox for now.
// TODO: Add filtering, aggregation, etc.
EventMetricData eventMetricData;
// set tag.
int tag = getTagId(msg);
// TODO: Replace the following line when we can serialize on the fly.
// eventMetricData.set_tag(tag);
// set timestamp of the event.
eventMetricData.set_timestamp_nanos(msg.entry_v1.sec * NS_PER_SEC + msg.entry_v1.nsec);
// start iterating k,v pairs.
android_log_context context =
create_android_log_parser(const_cast<log_msg*>(&msg)->msg() + sizeof(uint32_t),
const_cast<log_msg*>(&msg)->len() - sizeof(uint32_t));
android_log_list_element elem;
if (context) {
memset(&elem, 0, sizeof(elem));
size_t index = 0;
int32_t key = -1;
do {
elem = android_log_read_next(context);
switch ((int)elem.type) {
case EVENT_TYPE_INT:
if (index % 2 == 0) {
key = elem.data.int32;
} else {
// TODO: Fix the following lines when we can serialize on the fly.
/*
int32_t val = elem.data.int32;
KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
keyValuePair->set_key(key);
keyValuePair->set_value_int(val);
*/
}
index++;
break;
case EVENT_TYPE_FLOAT:
if (index % 2 == 1) {
// TODO: Fix the following lines when we can serialize on the fly.
/*
float val = elem.data.float32;
KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
keyValuePair->set_key(key);
keyValuePair->set_value_float(val);
*/
}
index++;
break;
case EVENT_TYPE_STRING:
if (index % 2 == 1) {
// TODO: Fix the following lines when we can serialize on the fly.
/*
char* val = elem.data.string;
KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
keyValuePair->set_key(key);
keyValuePair->set_value_str(val);
*/
}
index++;
break;
case EVENT_TYPE_LONG:
if (index % 2 == 1) {
// TODO: Fix the following lines when we can serialize on the fly.
/*
int64_t val = elem.data.int64;
KeyValuePair* keyValuePair = eventMetricData.add_key_value_pair();
keyValuePair->set_key(key);
keyValuePair->set_value_int(val);
*/
}
index++;
break;
case EVENT_TYPE_LIST:
break;
case EVENT_TYPE_LIST_STOP:
break;
case EVENT_TYPE_UNKNOWN:
break;
default:
elem.complete = true;
break;
}
if (elem.complete) {
break;
}
} while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
android_log_destroy(&context);
}
return eventMetricData;
}
// There is no existing hash function for the dimension key ("repeated KeyValuePair").
// Temporarily use a string concatenation as the hashable key.
// TODO: Find a better hash function for std::vector<KeyValuePair>.
HashableDimensionKey getHashableKey(std::vector<KeyValuePair> keys) {
std::string flattened;
for (const KeyValuePair& pair : keys) {
flattened += std::to_string(pair.key());
flattened += ":";
switch (pair.value_case()) {
case KeyValuePair::ValueCase::kValueStr:
flattened += pair.value_str();
break;
case KeyValuePair::ValueCase::kValueInt:
flattened += std::to_string(pair.value_int());
break;
case KeyValuePair::ValueCase::kValueBool:
flattened += std::to_string(pair.value_bool());
break;
case KeyValuePair::ValueCase::kValueFloat:
flattened += std::to_string(pair.value_float());
break;
default:
break;
}
flattened += "|";
}
return flattened;
}
} // namespace statsd
} // namespace os
} // namespace android
|