From 84eb44bb4c607dee58f86188b66078e4c5158f6d Mon Sep 17 00:00:00 2001 From: Ruchir Rastogi Date: Thu, 12 Mar 2020 18:48:48 -0700 Subject: [PATCH] Parse annotations within LogEvent This is a preliminary CL that parses annotations within LogEvent, but does nothing with the annotations provided. Annotation information will be exposed from LogEvent/FieldValue in a future CL. (That CL will also contain tests for parsing annotations.) Test: m statsd Bug: 150414043 Change-Id: Iae79644ea8455280a654076bfd5819927d4183d3 --- cmds/statsd/src/logd/LogEvent.cpp | 87 ++++++++++++++++++++----------- cmds/statsd/src/logd/LogEvent.h | 17 +++--- 2 files changed, 67 insertions(+), 37 deletions(-) diff --git a/cmds/statsd/src/logd/LogEvent.cpp b/cmds/statsd/src/logd/LogEvent.cpp index 258f84d0fb79b..b515d0a5b72f5 100644 --- a/cmds/statsd/src/logd/LogEvent.cpp +++ b/cmds/statsd/src/logd/LogEvent.cpp @@ -350,17 +350,19 @@ bool LogEvent::write(const AttributionNodeInternal& node) { return false; } -void LogEvent::parseInt32(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseInt32(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) { int32_t value = readNextValue(); addToValues(pos, depth, value, last); + parseAnnotations(numAnnotations); } -void LogEvent::parseInt64(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseInt64(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) { int64_t value = readNextValue(); addToValues(pos, depth, value, last); + parseAnnotations(numAnnotations); } -void LogEvent::parseString(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseString(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) { int32_t numBytes = readNextValue(); if ((uint32_t)numBytes > mRemainingLen) { mValid = false; @@ -371,20 +373,23 @@ void LogEvent::parseString(int32_t* pos, int32_t depth, bool* last) { mBuf += numBytes; mRemainingLen -= numBytes; addToValues(pos, depth, value, last); + parseAnnotations(numAnnotations); } -void LogEvent::parseFloat(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseFloat(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) { float value = readNextValue(); addToValues(pos, depth, value, last); + parseAnnotations(numAnnotations); } -void LogEvent::parseBool(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseBool(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) { // cast to int32_t because FieldValue does not support bools int32_t value = (int32_t)readNextValue(); addToValues(pos, depth, value, last); + parseAnnotations(numAnnotations); } -void LogEvent::parseByteArray(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseByteArray(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) { int32_t numBytes = readNextValue(); if ((uint32_t)numBytes > mRemainingLen) { mValid = false; @@ -395,9 +400,10 @@ void LogEvent::parseByteArray(int32_t* pos, int32_t depth, bool* last) { mBuf += numBytes; mRemainingLen -= numBytes; addToValues(pos, depth, value, last); + parseAnnotations(numAnnotations); } -void LogEvent::parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations) { int32_t numPairs = readNextValue(); for (pos[1] = 1; pos[1] <= numPairs; pos[1]++) { @@ -405,56 +411,79 @@ void LogEvent::parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last) { // parse key pos[2] = 1; - parseInt32(pos, 2, last); + parseInt32(pos, /*depth=*/2, last, /*numAnnotations=*/0); // parse value last[2] = true; - uint8_t typeId = getTypeId(readNextValue()); - switch (typeId) { + + uint8_t typeInfo = readNextValue(); + switch (getTypeId(typeInfo)) { case INT32_TYPE: pos[2] = 2; // pos[2] determined by index of type in KeyValuePair in atoms.proto - parseInt32(pos, 2, last); + parseInt32(pos, /*depth=*/2, last, /*numAnnotations=*/0); break; case INT64_TYPE: pos[2] = 3; - parseInt64(pos, 2, last); + parseInt64(pos, /*depth=*/2, last, /*numAnnotations=*/0); break; case STRING_TYPE: pos[2] = 4; - parseString(pos, 2, last); + parseString(pos, /*depth=*/2, last, /*numAnnotations=*/0); break; case FLOAT_TYPE: pos[2] = 5; - parseFloat(pos, 2, last); + parseFloat(pos, /*depth=*/2, last, /*numAnnotations=*/0); break; default: mValid = false; } } + parseAnnotations(numAnnotations); + pos[1] = pos[2] = 1; last[1] = last[2] = false; } -void LogEvent::parseAttributionChain(int32_t* pos, int32_t depth, bool* last) { +void LogEvent::parseAttributionChain(int32_t* pos, int32_t depth, bool* last, + uint8_t numAnnotations) { int32_t numNodes = readNextValue(); for (pos[1] = 1; pos[1] <= numNodes; pos[1]++) { last[1] = (pos[1] == numNodes); // parse uid pos[2] = 1; - parseInt32(pos, 2, last); + parseInt32(pos, /*depth=*/2, last, /*numAnnotations=*/0); // parse tag pos[2] = 2; last[2] = true; - parseString(pos, 2, last); + parseString(pos, /*depth=*/2, last, /*numAnnotations=*/0); } + parseAnnotations(numAnnotations); + pos[1] = pos[2] = 1; last[1] = last[2] = false; } +// TODO(b/151109630): store annotation information within LogEvent +void LogEvent::parseAnnotations(uint8_t numAnnotations) { + for (uint8_t i = 0; i < numAnnotations; i++) { + /*uint8_t annotationId = */ readNextValue(); + uint8_t annotationType = readNextValue(); + switch (annotationType) { + case BOOL_TYPE: + /*bool annotationValue = */ readNextValue(); + break; + case INT32_TYPE: + /*int32_t annotationValue =*/ readNextValue(); + break; + default: + mValid = false; + } + } +} // This parsing logic is tied to the encoding scheme used in StatsEvent.java and // stats_event.c @@ -475,6 +504,7 @@ bool LogEvent::parseBuffer(uint8_t* buf, size_t len) { typeInfo = readNextValue(); if (getTypeId(typeInfo) != INT64_TYPE) mValid = false; mElapsedTimestampNs = readNextValue(); + parseAnnotations(getNumAnnotations(typeInfo)); // atom-level annotations numElements--; typeInfo = readNextValue(); @@ -484,37 +514,36 @@ bool LogEvent::parseBuffer(uint8_t* buf, size_t len) { for (pos[0] = 1; pos[0] <= numElements && mValid; pos[0]++) { + last[0] = (pos[0] == numElements); + typeInfo = readNextValue(); uint8_t typeId = getTypeId(typeInfo); - last[0] = (pos[0] == numElements); - // TODO(b/144373276): handle errors passed to the socket - // TODO(b/144373257): parse annotations switch(typeId) { case BOOL_TYPE: - parseBool(pos, 0, last); + parseBool(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; case INT32_TYPE: - parseInt32(pos, 0, last); + parseInt32(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; case INT64_TYPE: - parseInt64(pos, 0, last); + parseInt64(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; case FLOAT_TYPE: - parseFloat(pos, 0, last); + parseFloat(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; case BYTE_ARRAY_TYPE: - parseByteArray(pos, 0, last); + parseByteArray(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; case STRING_TYPE: - parseString(pos, 0, last); + parseString(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; case KEY_VALUE_PAIRS_TYPE: - parseKeyValuePairs(pos, 0, last); + parseKeyValuePairs(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; case ATTRIBUTION_CHAIN_TYPE: - parseAttributionChain(pos, 0, last); + parseAttributionChain(pos, /*depth=*/0, last, getNumAnnotations(typeInfo)); break; default: mValid = false; @@ -531,7 +560,7 @@ uint8_t LogEvent::getTypeId(uint8_t typeInfo) { } uint8_t LogEvent::getNumAnnotations(uint8_t typeInfo) { - return (typeInfo >> 4) & 0x0F; + return (typeInfo >> 4) & 0x0F; // num annotations in upper 4 bytes } int64_t LogEvent::GetLong(size_t key, status_t* err) const { diff --git a/cmds/statsd/src/logd/LogEvent.h b/cmds/statsd/src/logd/LogEvent.h index b68eeb8d64998..6537f13c4089b 100644 --- a/cmds/statsd/src/logd/LogEvent.h +++ b/cmds/statsd/src/logd/LogEvent.h @@ -232,14 +232,15 @@ private: */ LogEvent(const LogEvent&); - void parseInt32(int32_t* pos, int32_t depth, bool* last); - void parseInt64(int32_t* pos, int32_t depth, bool* last); - void parseString(int32_t* pos, int32_t depth, bool* last); - void parseFloat(int32_t* pos, int32_t depth, bool* last); - void parseBool(int32_t* pos, int32_t depth, bool* last); - void parseByteArray(int32_t* pos, int32_t depth, bool* last); - void parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last); - void parseAttributionChain(int32_t* pos, int32_t depth, bool* last); + void parseInt32(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseInt64(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseString(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseFloat(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseBool(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseByteArray(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseKeyValuePairs(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseAttributionChain(int32_t* pos, int32_t depth, bool* last, uint8_t numAnnotations); + void parseAnnotations(uint8_t numAnnotations); /** * The below three variables are only valid during the execution of