ActionAnalyzer.hh 2.89 KB
Newer Older
Philip Trettner's avatar
Philip Trettner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once

#include <vector>
#include <map>
#include <cmath>
#include <chrono>
#include <ostream>

#include "common/property.hh"
#include "common/shared.hh"

namespace aion
{
class Action;
class ActionLabel;
AION_SHARED(class, ActionTree);
AION_SHARED(class, ActionAnalyzer);

/// Light class for analyzing a number of actions
/// Does pin the tree so should be relatively resistant
/// Moment-based statistics are calculated immidiately, percentile-based ones are deferred (except min/max)
/// Should not be used/trusted with zero entries
/// Times are either in NS or Secs
class ActionAnalyzer
{
private:
    /// pinned tree
    SharedActionTree mTree;

    /// analyzed actions
    std::vector<Action*> mActions;

    /// true iff actions already sorted
    bool mSorted = false;

    /// statistical moments
    int64_t mCount;
    int64_t mSumNs;
    double mVariance;
    int64_t mMin;
    int64_t mMax;

public: // properties
Philip Trettner's avatar
Philip Trettner committed
44
45
    AION_GETTER(Tree);
    AION_GETTER(Actions);
Philip Trettner's avatar
Philip Trettner committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

    int64_t count() const { return mCount; }
    int64_t totalTimeNS() const { return mSumNs; }
    double totalTime() const { return mSumNs * 1.e-9; }
    double averageNS() const { return mCount == 0 ? 0LL : mSumNs / mCount; }
    double average() const { return averageNS() * 1.e-9; }
    double varianceNS() const { return mVariance; }
    double variance() const { return mVariance * 1.e-18; }
    double standardDeviationNS() const { return std::sqrt(mVariance); }
    double standardDeviation() const { return std::sqrt(mVariance) * 1.e-9; }
    int64_t minNS() const { return mMin; }
    double min() const { return mMin * 1.e-9; }
    int64_t maxNS() const { return mMax; }
    double max() const { return mMax * 1.e-9; }
    /// returns the p-th percentile (p in [0, 100])
    int64_t percentileNS(int p);
Philip Trettner's avatar
Philip Trettner committed
62
    double percentile(int p) { return percentileNS(p) * 1.e-9; }
Philip Trettner's avatar
Philip Trettner committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    int64_t medianNS() { return percentileNS(50); }
    double median() { return medianNS() * 1.e-9; }    
public:
    /// generic constructor for a given set of actions
    ActionAnalyzer(SharedActionTree const& tree, std::vector<Action*> const& actions);
    ActionAnalyzer(SharedActionTree const& tree, std::vector<Action> const& actions);

    ActionAnalyzer(ActionAnalyzer&&) = default;     // move yes
    ActionAnalyzer(ActionAnalyzer const&) = delete; // copy no

    /// returns a new analyzer for every encountered label
    std::map<ActionLabel*, SharedActionAnalyzer> byLabel() const;

private:
    /// constructs a new analyzer without any actions at all
    ActionAnalyzer(SharedActionTree const& tree);

    /// sets actions and does basic math
    void setActions(std::vector<Action*> const& actions);
    void addAction(Action* a);
    void initActions();

    /// sorts the actions by duration if not already done
    void sortIfRequired();

public:
    /// dumps a summary of all actions into the given stream
    static void dumpSummary(std::ostream& oss, bool verbose);
};
}