Merge "statsd unit test and benchmark refactor" into rvc-dev

This commit is contained in:
Christine Tsai
2020-04-08 23:09:17 +00:00
committed by Android (Google) Code Review
20 changed files with 156 additions and 452 deletions

View File

@@ -14,12 +14,14 @@
* limitations under the License. * limitations under the License.
*/ */
#include <vector> #include <vector>
#include "benchmark/benchmark.h"
#include "FieldValue.h" #include "FieldValue.h"
#include "HashableDimensionKey.h" #include "HashableDimensionKey.h"
#include "benchmark/benchmark.h"
#include "logd/LogEvent.h" #include "logd/LogEvent.h"
#include "stats_log_util.h" #include "metric_util.h"
#include "stats_event.h" #include "stats_event.h"
#include "stats_log_util.h"
namespace android { namespace android {
namespace os { namespace os {
@@ -34,24 +36,13 @@ static void createLogEventAndMatcher(LogEvent* event, FieldMatcher* field_matche
std::vector<int> attributionUids = {100, 100}; std::vector<int> attributionUids = {100, 100};
std::vector<string> attributionTags = {"LOCATION", "LOCATION"}; std::vector<string> attributionTags = {"LOCATION", "LOCATION"};
writeAttribution(statsEvent, attributionUids, attributionTags);
vector<const char*> cTags(attributionTags.size());
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeFloat(statsEvent, 3.2f); AStatsEvent_writeFloat(statsEvent, 3.2f);
AStatsEvent_writeString(statsEvent, "LOCATION"); AStatsEvent_writeString(statsEvent, "LOCATION");
AStatsEvent_writeInt64(statsEvent, 990); AStatsEvent_writeInt64(statsEvent, 990);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, event);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
event->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
field_matcher->set_field(1); field_matcher->set_field(1);
auto child = field_matcher->add_child(); auto child = field_matcher->add_child();

View File

@@ -14,12 +14,14 @@
* limitations under the License. * limitations under the License.
*/ */
#include <vector> #include <vector>
#include "benchmark/benchmark.h"
#include "FieldValue.h" #include "FieldValue.h"
#include "HashableDimensionKey.h" #include "HashableDimensionKey.h"
#include "benchmark/benchmark.h"
#include "logd/LogEvent.h" #include "logd/LogEvent.h"
#include "stats_log_util.h" #include "metric_util.h"
#include "stats_event.h" #include "stats_event.h"
#include "stats_log_util.h"
namespace android { namespace android {
namespace os { namespace os {
@@ -34,24 +36,13 @@ static void createLogEventAndLink(LogEvent* event, Metric2Condition *link) {
std::vector<int> attributionUids = {100, 100}; std::vector<int> attributionUids = {100, 100};
std::vector<string> attributionTags = {"LOCATION", "LOCATION"}; std::vector<string> attributionTags = {"LOCATION", "LOCATION"};
writeAttribution(statsEvent, attributionUids, attributionTags);
vector<const char*> cTags(attributionTags.size());
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeFloat(statsEvent, 3.2f); AStatsEvent_writeFloat(statsEvent, 3.2f);
AStatsEvent_writeString(statsEvent, "LOCATION"); AStatsEvent_writeString(statsEvent, "LOCATION");
AStatsEvent_writeInt64(statsEvent, 990); AStatsEvent_writeInt64(statsEvent, 990);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, event);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
event->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
link->conditionId = 1; link->conditionId = 1;

View File

@@ -247,21 +247,37 @@ FieldMatcher CreateDimensions(const int atomId, const std::vector<int>& fields)
return dimensions; return dimensions;
} }
void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
const vector<string>& attributionTags) {
vector<const char*> cTags(attributionTags.size());
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
}
void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent) {
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
}
std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
uint64_t timestampNs, const android::view::DisplayStateEnum state) { uint64_t timestampNs, const android::view::DisplayStateEnum state) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -272,24 +288,12 @@ std::unique_ptr<LogEvent> CreateScheduledJobStateChangedEvent(
AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeString(statsEvent, jobName.c_str()); AStatsEvent_writeString(statsEvent, jobName.c_str());
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -319,24 +323,12 @@ std::unique_ptr<LogEvent> CreateSyncStateChangedEvent(uint64_t timestampNs,
AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeString(statsEvent, name.c_str()); AStatsEvent_writeString(statsEvent, name.c_str());
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }

View File

@@ -18,6 +18,7 @@
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
#include "src/StatsLogProcessor.h" #include "src/StatsLogProcessor.h"
#include "src/logd/LogEvent.h" #include "src/logd/LogEvent.h"
#include "stats_event.h"
#include "statslog.h" #include "statslog.h"
namespace android { namespace android {
@@ -92,6 +93,11 @@ FieldMatcher CreateAttributionUidAndTagDimensions(const int atomId,
FieldMatcher CreateAttributionUidDimensions(const int atomId, FieldMatcher CreateAttributionUidDimensions(const int atomId,
const std::vector<Position>& positions); const std::vector<Position>& positions);
void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
const vector<string>& attributionTags);
void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent);
// Create log event for screen state changed. // Create log event for screen state changed.
std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
uint64_t timestampNs, const android::view::DisplayStateEnum state); uint64_t timestampNs, const android::view::DisplayStateEnum state);

View File

@@ -41,22 +41,10 @@ void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timest
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestamp); AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeString(statsEvent, name.c_str()); AStatsEvent_writeString(statsEvent, name.c_str());
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
@@ -66,22 +54,10 @@ void makeLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timest
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestamp); AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeInt32(statsEvent, value); AStatsEvent_writeInt32(statsEvent, value);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
} // anonymous namespace } // anonymous namespace

