Chi-Tech
TimingLog.h
Go to the documentation of this file.
1#ifndef CHITECH_TIMINGLOG_H
2#define CHITECH_TIMINGLOG_H
3
4#include <cstddef>
5#include <map>
6#include <string>
7#include <vector>
8#include <memory>
9
10namespace chi
11{
12
13class TimingBlock;
14
15/**Utility class for defining time logs.*/
17{
18public:
19 /**Creates a time block and returns a reference to it. If the name
20 * is already taken or the parent name is not found then this method
21 * throws `std::invalid_argument`. The parent name may be empty, i.e. "",
22 * which will revert to the main timing block "ChiTech"*/
23 TimingBlock& CreateTimingBlock(const std::string& name,
24 const std::string& parent_name = "");
25 /**If the timing block with the given exists, this method returns that block,
26 * otherwise it creates a time block and returns a reference to it. If the
27 * parent name is not found then this method throws `std::invalid_argument`.
28 * The parent name may be empty, i.e. "", which will revert to the main timing
29 * block "ChiTech"*/
30 TimingBlock& CreateOrGetTimingBlock(const std::string& name,
31 const std::string& parent_name = "");
32 /**Returns a reference to the timing block with the given name. If a timing
33 * block with that name does not exist then this method will throw
34 * `std::invalid_argument`.*/
35 TimingBlock& GetTimingBlock(const std::string& name);
36
37protected:
38 std::map<std::string, std::unique_ptr<TimingBlock>> timing_blocks_;
39};
40
41/**Hierarchical timing block to efficiently capture timing.*/
43{
44public:
45 /**Constructs the given block with the given name.*/
46 explicit TimingBlock(const std::string& name);
47 /**Deleted copy constructor.*/
48 TimingBlock(const TimingBlock& other) = delete;
49 /**Deleted move constructor.*/
50 TimingBlock(TimingBlock&& other) = delete;
51
52 /**Begins a timing section by resetting the reference time.*/
53 void TimeSectionBegin();
54 /**Ends a timing section, contributes to the total time and adds an
55 * occurrence.*/
56 void TimeSectionEnd();
57
58 /**Returns the number of timing section occurrences.*/
59 size_t NumberOfOccurences() const;
60 /**Returns the computed total time over all occurrences.*/
61 double TotalTime() const;
62 /**Returns the average time per occurence.*/
63 double AverageTime() const;
64 /**Returns the last computed time differential.*/
65 double LastDelta() const;
66
67 /**Makes a string table of the timing block and all its children.*/
68 std::string MakeGraphString();
69
70protected:
71 friend class TimingLog;
72 /**Adds the supplied timing black as a child.*/
73 void AddChild(const TimingBlock& child_block);
74 /**Used when building the graph, this is a recursive function that adds
75 * each entry to a matrix.*/
76 void AppendGraphEntry(std::vector<std::vector<std::string>>& string_matrix,
77 const TimingBlock* parent,
78 const std::string& indent) const;
79
80 const std::string name_;
81 size_t num_occurences_ = 0;
82 double total_time_ = 0.0;
83 double reference_time_ = 0.0;
84 double last_delta_time_ = 0.0;
85 std::vector<const TimingBlock*> children_;
86};
87
88} // namespace chi
89
90#endif // CHITECH_TIMINGLOG_H
void TimeSectionEnd()
Definition: TimingLog.cc:83
void AppendGraphEntry(std::vector< std::vector< std::string > > &string_matrix, const TimingBlock *parent, const std::string &indent) const
Definition: TimingLog.cc:197
double TotalTime() const
Definition: TimingLog.cc:93
TimingBlock(const std::string &name)
Definition: TimingLog.cc:76
double last_delta_time_
Definition: TimingLog.h:84
double reference_time_
Definition: TimingLog.h:83
void TimeSectionBegin()
Definition: TimingLog.cc:78
double total_time_
Definition: TimingLog.h:82
void AddChild(const TimingBlock &child_block)
Definition: TimingLog.cc:107
const std::string name_
Definition: TimingLog.h:80
double LastDelta() const
Definition: TimingLog.cc:102
double AverageTime() const
Definition: TimingLog.cc:95
size_t NumberOfOccurences() const
Definition: TimingLog.cc:91
std::vector< const TimingBlock * > children_
Definition: TimingLog.h:85
size_t num_occurences_
Definition: TimingLog.h:81
std::string MakeGraphString()
Definition: TimingLog.cc:112
TimingBlock(TimingBlock &&other)=delete
TimingBlock(const TimingBlock &other)=delete
std::map< std::string, std::unique_ptr< TimingBlock > > timing_blocks_
Definition: TimingLog.h:38
TimingBlock & CreateTimingBlock(const std::string &name, const std::string &parent_name="")
Definition: TimingLog.cc:15
TimingBlock & CreateOrGetTimingBlock(const std::string &name, const std::string &parent_name="")
Definition: TimingLog.cc:53
TimingBlock & GetTimingBlock(const std::string &name)
Definition: TimingLog.cc:63