blob: aabeb292939e566cc45ec356477dc996099db3c0 [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
11/// This file declares the TimerTree class, which allows flat and
12/// cumulative execution time collection of call chains.
13///
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 Scull9612d322015-07-06 14:53:25 -070029 /// Timer tree index type. A variable of this type is used to access
30 /// an interior, not-necessarily-leaf node of the tree.
Jim Stichnoth7e571362015-01-09 11:43:26 -080031 typedef std::vector<class TimerTreeNode>::size_type TTindex;
Andrew Scull9612d322015-07-06 14:53:25 -070032 /// Representation of a path of leaf values leading to a particular
33 /// node. The representation happens to be in "reverse" order,
34 /// i.e. from leaf/interior to root, for implementation efficiency.
Jim Stichnoth380d7b92015-01-30 13:10:39 -080035 typedef llvm::SmallVector<TTindex, 8> PathType;
Andrew Scull9612d322015-07-06 14:53:25 -070036 /// Representation of a mapping of leaf node indexes from one timer
37 /// stack to another.
Jim Stichnoth380d7b92015-01-30 13:10:39 -080038 typedef std::vector<TimerIdT> TranslationType;
Jim Stichnoth7e571362015-01-09 11:43:26 -080039
Andrew Scull9612d322015-07-06 14:53:25 -070040 /// TimerTreeNode represents an interior or leaf node in the call tree.
41 /// It contains a list of children, a pointer to its parent, and the
42 /// timer ID for the node. It also holds the cumulative time spent at
43 /// this node and below. The children are always at a higher index in
44 /// the TimerTreeNode::Nodes array, and the parent is always at a lower
45 /// index.
Jim Stichnoth7e571362015-01-09 11:43:26 -080046 class TimerTreeNode {
47 TimerTreeNode &operator=(const TimerTreeNode &) = delete;
48
49 public:
Jim Stichnotheafb56c2015-06-22 10:35:22 -070050 TimerTreeNode() = default;
Jim Stichnoth7e571362015-01-09 11:43:26 -080051 TimerTreeNode(const TimerTreeNode &) = default;
52 std::vector<TTindex> Children; // indexed by TimerIdT
Jim Stichnotheafb56c2015-06-22 10:35:22 -070053 TTindex Parent = 0;
54 TimerIdT Interior = 0;
55 double Time = 0;
56 size_t UpdateCount = 0;
Jim Stichnoth7e571362015-01-09 11:43:26 -080057 };
58
Jim Stichnothc4554d72014-09-30 16:49:38 -070059public:
Jim Stichnoth8363a062014-10-07 10:02:38 -070060 enum TimerTag {
61#define X(tag) TT_##tag,
62 TIMERTREE_TABLE
63#undef X
64 TT__num
65 };
Jim Stichnothc6ead202015-02-24 09:30:30 -080066 explicit TimerStack(const IceString &Name);
Jim Stichnoth7e571362015-01-09 11:43:26 -080067 TimerStack(const TimerStack &) = default;
Jim Stichnoth8363a062014-10-07 10:02:38 -070068 TimerIdT getTimerID(const IceString &Name);
Jim Stichnoth380d7b92015-01-30 13:10:39 -080069 void mergeFrom(const TimerStack &Src);
Jim Stichnothd14b1a02014-10-08 08:28:36 -070070 void setName(const IceString &NewName) { Name = NewName; }
Jim Stichnoth380d7b92015-01-30 13:10:39 -080071 const IceString &getName() const { return Name; }
Jim Stichnothc4554d72014-09-30 16:49:38 -070072 void push(TimerIdT ID);
73 void pop(TimerIdT ID);
Jim Stichnothd14b1a02014-10-08 08:28:36 -070074 void reset();
Jim Stichnoth8363a062014-10-07 10:02:38 -070075 void dump(Ostream &Str, bool DumpCumulative);
Jim Stichnothc4554d72014-09-30 16:49:38 -070076
77private:
Jim Stichnothabce6e52014-10-14 11:09:27 -070078 void update(bool UpdateCounts);
Jim Stichnoth9c234e22014-10-01 09:28:21 -070079 static double timestamp();
Jim Stichnoth380d7b92015-01-30 13:10:39 -080080 TranslationType translateIDsFrom(const TimerStack &Src);
81 PathType getPath(TTindex Index, const TranslationType &Mapping) const;
82 TTindex getChildIndex(TTindex Parent, TimerIdT ID);
83 TTindex findPath(const PathType &Path);
Jim Stichnothd14b1a02014-10-08 08:28:36 -070084 IceString Name;
85 double FirstTimestamp;
Jim Stichnothc4554d72014-09-30 16:49:38 -070086 double LastTimestamp;
Jim Stichnotheafb56c2015-06-22 10:35:22 -070087 uint64_t StateChangeCount = 0;
Andrew Scull9612d322015-07-06 14:53:25 -070088 /// IDsIndex maps a symbolic timer name to its integer ID.
Jim Stichnoth8363a062014-10-07 10:02:38 -070089 std::map<IceString, TimerIdT> IDsIndex;
Andrew Scull9612d322015-07-06 14:53:25 -070090 std::vector<IceString> IDs; /// indexed by TimerIdT
91 std::vector<TimerTreeNode> Nodes; /// indexed by TTindex
92 std::vector<double> LeafTimes; /// indexed by TimerIdT
93 std::vector<size_t> LeafCounts; /// indexed by TimerIdT
Jim Stichnotheafb56c2015-06-22 10:35:22 -070094 TTindex StackTop = 0;
Jim Stichnothc4554d72014-09-30 16:49:38 -070095};
96
97} // end of namespace Ice
98
99#endif // SUBZERO_SRC_ICETIMERTREE_H