Use new socket schema within TestSlicedCondition
This CL serves as a prototype for future efforts to move statsd tests to
the new socket schema.
We also significantly refactor the LogEvent class by introducing two new
functions:
- LogEvent::LogEvent(int32_t uid, int32_t pid)
- bool LogEvent::parseBuffer(uint8_t* buf, size_t size)
The goal is that these two functions will be the only two entry points
into the class. Separating these two is helpful because (a) it allows
test and source code to use the same codepath and (b) it allows the
calling function to see if the atom encoding was successfully parsed
(note that constructors can't return values).
P.S. This CL breaks LogEventQueueTests, but that will be resolved once
all tests are moved to the new constructors.
Test: m statsd
Test: bit statsd_test:SimpleConditionTrackerTest#TestSlicedCondition
Bug: 149590301
Change-Id: Id3c9522c4467c4869ec97226734a556dd9dfb169
Merged-In: Id3c9522c4467c4869ec97226734a556dd9dfb169
(cherry picked from commit 14f56f6bc4)
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/condition/SimpleConditionTracker.h"
|
||||
#include "stats_event.h"
|
||||
#include "tests/statsd_test_util.h"
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
@@ -31,6 +32,8 @@ namespace android {
|
||||
namespace os {
|
||||
namespace statsd {
|
||||
|
||||
namespace {
|
||||
|
||||
const ConfigKey kConfigKey(0, 12345);
|
||||
|
||||
const int ATTRIBUTION_NODE_FIELD_ID = 1;
|
||||
@@ -57,24 +60,33 @@ SimplePredicate getWakeLockHeldCondition(bool countNesting, bool defaultFalse,
|
||||
return simplePredicate;
|
||||
}
|
||||
|
||||
void writeAttributionNodesToEvent(LogEvent* event, const std::vector<int> &uids) {
|
||||
std::vector<AttributionNodeInternal> nodes;
|
||||
for (size_t i = 0; i < uids.size(); ++i) {
|
||||
AttributionNodeInternal node;
|
||||
node.set_uid(uids[i]);
|
||||
nodes.push_back(node);
|
||||
void makeWakeLockEvent(LogEvent* logEvent, uint32_t atomId, uint64_t timestamp,
|
||||
const vector<int>& uids, const string& wl, int acquire) {
|
||||
AStatsEvent* statsEvent = AStatsEvent_obtain();
|
||||
AStatsEvent_setAtomId(statsEvent, atomId);
|
||||
AStatsEvent_overwriteTimestamp(statsEvent, timestamp);
|
||||
|
||||
vector<std::string> tags(uids.size()); // vector of empty strings
|
||||
vector<const char*> cTags(uids.size());
|
||||
for (int i = 0; i < cTags.size(); i++) {
|
||||
cTags[i] = tags[i].c_str();
|
||||
}
|
||||
event->write(nodes); // attribution chain.
|
||||
AStatsEvent_writeAttributionChain(statsEvent, reinterpret_cast<const uint32_t*>(uids.data()),
|
||||
cTags.data(), uids.size());
|
||||
|
||||
AStatsEvent_writeString(statsEvent, wl.c_str());
|
||||
AStatsEvent_writeInt32(statsEvent, acquire);
|
||||
AStatsEvent_build(statsEvent);
|
||||
|
||||
size_t size;
|
||||
uint8_t* buf = AStatsEvent_getBuffer(statsEvent, &size);
|
||||
logEvent->parseBuffer(buf, size);
|
||||
|
||||
AStatsEvent_release(statsEvent);
|
||||
}
|
||||
|
||||
// TODO(b/149590301): Update this helper to use new socket schema.
|
||||
//void makeWakeLockEvent(
|
||||
// LogEvent* event, const std::vector<int> &uids, const string& wl, int acquire) {
|
||||
// writeAttributionNodesToEvent(event, uids);
|
||||
// event->write(wl);
|
||||
// event->write(acquire);
|
||||
// event->init();
|
||||
//}
|
||||
} // anonymous namespace
|
||||
|
||||
|
||||
std::map<int64_t, HashableDimensionKey> getWakeLockQueryKey(
|
||||
const Position position,
|
||||
@@ -265,138 +277,128 @@ TEST(SimpleConditionTrackerTest, TestNonSlicedConditionNestCounting) {
|
||||
EXPECT_TRUE(changedCache[0]);
|
||||
}
|
||||
|
||||
// TODO(b/149590301): Update these tests to use new socket schema.
|
||||
//TEST(SimpleConditionTrackerTest, TestSlicedCondition) {
|
||||
// std::vector<sp<ConditionTracker>> allConditions;
|
||||
// for (Position position :
|
||||
// { Position::FIRST, Position::LAST}) {
|
||||
//
|
||||
// SimplePredicate simplePredicate = getWakeLockHeldCondition(
|
||||
// true /*nesting*/, true /*default to false*/, true /*output slice by uid*/,
|
||||
// position);
|
||||
// string conditionName = "WL_HELD_BY_UID2";
|
||||
//
|
||||
// unordered_map<int64_t, int> trackerNameIndexMap;
|
||||
// trackerNameIndexMap[StringToId("WAKE_LOCK_ACQUIRE")] = 0;
|
||||
// trackerNameIndexMap[StringToId("WAKE_LOCK_RELEASE")] = 1;
|
||||
// trackerNameIndexMap[StringToId("RELEASE_ALL")] = 2;
|
||||
//
|
||||
// SimpleConditionTracker conditionTracker(kConfigKey, StringToId(conditionName),
|
||||
// 0 /*condition tracker index*/, simplePredicate,
|
||||
// trackerNameIndexMap);
|
||||
//
|
||||
// std::vector<int> uids = {111, 222, 333};
|
||||
//
|
||||
// LogEvent event(1 /*tagId*/, 0 /*timestamp*/);
|
||||
// makeWakeLockEvent(&event, uids, "wl1", 1);
|
||||
//
|
||||
// // one matched start
|
||||
// vector<MatchingState> matcherState;
|
||||
// matcherState.push_back(MatchingState::kMatched);
|
||||
// matcherState.push_back(MatchingState::kNotMatched);
|
||||
// matcherState.push_back(MatchingState::kNotMatched);
|
||||
// vector<sp<ConditionTracker>> allPredicates;
|
||||
// vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
|
||||
// vector<bool> changedCache(1, false);
|
||||
//
|
||||
// conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
|
||||
// changedCache);
|
||||
//
|
||||
// if (position == Position::FIRST ||
|
||||
// position == Position::LAST) {
|
||||
// EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
|
||||
// } else {
|
||||
// EXPECT_EQ(uids.size(), conditionTracker.mSlicedConditionState.size());
|
||||
// }
|
||||
// EXPECT_TRUE(changedCache[0]);
|
||||
// if (position == Position::FIRST ||
|
||||
// position == Position::LAST) {
|
||||
// EXPECT_EQ(conditionTracker.getChangedToTrueDimensions(allConditions)->size(), 1u);
|
||||
// EXPECT_TRUE(conditionTracker.getChangedToFalseDimensions(allConditions)->empty());
|
||||
// } else {
|
||||
// EXPECT_EQ(conditionTracker.getChangedToTrueDimensions(allConditions)->size(), uids.size());
|
||||
// }
|
||||
//
|
||||
// // Now test query
|
||||
// const auto queryKey = getWakeLockQueryKey(position, uids, conditionName);
|
||||
// conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
//
|
||||
// conditionTracker.isConditionMet(queryKey, allPredicates,
|
||||
// false,
|
||||
// conditionCache);
|
||||
// EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
|
||||
//
|
||||
// // another wake lock acquired by this uid
|
||||
// LogEvent event2(1 /*tagId*/, 0 /*timestamp*/);
|
||||
// makeWakeLockEvent(&event2, uids, "wl2", 1);
|
||||
// matcherState.clear();
|
||||
// matcherState.push_back(MatchingState::kMatched);
|
||||
// matcherState.push_back(MatchingState::kNotMatched);
|
||||
// conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
// changedCache[0] = false;
|
||||
// conditionTracker.evaluateCondition(event2, matcherState, allPredicates, conditionCache,
|
||||
// changedCache);
|
||||
// EXPECT_FALSE(changedCache[0]);
|
||||
// if (position == Position::FIRST ||
|
||||
// position == Position::LAST) {
|
||||
// EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
|
||||
// } else {
|
||||
// EXPECT_EQ(uids.size(), conditionTracker.mSlicedConditionState.size());
|
||||
// }
|
||||
// EXPECT_TRUE(conditionTracker.getChangedToTrueDimensions(allConditions)->empty());
|
||||
// EXPECT_TRUE(conditionTracker.getChangedToFalseDimensions(allConditions)->empty());
|
||||
//
|
||||
//
|
||||
// // wake lock 1 release
|
||||
// LogEvent event3(1 /*tagId*/, 0 /*timestamp*/);
|
||||
// makeWakeLockEvent(&event3, uids, "wl1", 0); // now release it.
|
||||
// matcherState.clear();
|
||||
// matcherState.push_back(MatchingState::kNotMatched);
|
||||
// matcherState.push_back(MatchingState::kMatched);
|
||||
// conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
// changedCache[0] = false;
|
||||
// conditionTracker.evaluateCondition(event3, matcherState, allPredicates, conditionCache,
|
||||
// changedCache);
|
||||
// // nothing changes, because wake lock 2 is still held for this uid
|
||||
// EXPECT_FALSE(changedCache[0]);
|
||||
// if (position == Position::FIRST ||
|
||||
// position == Position::LAST) {
|
||||
// EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
|
||||
// } else {
|
||||
// EXPECT_EQ(uids.size(), conditionTracker.mSlicedConditionState.size());
|
||||
// }
|
||||
// EXPECT_TRUE(conditionTracker.getChangedToTrueDimensions(allConditions)->empty());
|
||||
// EXPECT_TRUE(conditionTracker.getChangedToFalseDimensions(allConditions)->empty());
|
||||
//
|
||||
// LogEvent event4(1 /*tagId*/, 0 /*timestamp*/);
|
||||
// makeWakeLockEvent(&event4, uids, "wl2", 0); // now release it.
|
||||
// matcherState.clear();
|
||||
// matcherState.push_back(MatchingState::kNotMatched);
|
||||
// matcherState.push_back(MatchingState::kMatched);
|
||||
// conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
// changedCache[0] = false;
|
||||
// conditionTracker.evaluateCondition(event4, matcherState, allPredicates, conditionCache,
|
||||
// changedCache);
|
||||
// EXPECT_EQ(0UL, conditionTracker.mSlicedConditionState.size());
|
||||
// EXPECT_TRUE(changedCache[0]);
|
||||
// if (position == Position::FIRST ||
|
||||
// position == Position::LAST) {
|
||||
// EXPECT_EQ(conditionTracker.getChangedToFalseDimensions(allConditions)->size(), 1u);
|
||||
// EXPECT_TRUE(conditionTracker.getChangedToTrueDimensions(allConditions)->empty());
|
||||
// } else {
|
||||
// EXPECT_EQ(conditionTracker.getChangedToFalseDimensions(allConditions)->size(), uids.size());
|
||||
// }
|
||||
//
|
||||
// // query again
|
||||
// conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
// conditionTracker.isConditionMet(queryKey, allPredicates,
|
||||
// false,
|
||||
// conditionCache);
|
||||
// EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
TEST(SimpleConditionTrackerTest, TestSlicedCondition) {
|
||||
std::vector<sp<ConditionTracker>> allConditions;
|
||||
for (Position position : {Position::FIRST, Position::LAST}) {
|
||||
SimplePredicate simplePredicate = getWakeLockHeldCondition(
|
||||
true /*nesting*/, true /*default to false*/, true /*output slice by uid*/,
|
||||
position);
|
||||
string conditionName = "WL_HELD_BY_UID2";
|
||||
|
||||
unordered_map<int64_t, int> trackerNameIndexMap;
|
||||
trackerNameIndexMap[StringToId("WAKE_LOCK_ACQUIRE")] = 0;
|
||||
trackerNameIndexMap[StringToId("WAKE_LOCK_RELEASE")] = 1;
|
||||
trackerNameIndexMap[StringToId("RELEASE_ALL")] = 2;
|
||||
|
||||
SimpleConditionTracker conditionTracker(kConfigKey, StringToId(conditionName),
|
||||
0 /*condition tracker index*/, simplePredicate,
|
||||
trackerNameIndexMap);
|
||||
|
||||
std::vector<int> uids = {111, 222, 333};
|
||||
|
||||
LogEvent event(/*uid=*/-1, /*pid=*/-1);
|
||||
makeWakeLockEvent(&event, /*atomId=*/ 1, /*timestamp=*/ 0, uids, "wl1", /*acquire=*/ 1);
|
||||
|
||||
// one matched start
|
||||
vector<MatchingState> matcherState;
|
||||
matcherState.push_back(MatchingState::kMatched);
|
||||
matcherState.push_back(MatchingState::kNotMatched);
|
||||
matcherState.push_back(MatchingState::kNotMatched);
|
||||
vector<sp<ConditionTracker>> allPredicates;
|
||||
vector<ConditionState> conditionCache(1, ConditionState::kNotEvaluated);
|
||||
vector<bool> changedCache(1, false);
|
||||
|
||||
conditionTracker.evaluateCondition(event, matcherState, allPredicates, conditionCache,
|
||||
changedCache);
|
||||
|
||||
if (position == Position::FIRST || position == Position::LAST) {
|
||||
EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
|
||||
} else {
|
||||
EXPECT_EQ(uids.size(), conditionTracker.mSlicedConditionState.size());
|
||||
}
|
||||
EXPECT_TRUE(changedCache[0]);
|
||||
if (position == Position::FIRST || position == Position::LAST) {
|
||||
EXPECT_EQ(conditionTracker.getChangedToTrueDimensions(allConditions)->size(), 1u);
|
||||
EXPECT_TRUE(conditionTracker.getChangedToFalseDimensions(allConditions)->empty());
|
||||
} else {
|
||||
EXPECT_EQ(conditionTracker.getChangedToTrueDimensions(allConditions)->size(),
|
||||
uids.size());
|
||||
}
|
||||
|
||||
// Now test query
|
||||
const auto queryKey = getWakeLockQueryKey(position, uids, conditionName);
|
||||
conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
|
||||
conditionTracker.isConditionMet(queryKey, allPredicates, false, conditionCache);
|
||||
EXPECT_EQ(ConditionState::kTrue, conditionCache[0]);
|
||||
|
||||
// another wake lock acquired by this uid
|
||||
LogEvent event2(/*uid=*/-1, /*pid=*/-1);
|
||||
makeWakeLockEvent(&event2, /*atomId=*/ 1, /*timestamp=*/ 0, uids, "wl2", /*acquire=*/ 1);
|
||||
matcherState.clear();
|
||||
matcherState.push_back(MatchingState::kMatched);
|
||||
matcherState.push_back(MatchingState::kNotMatched);
|
||||
conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
changedCache[0] = false;
|
||||
conditionTracker.evaluateCondition(event2, matcherState, allPredicates, conditionCache,
|
||||
changedCache);
|
||||
EXPECT_FALSE(changedCache[0]);
|
||||
if (position == Position::FIRST || position == Position::LAST) {
|
||||
EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
|
||||
} else {
|
||||
EXPECT_EQ(uids.size(), conditionTracker.mSlicedConditionState.size());
|
||||
}
|
||||
EXPECT_TRUE(conditionTracker.getChangedToTrueDimensions(allConditions)->empty());
|
||||
EXPECT_TRUE(conditionTracker.getChangedToFalseDimensions(allConditions)->empty());
|
||||
|
||||
|
||||
// wake lock 1 release
|
||||
LogEvent event3(/*uid=*/-1, /*pid=*/-1);
|
||||
makeWakeLockEvent(&event3, /*atomId=*/1, /*timestamp=*/0, uids, "wl1", /*acquire=*/0);
|
||||
matcherState.clear();
|
||||
matcherState.push_back(MatchingState::kNotMatched);
|
||||
matcherState.push_back(MatchingState::kMatched);
|
||||
conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
changedCache[0] = false;
|
||||
conditionTracker.evaluateCondition(event3, matcherState, allPredicates, conditionCache,
|
||||
changedCache);
|
||||
// nothing changes, because wake lock 2 is still held for this uid
|
||||
EXPECT_FALSE(changedCache[0]);
|
||||
if (position == Position::FIRST || position == Position::LAST) {
|
||||
EXPECT_EQ(1UL, conditionTracker.mSlicedConditionState.size());
|
||||
} else {
|
||||
EXPECT_EQ(uids.size(), conditionTracker.mSlicedConditionState.size());
|
||||
}
|
||||
EXPECT_TRUE(conditionTracker.getChangedToTrueDimensions(allConditions)->empty());
|
||||
EXPECT_TRUE(conditionTracker.getChangedToFalseDimensions(allConditions)->empty());
|
||||
|
||||
LogEvent event4(/*uid=*/-1, /*pid=*/-1);
|
||||
makeWakeLockEvent(&event, /*atomId=*/1, /*timestamp=*/ 0, uids, "wl2", /*acquire=*/0);
|
||||
matcherState.clear();
|
||||
matcherState.push_back(MatchingState::kNotMatched);
|
||||
matcherState.push_back(MatchingState::kMatched);
|
||||
conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
changedCache[0] = false;
|
||||
conditionTracker.evaluateCondition(event4, matcherState, allPredicates, conditionCache,
|
||||
changedCache);
|
||||
EXPECT_EQ(0UL, conditionTracker.mSlicedConditionState.size());
|
||||
EXPECT_TRUE(changedCache[0]);
|
||||
if (position == Position::FIRST || position == Position::LAST) {
|
||||
EXPECT_EQ(conditionTracker.getChangedToFalseDimensions(allConditions)->size(), 1u);
|
||||
EXPECT_TRUE(conditionTracker.getChangedToTrueDimensions(allConditions)->empty());
|
||||
} else {
|
||||
EXPECT_EQ(conditionTracker.getChangedToFalseDimensions(allConditions)->size(),
|
||||
uids.size());
|
||||
}
|
||||
|
||||
// query again
|
||||
conditionCache[0] = ConditionState::kNotEvaluated;
|
||||
conditionTracker.isConditionMet(queryKey, allPredicates, false, conditionCache);
|
||||
EXPECT_EQ(ConditionState::kFalse, conditionCache[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//TEST(SimpleConditionTrackerTest, TestSlicedWithNoOutputDim) {
|
||||
// std::vector<sp<ConditionTracker>> allConditions;
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user