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

Commit 580ea321 authored by Yao Chen's avatar Yao Chen
Browse files

Add StateTracker.

StateTracker is a special condition tracker that's based on a state atom.
State atoms are annotated in atoms.proto.

The rules for StateTracker:
 1. must not have "stop". must have "dimension"
 2. must be based on a state atom.
 3. it must have the all primary fields and the exclusive state field in its dimension.

 For example UidProcessStateTracker, will have output dimension {uid, state}.

Test: unit tests added.
Change-Id: I6b77e58e9fabe61f7326daf929577d8b2cfbf27b
parent aec69501
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ statsd_common_src := \
    src/condition/condition_util.cpp \
    src/condition/SimpleConditionTracker.cpp \
    src/condition/ConditionWizard.cpp \
    src/condition/StateTracker.cpp \
    src/config/ConfigKey.cpp \
    src/config/ConfigListener.cpp \
    src/config/ConfigManager.cpp \
@@ -189,6 +190,7 @@ LOCAL_SRC_FILES := \
    tests/FieldValue_test.cpp \
    tests/condition/CombinationConditionTracker_test.cpp \
    tests/condition/SimpleConditionTracker_test.cpp \
    tests/condition/StateTracker_test.cpp \
    tests/metrics/OringDurationTracker_test.cpp \
    tests/metrics/MaxDurationTracker_test.cpp \
    tests/metrics/CountMetricProducer_test.cpp \
+13 −4
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ int32_t encodeMatcherMask(int32_t mask[], int32_t depth);
inline int32_t getSimpleField(size_t field) {
    return ((int32_t)field << 8 * 2);
}

/**
 * Field is a wrapper class for 2 integers that represents the field of a log element in its Atom
 * proto.
@@ -201,9 +202,9 @@ public:
 *     }
 *
 * We translate the FieldMatcher into a Field, and mask
 * First: [Matcher Field] 0x02010101  [Mask]0xffff7fff
 * Last:  [Matcher Field] 0x02018001  [Mask]0xffff80ff
 * Any:   [Matcher Field] 0x02010001  [Mask]0xffff00ff
 * First: [Matcher Field] 0x02010101  [Mask]0xff7f7f7f
 * Last:  [Matcher Field] 0x02018001  [Mask]0xff7f807f
 * Any:   [Matcher Field] 0x02010001  [Mask]0xff7f007f
 *
 * [To match a log Field with a Matcher] we apply the bit mask to the log Field and check if
 * the result is equal to the Matcher Field. That's a bit wise AND operation + check if 2 ints are
@@ -235,8 +236,16 @@ struct Matcher {

    inline bool operator!=(const Matcher& that) const {
        return mMatcher != that.getMatcher() || mMask != that.getMask();
    }

    inline bool operator==(const Matcher& that) const {
        return mMatcher == that.mMatcher && mMask == that.mMask;
    }
};
};

inline Matcher getSimpleMatcher(int32_t tag, size_t field) {
    return Matcher(Field(tag, getSimpleField(field)), 0xff7f0000);
}

/**
 * A wrapper for a union type to contain multiple types of values.
+23 −0
Original line number Diff line number Diff line
@@ -52,6 +52,29 @@ public:
            const vector<Matcher>& dimensionFields,
            std::unordered_set<HashableDimensionKey>& dimensionsKeySet) const override;

    // Only one child predicate can have dimension.
    const std::set<HashableDimensionKey>* getChangedToTrueDimensions(
            const std::vector<sp<ConditionTracker>>& allConditions) const override {
        for (const auto& child : mChildren) {
            auto result = allConditions[child]->getChangedToTrueDimensions(allConditions);
            if (result != nullptr) {
                return result;
            }
        }
        return nullptr;
    }
    // Only one child predicate can have dimension.
    const std::set<HashableDimensionKey>* getChangedToFalseDimensions(
            const std::vector<sp<ConditionTracker>>& allConditions) const override {
        for (const auto& child : mChildren) {
            auto result = allConditions[child]->getChangedToFalseDimensions(allConditions);
            if (result != nullptr) {
                return result;
            }
        }
        return nullptr;
    }

private:
    LogicalOperation mLogicalOperation;

+5 −0
Original line number Diff line number Diff line
@@ -111,6 +111,11 @@ public:
        return mSliced;
    }

    virtual const std::set<HashableDimensionKey>* getChangedToTrueDimensions(
            const std::vector<sp<ConditionTracker>>& allConditions) const = 0;
    virtual const std::set<HashableDimensionKey>* getChangedToFalseDimensions(
            const std::vector<sp<ConditionTracker>>& allConditions) const = 0;

protected:
    const int64_t mConditionId;

+10 −0
Original line number Diff line number Diff line
@@ -41,6 +41,16 @@ ConditionState ConditionWizard::getMetConditionDimension(
                                 *dimensionsKeySet);
}

const set<HashableDimensionKey>* ConditionWizard::getChangedToTrueDimensions(
        const int index) const {
    return mAllConditions[index]->getChangedToTrueDimensions(mAllConditions);
}

const set<HashableDimensionKey>* ConditionWizard::getChangedToFalseDimensions(
        const int index) const {
    return mAllConditions[index]->getChangedToFalseDimensions(mAllConditions);
}

}  // namespace statsd
}  // namespace os
}  // namespace android
 No newline at end of file
Loading