blob: 5f068c171d53b1b6a59f926b9b45ca6e54109999 [file] [log] [blame]
Jim Stichnothc4554d72014-09-30 16:49:38 -07001//===- subzero/src/IceTimerTree.h - Pass timer defs -------------*- C++ -*-===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Andrew Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
Jim Stichnoth92a6e5b2015-12-02 16:52:44 -080011/// \brief Declares the TimerTree class, which allows flat and cumulative
Andrew Scull57e12682015-09-16 11:30:19 -070012/// execution time collection of call chains.
Andrew Scull9612d322015-07-06 14:53:25 -070013///
Jim Stichnothc4554d72014-09-30 16:49:38 -070014//===----------------------------------------------------------------------===//
15
16#ifndef SUBZERO_SRC_ICETIMERTREE_H
17#define SUBZERO_SRC_ICETIMERTREE_H
18
John Porto67f8de92015-06-25 10:14:17 -070019// TODO(jpp): Refactor IceDefs.
20#include "IceDefs.h"
Jim Stichnoth8363a062014-10-07 10:02:38 -070021#include "IceTimerTree.def"
22
Jim Stichnothc4554d72014-09-30 16:49:38 -070023namespace Ice {
24
Jim Stichnothc4554d72014-09-30 16:49:38 -070025class TimerStack {
Jim Stichnothc6ead202015-02-24 09:30:30 -080026 TimerStack() = delete;
Jim Stichnoth0795ba02014-10-01 14:23:01 -070027 TimerStack &operator=(const TimerStack &) = delete;
Jim Stichnothc4554d72014-09-30 16:49:38 -070028
Andrew Scull57e12682015-09-16 11:30:19 -070029 /// Timer tree index type. A variable of this type is used to access an
30 /// interior, not-necessarily-leaf node of the tree.
Andrew Scull8072bae2015-09-14 16:01:26 -070031 using TTindex = std::vector<class TimerTreeNode>::size_type;
Andrew Scull57e12682015-09-16 11:30:19 -070032 /// Representation of a path of leaf values leading to a particular node. The
33 /// representation happens to be in "reverse" order, i.e. from leaf/interior
34 /// to root, for implementation efficiency.
Andrew Scull8072bae2015-09-14 16:01:26 -070035 using PathType = llvm::SmallVector<TTindex, 8>;
Andrew Scull57e12682015-09-16 11:30:19 -070036 /// Representation of a mapping of leaf node indexes from one timer stack to
37 /// another.
Andrew Scull8072bae2015-09-14 16:01:26 -070038 using TranslationType = std::vector<TimerIdT>;
Jim Stichnoth7e571362015-01-09 11:43:26 -080039
Andrew Scull57e12682015-09-16 11:30:19 -070040 /// TimerTreeNode represents an interior or leaf node in the call tree. It
41 /// contains a list of children, a pointer to its parent, and the timer ID for
42 /// the node. It also holds the cumulative time spent at this node and below.
43 /// The children are always at a higher index in the TimerTreeNode::Nodes
44 /// array, and the parent is always at a lower index.
Jim Stichnoth7e571362015-01-09 11:43:26 -080045 class TimerTreeNode {
46 TimerTreeNode &operator=(const TimerTreeNode &) = delete;
47
48 public:
Jim Stichnotheafb56c2015-06-22 10:35:22 -070049 TimerTreeNode() = default;
Jim Stichnoth7e571362015-01-09 11:43:26 -080050 TimerTreeNode(const TimerTreeNode &) = default;
51 std::vector<TTindex> Children; // indexed by TimerIdT
Jim Stichnotheafb56c2015-06-22 10:35:22 -070052 TTindex Parent = 0;
53 TimerIdT Interior = 0;
54 double Time = 0;
55 size_t UpdateCount = 0;
Jim Stichnoth7e571362015-01-09 11:43:26 -080056 };
57
Jim Stichnothc4554d72014-09-30 16:49:38 -070058public:
Jim Stichnoth8363a062014-10-07 10:02:38 -070059 enum TimerTag {
60#define X(tag) TT_##tag,
61 TIMERTREE_TABLE
62#undef X
63 TT__num
64 };
Jim Stichnoth467ffe52016-03-29 15:01:06 -070065 explicit TimerStack(const std::string &Name);
Jim Stichnoth7e571362015-01-09 11:43:26 -080066 TimerStack(const TimerStack &) = default;
Jim Stichnoth467ffe52016-03-29 15:01:06 -070067 TimerIdT getTimerID(const std::string &Name);
Jim Stichnoth380d7b92015-01-30 13:10:39 -080068 void mergeFrom(const TimerStack &Src);
Jim Stichnoth467ffe52016-03-29 15:01:06 -070069 void setName(const std::string &NewName) { Name = NewName; }
70 const std::string &getName() const { return Name; }
Jim Stichnothc4554d72014-09-30 16:49:38 -070071 void push(TimerIdT ID);
72 void pop(TimerIdT ID);
Jim Stichnothd14b1a02014-10-08 08:28:36 -070073 void reset();
Jim Stichnoth8363a062014-10-07 10:02:38 -070074 void dump(Ostream &Str, bool DumpCumulative);
Jim Stichnothc4554d72014-09-30 16:49:38 -070075
76private:
Jim Stichnothabce6e52014-10-14 11:09:27 -070077 void update(bool UpdateCounts);
Jim Stichnoth9c234e22014-10-01 09:28:21 -070078 static double timestamp();
Jim Stichnoth380d7b92015-01-30 13:10:39 -080079 TranslationType translateIDsFrom(const TimerStack &Src);
80 PathType getPath(TTindex Index, const TranslationType &Mapping) const;
81 TTindex getChildIndex(TTindex Parent, TimerIdT ID);
82 TTindex findPath(const PathType &Path);
Jim Stichnoth467ffe52016-03-29 15:01:06 -070083 std::string Name;
Jim Stichnothd14b1a02014-10-08 08:28:36 -070084 double FirstTimestamp;
Jim Stichnothc4554d72014-09-30 16:49:38 -070085 double LastTimestamp;
Jim Stichnotheafb56c2015-06-22 10:35:22 -070086 uint64_t StateChangeCount = 0;
Andrew Scull9612d322015-07-06 14:53:25 -070087 /// IDsIndex maps a symbolic timer name to its integer ID.
Jim Stichnoth467ffe52016-03-29 15:01:06 -070088 std::map<std::string, TimerIdT> IDsIndex;
89 std::vector<std::string> IDs; /// indexed by TimerIdT
Andrew Scull9612d322015-07-06 14:53:25 -070090 std::vector<TimerTreeNode> Nodes; /// indexed by TTindex
91 std::vector<double> LeafTimes; /// indexed by TimerIdT
92 std::vector<size_t> LeafCounts; /// indexed by TimerIdT
Jim Stichnotheafb56c2015-06-22 10:35:22 -070093 TTindex StackTop = 0;
Jim Stichnothc4554d72014-09-30 16:49:38 -070094};
95
96} // end of namespace Ice
97
98#endif // SUBZERO_SRC_ICETIMERTREE_H