Skip to content

Commit d7b08c7

Browse files
Christine TsaiAndroid (Google) Code Review
authored andcommitted
Merge "Change SimplePredicate InitialValue default to UNKNOWN" into rvc-dev
2 parents a9c6632 + 70a7ee5 commit d7b08c7

8 files changed

Lines changed: 290 additions & 22 deletions

File tree

cmds/statsd/src/StatsLogProcessor.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,6 @@ class StatsLogProcessor : public ConfigListener, public virtual PackageInfoListe
307307
FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation);
308308
FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition);
309309
FRIEND_TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents);
310-
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
311-
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
312-
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation);
313310

314311
FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
315312
FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
@@ -328,6 +325,7 @@ class StatsLogProcessor : public ConfigListener, public virtual PackageInfoListe
328325
FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithSameDeactivation);
329326
FRIEND_TEST(MetricActivationE2eTest, TestCountMetricWithTwoMetricsTwoDeactivations);
330327

328+
FRIEND_TEST(CountMetricE2eTest, TestInitialConditionChanges);
331329
FRIEND_TEST(CountMetricE2eTest, TestSlicedState);
332330
FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithMap);
333331
FRIEND_TEST(CountMetricE2eTest, TestMultipleSlicedStates);
@@ -345,6 +343,10 @@ class StatsLogProcessor : public ConfigListener, public virtual PackageInfoListe
345343
FRIEND_TEST(DurationMetricE2eTest, TestSlicedStatePrimaryFieldsNotSubsetDimInWhat);
346344
FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedStatePrimaryFieldsSubset);
347345

346+
FRIEND_TEST(ValueMetricE2eTest, TestInitialConditionChanges);
347+
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
348+
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
349+
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation);
348350
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState);
349351
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithDimensions);
350352
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithIncorrectDimensions);

cmds/statsd/src/metrics/MetricProducer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ class MetricProducer : public virtual android::RefBase, public virtual StateList
459459
FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithMap);
460460
FRIEND_TEST(CountMetricE2eTest, TestMultipleSlicedStates);
461461
FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithPrimaryFields);
462+
FRIEND_TEST(CountMetricE2eTest, TestInitialConditionChanges);
462463

463464
FRIEND_TEST(DurationMetricE2eTest, TestOneBucket);
464465
FRIEND_TEST(DurationMetricE2eTest, TestTwoBuckets);
@@ -488,6 +489,7 @@ class MetricProducer : public virtual android::RefBase, public virtual StateList
488489
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState);
489490
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithDimensions);
490491
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithIncorrectDimensions);
492+
FRIEND_TEST(ValueMetricE2eTest, TestInitialConditionChanges);
491493
};
492494

493495
} // namespace statsd

cmds/statsd/src/metrics/MetricsManager.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,6 @@ class MetricsManager : public virtual android::RefBase, public virtual PullUidPr
292292
FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsWithActivation);
293293
FRIEND_TEST(GaugeMetricE2eTest, TestRandomSamplePulledEventsNoCondition);
294294
FRIEND_TEST(GaugeMetricE2eTest, TestConditionChangeToTrueSamplePulledEvents);
295-
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
296-
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
297-
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation);
298295

299296
FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_single_bucket);
300297
FRIEND_TEST(AnomalyDetectionE2eTest, TestSlicedCountMetric_multiple_buckets);
@@ -322,6 +319,7 @@ class MetricsManager : public virtual android::RefBase, public virtual PullUidPr
322319
TestActivationOnBootMultipleActivationsDifferentActivationTypes);
323320
FRIEND_TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart);
324321

322+
FRIEND_TEST(CountMetricE2eTest, TestInitialConditionChanges);
325323
FRIEND_TEST(CountMetricE2eTest, TestSlicedState);
326324
FRIEND_TEST(CountMetricE2eTest, TestSlicedStateWithMap);
327325
FRIEND_TEST(CountMetricE2eTest, TestMultipleSlicedStates);
@@ -339,6 +337,10 @@ class MetricsManager : public virtual android::RefBase, public virtual PullUidPr
339337
FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedStatePrimaryFieldsSuperset);
340338
FRIEND_TEST(DurationMetricE2eTest, TestWithSlicedStatePrimaryFieldsSubset);
341339

340+
FRIEND_TEST(ValueMetricE2eTest, TestInitialConditionChanges);
341+
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents);
342+
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_LateAlarm);
343+
FRIEND_TEST(ValueMetricE2eTest, TestPulledEvents_WithActivation);
342344
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState);
343345
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithDimensions);
344346
FRIEND_TEST(ValueMetricE2eTest, TestInitWithSlicedState_WithIncorrectDimensions);