View File

@@ -48,15 +48,9 @@ void makeIntLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t tim
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestamp); AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
AStatsEvent_writeInt32(statsEvent, value); AStatsEvent_writeInt32(statsEvent, value);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
void makeFloatLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, void makeFloatLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
@@ -64,15 +58,9 @@ void makeFloatLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t t
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestamp); AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
AStatsEvent_writeFloat(statsEvent, floatValue); AStatsEvent_writeFloat(statsEvent, floatValue);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
void makeStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, void makeStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
@@ -80,15 +68,9 @@ void makeStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestamp); AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
AStatsEvent_writeString(statsEvent, name.c_str()); AStatsEvent_writeString(statsEvent, name.c_str());
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
void makeIntStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, void makeIntStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
@@ -99,13 +81,8 @@ void makeIntStringLogEvent(LogEvent* logEvent, const int32_t atomId, const int64
AStatsEvent_writeInt32(statsEvent, value); AStatsEvent_writeInt32(statsEvent, value);
AStatsEvent_writeString(statsEvent, name.c_str()); AStatsEvent_writeString(statsEvent, name.c_str());
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
void makeAttributionLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, void makeAttributionLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
@@ -115,22 +92,10 @@ void makeAttributionLogEvent(LogEvent* logEvent, const int32_t atomId, const int
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestamp); AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeString(statsEvent, name.c_str()); AStatsEvent_writeString(statsEvent, name.c_str());
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
void makeBoolLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp, void makeBoolLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t timestamp,
@@ -141,13 +106,8 @@ void makeBoolLogEvent(LogEvent* logEvent, const int32_t atomId, const int64_t ti
AStatsEvent_writeBool(statsEvent, bool1); AStatsEvent_writeBool(statsEvent, bool1);
AStatsEvent_writeBool(statsEvent, bool2); AStatsEvent_writeBool(statsEvent, bool2);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
} // anonymous namespace } // anonymous namespace

View File

@@ -67,22 +67,12 @@ void makeWakeLockEvent(LogEvent* logEvent, uint32_t atomId, uint64_t timestamp,
AStatsEvent_overwriteTimestamp(statsEvent, timestamp); AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
vector<std::string> tags(uids.size()); // vector of empty strings vector<std::string> tags(uids.size()); // vector of empty strings
vector<const char*> cTags(uids.size()); writeAttribution(statsEvent, uids, tags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = tags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent, reinterpret_cast<const uint32_t*>(uids.data()),
cTags.data(), uids.size());
AStatsEvent_writeString(statsEvent, wl.c_str()); AStatsEvent_writeString(statsEvent, wl.c_str());
AStatsEvent_writeInt32(statsEvent, acquire); AStatsEvent_writeInt32(statsEvent, acquire);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
} // anonymous namespace } // anonymous namespace

View File

@@ -84,14 +84,9 @@ std::unique_ptr<LogEvent> CreateAppStartOccurredEvent(
AStatsEvent_writeString(statsEvent, calling_pkg_name.c_str()); AStatsEvent_writeString(statsEvent, calling_pkg_name.c_str());
AStatsEvent_writeInt32(statsEvent, is_instant_app); AStatsEvent_writeInt32(statsEvent, is_instant_app);
AStatsEvent_writeInt32(statsEvent, activity_start_msec); AStatsEvent_writeInt32(statsEvent, activity_start_msec);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }

View File

@@ -64,16 +64,10 @@ std::unique_ptr<LogEvent> createSimpleEvent(int64_t eventTimeNs, int64_t value)
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, pullTagId); AStatsEvent_setAtomId(statsEvent, pullTagId);
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
AStatsEvent_writeInt64(statsEvent, value); AStatsEvent_writeInt64(statsEvent, value);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }

