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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
* Copyright (C) 2019 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 "include/stats_event.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "stats_buffer_writer.h"
#define LOGGER_ENTRY_MAX_PAYLOAD 4068
// Max payload size is 4 bytes less as 4 bytes are reserved for stats_eventTag.
// See android_util_Stats_Log.cpp
#define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - 4)
/* POSITIONS */
#define POS_NUM_ELEMENTS 1
#define POS_TIMESTAMP (POS_NUM_ELEMENTS + sizeof(uint8_t))
#define POS_ATOM_ID (POS_TIMESTAMP + sizeof(uint8_t) + sizeof(uint64_t))
#define POS_FIRST_FIELD (POS_ATOM_ID + sizeof(uint8_t) + sizeof(uint32_t))
/* LIMITS */
#define MAX_ANNOTATION_COUNT 15
#define MAX_BYTE_VALUE 127 // parsing side requires that lengths fit in 7 bits
// The stats_event struct holds the serialized encoding of an event
// within a buf. Also includes other required fields.
struct stats_event {
uint8_t* buf;
size_t lastFieldPos; // location of last field within the buf
size_t size; // number of valid bytes within buffer
uint32_t numElements;
uint32_t atomId;
uint32_t errors;
bool truncate;
bool built;
};
static int64_t get_elapsed_realtime_ns() {
struct timespec t;
t.tv_sec = t.tv_nsec = 0;
clock_gettime(CLOCK_BOOTTIME, &t);
return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec;
}
struct stats_event* stats_event_obtain() {
struct stats_event* event = malloc(sizeof(struct stats_event));
event->buf = (uint8_t*)calloc(MAX_EVENT_PAYLOAD, 1);
event->buf[0] = OBJECT_TYPE;
event->atomId = 0;
event->errors = 0;
event->truncate = true; // truncate for both pulled and pushed atoms
event->built = false;
// place the timestamp
uint64_t timestampNs = get_elapsed_realtime_ns();
event->buf[POS_TIMESTAMP] = INT64_TYPE;
memcpy(&event->buf[POS_TIMESTAMP + sizeof(uint8_t)], ×tampNs, sizeof(timestampNs));
event->numElements = 1;
event->lastFieldPos = 0; // 0 since we haven't written a field yet
event->size = POS_FIRST_FIELD;
return event;
}
void stats_event_release(struct stats_event* event) {
free(event->buf);
free(event);
}
void stats_event_set_atom_id(struct stats_event* event, uint32_t atomId) {
event->atomId = atomId;
event->buf[POS_ATOM_ID] = INT32_TYPE;
memcpy(&event->buf[POS_ATOM_ID + sizeof(uint8_t)], &atomId, sizeof(atomId));
event->numElements++;
}
// Side-effect: modifies event->errors if the buffer would overflow
static bool overflows(struct stats_event* event, size_t size) {
if (event->size + size > MAX_EVENT_PAYLOAD) {
event->errors |= ERROR_OVERFLOW;
return true;
}
return false;
}
// Side-effect: all append functions increment event->size if there is
// sufficient space within the buffer to place the value
static void append_byte(struct stats_event* event, uint8_t value) {
if (!overflows(event, sizeof(value))) {
event->buf[event->size] = value;
event->size += sizeof(value);
}
}
static void append_bool(struct stats_event* event, bool value) {
append_byte(event, (uint8_t)value);
}
static void append_int32(struct stats_event* event, int32_t value) {
if (!overflows(event, sizeof(value))) {
memcpy(&event->buf[event->size], &value, sizeof(value));
event->size += sizeof(value);
}
}
static void append_int64(struct stats_event* event, int64_t value) {
if (!overflows(event, sizeof(value))) {
memcpy(&event->buf[event->size], &value, sizeof(value));
event->size += sizeof(value);
}
}
static void append_float(struct stats_event* event, float value) {
if (!overflows(event, sizeof(value))) {
memcpy(&event->buf[event->size], &value, sizeof(value));
event->size += sizeof(float);
}
}
static void append_byte_array(struct stats_event* event, const uint8_t* buf, size_t size) {
if (!overflows(event, size)) {
memcpy(&event->buf[event->size], buf, size);
event->size += size;
}
}
// Side-effect: modifies event->errors if buf is not properly null-terminated
static void append_string(struct stats_event* event, const char* buf) {
size_t size = strnlen(buf, MAX_EVENT_PAYLOAD);
if (size == MAX_EVENT_PAYLOAD) {
event->errors |= ERROR_STRING_NOT_NULL_TERMINATED;
return;
}
append_int32(event, size);
append_byte_array(event, (uint8_t*)buf, size);
}
static void start_field(struct stats_event* event, uint8_t typeId) {
event->lastFieldPos = event->size;
append_byte(event, typeId);
event->numElements++;
}
void stats_event_write_int32(struct stats_event* event, int32_t value) {
if (event->errors) return;
start_field(event, INT32_TYPE);
append_int32(event, value);
}
void stats_event_write_int64(struct stats_event* event, int64_t value) {
if (event->errors) return;
start_field(event, INT64_TYPE);
append_int64(event, value);
}
void stats_event_write_float(struct stats_event* event, float value) {
if (event->errors) return;
start_field(event, FLOAT_TYPE);
append_float(event, value);
}
void stats_event_write_bool(struct stats_event* event, bool value) {
if (event->errors) return;
start_field(event, BOOL_TYPE);
append_bool(event, value);
}
void stats_event_write_byte_array(struct stats_event* event, const uint8_t* buf, size_t numBytes) {
if (event->errors) return;
start_field(event, BYTE_ARRAY_TYPE);
append_int32(event, numBytes);
append_byte_array(event, buf, numBytes);
}
// Value is assumed to be encoded using UTF8
void stats_event_write_string8(struct stats_event* event, const char* value) {
if (event->errors) return;
start_field(event, STRING_TYPE);
append_string(event, value);
}
// Tags are assumed to be encoded using UTF8
void stats_event_write_attribution_chain(struct stats_event* event, const uint32_t* uids,
const char* const* tags, uint8_t numNodes) {
if (numNodes > MAX_BYTE_VALUE) event->errors |= ERROR_ATTRIBUTION_CHAIN_TOO_LONG;
if (event->errors) return;
start_field(event, ATTRIBUTION_CHAIN_TYPE);
append_byte(event, numNodes);
for (uint8_t i = 0; i < numNodes; i++) {
append_int32(event, uids[i]);
append_string(event, tags[i]);
}
}
void stats_event_write_key_value_pairs(struct stats_event* event, struct key_value_pair* pairs,
uint8_t numPairs) {
if (numPairs > MAX_BYTE_VALUE) event->errors |= ERROR_TOO_MANY_KEY_VALUE_PAIRS;
if (event->errors) return;
start_field(event, KEY_VALUE_PAIRS_TYPE);
append_byte(event, numPairs);
for (uint8_t i = 0; i < numPairs; i++) {
append_int32(event, pairs[i].key);
append_byte(event, pairs[i].valueType);
switch (pairs[i].valueType) {
case INT32_TYPE:
append_int32(event, pairs[i].int32Value);
break;
case INT64_TYPE:
append_int64(event, pairs[i].int64Value);
break;
case FLOAT_TYPE:
append_float(event, pairs[i].floatValue);
break;
case STRING_TYPE:
append_string(event, pairs[i].stringValue);
break;
default:
event->errors |= ERROR_INVALID_VALUE_TYPE;
return;
}
}
}
// Side-effect: modifies event->errors if field has too many annotations
static void increment_annotation_count(struct stats_event* event) {
uint8_t fieldType = event->buf[event->lastFieldPos] & 0x0F;
uint32_t oldAnnotationCount = (event->buf[event->lastFieldPos] & 0xF0) >> 4;
uint32_t newAnnotationCount = oldAnnotationCount + 1;
if (newAnnotationCount > MAX_ANNOTATION_COUNT) {
event->errors |= ERROR_TOO_MANY_ANNOTATIONS;
return;
}
event->buf[event->lastFieldPos] = (((uint8_t)newAnnotationCount << 4) & 0xF0) | fieldType;
}
void stats_event_add_bool_annotation(struct stats_event* event, uint8_t annotationId, bool value) {
if (event->lastFieldPos == 0) event->errors |= ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD;
if (annotationId > MAX_BYTE_VALUE) event->errors |= ERROR_ANNOTATION_ID_TOO_LARGE;
if (event->errors) return;
append_byte(event, annotationId);
append_byte(event, BOOL_TYPE);
append_bool(event, value);
increment_annotation_count(event);
}
void stats_event_add_int32_annotation(struct stats_event* event, uint8_t annotationId,
int32_t value) {
if (event->lastFieldPos == 0) event->errors |= ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD;
if (annotationId > MAX_BYTE_VALUE) event->errors |= ERROR_ANNOTATION_ID_TOO_LARGE;
if (event->errors) return;
append_byte(event, annotationId);
append_byte(event, INT32_TYPE);
append_int32(event, value);
increment_annotation_count(event);
}
uint32_t stats_event_get_atom_id(struct stats_event* event) {
return event->atomId;
}
uint8_t* stats_event_get_buffer(struct stats_event* event, size_t* size) {
if (size) *size = event->size;
return event->buf;
}
uint32_t stats_event_get_errors(struct stats_event* event) {
return event->errors;
}
void stats_event_truncate_buffer(struct stats_event* event, bool truncate) {
event->truncate = truncate;
}
void stats_event_build(struct stats_event* event) {
if (event->built) return;
if (event->atomId == 0) event->errors |= ERROR_NO_ATOM_ID;
if (event->numElements > MAX_BYTE_VALUE) {
event->errors |= ERROR_TOO_MANY_FIELDS;
} else {
event->buf[POS_NUM_ELEMENTS] = event->numElements;
}
// If there are errors, rewrite buffer.
if (event->errors) {
event->buf[POS_NUM_ELEMENTS] = 3;
event->buf[POS_FIRST_FIELD] = ERROR_TYPE;
memcpy(&event->buf[POS_FIRST_FIELD + sizeof(uint8_t)], &event->errors,
sizeof(event->errors));
event->size = POS_FIRST_FIELD + sizeof(uint8_t) + sizeof(uint32_t);
}
// Truncate the buffer to the appropriate length in order to limit our
// memory usage.
if (event->truncate) event->buf = (uint8_t*)realloc(event->buf, event->size);
event->built = true;
}
int stats_event_write(struct stats_event* event) {
stats_event_build(event);
return write_buffer_to_statsd(&event->buf, event->size, event->atomId);
}
struct stats_event_api_table table = {
stats_event_obtain,
stats_event_build,
stats_event_write,
stats_event_release,
stats_event_set_atom_id,
stats_event_write_int32,
stats_event_write_int64,
stats_event_write_float,
stats_event_write_bool,
stats_event_write_byte_array,
stats_event_write_string8,
stats_event_write_attribution_chain,
stats_event_write_key_value_pairs,
stats_event_add_bool_annotation,
stats_event_add_int32_annotation,
stats_event_get_atom_id,
stats_event_get_buffer,
stats_event_get_errors,
};
|