cmds/statsd/src/statsd_config.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ message SimplePredicate {
131131
UNKNOWN = 0;
132132
FALSE = 1;
133133
}
134-
optional InitialValue initial_value = 5 [default = FALSE];
134+
optional InitialValue initial_value = 5 [default = UNKNOWN];
135135

136136
optional FieldMatcher dimensions = 6;
137137
}

cmds/statsd/tests/e2e/CountMetric_e2e_test.cpp

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,92 @@ namespace statsd {
2727

2828
#ifdef __ANDROID__
2929

30+
/**
31+
* Tests the initial condition and condition after the first log events for
32+
* count metrics with either a combination condition or simple condition.
33+
*
34+
* Metrics should be initialized with condition kUnknown (given that the
35+
* predicate is using the default InitialValue of UNKNOWN). The condition should
36+
* be updated to either kFalse or kTrue if a condition event is logged for all
37+
* children conditions.
38+
*/
39+
TEST(CountMetricE2eTest, TestInitialConditionChanges) {
40+
// Initialize config.
41+
StatsdConfig config;
42+
config.add_allowed_log_source("AID_ROOT"); // LogEvent defaults to UID of root.
43+
config.add_default_pull_packages("AID_ROOT"); // Fake puller is registered with root.
44+
45+
auto syncStartMatcher = CreateSyncStartAtomMatcher();
46+
*config.add_atom_matcher() = syncStartMatcher;
47+
*config.add_atom_matcher() = CreateScreenTurnedOnAtomMatcher();
48+
*config.add_atom_matcher() = CreateScreenTurnedOffAtomMatcher();
49+
*config.add_atom_matcher() = CreateBatteryStateNoneMatcher();
50+
*config.add_atom_matcher() = CreateBatteryStateUsbMatcher();
51+
52+
auto screenOnPredicate = CreateScreenIsOnPredicate();
53+
*config.add_predicate() = screenOnPredicate;
54+
55+
auto deviceUnpluggedPredicate = CreateDeviceUnpluggedPredicate();
56+
*config.add_predicate() = deviceUnpluggedPredicate;
57+
58+
auto screenOnOnBatteryPredicate = config.add_predicate();
59+
screenOnOnBatteryPredicate->set_id(StringToId("screenOnOnBatteryPredicate"));
60+
screenOnOnBatteryPredicate->mutable_combination()->set_operation(LogicalOperation::AND);
61+
addPredicateToPredicateCombination(screenOnPredicate, screenOnOnBatteryPredicate);
62+
addPredicateToPredicateCombination(deviceUnpluggedPredicate, screenOnOnBatteryPredicate);
63+
64+
// CountSyncStartWhileScreenOnOnBattery (CombinationCondition)
65+
CountMetric* countMetric1 = config.add_count_metric();
66+
countMetric1->set_id(StringToId("CountSyncStartWhileScreenOnOnBattery"));
67+
countMetric1->set_what(syncStartMatcher.id());
68+
countMetric1->set_condition(screenOnOnBatteryPredicate->id());
69+
countMetric1->set_bucket(FIVE_MINUTES);
70+
71+
// CountSyncStartWhileOnBattery (SimpleCondition)
72+
CountMetric* countMetric2 = config.add_count_metric();
73+
countMetric2->set_id(StringToId("CountSyncStartWhileOnBatterySliceScreen"));
74+
countMetric2->set_what(syncStartMatcher.id());
75+
countMetric2->set_condition(deviceUnpluggedPredicate.id());
76+
countMetric2->set_bucket(FIVE_MINUTES);
77+
78+
const uint64_t bucketStartTimeNs = 10000000000; // 0:10
79+
const uint64_t bucketSizeNs =
80+
TimeUnitToBucketSizeInMillis(config.count_metric(0).bucket()) * 1000000LL;
81+
int uid = 12345;
82+
int64_t cfgId = 98765;
83+
ConfigKey cfgKey(uid, cfgId);
84+
auto processor = CreateStatsLogProcessor(bucketStartTimeNs, bucketStartTimeNs, config, cfgKey);
85+
86+
EXPECT_EQ(processor->mMetricsManagers.size(), 1u);
87+
sp<MetricsManager> metricsManager = processor->mMetricsManagers.begin()->second;
88+
EXPECT_TRUE(metricsManager->isConfigValid());
89+
EXPECT_EQ(2, metricsManager->mAllMetricProducers.size());
90+
91+
sp<MetricProducer> metricProducer1 = metricsManager->mAllMetricProducers[0];
92+
sp<MetricProducer> metricProducer2 = metricsManager->mAllMetricProducers[1];
93+
94+
EXPECT_EQ(ConditionState::kUnknown, metricProducer1->mCondition);
95+
EXPECT_EQ(ConditionState::kUnknown, metricProducer2->mCondition);
96+
97+
auto screenOnEvent =
98+
CreateScreenStateChangedEvent(bucketStartTimeNs + 30, android::view::DISPLAY_STATE_ON);
99+
processor->OnLogEvent(screenOnEvent.get());
100+
EXPECT_EQ(ConditionState::kUnknown, metricProducer1->mCondition);
101+
EXPECT_EQ(ConditionState::kUnknown, metricProducer2->mCondition);
102+
103+
auto pluggedUsbEvent = CreateBatteryStateChangedEvent(
104+
bucketStartTimeNs + 50, BatteryPluggedStateEnum::BATTERY_PLUGGED_USB);
105+
processor->OnLogEvent(pluggedUsbEvent.get());
106+
EXPECT_EQ(ConditionState::kFalse, metricProducer1->mCondition);
107+
EXPECT_EQ(ConditionState::kFalse, metricProducer2->mCondition);
108+
109+
auto pluggedNoneEvent = CreateBatteryStateChangedEvent(
110+
bucketStartTimeNs + 70, BatteryPluggedStateEnum::BATTERY_PLUGGED_NONE);
111+
processor->OnLogEvent(pluggedNoneEvent.get());
112+
EXPECT_EQ(ConditionState::kTrue, metricProducer1->mCondition);
113+
EXPECT_EQ(ConditionState::kTrue, metricProducer2->mCondition);
114+
}
115+
30116
/**
31117
* Test a count metric that has one slice_by_state with no primary fields.
32118
*
@@ -85,7 +171,7 @@ TEST(CountMetricE2eTest, TestSlicedState) {
85171
x x x x x x (syncStartEvents)
86172
| | (ScreenIsOnEvent)
87173
| | (ScreenIsOffEvent)
88-
| (ScreenUnknownEvent)
174+
| (ScreenDozeEvent)
89175
*/
90176
// Initialize log events - first bucket.
91177
std::vector<int> attributionUids1 = {123};
@@ -243,9 +329,8 @@ TEST(CountMetricE2eTest, TestSlicedStateWithMap) {
243329
|-----------------------------|-----------------------------|--
244330
x x x x x x x x x (syncStartEvents)
245331
-----------------------------------------------------------SCREEN_OFF events
246-
| (ScreenStateUnknownEvent = 0)
247332
| | (ScreenStateOffEvent = 1)
248-
| (ScreenStateDozeEvent = 3)
333+
| | (ScreenStateDozeEvent = 3)
249334
| (ScreenStateDozeSuspendEvent =
250335
4)
251336
-----------------------------------------------------------SCREEN_ON events
@@ -262,7 +347,7 @@ TEST(CountMetricE2eTest, TestSlicedStateWithMap) {
262347
attributionTags1, "sync_name")); // 0:30
263348
events.push_back(CreateScreenStateChangedEvent(
264349
bucketStartTimeNs + 30 * NS_PER_SEC,
265-
android::view::DisplayStateEnum::DISPLAY_STATE_UNKNOWN)); // 0:40
350+
android::view::DisplayStateEnum::DISPLAY_STATE_DOZE)); // 0:40
266351
events.push_back(CreateSyncStartEvent(bucketStartTimeNs + 60 * NS_PER_SEC, attributionUids1,
267352
attributionTags1, "sync_name")); // 1:10
268353
events.push_back(CreateScreenStateChangedEvent(
@@ -625,9 +710,8 @@ TEST(CountMetricE2eTest, TestMultipleSlicedStates) {
625710
|------------------------|------------------------|--
626711
1 1 1 1 1 2 1 1 2 (AppCrashEvents)
627712
---------------------------------------------------SCREEN_OFF events
628-
| (ScreenUnknownEvent = 0)
629713
| | (ScreenOffEvent = 1)
630-
| (ScreenDozeEvent = 3)
714+
| | (ScreenDozeEvent = 3)
631715
---------------------------------------------------SCREEN_ON events
632716
| | (ScreenOnEvent = 2)
633717
| (ScreenOnSuspendEvent = 6)
@@ -660,7 +744,7 @@ TEST(CountMetricE2eTest, TestMultipleSlicedStates) {
660744
CreateAppCrashOccurredEvent(bucketStartTimeNs + 20 * NS_PER_SEC, 1 /*uid*/)); // 0:30
661745
events.push_back(CreateScreenStateChangedEvent(
662746
bucketStartTimeNs + 30 * NS_PER_SEC,
663-
android::view::DisplayStateEnum::DISPLAY_STATE_UNKNOWN)); // 0:40
747+
android::view::DisplayStateEnum::DISPLAY_STATE_DOZE)); // 0:40
664748
events.push_back(
665749
CreateAppCrashOccurredEvent(bucketStartTimeNs + 60 * NS_PER_SEC, 1 /*uid*/)); // 1:10
666750
events.push_back(CreateUidProcessStateChangedEvent(

0 commit comments

Comments
 (0)