View File

@@ -23,6 +23,7 @@
#include "../metrics/metrics_test_helper.h" #include "../metrics/metrics_test_helper.h"
#include "stats_event.h" #include "stats_event.h"
#include "statslog_statsdtest.h" #include "statslog_statsdtest.h"
#include "tests/statsd_test_util.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
@@ -71,14 +72,9 @@ std::shared_ptr<LogEvent> makeUidLogEvent(uint64_t timestampNs, int uid, int dat
AStatsEvent_writeInt32(statsEvent, uid); AStatsEvent_writeInt32(statsEvent, uid);
AStatsEvent_writeInt32(statsEvent, data1); AStatsEvent_writeInt32(statsEvent, data1);
AStatsEvent_writeInt32(statsEvent, data2); AStatsEvent_writeInt32(statsEvent, data2);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::shared_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::shared_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -86,16 +82,10 @@ std::shared_ptr<LogEvent> makeNonUidAtomLogEvent(uint64_t timestampNs, int data1
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, nonUidAtomTagId); AStatsEvent_setAtomId(statsEvent, nonUidAtomTagId);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, data1); AStatsEvent_writeInt32(statsEvent, data1);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::shared_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::shared_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }

View File

@@ -21,6 +21,7 @@
#include <thread> #include <thread>
#include "stats_event.h" #include "stats_event.h"
#include "tests/statsd_test_util.h"
namespace android { namespace android {
namespace os { namespace os {
@@ -37,14 +38,9 @@ std::unique_ptr<LogEvent> makeLogEvent(uint64_t timestampNs) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, 10); AStatsEvent_setAtomId(statsEvent, 10);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }

View File

@@ -46,26 +46,17 @@ void makeLogEvent(LogEvent* logEvent, int64_t timestampNs, int atomId) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
void makeLogEvent(LogEvent* logEvent, int64_t timestampNs, int atomId, string uid) { void makeLogEvent(LogEvent* logEvent, int64_t timestampNs, int atomId, string uid) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeString(statsEvent, uid.c_str()); AStatsEvent_writeString(statsEvent, uid.c_str());
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
} // namespace } // namespace

View File

@@ -26,6 +26,7 @@
#include "src/condition/ConditionWizard.h" #include "src/condition/ConditionWizard.h"
#include "src/stats_log_util.h" #include "src/stats_log_util.h"
#include "stats_event.h" #include "stats_event.h"
#include "tests/statsd_test_util.h"
using namespace android::os::statsd; using namespace android::os::statsd;
using namespace testing; using namespace testing;
@@ -48,12 +49,8 @@ void makeLogEvent(LogEvent* logEvent, int64_t timestampNs, int atomId) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
} // namespace } // namespace

View File

@@ -43,14 +43,9 @@ void makeLogEvent(LogEvent* logEvent, int32_t atomId, int64_t timestampNs, strin
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeString(statsEvent, str.c_str()); AStatsEvent_writeString(statsEvent, str.c_str());
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
} // anonymous namespace } // anonymous namespace

View File

@@ -64,14 +64,9 @@ shared_ptr<LogEvent> makeLogEvent(int32_t atomId, int64_t timestampNs, int32_t v
AStatsEvent_writeInt32(statsEvent, value1); AStatsEvent_writeInt32(statsEvent, value1);
AStatsEvent_writeString(statsEvent, str1.c_str()); AStatsEvent_writeString(statsEvent, str1.c_str());
AStatsEvent_writeInt32(statsEvent, value2); AStatsEvent_writeInt32(statsEvent, value2);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
} // anonymous namespace } // anonymous namespace

View File

@@ -611,7 +611,7 @@ TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition) {
vector<shared_ptr<LogEvent>> allData; vector<shared_ptr<LogEvent>> allData;
allData.clear(); allData.clear();
allData.push_back(CreateTwoValueLogEvent(tagId, bucket2StartTimeNs + 1, 1, 110)); allData.push_back(CreateRepeatedValueLogEvent(tagId, bucket2StartTimeNs + 1, 110));
valueProducer->onDataPulled(allData, /** succeed */ true, bucket2StartTimeNs); valueProducer->onDataPulled(allData, /** succeed */ true, bucket2StartTimeNs);
assertPastBucketValuesSingleKey(valueProducer->mPastBuckets, {10}, {bucketSizeNs - 8}); assertPastBucketValuesSingleKey(valueProducer->mPastBuckets, {10}, {bucketSizeNs - 8});
@@ -656,7 +656,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithUpgrade) {
valueProducer.prepareFirstBucket(); valueProducer.prepareFirstBucket();
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size());
@@ -665,14 +665,14 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithUpgrade) {
EXPECT_EQ(bucketStartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs); EXPECT_EQ(bucketStartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 59 * NS_PER_SEC, 1, 10); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 59 * NS_PER_SEC, 10);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event2);
EXPECT_EQ(1UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); EXPECT_EQ(1UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
EXPECT_EQ(bucketStartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs); EXPECT_EQ(bucketStartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs);
// Next value should create a new bucket. // Next value should create a new bucket.
LogEvent event3(/*uid=*/0, /*pid=*/0); LogEvent event3(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event3, tagId, bucketStartTimeNs + 65 * NS_PER_SEC, 1, 10); CreateRepeatedValueLogEvent(&event3, tagId, bucketStartTimeNs + 65 * NS_PER_SEC, 10);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event3);
EXPECT_EQ(2UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); EXPECT_EQ(2UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size());
EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, valueProducer.mCurrentBucketStartTimeNs); EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, valueProducer.mCurrentBucketStartTimeNs);
@@ -812,10 +812,10 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition) {
valueProducer.prepareFirstBucket(); valueProducer.prepareFirstBucket();
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 1, 20); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 20);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
// has one slice // has one slice
@@ -856,7 +856,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithCondition) {
valueProducer.mCondition = ConditionState::kFalse; valueProducer.mCondition = ConditionState::kFalse;
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
// has 1 slice // has 1 slice
EXPECT_EQ(0UL, valueProducer.mCurrentSlicedBucket.size()); EXPECT_EQ(0UL, valueProducer.mCurrentSlicedBucket.size());
@@ -864,7 +864,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithCondition) {
valueProducer.onConditionChangedLocked(true, bucketStartTimeNs + 15); valueProducer.onConditionChangedLocked(true, bucketStartTimeNs + 15);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 1, 20); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 20);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event2);
// has one slice // has one slice
@@ -875,7 +875,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithCondition) {
EXPECT_EQ(20, curInterval.value.long_value); EXPECT_EQ(20, curInterval.value.long_value);
LogEvent event3(/*uid=*/0, /*pid=*/0); LogEvent event3(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event3, tagId, bucketStartTimeNs + 30, 1, 30); CreateRepeatedValueLogEvent(&event3, tagId, bucketStartTimeNs + 30, 30);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event3);
// has one slice // has one slice
@@ -886,7 +886,7 @@ TEST(ValueMetricProducerTest, TestPushedEventsWithCondition) {
valueProducer.onConditionChangedLocked(false, bucketStartTimeNs + 35); valueProducer.onConditionChangedLocked(false, bucketStartTimeNs + 35);
LogEvent event4(/*uid=*/0, /*pid=*/0); LogEvent event4(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event4, tagId, bucketStartTimeNs + 40, 1, 40); CreateRepeatedValueLogEvent(&event4, tagId, bucketStartTimeNs + 40, 40);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event4); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event4);
// has one slice // has one slice
@@ -1195,10 +1195,10 @@ TEST(ValueMetricProducerTest, TestPushedAggregateMin) {
valueProducer.prepareFirstBucket(); valueProducer.prepareFirstBucket();
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 1, 20); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 20);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
// has one slice // has one slice
@@ -1238,10 +1238,10 @@ TEST(ValueMetricProducerTest, TestPushedAggregateMax) {
valueProducer.prepareFirstBucket(); valueProducer.prepareFirstBucket();
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 1, 20); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 20);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
// has one slice // has one slice
@@ -1283,10 +1283,10 @@ TEST(ValueMetricProducerTest, TestPushedAggregateAvg) {
valueProducer.prepareFirstBucket(); valueProducer.prepareFirstBucket();
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 1, 15); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 15);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
// has one slice // has one slice
EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size());
@@ -1331,10 +1331,10 @@ TEST(ValueMetricProducerTest, TestPushedAggregateSum) {
valueProducer.prepareFirstBucket(); valueProducer.prepareFirstBucket();
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 1, 15); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 20, 15);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
// has one slice // has one slice
EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size());
@@ -1374,10 +1374,10 @@ TEST(ValueMetricProducerTest, TestSkipZeroDiffOutput) {
valueProducer.prepareFirstBucket(); valueProducer.prepareFirstBucket();
LogEvent event1(/*uid=*/0, /*pid=*/0); LogEvent event1(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 1, 10); CreateRepeatedValueLogEvent(&event1, tagId, bucketStartTimeNs + 10, 10);
LogEvent event2(/*uid=*/0, /*pid=*/0); LogEvent event2(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event2, tagId, bucketStartTimeNs + 15, 1, 15); CreateRepeatedValueLogEvent(&event2, tagId, bucketStartTimeNs + 15, 15);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event1);
// has one slice // has one slice
EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size());
@@ -1398,7 +1398,7 @@ TEST(ValueMetricProducerTest, TestSkipZeroDiffOutput) {
// no change in data. // no change in data.
LogEvent event3(/*uid=*/0, /*pid=*/0); LogEvent event3(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event3, tagId, bucket2StartTimeNs + 10, 1, 15); CreateRepeatedValueLogEvent(&event3, tagId, bucket2StartTimeNs + 10, 15);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event3);
EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size());
curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0];
@@ -1408,7 +1408,7 @@ TEST(ValueMetricProducerTest, TestSkipZeroDiffOutput) {
EXPECT_EQ(true, curInterval.hasValue); EXPECT_EQ(true, curInterval.hasValue);
LogEvent event4(/*uid=*/0, /*pid=*/0); LogEvent event4(/*uid=*/0, /*pid=*/0);
CreateTwoValueLogEvent(&event4, tagId, bucket2StartTimeNs + 15, 1, 15); CreateRepeatedValueLogEvent(&event4, tagId, bucket2StartTimeNs + 15, 15);
valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event4); valueProducer.onMatchedLogEvent(1 /*log matcher index*/, event4);
EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size());
curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0];
@@ -2166,7 +2166,7 @@ TEST(ValueMetricProducerTest_BucketDrop, TestInvalidBucketWhenInitialPullFailed)
// Bucket start. // Bucket start.
vector<shared_ptr<LogEvent>> allData; vector<shared_ptr<LogEvent>> allData;
allData.clear(); allData.clear();
allData.push_back(CreateTwoValueLogEvent(tagId, bucketStartTimeNs + 1, 1, 110)); allData.push_back(CreateRepeatedValueLogEvent(tagId, bucketStartTimeNs + 1, 110));
valueProducer->onDataPulled(allData, /** succeed */ false, bucketStartTimeNs); valueProducer->onDataPulled(allData, /** succeed */ false, bucketStartTimeNs);
valueProducer->onConditionChanged(false, bucketStartTimeNs + 2); valueProducer->onConditionChanged(false, bucketStartTimeNs + 2);
@@ -2174,7 +2174,7 @@ TEST(ValueMetricProducerTest_BucketDrop, TestInvalidBucketWhenInitialPullFailed)
// Bucket end. // Bucket end.
allData.clear(); allData.clear();
allData.push_back(CreateTwoValueLogEvent(tagId, bucket2StartTimeNs + 1, 1, 140)); allData.push_back(CreateRepeatedValueLogEvent(tagId, bucket2StartTimeNs + 1, 140));
valueProducer->onDataPulled(allData, /** succeed */ true, bucket2StartTimeNs); valueProducer->onDataPulled(allData, /** succeed */ true, bucket2StartTimeNs);
valueProducer->flushIfNeededLocked(bucket2StartTimeNs + 1); valueProducer->flushIfNeededLocked(bucket2StartTimeNs + 1);

