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

Commit a9a310ec authored by Chenjie Yu's avatar Chenjie Yu
Browse files

metric activation on boot

Bug: 123038368
Test: unit test
Change-Id: Id374bdfd8d15264ada0e7bac0388080be308ac8f
parent da123a8e
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -526,9 +526,10 @@ void StatsLogProcessor::WriteMetricsActivationToDisk(int64_t currentTimeNs) {
        proto.write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_ID, (long long)pair.first.GetId());
        proto.write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_UID, pair.first.GetUid());

        vector<const MetricProducer*> acrtiveMetrics;
        pair.second->getActiveMetrics(acrtiveMetrics);
        for (const MetricProducer* metric : acrtiveMetrics) {
        vector<MetricProducer*> activeMetrics;
        pair.second->prepForShutDown(currentTimeNs);
        pair.second->getActiveMetrics(activeMetrics);
        for (MetricProducer* metric : activeMetrics) {
            if (metric->isActive()) {
                uint64_t metricToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
                                                   FIELD_ID_ACTIVE_METRIC);
+1 −0
Original line number Diff line number Diff line
@@ -195,6 +195,7 @@ private:
    FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
    FRIEND_TEST(StatsLogProcessorTest, TestDropWhenByteSizeTooLarge);
    FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
    FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);

    FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration1);
    FRIEND_TEST(WakelockDurationE2eTest, TestAggregatedPredicateDimensionsForSumDuration2);
+30 −6
Original line number Diff line number Diff line
@@ -107,6 +107,10 @@ void MetricProducer::activateLocked(int activationTrackerIndex, int64_t elapsedT
    if (it == mEventActivationMap.end()) {
        return;
    }
    if (mActivationType == MetricActivation::ACTIVATE_ON_BOOT) {
        it->second.state = ActivationState::kActiveOnBoot;
        return;
    }
    it->second.activation_ns = elapsedTimestampNs;
    it->second.state = ActivationState::kActive;
    mIsActive = true;
@@ -116,12 +120,19 @@ void MetricProducer::setActiveLocked(int64_t currentTimeNs, int64_t remainingTtl
    if (mEventActivationMap.size() == 0) {
        return;
    }
    auto& activation = mEventActivationMap.begin()->second;
    for (auto& pair : mEventActivationMap) {
        auto& activation = pair.second;
        if (activation.ttl_ns >= remainingTtlNs) {
            activation.activation_ns = currentTimeNs + remainingTtlNs - activation.ttl_ns;
            activation.state = kActive;
            mIsActive = true;
    VLOG("setting new activation time to %lld, %lld, %lld", (long long)activation.activation_ns,
         (long long)currentTimeNs, (long long)remainingTtlNs);
            VLOG("setting new activation time to %lld, %lld, %lld",
                 (long long)activation.activation_ns, (long long)currentTimeNs,
                 (long long)remainingTtlNs);
            return;
        }
    }
    ALOGE("Required ttl is longer than all possible activations.");
}

int64_t MetricProducer::getRemainingTtlNsLocked(int64_t currentTimeNs) const {
@@ -135,6 +146,19 @@ int64_t MetricProducer::getRemainingTtlNsLocked(int64_t currentTimeNs) const {
    return maxTtl;
}

void MetricProducer::prepActiveForBootIfNecessaryLocked(int64_t currentTimeNs) {
    if (mActivationType != MetricActivation::ACTIVATE_ON_BOOT) {
        return;
    }
    for (auto& activation : mEventActivationMap) {
        if (activation.second.state == kActiveOnBoot) {
            activation.second.state = kActive;
            activation.second.activation_ns = currentTimeNs;
            mIsActive = true;
        }
    }
}

}  // namespace statsd
}  // namespace os
}  // namespace android
+18 −3
Original line number Diff line number Diff line
@@ -37,12 +37,13 @@ namespace statsd {
// If the metric has no activation requirement, it will be active once the metric producer is
// created.
// If the metric needs to be activated by atoms, the metric producer will start
// with kNotActive state, turn to kActive when the activation event arrives, become kNotActive
// when it reaches the duration limit (timebomb). If the activation event arrives again before
// or after it expires, the event producer will be re-activated and ttl will be reset.
// with kNotActive state, turn to kActive or kActiveOnBoot when the activation event arrives, become
// kNotActive when it reaches the duration limit (timebomb). If the activation event arrives again
// before or after it expires, the event producer will be re-activated and ttl will be reset.
enum ActivationState {
    kNotActive = 0,
    kActive = 1,
    kActiveOnBoot = 2,
};

// A MetricProducer is responsible for compute one single metrics, creating stats log report, and
@@ -218,8 +219,17 @@ public:
        return isActiveLocked();
    }

    void prepActiveForBootIfNecessary(int64_t currentTimeNs) {
        std::lock_guard<std::mutex> lock(mMutex);
        prepActiveForBootIfNecessaryLocked(currentTimeNs);
    }

    void addActivation(int activationTrackerIndex, int64_t ttl_seconds);

    inline void setActivationType(const MetricActivation::ActivationType& activationType) {
        mActivationType = activationType;
    }

    void flushIfExpire(int64_t elapsedTimestampNs);

protected:
@@ -243,6 +253,8 @@ protected:
        return mIsActive;
    }

    void prepActiveForBootIfNecessaryLocked(int64_t currentTimeNs);

    int64_t getRemainingTtlNsLocked(int64_t currentTimeNs) const;

    void setActiveLocked(int64_t currentTimeNs, int64_t remainingTtlNs);
@@ -367,9 +379,12 @@ protected:

    bool mIsActive;

    MetricActivation::ActivationType mActivationType;

    FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);

    FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
    FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
};

}  // namespace statsd
+8 −1
Original line number Diff line number Diff line
@@ -128,7 +128,7 @@ public:
        return mIsActive;
    }

    inline void getActiveMetrics(std::vector<const MetricProducer*>& metrics) const {
    inline void getActiveMetrics(std::vector<MetricProducer*>& metrics) const {
        for (const auto& metric : mAllMetricProducers) {
            if (metric->isActive()) {
                metrics.push_back(metric.get());
@@ -136,6 +136,12 @@ public:
        }
    }

    inline void prepForShutDown(int64_t currentTimeNs) {
        for (const auto& metric : mAllMetricProducers) {
            metric->prepActiveForBootIfNecessary(currentTimeNs);
        }
    }

    void setActiveMetrics(ActiveConfig config, int64_t currentTimeNs);

private:
@@ -267,6 +273,7 @@ private:
    FRIEND_TEST(MetricActivationE2eTest, TestCountMetric);

    FRIEND_TEST(StatsLogProcessorTest, TestActiveConfigMetricDiskWriteRead);
    FRIEND_TEST(StatsLogProcessorTest, TestActivationOnBoot);
};

}  // namespace statsd
Loading