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

Commit 4aa16773 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "One more feature in DurationMetric -- nesting on top of Or and Max"

parents b3bd84d7 0ea19901
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ const int FIELD_ID_DURATION = 3;
DurationMetricProducer::DurationMetricProducer(const DurationMetric& metric,
                                               const int conditionIndex, const size_t startIndex,
                                               const size_t stopIndex, const size_t stopAllIndex,
                                               const bool nesting,
                                               const sp<ConditionWizard>& wizard,
                                               const vector<KeyMatcher>& internalDimension,
                                               const uint64_t startTimeNs)
@@ -71,6 +72,7 @@ DurationMetricProducer::DurationMetricProducer(const DurationMetric& metric,
      mStartIndex(startIndex),
      mStopIndex(stopIndex),
      mStopAllIndex(stopAllIndex),
      mNested(nesting),
      mInternalDimension(internalDimension) {
    // TODO: The following boiler plate code appears in all MetricProducers, but we can't abstract
    // them in the base class, because the proto generated CountMetric, and DurationMetric are
@@ -111,11 +113,11 @@ unique_ptr<DurationTracker> DurationMetricProducer::createDurationTracker(
        vector<DurationBucket>& bucket) {
    switch (mMetric.type()) {
        case DurationMetric_AggregationType_DURATION_SUM:
            return make_unique<OringDurationTracker>(mWizard, mConditionTrackerIndex,
            return make_unique<OringDurationTracker>(mWizard, mConditionTrackerIndex, mNested,
                                                     mCurrentBucketStartTimeNs, mBucketSizeNs,
                                                     bucket);
        case DurationMetric_AggregationType_DURATION_MAX_SPARSE:
            return make_unique<MaxDurationTracker>(mWizard, mConditionTrackerIndex,
            return make_unique<MaxDurationTracker>(mWizard, mConditionTrackerIndex, mNested,
                                                   mCurrentBucketStartTimeNs, mBucketSizeNs,
                                                   bucket);
    }
@@ -270,7 +272,7 @@ void DurationMetricProducer::onMatchedLogEventInternal(
    if (matcherIndex == mStartIndex) {
        it->second->noteStart(atomKey, condition, event.GetTimestampNs(), conditionKeys);
    } else if (matcherIndex == mStopIndex) {
        it->second->noteStop(atomKey, event.GetTimestampNs());
        it->second->noteStop(atomKey, event.GetTimestampNs(), false);
    }
}

+5 −1
Original line number Diff line number Diff line
@@ -39,7 +39,8 @@ class DurationMetricProducer : public MetricProducer {
public:
    DurationMetricProducer(const DurationMetric& durationMetric, const int conditionIndex,
                           const size_t startIndex, const size_t stopIndex,
                           const size_t stopAllIndex, const sp<ConditionWizard>& wizard,
                           const size_t stopAllIndex, const bool nesting,
                           const sp<ConditionWizard>& wizard,
                           const vector<KeyMatcher>& internalDimension, const uint64_t startTimeNs);

    virtual ~DurationMetricProducer();
@@ -80,6 +81,9 @@ private:
    // Index of the SimpleLogEntryMatcher which defines the stop all for all dimensions.
    const size_t mStopAllIndex;

    // nest counting -- for the same key, stops must match the number of starts to make real stop
    const bool mNested;

    // The dimension from the atom predicate. e.g., uid, wakelock name.
    const vector<KeyMatcher> mInternalDimension;

+17 −8
Original line number Diff line number Diff line
@@ -34,6 +34,10 @@ enum DurationState {
// Hold duration information for one atom level duration in current on-going bucket.
struct DurationInfo {
    DurationState state;

    // the number of starts seen.
    int32_t startCount;

    // most recent start time.
    int64_t lastStartTime;
    // existing duration in current bucket.
@@ -42,7 +46,7 @@ struct DurationInfo {
    // cache the HashableDimensionKeys we need to query the condition for this duration event.
    ConditionKey conditionKeys;

    DurationInfo() : state(kStopped), lastStartTime(0), lastDuration(0){};
    DurationInfo() : state(kStopped), startCount(0), lastStartTime(0), lastDuration(0){};
};

struct DurationBucket {
@@ -53,18 +57,21 @@ struct DurationBucket {

class DurationTracker {
public:
    DurationTracker(sp<ConditionWizard> wizard, int conditionIndex, uint64_t currentBucketStartNs,
                    uint64_t bucketSizeNs, std::vector<DurationBucket>& bucket)
    DurationTracker(sp<ConditionWizard> wizard, int conditionIndex, bool nesting,
                    uint64_t currentBucketStartNs, uint64_t bucketSizeNs,
                    std::vector<DurationBucket>& bucket)
        : mWizard(wizard),
          mConditionTrackerIndex(conditionIndex),
          mCurrentBucketStartTimeNs(currentBucketStartNs),
          mBucketSizeNs(bucketSizeNs),
          mNested(nesting),
          mCurrentBucketStartTimeNs(currentBucketStartNs),
          mBucket(bucket),
          mDuration(0){};
    virtual ~DurationTracker(){};
    virtual void noteStart(const HashableDimensionKey& key, bool condition,
                           const uint64_t eventTime, const ConditionKey& conditionKey) = 0;
    virtual void noteStop(const HashableDimensionKey& key, const uint64_t eventTime) = 0;
    virtual void noteStop(const HashableDimensionKey& key, const uint64_t eventTime,
                          const bool stopAll) = 0;
    virtual void noteStopAll(const uint64_t eventTime) = 0;
    virtual void onSlicedConditionMayChange(const uint64_t timestamp) = 0;
    virtual void onConditionChanged(bool condition, const uint64_t timestamp) = 0;
@@ -75,11 +82,13 @@ public:
protected:
    sp<ConditionWizard> mWizard;

    int mConditionTrackerIndex;
    const int mConditionTrackerIndex;

    uint64_t mCurrentBucketStartTimeNs;
    const int64_t mBucketSizeNs;

    const bool mNested;

    int64_t mBucketSizeNs;
    uint64_t mCurrentBucketStartTimeNs;

    std::vector<DurationBucket>& mBucket;  // where to write output

+25 −14
Original line number Diff line number Diff line
@@ -23,10 +23,10 @@ namespace android {
namespace os {
namespace statsd {

MaxDurationTracker::MaxDurationTracker(sp<ConditionWizard> wizard, int conditionIndex,
MaxDurationTracker::MaxDurationTracker(sp<ConditionWizard> wizard, int conditionIndex, bool nesting,
                                       uint64_t currentBucketStartNs, uint64_t bucketSizeNs,
                                       std::vector<DurationBucket>& bucket)
    : DurationTracker(wizard, conditionIndex, currentBucketStartNs, bucketSizeNs, bucket) {
    : DurationTracker(wizard, conditionIndex, nesting, currentBucketStartNs, bucketSizeNs, bucket) {
}

void MaxDurationTracker::noteStart(const HashableDimensionKey& key, bool condition,
@@ -38,10 +38,10 @@ void MaxDurationTracker::noteStart(const HashableDimensionKey& key, bool conditi

    switch (duration.state) {
        case kStarted:
            // The same event is already started. Because we are not counting nesting, so ignore.
            duration.startCount++;
            break;
        case kPaused:
            // Safe to do nothing here. Paused means started but condition is false.
            duration.startCount++;
            break;
        case kStopped:
            if (!condition) {
@@ -51,11 +51,13 @@ void MaxDurationTracker::noteStart(const HashableDimensionKey& key, bool conditi
                duration.state = DurationState::kStarted;
                duration.lastStartTime = eventTime;
            }
            duration.startCount = 1;
            break;
    }
}

void MaxDurationTracker::noteStop(const HashableDimensionKey& key, const uint64_t eventTime) {
void MaxDurationTracker::noteStop(const HashableDimensionKey& key, const uint64_t eventTime,
                                  bool forceStop) {
    VLOG("MaxDuration: key %s stop", key.c_str());
    if (mInfos.find(key) == mInfos.end()) {
        // we didn't see a start event before. do nothing.
@@ -68,16 +70,23 @@ void MaxDurationTracker::noteStop(const HashableDimensionKey& key, const uint64_
            // already stopped, do nothing.
            break;
        case DurationState::kStarted: {
            duration.startCount--;
            if (forceStop || !mNested || duration.startCount <= 0) {
                duration.state = DurationState::kStopped;
                int64_t durationTime = eventTime - duration.lastStartTime;
            VLOG("Max, key %s, Stop %lld %lld %lld", key.c_str(), (long long)duration.lastStartTime,
                 (long long)eventTime, (long long)durationTime);
                VLOG("Max, key %s, Stop %lld %lld %lld", key.c_str(),
                     (long long)duration.lastStartTime, (long long)eventTime,
                     (long long)durationTime);
                duration.lastDuration = duration.lastDuration + durationTime;
                VLOG("  record duration: %lld ", (long long)duration.lastDuration);
            }
            break;
        }
        case DurationState::kPaused: {
            duration.startCount--;
            if (forceStop || !mNested || duration.startCount <= 0) {
                duration.state = DurationState::kStopped;
            }
            break;
        }
    }
@@ -88,11 +97,13 @@ void MaxDurationTracker::noteStop(const HashableDimensionKey& key, const uint64_
    }
    // Once an atom duration ends, we erase it. Next time, if we see another atom event with the
    // same name, they are still considered as different atom durations.
    if (duration.state == DurationState::kStopped) {
        mInfos.erase(key);
    }
}
void MaxDurationTracker::noteStopAll(const uint64_t eventTime) {
    for (auto& pair : mInfos) {
        noteStop(pair.first, eventTime);
        noteStop(pair.first, eventTime, true);
    }
}

+3 −2
Original line number Diff line number Diff line
@@ -28,12 +28,13 @@ namespace statsd {
// they stop or bucket expires.
class MaxDurationTracker : public DurationTracker {
public:
    MaxDurationTracker(sp<ConditionWizard> wizard, int conditionIndex,
    MaxDurationTracker(sp<ConditionWizard> wizard, int conditionIndex, bool nesting,
                       uint64_t currentBucketStartNs, uint64_t bucketSizeNs,
                       std::vector<DurationBucket>& bucket);
    void noteStart(const HashableDimensionKey& key, bool condition, const uint64_t eventTime,
                   const ConditionKey& conditionKey) override;
    void noteStop(const HashableDimensionKey& key, const uint64_t eventTime) override;
    void noteStop(const HashableDimensionKey& key, const uint64_t eventTime,
                  const bool stopAll) override;
    void noteStopAll(const uint64_t eventTime) override;
    bool flushIfNeeded(uint64_t timestampNs) override;
    void onSlicedConditionMayChange(const uint64_t timestamp) override;
Loading