View File

@@ -171,13 +171,9 @@ shared_ptr<LogEvent> makeCpuActiveTimeAtom(int32_t uid, int64_t timeMillis) {
AStatsEvent_overwriteTimestamp(statsEvent, 1111L); AStatsEvent_overwriteTimestamp(statsEvent, 1111L);
AStatsEvent_writeInt32(statsEvent, uid); AStatsEvent_writeInt32(statsEvent, uid);
AStatsEvent_writeInt64(statsEvent, timeMillis); AStatsEvent_writeInt64(statsEvent, timeMillis);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); std::shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
return logEvent; return logEvent;
} }

View File

@@ -62,7 +62,7 @@ int getStateInt(StateManager& mgr, int atomId, const HashableDimensionKey& query
// START: build event functions. // START: build event functions.
// Incorrect event - missing fields // Incorrect event - missing fields
std::shared_ptr<LogEvent> buildIncorrectOverlayEvent(int uid, const std::string& packageName, std::unique_ptr<LogEvent> buildIncorrectOverlayEvent(int uid, const std::string& packageName,
int state) { int state) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, util::OVERLAY_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::OVERLAY_STATE_CHANGED);
@@ -72,14 +72,9 @@ std::shared_ptr<LogEvent> buildIncorrectOverlayEvent(int uid, const std::string&
AStatsEvent_writeString(statsEvent, packageName.c_str()); AStatsEvent_writeString(statsEvent, packageName.c_str());
// Missing field 3 - using_alert_window. // Missing field 3 - using_alert_window.
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -93,14 +88,9 @@ std::unique_ptr<LogEvent> buildOverlayEventBadStateType(int uid, const std::stri
AStatsEvent_writeString(statsEvent, packageName.c_str()); AStatsEvent_writeString(statsEvent, packageName.c_str());
AStatsEvent_writeInt32(statsEvent, true); // using_alert_window AStatsEvent_writeInt32(statsEvent, true); // using_alert_window
AStatsEvent_writeString(statsEvent, "string"); // exclusive state: string instead of int AStatsEvent_writeString(statsEvent, "string"); // exclusive state: string instead of int
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
// END: build event functions. // END: build event functions.

View File

@@ -507,23 +507,26 @@ void getPartialWakelockKey(int uid, HashableDimensionKey* key) {
} }
// END: get primary key functions // END: get primary key functions
shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1, void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
int32_t value2) { const vector<string>& attributionTags) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); vector<const char*> cTags(attributionTags.size());
AStatsEvent_setAtomId(statsEvent, atomId); for (int i = 0; i < cTags.size(); i++) {
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeInt32(statsEvent, value1); AStatsEvent_writeAttributionChain(statsEvent,
AStatsEvent_writeInt32(statsEvent, value2); reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
}
void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent) {
AStatsEvent_build(statsEvent); AStatsEvent_build(statsEvent);
size_t size; size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size); uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
return logEvent; AStatsEvent_release(statsEvent);
} }
void CreateTwoValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1, void CreateTwoValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs, int32_t value1,
@@ -534,31 +537,14 @@ void CreateTwoValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs,
AStatsEvent_writeInt32(statsEvent, value1); AStatsEvent_writeInt32(statsEvent, value1);
AStatsEvent_writeInt32(statsEvent, value2); AStatsEvent_writeInt32(statsEvent, value2);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
shared_ptr<LogEvent> CreateThreeValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1, shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1,
int32_t value2, int32_t value3) { int32_t value2) {
AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
AStatsEvent_writeInt32(statsEvent, value1);
AStatsEvent_writeInt32(statsEvent, value2);
AStatsEvent_writeInt32(statsEvent, value3);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); CreateTwoValueLogEvent(logEvent.get(), atomId, eventTimeNs, value1, value2);
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -571,29 +557,14 @@ void CreateThreeValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeN
AStatsEvent_writeInt32(statsEvent, value1); AStatsEvent_writeInt32(statsEvent, value1);
AStatsEvent_writeInt32(statsEvent, value2); AStatsEvent_writeInt32(statsEvent, value2);
AStatsEvent_writeInt32(statsEvent, value3); AStatsEvent_writeInt32(statsEvent, value3);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
shared_ptr<LogEvent> CreateRepeatedValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value) { shared_ptr<LogEvent> CreateThreeValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1,
AStatsEvent* statsEvent = AStatsEvent_obtain(); int32_t value2, int32_t value3) {
AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
AStatsEvent_writeInt32(statsEvent, value);
AStatsEvent_writeInt32(statsEvent, value);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); CreateThreeValueLogEvent(logEvent.get(), atomId, eventTimeNs, value1, value2, value3);
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -605,26 +576,13 @@ void CreateRepeatedValueLogEvent(LogEvent* logEvent, int atomId, int64_t eventTi
AStatsEvent_writeInt32(statsEvent, value); AStatsEvent_writeInt32(statsEvent, value);
AStatsEvent_writeInt32(statsEvent, value); AStatsEvent_writeInt32(statsEvent, value);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent);
} }
shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs) { shared_ptr<LogEvent> CreateRepeatedValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value) {
AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0); shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); CreateRepeatedValueLogEvent(logEvent.get(), atomId, eventTimeNs, value);
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -632,12 +590,14 @@ void CreateNoValuesLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs)
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId); AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs); AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
AStatsEvent_build(statsEvent);
size_t size; parseStatsEventToLogEvent(statsEvent, logEvent);
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size); }
logEvent->parseBuffer(buf, size);
AStatsEvent_release(statsEvent); shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs) {
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
CreateNoValuesLogEvent(logEvent.get(), atomId, eventTimeNs);
return logEvent;
} }
std::unique_ptr<LogEvent> CreateScreenStateChangedEvent( std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
@@ -645,16 +605,10 @@ std::unique_ptr<LogEvent> CreateScreenStateChangedEvent(
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::SCREEN_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -662,16 +616,10 @@ std::unique_ptr<LogEvent> CreateBatterySaverOnEvent(uint64_t timestampNs) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::ON); AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::ON);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -679,16 +627,10 @@ std::unique_ptr<LogEvent> CreateBatterySaverOffEvent(uint64_t timestampNs) {
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::BATTERY_SAVER_MODE_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::OFF); AStatsEvent_writeInt32(statsEvent, BatterySaverModeStateChanged::OFF);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -696,16 +638,10 @@ std::unique_ptr<LogEvent> CreateBatteryStateChangedEvent(const uint64_t timestam
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, util::PLUGGED_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::PLUGGED_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -713,16 +649,10 @@ std::unique_ptr<LogEvent> CreateScreenBrightnessChangedEvent(uint64_t timestampN
AStatsEvent* statsEvent = AStatsEvent_obtain(); AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, util::SCREEN_BRIGHTNESS_CHANGED); AStatsEvent_setAtomId(statsEvent, util::SCREEN_BRIGHTNESS_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, level); AStatsEvent_writeInt32(statsEvent, level);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -733,24 +663,12 @@ std::unique_ptr<LogEvent> CreateScheduledJobStateChangedEvent(
AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::SCHEDULED_JOB_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeString(statsEvent, jobName.c_str()); AStatsEvent_writeString(statsEvent, jobName.c_str());
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -780,25 +698,13 @@ std::unique_ptr<LogEvent> CreateWakelockStateChangedEvent(uint64_t timestampNs,
AStatsEvent_setAtomId(statsEvent, util::WAKELOCK_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::WAKELOCK_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeInt32(statsEvent, android::os::WakeLockLevelEnum::PARTIAL_WAKE_LOCK); AStatsEvent_writeInt32(statsEvent, android::os::WakeLockLevelEnum::PARTIAL_WAKE_LOCK);
AStatsEvent_writeString(statsEvent, wakelockName.c_str()); AStatsEvent_writeString(statsEvent, wakelockName.c_str());
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -828,14 +734,9 @@ std::unique_ptr<LogEvent> CreateActivityForegroundStateChangedEvent(
AStatsEvent_writeString(statsEvent, "pkg_name"); AStatsEvent_writeString(statsEvent, "pkg_name");
AStatsEvent_writeString(statsEvent, "class_name"); AStatsEvent_writeString(statsEvent, "class_name");
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -858,24 +759,12 @@ std::unique_ptr<LogEvent> CreateSyncStateChangedEvent(uint64_t timestampNs,
AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::SYNC_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeString(statsEvent, name.c_str()); AStatsEvent_writeString(statsEvent, name.c_str());
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -904,14 +793,9 @@ std::unique_ptr<LogEvent> CreateProcessLifeCycleStateChangedEvent(
AStatsEvent_writeInt32(statsEvent, uid); AStatsEvent_writeInt32(statsEvent, uid);
AStatsEvent_writeString(statsEvent, ""); AStatsEvent_writeString(statsEvent, "");
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -928,14 +812,9 @@ std::unique_ptr<LogEvent> CreateAppCrashOccurredEvent(uint64_t timestampNs, cons
AStatsEvent_writeInt32(statsEvent, uid); AStatsEvent_writeInt32(statsEvent, uid);
AStatsEvent_writeString(statsEvent, "eventType"); AStatsEvent_writeString(statsEvent, "eventType");
AStatsEvent_writeString(statsEvent, "processName"); AStatsEvent_writeString(statsEvent, "processName");
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -948,14 +827,9 @@ std::unique_ptr<LogEvent> CreateIsolatedUidChangedEvent(uint64_t timestampNs, in
AStatsEvent_writeInt32(statsEvent, hostUid); AStatsEvent_writeInt32(statsEvent, hostUid);
AStatsEvent_writeInt32(statsEvent, isolatedUid); AStatsEvent_writeInt32(statsEvent, isolatedUid);
AStatsEvent_writeInt32(statsEvent, is_create); AStatsEvent_writeInt32(statsEvent, is_create);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -967,14 +841,9 @@ std::unique_ptr<LogEvent> CreateUidProcessStateChangedEvent(
AStatsEvent_writeInt32(statsEvent, uid); AStatsEvent_writeInt32(statsEvent, uid);
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -988,26 +857,14 @@ std::unique_ptr<LogEvent> CreateBleScanStateChangedEvent(uint64_t timestampNs,
AStatsEvent_setAtomId(statsEvent, util::BLE_SCAN_STATE_CHANGED); AStatsEvent_setAtomId(statsEvent, util::BLE_SCAN_STATE_CHANGED);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
vector<const char*> cTags(attributionTags.size()); writeAttribution(statsEvent, attributionUids, attributionTags);
for (int i = 0; i < cTags.size(); i++) {
cTags[i] = attributionTags[i].c_str();
}
AStatsEvent_writeAttributionChain(statsEvent,
reinterpret_cast<const uint32_t*>(attributionUids.data()),
cTags.data(), attributionUids.size());
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_writeInt32(statsEvent, filtered); // filtered AStatsEvent_writeInt32(statsEvent, filtered); // filtered
AStatsEvent_writeInt32(statsEvent, firstMatch); // first match AStatsEvent_writeInt32(statsEvent, firstMatch); // first match
AStatsEvent_writeInt32(statsEvent, opportunistic); // opportunistic AStatsEvent_writeInt32(statsEvent, opportunistic); // opportunistic
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }
@@ -1023,14 +880,9 @@ std::unique_ptr<LogEvent> CreateOverlayStateChangedEvent(int64_t timestampNs, co
AStatsEvent_writeString(statsEvent, packageName.c_str()); AStatsEvent_writeString(statsEvent, packageName.c_str());
AStatsEvent_writeInt32(statsEvent, usingAlertWindow); AStatsEvent_writeInt32(statsEvent, usingAlertWindow);
AStatsEvent_writeInt32(statsEvent, state); AStatsEvent_writeInt32(statsEvent, state);
AStatsEvent_build(statsEvent);
size_t size;
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0); std::unique_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
logEvent->parseBuffer(buf, size); parseStatsEventToLogEvent(statsEvent, logEvent.get());
AStatsEvent_release(statsEvent);
return logEvent; return logEvent;
} }

View File

@@ -25,6 +25,7 @@
#include "src/hash.h" #include "src/hash.h"
#include "src/logd/LogEvent.h" #include "src/logd/LogEvent.h"
#include "src/stats_log_util.h" #include "src/stats_log_util.h"
#include "stats_event.h"
#include "statslog_statsdtest.h" #include "statslog_statsdtest.h"
namespace android { namespace android {
@@ -189,6 +190,12 @@ void getPartialWakelockKey(int uid, const std::string& tag, HashableDimensionKey
void getPartialWakelockKey(int uid, HashableDimensionKey* key); void getPartialWakelockKey(int uid, HashableDimensionKey* key);
// END: get primary key functions // END: get primary key functions
void writeAttribution(AStatsEvent* statsEvent, const vector<int>& attributionUids,
const vector<string>& attributionTags);
// Builds statsEvent to get buffer that is parsed into logEvent then releases statsEvent.
void parseStatsEventToLogEvent(AStatsEvent* statsEvent, LogEvent* logEvent);
shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1, shared_ptr<LogEvent> CreateTwoValueLogEvent(int atomId, int64_t eventTimeNs, int32_t value1,
int32_t value2); int32_t value2);