David Blaikie | 87299ad | 2017-01-16 20:36:26 +0000 | [diff] [blame^] | 1 | //===-- xray-graph.h - XRay Function Call Graph Renderer --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Generate a DOT file to represent the function call graph encountered in |
| 11 | // the trace. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef XRAY_GRAPH_H |
| 16 | #define XRAY_GRAPH_H |
| 17 | |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "func-id-helper.h" |
| 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/XRay/Trace.h" |
| 25 | #include "llvm/XRay/XRayRecord.h" |
| 26 | |
| 27 | namespace llvm { |
| 28 | namespace xray { |
| 29 | |
| 30 | /// A class encapsulating the logic related to analyzing XRay traces, producting |
| 31 | /// Graphs from them and then exporting those graphs for review. |
| 32 | class GraphRenderer { |
| 33 | public: |
| 34 | /// An inner struct for common timing statistics information |
| 35 | struct TimeStat { |
| 36 | uint64_t Count; |
| 37 | double Min; |
| 38 | double Median; |
| 39 | double Pct90; |
| 40 | double Pct99; |
| 41 | double Max; |
| 42 | double Sum; |
| 43 | }; |
| 44 | |
| 45 | /// An inner struct for storing edge attributes for our graph. Here the |
| 46 | /// attributes are mainly function call statistics. |
| 47 | /// |
| 48 | /// FIXME: expand to contain more information eg call latencies. |
| 49 | struct EdgeAttribute { |
| 50 | TimeStat S; |
| 51 | std::vector<uint64_t> Timings; |
| 52 | }; |
| 53 | |
| 54 | /// An Inner Struct for storing vertex attributes, at the moment just |
| 55 | /// SymbolNames, however in future we could store bulk function statistics. |
| 56 | /// |
| 57 | /// FIXME: Store more attributes based on instrumentation map. |
| 58 | struct VertexAttribute { |
| 59 | std::string SymbolName; |
| 60 | TimeStat S; |
| 61 | }; |
| 62 | |
| 63 | private: |
| 64 | /// The Graph stored in an edge-list like format, with the edges also having |
| 65 | /// An attached set of attributes. |
| 66 | DenseMap<int32_t, DenseMap<int32_t, EdgeAttribute>> Graph; |
| 67 | |
| 68 | /// Graph Vertex Attributes. These are presently stored seperate from the |
| 69 | /// main graph. |
| 70 | DenseMap<int32_t, VertexAttribute> VertexAttrs; |
| 71 | |
| 72 | struct FunctionAttr { |
| 73 | int32_t FuncId; |
| 74 | uint64_t TSC; |
| 75 | }; |
| 76 | |
| 77 | /// Use a Map to store the Function stack for each thread whilst building the |
| 78 | /// graph. |
| 79 | /// |
| 80 | /// FIXME: Perhaps we can Build this into LatencyAccountant? or vise versa? |
| 81 | DenseMap<pid_t, SmallVector<FunctionAttr, 4>> PerThreadFunctionStack; |
| 82 | |
| 83 | /// Usefull object for getting human readable Symbol Names. |
| 84 | FuncIdConversionHelper &FuncIdHelper; |
| 85 | bool DeduceSiblingCalls = false; |
| 86 | uint64_t CurrentMaxTSC = 0; |
| 87 | |
| 88 | /// A private function to help implement the statistic generation functions; |
| 89 | template <typename U> |
| 90 | void getStats(U begin, U end, GraphRenderer::TimeStat &S); |
| 91 | |
| 92 | /// Calculates latency statistics for each edge and stores the data in the |
| 93 | /// Graph |
| 94 | void calculateEdgeStatistics(); |
| 95 | |
| 96 | /// Calculates latency statistics for each vertex and stores the data in the |
| 97 | /// Graph |
| 98 | void calculateVertexStatistics(); |
| 99 | |
| 100 | /// Normalises latency statistics for each edge and vertex by CycleFrequency; |
| 101 | void normaliseStatistics(double CycleFrequency); |
| 102 | |
| 103 | public: |
| 104 | /// Takes in a reference to a FuncIdHelper in order to have ready access to |
| 105 | /// Symbol names. |
| 106 | explicit GraphRenderer(FuncIdConversionHelper &FuncIdHelper, bool DSC) |
| 107 | : FuncIdHelper(FuncIdHelper), DeduceSiblingCalls(DSC) {} |
| 108 | |
| 109 | /// Process an Xray record and expand the graph. |
| 110 | /// |
| 111 | /// This Function will return true on success, or false if records are not |
| 112 | /// presented in per-thread call-tree DFS order. (That is for each thread the |
| 113 | /// Records should be in order runtime on an ideal system.) |
| 114 | /// |
| 115 | /// FIXME: Make this more robust against small irregularities. |
| 116 | bool accountRecord(const XRayRecord &Record); |
| 117 | |
| 118 | /// An enum for enumerating the various statistics gathered on latencies |
| 119 | enum class StatType { COUNT, MIN, MED, PCT90, PCT99, MAX, SUM }; |
| 120 | |
| 121 | /// Output the Embedded graph in DOT format on \p OS, labeling the edges by |
| 122 | /// \p T |
| 123 | void exportGraphAsDOT(raw_ostream &OS, const XRayFileHeader &H, |
| 124 | StatType T = StatType::COUNT); |
| 125 | }; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | #endif // XRAY_GRAPH_H |