Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 084e5040 authored by Tej Singh's avatar Tej Singh
Browse files

Partial config update: test update states

Adds a unit test for updating states. Mostly to ensure that replaced
states are calculated correctly.

Also refactor the logic a bit to make it easier to test.

Test: atest statsd_test
Bug: 162323667
Change-Id: Ie0e72c25c879d1c4dd59c330d40e5757ef750ec5
parent 9465da16
Loading
Loading
Loading
Loading
+21 −13
Original line number Diff line number Diff line
@@ -396,6 +396,23 @@ bool updateConditions(const ConfigKey& key, const StatsdConfig& config,
    return true;
}

bool updateStates(const StatsdConfig& config, const map<int64_t, uint64_t>& oldStateProtoHashes,
                  unordered_map<int64_t, int>& stateAtomIdMap,
                  unordered_map<int64_t, unordered_map<int, int64_t>>& allStateGroupMaps,
                  map<int64_t, uint64_t>& newStateProtoHashes, set<int64_t>& replacedStates) {
    // Share with metrics_manager_util.
    if (!initStates(config, stateAtomIdMap, allStateGroupMaps, newStateProtoHashes)) {
        return false;
    }

    for (const auto& [stateId, stateHash] : oldStateProtoHashes) {
        const auto& it = newStateProtoHashes.find(stateId);
        if (it != newStateProtoHashes.end() && it->second != stateHash) {
            replacedStates.insert(stateId);
        }
    }
    return true;
}
// Returns true if any matchers in the metric activation were replaced.
bool metricActivationDepsChange(const StatsdConfig& config,
                                const unordered_map<int64_t, int>& metricToActivationMap,
@@ -1042,6 +1059,7 @@ bool updateStatsdConfig(const ConfigKey& key, const StatsdConfig& config, const
                        set<int64_t>& noReportMetricIds) {
    set<int64_t> replacedMatchers;
    set<int64_t> replacedConditions;
    set<int64_t> replacedStates;
    set<int64_t> replacedMetrics;
    vector<ConditionState> conditionCache;
    unordered_map<int64_t, int> stateAtomIdMap;
@@ -1053,7 +1071,6 @@ bool updateStatsdConfig(const ConfigKey& key, const StatsdConfig& config, const
        ALOGE("updateAtomMatchingTrackers failed");
        return false;
    }
    VLOG("updateAtomMatchingTrackers succeeded");

    if (!updateConditions(key, config, newAtomMatchingTrackerMap, replacedMatchers,
                          oldConditionTrackerMap, oldConditionTrackers, newConditionTrackerMap,
@@ -1062,21 +1079,12 @@ bool updateStatsdConfig(const ConfigKey& key, const StatsdConfig& config, const
        ALOGE("updateConditions failed");
        return false;
    }
    VLOG("updateConditions succeeded");

    // Share with metrics_manager_util,
    if (!initStates(config, stateAtomIdMap, allStateGroupMaps, newStateProtoHashes)) {
        ALOGE("initStates failed");
    if (!updateStates(config, oldStateProtoHashes, stateAtomIdMap, allStateGroupMaps,
                      newStateProtoHashes, replacedStates)) {
        ALOGE("updateStates failed");
        return false;
    }

    set<int64_t> replacedStates;
    for (const auto& [stateId, stateHash] : oldStateProtoHashes) {
        const auto& it = newStateProtoHashes.find(stateId);
        if (it != newStateProtoHashes.end() && it->second != stateHash) {
            replacedStates.insert(stateId);
        }
    }
    if (!updateMetrics(key, config, timeBaseNs, currentTimeNs, pullerManager,
                       oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, replacedMatchers,
                       newAtomMatchingTrackers, newConditionTrackerMap, replacedConditions,
+7 −0
Original line number Diff line number Diff line
@@ -126,6 +126,13 @@ bool updateConditions(const ConfigKey& key, const StatsdConfig& config,
                      std::vector<ConditionState>& conditionCache,
                      std::set<int64_t>& replacedConditions);

bool updateStates(const StatsdConfig& config,
                  const std::map<int64_t, uint64_t>& oldStateProtoHashes,
                  std::unordered_map<int64_t, int>& stateAtomIdMap,
                  std::unordered_map<int64_t, std::unordered_map<int, int64_t>>& allStateGroupMaps,
                  std::map<int64_t, uint64_t>& newStateProtoHashes,
                  std::set<int64_t>& replacedStates);

// Function to determine the update status (preserve/replace/new) of all metrics in the config.
// [config]: the input StatsdConfig
// [oldMetricProducerMap]: metric id to index mapping in the existing MetricsManager
+51 −0
Original line number Diff line number Diff line
@@ -997,6 +997,57 @@ TEST_F(ConfigUpdateTest, TestUpdateConditions) {
    EXPECT_THAT(combinationTracker1->mSlicedChildren, IsEmpty());
}

TEST_F(ConfigUpdateTest, TestUpdateStates) {
    StatsdConfig config;
    // Add states.
    // Will be replaced because we add a state map.
    State state1 = CreateScreenState();
    int64_t state1Id = state1.id();
    *config.add_state() = state1;

    // Will be preserved.
    State state2 = CreateUidProcessState();
    int64_t state2Id = state2.id();
    *config.add_state() = state2;

    // Will be replaced since the atom changes from overlay to screen.
    State state3 = CreateOverlayState();
    int64_t state3Id = state3.id();
    *config.add_state() = state3;

    EXPECT_TRUE(initConfig(config));

    // Change definitions of state1 and state3.
    int64_t screenOnId = 0x4321, screenOffId = 0x1234;
    *state1.mutable_map() = CreateScreenStateSimpleOnOffMap(screenOnId, screenOffId);
    state3.set_atom_id(util::SCREEN_STATE_CHANGED);

    StatsdConfig newConfig;
    *newConfig.add_state() = state3;
    *newConfig.add_state() = state1;
    *newConfig.add_state() = state2;

    unordered_map<int64_t, int> stateAtomIdMap;
    unordered_map<int64_t, unordered_map<int, int64_t>> allStateGroupMaps;
    map<int64_t, uint64_t> newStateProtoHashes;
    set<int64_t> replacedStates;
    EXPECT_TRUE(updateStates(newConfig, oldStateHashes, stateAtomIdMap, allStateGroupMaps,
                             newStateProtoHashes, replacedStates));
    EXPECT_THAT(replacedStates, ContainerEq(set({state1Id, state3Id})));

    unordered_map<int64_t, int> expectedStateAtomIdMap = {
            {state1Id, util::SCREEN_STATE_CHANGED},
            {state2Id, util::UID_PROCESS_STATE_CHANGED},
            {state3Id, util::SCREEN_STATE_CHANGED}};
    EXPECT_THAT(stateAtomIdMap, ContainerEq(expectedStateAtomIdMap));

    unordered_map<int64_t, unordered_map<int, int64_t>> expectedStateGroupMaps = {
            {state1Id,
             {{android::view::DisplayStateEnum::DISPLAY_STATE_OFF, screenOffId},
              {android::view::DisplayStateEnum::DISPLAY_STATE_ON, screenOnId}}}};
    EXPECT_THAT(allStateGroupMaps, ContainerEq(expectedStateGroupMaps));
}

TEST_F(ConfigUpdateTest, TestEventMetricPreserve) {
    StatsdConfig config;
    AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher();