Merge "Add tests for mapIsolatedUidToHostUid methods" into rvc-dev

This commit is contained in:
TreeHugger Robot
2020-04-29 19:14:26 +00:00
committed by Android (Google) Code Review
7 changed files with 493 additions and 168 deletions

View File

@@ -454,9 +454,9 @@ message Atom {
BluetoothActivityInfo bluetooth_activity_info = 10007 [(module) = "framework"]; BluetoothActivityInfo bluetooth_activity_info = 10007 [(module) = "framework"];
ProcessMemoryState process_memory_state = 10013 [(module) = "framework"]; ProcessMemoryState process_memory_state = 10013 [(module) = "framework"];
SystemElapsedRealtime system_elapsed_realtime = 10014 [(module) = "framework"]; SystemElapsedRealtime system_elapsed_realtime = 10014 [(module) = "framework"];
SystemUptime system_uptime = 10015 [(module) = "framework", (module) = "statsdtest"]; SystemUptime system_uptime = 10015 [(module) = "framework"];
CpuActiveTime cpu_active_time = 10016 [(module) = "framework", (module) = "statsdtest"]; CpuActiveTime cpu_active_time = 10016 [(module) = "framework", (module) = "statsdtest"];
CpuClusterTime cpu_cluster_time = 10017 [(module) = "framework", (module) = "statsdtest"]; CpuClusterTime cpu_cluster_time = 10017 [(module) = "framework"];
DiskSpace disk_space = 10018 [deprecated=true, (module) = "statsdtest"]; DiskSpace disk_space = 10018 [deprecated=true, (module) = "statsdtest"];
RemainingBatteryCapacity remaining_battery_capacity = 10019 [(module) = "framework"]; RemainingBatteryCapacity remaining_battery_capacity = 10019 [(module) = "framework"];
FullBatteryCapacity full_battery_capacity = 10020 [(module) = "framework"]; FullBatteryCapacity full_battery_capacity = 10020 [(module) = "framework"];

View File

@@ -1691,6 +1691,111 @@ TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart) {
&buffer); &buffer);
} }
TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogHostUid) {
int hostUid = 20;
int isolatedUid = 30;
uint64_t eventTimeNs = 12355;
int atomId = 89;
int field1 = 90;
int field2 = 28;
sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid});
ConfigKey cfgKey;
StatsdConfig config = MakeConfig(false);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap);
shared_ptr<LogEvent> logEvent = makeUidLogEvent(atomId, eventTimeNs, hostUid, field1, field2);
processor->OnLogEvent(logEvent.get());
const vector<FieldValue>* actualFieldValues = &logEvent->getValues();
EXPECT_EQ(3, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(field1, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(field2, actualFieldValues->at(2).mValue.int_value);
}
TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogIsolatedUid) {
int hostUid = 20;
int isolatedUid = 30;
uint64_t eventTimeNs = 12355;
int atomId = 89;
int field1 = 90;
int field2 = 28;
sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid});
ConfigKey cfgKey;
StatsdConfig config = MakeConfig(false);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap);
shared_ptr<LogEvent> logEvent =
makeUidLogEvent(atomId, eventTimeNs, isolatedUid, field1, field2);
processor->OnLogEvent(logEvent.get());
const vector<FieldValue>* actualFieldValues = &logEvent->getValues();
EXPECT_EQ(3, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(field1, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(field2, actualFieldValues->at(2).mValue.int_value);
}
TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogHostUidAttributionChain) {
int hostUid = 20;
int isolatedUid = 30;
uint64_t eventTimeNs = 12355;
int atomId = 89;
int field1 = 90;
int field2 = 28;
sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid});
ConfigKey cfgKey;
StatsdConfig config = MakeConfig(false);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap);
shared_ptr<LogEvent> logEvent = makeAttributionLogEvent(atomId, eventTimeNs, {hostUid, 200},
{"tag1", "tag2"}, field1, field2);
processor->OnLogEvent(logEvent.get());
const vector<FieldValue>* actualFieldValues = &logEvent->getValues();
EXPECT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(200, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(field1, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(field2, actualFieldValues->at(5).mValue.int_value);
}
TEST(StatsLogProcessorTest_mapIsolatedUidToHostUid, LogIsolatedUidAttributionChain) {
int hostUid = 20;
int isolatedUid = 30;
uint64_t eventTimeNs = 12355;
int atomId = 89;
int field1 = 90;
int field2 = 28;
sp<MockUidMap> mockUidMap = makeMockUidMapForOneHost(hostUid, {isolatedUid});
ConfigKey cfgKey;
StatsdConfig config = MakeConfig(false);
sp<StatsLogProcessor> processor =
CreateStatsLogProcessor(1, 1, config, cfgKey, nullptr, 0, mockUidMap);
shared_ptr<LogEvent> logEvent = makeAttributionLogEvent(atomId, eventTimeNs, {isolatedUid, 200},
{"tag1", "tag2"}, field1, field2);
processor->OnLogEvent(logEvent.get());
const vector<FieldValue>* actualFieldValues = &logEvent->getValues();
EXPECT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(200, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(field1, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(field2, actualFieldValues->at(5).mValue.int_value);
}
#else #else
GTEST_LOG_(INFO) << "This test does nothing.\n"; GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif #endif

View File

@@ -13,13 +13,15 @@
// limitations under the License. // limitations under the License.
#include "src/anomaly/AnomalyTracker.h" #include "src/anomaly/AnomalyTracker.h"
#include "../metrics/metrics_test_helper.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <vector> #include <vector>
#include "tests/statsd_test_util.h"
using namespace testing; using namespace testing;
using android::sp; using android::sp;
using std::set; using std::set;

View File

@@ -21,9 +21,9 @@
#include <vector> #include <vector>
#include "../metrics/metrics_test_helper.h" #include "../metrics/metrics_test_helper.h"
#include "FieldValue.h"
#include "annotations.h" #include "annotations.h"
#include "stats_event.h" #include "stats_event.h"
#include "statslog_statsdtest.h"
#include "tests/statsd_test_util.h" #include "tests/statsd_test_util.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
@@ -33,207 +33,371 @@ namespace os {
namespace statsd { namespace statsd {
using namespace testing; using namespace testing;
using std::make_shared;
using std::shared_ptr; using std::shared_ptr;
using std::vector; using std::vector;
using testing::Contains;
/* /*
* Test merge isolated and host uid * Test merge isolated and host uid
*/ */
namespace { namespace {
int uidAtomTagId = util::CPU_CLUSTER_TIME; const int uidAtomTagId = 100;
const vector<int> uidAdditiveFields = {3}; const vector<int> additiveFields = {3};
int nonUidAtomTagId = util::SYSTEM_UPTIME; const int nonUidAtomTagId = 200;
int timestamp = 1234; const int timestamp = 1234;
int isolatedUid = 30; const int isolatedUid1 = 30;
int isolatedAdditiveData = 31; const int isolatedUid2 = 40;
int isolatedNonAdditiveData = 32; const int isolatedNonAdditiveData = 32;
int hostUid = 20; const int isolatedAdditiveData = 31;
int hostAdditiveData = 21; const int hostUid = 20;
int hostNonAdditiveData = 22; const int hostNonAdditiveData = 22;
const int hostAdditiveData = 21;
const int attributionAtomTagId = 300;
void extractIntoVector(vector<shared_ptr<LogEvent>> events, sp<MockUidMap> makeMockUidMap() {
vector<vector<int>>& ret) { return makeMockUidMapForOneHost(hostUid, {isolatedUid1, isolatedUid2});
ret.clear();
status_t err;
for (const auto& event : events) {
vector<int> vec;
vec.push_back(event->GetInt(1, &err));
vec.push_back(event->GetInt(2, &err));
vec.push_back(event->GetInt(3, &err));
ret.push_back(vec);
}
}
std::shared_ptr<LogEvent> makeUidLogEvent(uint64_t timestampNs, int uid, int data1, int data2) {
AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, uidAtomTagId);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, uid);
AStatsEvent_addBoolAnnotation(statsEvent, ANNOTATION_ID_IS_UID, true);
AStatsEvent_writeInt32(statsEvent, data1);
AStatsEvent_writeInt32(statsEvent, data2);
std::shared_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
parseStatsEventToLogEvent(statsEvent, logEvent.get());
return logEvent;
}
std::shared_ptr<LogEvent> makeNonUidAtomLogEvent(uint64_t timestampNs, int data1) {
AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, nonUidAtomTagId);
AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
AStatsEvent_writeInt32(statsEvent, data1);
std::shared_ptr<LogEvent> logEvent = std::make_unique<LogEvent>(/*uid=*/0, /*pid=*/0);
parseStatsEventToLogEvent(statsEvent, logEvent.get());
return logEvent;
} }
} // anonymous namespace } // anonymous namespace
TEST(puller_util, MergeNoDimension) { TEST(PullerUtilTest, MergeNoDimension) {
vector<shared_ptr<LogEvent>> inputData; vector<shared_ptr<LogEvent>> data = {
// 30->22->31 // 30->22->31
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, isolatedUid1, hostNonAdditiveData,
makeUidLogEvent(timestamp, isolatedUid, hostNonAdditiveData, isolatedAdditiveData)); isolatedAdditiveData),
// 20->22->21 // 20->22->21
inputData.push_back(makeUidLogEvent(timestamp, hostUid, hostNonAdditiveData, hostAdditiveData)); makeUidLogEvent(uidAtomTagId, timestamp, hostUid, hostNonAdditiveData,
hostAdditiveData),
};
sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>(); sp<MockUidMap> uidMap = makeMockUidMap();
EXPECT_CALL(*uidMap, getHostUidOrSelf(isolatedUid)).WillRepeatedly(Return(hostUid)); mapAndMergeIsolatedUidsToHostUid(data, uidMap, uidAtomTagId, additiveFields);
EXPECT_CALL(*uidMap, getHostUidOrSelf(Ne(isolatedUid))).WillRepeatedly(ReturnArg<0>());
mapAndMergeIsolatedUidsToHostUid(inputData, uidMap, uidAtomTagId, uidAdditiveFields);
vector<vector<int>> actual; ASSERT_EQ(1, (int)data.size());
extractIntoVector(inputData, actual); const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
vector<int> expectedV1 = {20, 22, 52}; ASSERT_EQ(3, actualFieldValues->size());
EXPECT_EQ(1, (int)actual.size()); EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_THAT(actual, Contains(expectedV1)); EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData + hostAdditiveData, actualFieldValues->at(2).mValue.int_value);
} }
TEST(puller_util, MergeWithDimension) { TEST(PullerUtilTest, MergeWithDimension) {
vector<shared_ptr<LogEvent>> inputData; vector<shared_ptr<LogEvent>> data = {
// 30->32->31 // 30->32->31
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, isolatedUid1, isolatedNonAdditiveData,
makeUidLogEvent(timestamp, isolatedUid, isolatedNonAdditiveData, isolatedAdditiveData)); isolatedAdditiveData),
// 20->32->21 // 20->32->21
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, hostUid, isolatedNonAdditiveData,
makeUidLogEvent(timestamp, hostUid, isolatedNonAdditiveData, hostAdditiveData)); hostAdditiveData),
// 20->22->21 // 20->22->21
inputData.push_back(makeUidLogEvent(timestamp, hostUid, hostNonAdditiveData, hostAdditiveData)); makeUidLogEvent(uidAtomTagId, timestamp, hostUid, hostNonAdditiveData,
hostAdditiveData),
};
sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>(); sp<MockUidMap> uidMap = makeMockUidMap();
EXPECT_CALL(*uidMap, getHostUidOrSelf(isolatedUid)).WillRepeatedly(Return(hostUid)); mapAndMergeIsolatedUidsToHostUid(data, uidMap, uidAtomTagId, additiveFields);
EXPECT_CALL(*uidMap, getHostUidOrSelf(Ne(isolatedUid))).WillRepeatedly(ReturnArg<0>());
mapAndMergeIsolatedUidsToHostUid(inputData, uidMap, uidAtomTagId, uidAdditiveFields);
vector<vector<int>> actual; ASSERT_EQ(2, (int)data.size());
extractIntoVector(inputData, actual);
vector<int> expectedV1 = {20, 22, 21}; const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
vector<int> expectedV2 = {20, 32, 52}; ASSERT_EQ(3, actualFieldValues->size());
EXPECT_EQ(2, (int)actual.size()); EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_THAT(actual, Contains(expectedV1)); EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_THAT(actual, Contains(expectedV2)); EXPECT_EQ(hostAdditiveData, actualFieldValues->at(2).mValue.int_value);
actualFieldValues = &data[1]->getValues();
ASSERT_EQ(3, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(hostAdditiveData + isolatedAdditiveData, actualFieldValues->at(2).mValue.int_value);
} }
TEST(puller_util, NoMergeHostUidOnly) { TEST(PullerUtilTest, NoMergeHostUidOnly) {
vector<shared_ptr<LogEvent>> inputData; vector<shared_ptr<LogEvent>> data = {
// 20->32->31 // 20->32->31
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, hostUid, isolatedNonAdditiveData,
makeUidLogEvent(timestamp, hostUid, isolatedNonAdditiveData, isolatedAdditiveData)); isolatedAdditiveData),
// 20->22->21 // 20->22->21
inputData.push_back(makeUidLogEvent(timestamp, hostUid, hostNonAdditiveData, hostAdditiveData)); makeUidLogEvent(uidAtomTagId, timestamp, hostUid, hostNonAdditiveData,
hostAdditiveData),
};
sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>(); sp<MockUidMap> uidMap = makeMockUidMap();
EXPECT_CALL(*uidMap, getHostUidOrSelf(isolatedUid)).WillRepeatedly(Return(hostUid)); mapAndMergeIsolatedUidsToHostUid(data, uidMap, uidAtomTagId, additiveFields);
EXPECT_CALL(*uidMap, getHostUidOrSelf(Ne(isolatedUid))).WillRepeatedly(ReturnArg<0>());
mapAndMergeIsolatedUidsToHostUid(inputData, uidMap, uidAtomTagId, uidAdditiveFields);
// 20->32->31 ASSERT_EQ(2, (int)data.size());
// 20->22->21
vector<vector<int>> actual; const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
extractIntoVector(inputData, actual); ASSERT_EQ(3, actualFieldValues->size());
vector<int> expectedV1 = {20, 32, 31}; EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
vector<int> expectedV2 = {20, 22, 21}; EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(2, (int)actual.size()); EXPECT_EQ(hostAdditiveData, actualFieldValues->at(2).mValue.int_value);
EXPECT_THAT(actual, Contains(expectedV1));
EXPECT_THAT(actual, Contains(expectedV2)); actualFieldValues = &data[1]->getValues();
ASSERT_EQ(3, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData, actualFieldValues->at(2).mValue.int_value);
} }
TEST(puller_util, IsolatedUidOnly) { TEST(PullerUtilTest, IsolatedUidOnly) {
vector<shared_ptr<LogEvent>> inputData; vector<shared_ptr<LogEvent>> data = {
// 30->32->31 // 30->32->31
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, isolatedUid1, isolatedNonAdditiveData,
makeUidLogEvent(timestamp, hostUid, isolatedNonAdditiveData, isolatedAdditiveData)); isolatedAdditiveData),
// 30->22->21 // 30->22->21
inputData.push_back(makeUidLogEvent(timestamp, hostUid, hostNonAdditiveData, hostAdditiveData)); makeUidLogEvent(uidAtomTagId, timestamp, isolatedUid1, hostNonAdditiveData,
hostAdditiveData),
};
sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>(); sp<MockUidMap> uidMap = makeMockUidMap();
EXPECT_CALL(*uidMap, getHostUidOrSelf(isolatedUid)).WillRepeatedly(Return(hostUid)); mapAndMergeIsolatedUidsToHostUid(data, uidMap, uidAtomTagId, additiveFields);
EXPECT_CALL(*uidMap, getHostUidOrSelf(Ne(isolatedUid))).WillRepeatedly(ReturnArg<0>());
mapAndMergeIsolatedUidsToHostUid(inputData, uidMap, uidAtomTagId, uidAdditiveFields); ASSERT_EQ(2, (int)data.size());
// 20->32->31 // 20->32->31
const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
ASSERT_EQ(3, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(hostAdditiveData, actualFieldValues->at(2).mValue.int_value);
// 20->22->21 // 20->22->21
vector<vector<int>> actual; actualFieldValues = &data[1]->getValues();
extractIntoVector(inputData, actual); ASSERT_EQ(3, actualFieldValues->size());
vector<int> expectedV1 = {20, 32, 31}; EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
vector<int> expectedV2 = {20, 22, 21}; EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(2, (int)actual.size()); EXPECT_EQ(isolatedAdditiveData, actualFieldValues->at(2).mValue.int_value);
EXPECT_THAT(actual, Contains(expectedV1));
EXPECT_THAT(actual, Contains(expectedV2));
} }
TEST(puller_util, MultipleIsolatedUidToOneHostUid) { TEST(PullerUtilTest, MultipleIsolatedUidToOneHostUid) {
vector<shared_ptr<LogEvent>> inputData; vector<shared_ptr<LogEvent>> data = {
// 30->32->31 // 30->32->31
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, isolatedUid1, isolatedNonAdditiveData,
makeUidLogEvent(timestamp, isolatedUid, isolatedNonAdditiveData, isolatedAdditiveData)); isolatedAdditiveData),
// 31->32->21 // 31->32->21
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, isolatedUid2, isolatedNonAdditiveData,
makeUidLogEvent(timestamp, isolatedUid + 1, isolatedNonAdditiveData, hostAdditiveData)); hostAdditiveData),
// 20->32->21 // 20->32->21
inputData.push_back( makeUidLogEvent(uidAtomTagId, timestamp, hostUid, isolatedNonAdditiveData,
makeUidLogEvent(timestamp, hostUid, isolatedNonAdditiveData, hostAdditiveData)); hostAdditiveData),
};
sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>(); sp<MockUidMap> uidMap = makeMockUidMap();
EXPECT_CALL(*uidMap, getHostUidOrSelf(_)).WillRepeatedly(Return(hostUid)); mapAndMergeIsolatedUidsToHostUid(data, uidMap, uidAtomTagId, additiveFields);
mapAndMergeIsolatedUidsToHostUid(inputData, uidMap, uidAtomTagId, uidAdditiveFields);
vector<vector<int>> actual; ASSERT_EQ(1, (int)data.size());
extractIntoVector(inputData, actual);
vector<int> expectedV1 = {20, 32, 73}; const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
EXPECT_EQ(1, (int)actual.size()); ASSERT_EQ(3, actualFieldValues->size());
EXPECT_THAT(actual, Contains(expectedV1)); EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(1).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData + hostAdditiveData + hostAdditiveData,
actualFieldValues->at(2).mValue.int_value);
} }
TEST(puller_util, NoNeedToMerge) { TEST(PullerUtilTest, NoNeedToMerge) {
vector<shared_ptr<LogEvent>> inputData; vector<shared_ptr<LogEvent>> data = {
// 32->31
CreateTwoValueLogEvent(nonUidAtomTagId, timestamp, isolatedNonAdditiveData,
isolatedAdditiveData),
// 32 // 22->21
inputData.push_back(makeNonUidAtomLogEvent(timestamp, isolatedNonAdditiveData)); CreateTwoValueLogEvent(nonUidAtomTagId, timestamp, hostNonAdditiveData,
hostAdditiveData),
// 22 };
inputData.push_back(makeNonUidAtomLogEvent(timestamp, hostNonAdditiveData));
sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>(); sp<MockUidMap> uidMap = makeMockUidMap();
mapAndMergeIsolatedUidsToHostUid(inputData, uidMap, nonUidAtomTagId, {} /*no additive fields*/); mapAndMergeIsolatedUidsToHostUid(data, uidMap, nonUidAtomTagId, {} /*no additive fields*/);
EXPECT_EQ(2, (int)inputData.size()); ASSERT_EQ(2, (int)data.size());
const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
ASSERT_EQ(2, actualFieldValues->size());
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData, actualFieldValues->at(1).mValue.int_value);
actualFieldValues = &data[1]->getValues();
ASSERT_EQ(2, actualFieldValues->size());
EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ(hostAdditiveData, actualFieldValues->at(1).mValue.int_value);
}
TEST(PullerUtilTest, MergeNoDimensionAttributionChain) {
vector<shared_ptr<LogEvent>> data = {
// 30->tag1->400->tag2->22->31
makeAttributionLogEvent(attributionAtomTagId, timestamp, {isolatedUid1, 400},
{"tag1", "tag2"}, hostNonAdditiveData, isolatedAdditiveData),
// 20->tag1->400->tag2->22->21
makeAttributionLogEvent(attributionAtomTagId, timestamp, {hostUid, 400},
{"tag1", "tag2"}, hostNonAdditiveData, hostAdditiveData),
};
sp<MockUidMap> uidMap = makeMockUidMap();
mapAndMergeIsolatedUidsToHostUid(data, uidMap, attributionAtomTagId, additiveFields);
ASSERT_EQ(1, (int)data.size());
const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(400, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData + hostAdditiveData, actualFieldValues->at(5).mValue.int_value);
}
TEST(PullerUtilTest, MergeWithDimensionAttributionChain) {
vector<shared_ptr<LogEvent>> data = {
// 200->tag1->30->tag2->32->31
makeAttributionLogEvent(attributionAtomTagId, timestamp, {200, isolatedUid1},
{"tag1", "tag2"}, isolatedNonAdditiveData,
isolatedAdditiveData),
// 200->tag1->20->tag2->32->21
makeAttributionLogEvent(attributionAtomTagId, timestamp, {200, hostUid},
{"tag1", "tag2"}, isolatedNonAdditiveData, hostAdditiveData),
// 200->tag1->20->tag2->22->21
makeAttributionLogEvent(attributionAtomTagId, timestamp, {200, hostUid},
{"tag1", "tag2"}, hostNonAdditiveData, hostAdditiveData),
};
sp<MockUidMap> uidMap = makeMockUidMap();
mapAndMergeIsolatedUidsToHostUid(data, uidMap, attributionAtomTagId, additiveFields);
ASSERT_EQ(2, (int)data.size());
const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(200, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(hostUid, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(hostAdditiveData, actualFieldValues->at(5).mValue.int_value);
actualFieldValues = &data[1]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(200, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(hostUid, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(hostAdditiveData + isolatedAdditiveData, actualFieldValues->at(5).mValue.int_value);
}
TEST(PullerUtilTest, NoMergeHostUidOnlyAttributionChain) {
vector<shared_ptr<LogEvent>> data = {
// 20->tag1->400->tag2->32->31
makeAttributionLogEvent(attributionAtomTagId, timestamp, {hostUid, 400},
{"tag1", "tag2"}, isolatedNonAdditiveData,
isolatedAdditiveData),
// 20->tag1->400->tag2->22->21
makeAttributionLogEvent(attributionAtomTagId, timestamp, {hostUid, 400},
{"tag1", "tag2"}, hostNonAdditiveData, hostAdditiveData),
};
sp<MockUidMap> uidMap = makeMockUidMap();
mapAndMergeIsolatedUidsToHostUid(data, uidMap, attributionAtomTagId, additiveFields);
ASSERT_EQ(2, (int)data.size());
const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(400, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(hostAdditiveData, actualFieldValues->at(5).mValue.int_value);
actualFieldValues = &data[1]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(400, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData, actualFieldValues->at(5).mValue.int_value);
}
TEST(PullerUtilTest, IsolatedUidOnlyAttributionChain) {
vector<shared_ptr<LogEvent>> data = {
// 30->tag1->400->tag2->32->31
makeAttributionLogEvent(attributionAtomTagId, timestamp, {isolatedUid1, 400},
{"tag1", "tag2"}, isolatedNonAdditiveData,
isolatedAdditiveData),
// 30->tag1->400->tag2->22->21
makeAttributionLogEvent(attributionAtomTagId, timestamp, {isolatedUid1, 400},
{"tag1", "tag2"}, hostNonAdditiveData, hostAdditiveData),
};
sp<MockUidMap> uidMap = makeMockUidMap();
mapAndMergeIsolatedUidsToHostUid(data, uidMap, attributionAtomTagId, additiveFields);
ASSERT_EQ(2, (int)data.size());
// 20->tag1->400->tag2->32->31
const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(400, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(hostNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(hostAdditiveData, actualFieldValues->at(5).mValue.int_value);
// 20->tag1->400->tag2->22->21
actualFieldValues = &data[1]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(400, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData, actualFieldValues->at(5).mValue.int_value);
}
TEST(PullerUtilTest, MultipleIsolatedUidToOneHostUidAttributionChain) {
vector<shared_ptr<LogEvent>> data = {
// 30->tag1->400->tag2->32->31
makeAttributionLogEvent(attributionAtomTagId, timestamp, {isolatedUid1, 400},
{"tag1", "tag2"}, isolatedNonAdditiveData,
isolatedAdditiveData),
// 31->tag1->400->tag2->32->21
makeAttributionLogEvent(attributionAtomTagId, timestamp, {isolatedUid2, 400},
{"tag1", "tag2"}, isolatedNonAdditiveData, hostAdditiveData),
// 20->tag1->400->tag2->32->21
makeAttributionLogEvent(attributionAtomTagId, timestamp, {hostUid, 400},
{"tag1", "tag2"}, isolatedNonAdditiveData, hostAdditiveData),
};
sp<MockUidMap> uidMap = makeMockUidMap();
mapAndMergeIsolatedUidsToHostUid(data, uidMap, attributionAtomTagId, additiveFields);
ASSERT_EQ(1, (int)data.size());
const vector<FieldValue>* actualFieldValues = &data[0]->getValues();
ASSERT_EQ(6, actualFieldValues->size());
EXPECT_EQ(hostUid, actualFieldValues->at(0).mValue.int_value);
EXPECT_EQ("tag1", actualFieldValues->at(1).mValue.str_value);
EXPECT_EQ(400, actualFieldValues->at(2).mValue.int_value);
EXPECT_EQ("tag2", actualFieldValues->at(3).mValue.str_value);
EXPECT_EQ(isolatedNonAdditiveData, actualFieldValues->at(4).mValue.int_value);
EXPECT_EQ(isolatedAdditiveData + hostAdditiveData + hostAdditiveData,
actualFieldValues->at(5).mValue.int_value);
} }
} // namespace statsd } // namespace statsd

View File

@@ -48,12 +48,6 @@ public:
void(const ConfigKey& configKey, wp<PullUidProvider> provider)); void(const ConfigKey& configKey, wp<PullUidProvider> provider));
}; };
class MockUidMap : public UidMap {
public:
MOCK_CONST_METHOD1(getHostUidOrSelf, int(int uid));
MOCK_CONST_METHOD1(getAppUid, std::set<int32_t>(const string& package));
};
HashableDimensionKey getMockedDimensionKey(int tagId, int key, std::string value); HashableDimensionKey getMockedDimensionKey(int tagId, int key, std::string value);
MetricDimensionKey getMockedMetricDimensionKey(int tagId, int key, std::string value); MetricDimensionKey getMockedMetricDimensionKey(int tagId, int key, std::string value);

View File

@@ -563,6 +563,48 @@ shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs) {
return logEvent; return logEvent;
} }
shared_ptr<LogEvent> makeUidLogEvent(int atomId, int64_t eventTimeNs, int uid, int data1,
int data2) {
AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
AStatsEvent_writeInt32(statsEvent, uid);
AStatsEvent_addBoolAnnotation(statsEvent, ANNOTATION_ID_IS_UID, true);
AStatsEvent_writeInt32(statsEvent, data1);
AStatsEvent_writeInt32(statsEvent, data2);
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
parseStatsEventToLogEvent(statsEvent, logEvent.get());
return logEvent;
}
shared_ptr<LogEvent> makeAttributionLogEvent(int atomId, int64_t eventTimeNs,
const vector<int>& uids, const vector<string>& tags,
int data1, int data2) {
AStatsEvent* statsEvent = AStatsEvent_obtain();
AStatsEvent_setAtomId(statsEvent, atomId);
AStatsEvent_overwriteTimestamp(statsEvent, eventTimeNs);
writeAttribution(statsEvent, uids, tags);
AStatsEvent_writeInt32(statsEvent, data1);
AStatsEvent_writeInt32(statsEvent, data2);
shared_ptr<LogEvent> logEvent = std::make_shared<LogEvent>(/*uid=*/0, /*pid=*/0);
parseStatsEventToLogEvent(statsEvent, logEvent.get());
return logEvent;
}
sp<MockUidMap> makeMockUidMapForOneHost(int hostUid, const vector<int>& isolatedUids) {
sp<MockUidMap> uidMap = new NaggyMock<MockUidMap>();
EXPECT_CALL(*uidMap, getHostUidOrSelf(_)).WillRepeatedly(ReturnArg<0>());
for (const int isolatedUid : isolatedUids) {
EXPECT_CALL(*uidMap, getHostUidOrSelf(isolatedUid)).WillRepeatedly(Return(hostUid));
}
return uidMap;
}
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();
@@ -878,8 +920,7 @@ std::unique_ptr<LogEvent> CreateOverlayStateChangedEvent(int64_t timestampNs, co
sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs,
const StatsdConfig& config, const ConfigKey& key, const StatsdConfig& config, const ConfigKey& key,
const shared_ptr<IPullAtomCallback>& puller, const shared_ptr<IPullAtomCallback>& puller,
const int32_t atomTag) { const int32_t atomTag, const sp<UidMap> uidMap) {
sp<UidMap> uidMap = new UidMap();
sp<StatsPullerManager> pullerManager = new StatsPullerManager(); sp<StatsPullerManager> pullerManager = new StatsPullerManager();
if (puller != nullptr) { if (puller != nullptr) {
pullerManager->RegisterPullAtomCallback(/*uid=*/0, atomTag, NS_PER_SEC, NS_PER_SEC * 10, {}, pullerManager->RegisterPullAtomCallback(/*uid=*/0, atomTag, NS_PER_SEC, NS_PER_SEC * 10, {},

View File

@@ -17,6 +17,7 @@
#include <aidl/android/os/BnPullAtomCallback.h> #include <aidl/android/os/BnPullAtomCallback.h>
#include <aidl/android/os/IPullAtomCallback.h> #include <aidl/android/os/IPullAtomCallback.h>
#include <aidl/android/os/IPullAtomResultReceiver.h> #include <aidl/android/os/IPullAtomResultReceiver.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" #include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
@@ -24,6 +25,7 @@
#include "src/StatsLogProcessor.h" #include "src/StatsLogProcessor.h"
#include "src/hash.h" #include "src/hash.h"
#include "src/logd/LogEvent.h" #include "src/logd/LogEvent.h"
#include "src/packages/UidMap.h"
#include "src/stats_log_util.h" #include "src/stats_log_util.h"
#include "stats_event.h" #include "stats_event.h"
#include "statslog_statsdtest.h" #include "statslog_statsdtest.h"
@@ -32,6 +34,7 @@ namespace android {
namespace os { namespace os {
namespace statsd { namespace statsd {
using namespace testing;
using ::aidl::android::os::BnPullAtomCallback; using ::aidl::android::os::BnPullAtomCallback;
using ::aidl::android::os::IPullAtomCallback; using ::aidl::android::os::IPullAtomCallback;
using ::aidl::android::os::IPullAtomResultReceiver; using ::aidl::android::os::IPullAtomResultReceiver;
@@ -44,6 +47,12 @@ const int UID_PROCESS_STATE_ATOM_ID = util::UID_PROCESS_STATE_CHANGED;
enum BucketSplitEvent { APP_UPGRADE, BOOT_COMPLETE }; enum BucketSplitEvent { APP_UPGRADE, BOOT_COMPLETE };
class MockUidMap : public UidMap {
public:
MOCK_METHOD(int, getHostUidOrSelf, (int uid), (const));
MOCK_METHOD(std::set<int32_t>, getAppUid, (const string& package), (const));
};
// Converts a ProtoOutputStream to a StatsLogReport proto. // Converts a ProtoOutputStream to a StatsLogReport proto.
StatsLogReport outputStreamToProto(ProtoOutputStream* proto); StatsLogReport outputStreamToProto(ProtoOutputStream* proto);
@@ -212,6 +221,15 @@ std::shared_ptr<LogEvent> CreateNoValuesLogEvent(int atomId, int64_t eventTimeNs
void CreateNoValuesLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs); void CreateNoValuesLogEvent(LogEvent* logEvent, int atomId, int64_t eventTimeNs);
std::shared_ptr<LogEvent> makeUidLogEvent(int atomId, int64_t eventTimeNs, int uid, int data1,
int data2);
std::shared_ptr<LogEvent> makeAttributionLogEvent(int atomId, int64_t eventTimeNs,
const vector<int>& uids,
const vector<string>& tags, int data1, int data2);
sp<MockUidMap> makeMockUidMapForOneHost(int hostUid, const vector<int>& isolatedUids);
// 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);
@@ -293,7 +311,8 @@ std::unique_ptr<LogEvent> CreateOverlayStateChangedEvent(int64_t timestampNs, co
sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs, sp<StatsLogProcessor> CreateStatsLogProcessor(const int64_t timeBaseNs, const int64_t currentTimeNs,
const StatsdConfig& config, const ConfigKey& key, const StatsdConfig& config, const ConfigKey& key,
const shared_ptr<IPullAtomCallback>& puller = nullptr, const shared_ptr<IPullAtomCallback>& puller = nullptr,
const int32_t atomTag = 0 /*for puller only*/); const int32_t atomTag = 0 /*for puller only*/,
const sp<UidMap> = new UidMap());
// Util function to sort the log events by timestamp. // Util function to sort the log events by timestamp.
void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events); void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events);