Loading libstats/socket/include/stats_event.h +1 −0 Original line number Diff line number Diff line Loading @@ -152,6 +152,7 @@ void AStatsEvent_addBoolAnnotation(AStatsEvent* event, uint8_t annotationId, boo void AStatsEvent_addInt32Annotation(AStatsEvent* event, uint8_t annotationId, int32_t value); // Internal/test APIs. Should not be exposed outside of the APEX. void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs); uint32_t AStatsEvent_getAtomId(AStatsEvent* event); // Size is an output parameter. uint8_t* AStatsEvent_getBuffer(AStatsEvent* event, size_t* size); Loading libstats/socket/stats_event.c +8 −0 Original line number Diff line number Diff line Loading @@ -115,6 +115,14 @@ void AStatsEvent_setAtomId(AStatsEvent* event, uint32_t atomId) { event->numElements++; } // Overwrites the timestamp populated in AStatsEvent_obtain with a custom // timestamp. Should only be called from test code. void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs) { memcpy(&event->buf[POS_TIMESTAMP + sizeof(uint8_t)], ×tampNs, sizeof(timestampNs)); // Do not increment numElements because we already accounted for the timestamp // within AStatsEvent_obtain. } // Side-effect: modifies event->errors if the buffer would overflow static bool overflows(AStatsEvent* event, size_t size) { if (event->size + size > MAX_EVENT_PAYLOAD) { Loading libstats/socket/tests/stats_event_test.cpp +18 −0 Original line number Diff line number Diff line Loading @@ -318,3 +318,21 @@ TEST(StatsEventTest, TestOverflowError) { AStatsEvent_release(event); } TEST(StatsEventTest, TestOverwriteTimestamp) { uint32_t atomId = 100; int64_t expectedTimestamp = 0x123456789; AStatsEvent* event = AStatsEvent_obtain(); AStatsEvent_setAtomId(event, atomId); AStatsEvent_overwriteTimestamp(event, expectedTimestamp); AStatsEvent_build(event); uint8_t* buffer = AStatsEvent_getBuffer(event, NULL); // Make sure that the timestamp is being overwritten. checkMetadata(&buffer, /*numElements=*/0, /*startTime=*/expectedTimestamp, /*endTime=*/expectedTimestamp, atomId); EXPECT_EQ(AStatsEvent_getErrors(event), 0); AStatsEvent_release(event); } Loading
libstats/socket/include/stats_event.h +1 −0 Original line number Diff line number Diff line Loading @@ -152,6 +152,7 @@ void AStatsEvent_addBoolAnnotation(AStatsEvent* event, uint8_t annotationId, boo void AStatsEvent_addInt32Annotation(AStatsEvent* event, uint8_t annotationId, int32_t value); // Internal/test APIs. Should not be exposed outside of the APEX. void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs); uint32_t AStatsEvent_getAtomId(AStatsEvent* event); // Size is an output parameter. uint8_t* AStatsEvent_getBuffer(AStatsEvent* event, size_t* size); Loading
libstats/socket/stats_event.c +8 −0 Original line number Diff line number Diff line Loading @@ -115,6 +115,14 @@ void AStatsEvent_setAtomId(AStatsEvent* event, uint32_t atomId) { event->numElements++; } // Overwrites the timestamp populated in AStatsEvent_obtain with a custom // timestamp. Should only be called from test code. void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs) { memcpy(&event->buf[POS_TIMESTAMP + sizeof(uint8_t)], ×tampNs, sizeof(timestampNs)); // Do not increment numElements because we already accounted for the timestamp // within AStatsEvent_obtain. } // Side-effect: modifies event->errors if the buffer would overflow static bool overflows(AStatsEvent* event, size_t size) { if (event->size + size > MAX_EVENT_PAYLOAD) { Loading
libstats/socket/tests/stats_event_test.cpp +18 −0 Original line number Diff line number Diff line Loading @@ -318,3 +318,21 @@ TEST(StatsEventTest, TestOverflowError) { AStatsEvent_release(event); } TEST(StatsEventTest, TestOverwriteTimestamp) { uint32_t atomId = 100; int64_t expectedTimestamp = 0x123456789; AStatsEvent* event = AStatsEvent_obtain(); AStatsEvent_setAtomId(event, atomId); AStatsEvent_overwriteTimestamp(event, expectedTimestamp); AStatsEvent_build(event); uint8_t* buffer = AStatsEvent_getBuffer(event, NULL); // Make sure that the timestamp is being overwritten. checkMetadata(&buffer, /*numElements=*/0, /*startTime=*/expectedTimestamp, /*endTime=*/expectedTimestamp, atomId); EXPECT_EQ(AStatsEvent_getErrors(event), 0); AStatsEvent_release(event); }