%!% !TEX program = xelatex
%!% !TEX options = --shell-escape
\documentclass{standalone}

\usepackage{fontspec}
\setsansfont{Source Sans Pro}[BoldFont={* Semibold}, BoldItalicFont={* Semibold Italic}]
\setmainfont{Source Sans Pro}[BoldFont={* Semibold}, BoldItalicFont={* Semibold Italic}]
\newfontfamily{\lightfont}{Source Sans Pro Light}[Scale=MatchUppercase, ItalicFont={* Italic}, BoldFont={Source Sans Pro}, BoldItalicFont={Source Sans Pro Italic}]
\newcommand\textlight[1]{{\lightfont #1}}
\newfontfamily{\boldfont}{Source Sans Pro Bold}[BoldFont={Source Sans Pro Black}]
\newcommand\textbold[1]{{\boldfont #1}}
\newfontfamily{\boldboldfont}{Source Sans Pro Black}
\newcommand\textboldbold[1]{{\boldboldfont #1}}
% \setmonofont{Source Code Pro}[Scale=MatchLowercase]
\setmonofont{Fira Mono}[Scale=MatchLowercase, ItalicFont={Source Code Pro Italic}, BoldFont={* Medium}]%
\usepackage{xcolor-solarized}
\usepackage{tikz}
\usepackage{relsize}
\title{POWER8NVL CPI Stack}
\author{Andreas Herten}
\usepackage[pdfusetitle]{hyperref}
\usetikzlibrary{fit, trees, positioning}


\let\oldtt\texttt
\renewcommand{\texttt}[1]{\smaller\oldtt{#1}}

\begin{document}
	% level distance = distance in horizontal direction
	% sibling distance = distance to top and bottom
	\begin{tikzpicture}[
		font=\footnotesize,
		grow=right,
		align=center,
		% every node/.style={draw},
		every label/.style={text=gray, font=\itshape\relsize{-4}},
		label distance=-1mm,
		level 1/.style={sibling distance=5cm, level distance=3.1cm},
		level 2/.style={sibling distance=1.2cm, level distance=4.4cm},
		level 3/.style={sibling distance=1.5cm, level distance=4.4cm},
		level 4/.style={sibling distance=0.7cm, level distance=4.1cm},
		level 5/.style={sibling distance=0.9cm, level distance=5cm},
		% lsu/.style={sibling distance=0.45cm},
		stall/.style={},
		stall lsu/.style={sibling distance=0.9cm},
		stall lsu reject/.style={sibling distance=0.9cm},
		stall lsu dcache miss/.style={sibling distance=1.2cm},
		stall lsu dcache miss l3 miss/.style={sibling distance=0.9cm, yshift=-1.4cm},
		stall lsu dcache miss l2l3 hit/.style={sibling distance=1.1cm, yshift=0.4cm},
		derived/.style={fill=gray!10},
		stalled/.style={draw=orange, densely dashed},
		blocked/.style={draw=red, densely dotted},
		empty/.style={draw=solarized-violet, dotted},
		table empty/.style={sibling distance=1.cm},
		table empty dispatch held/.style={sibling distance=1.cm},
		table empty icache miss/.style=table empty dispatch held,
		thread/.style={sibling distance=0.9cm}
	]
		\node [draw, thick] (cycles) {Cycles\\\texttt{PM\_RUN\_CYC}}
			[edge from parent fork right]
			child [sibling distance=3.2cm] {node [derived] (other) {Other\\\texttt{OTHER\_CPI}}}
			child [sibling distance=3.8cm] {node (table empty) [empty, solid] {Nothing to Dispatch\\\texttt{PM\_GCT\_NOSLOT\_CYC}}
				child [table empty] {node [empty, derived] {Other}}
				child [table empty, yshift=-2mm] {node [empty, derived] {Dispatch Held}
					child [table empty dispatch held] {node [empty] {Dispatch Held: Other\\\texttt{PM\_GCT\_NOSLOT\_DISP\_HELD\_OTHER}}}
					child [table empty dispatch held] {node [empty] {Dispatch Held: Issue Queue\\\texttt{PM\_GCT\_NOSLOT\_DISP\_HELD\_ISSQ}}}
					child [table empty dispatch held] {node [empty] {Dispatch Held: Store Queue\\\texttt{PM\_GCT\_NOSLOT\_DISP\_HELD\_SRQ}}}
					child [table empty dispatch held] {node [empty] {Dispatch Held: Mapper\\\texttt{PM\_GCT\_NOSLOT\_DISP\_HELD\_MAP}}}
				}
				child [table empty, yshift=-1mm] {node [empty] {Branch Mispredict\\and I-Cache Miss\\\texttt{PM\_GCT\_NOSLOT\_BR\_MPRED\_ICMISS}}}
				child [table empty] {node [empty] {Branch Mispredict\\\texttt{PM\_GCT\_NOSLOT\_BR\_MPRED}}}
				child [table empty] {node [empty] {I-Cache Miss\\\texttt{PM\_GCT\_NOSLOT\_IC\_MISS}}
					child [table empty icache miss] {node [empty, derived] {Other}}
					% child {node [empty] {L3 Mi}}
					child [table empty icache miss] {node [empty] {L3 Miss\\\texttt{PM\_GCT\_NOSLOT\_IC\_L3MISS}}}
				}
			}
			child {node (run inst) {Run Instruction\\\texttt{PM\_RUN\_INST\_CMPL}}}
			child {node (stalls) [stalled, solid] {Stalled Cycles\\\texttt{PM\_CMPLU\_STALL}}
				child [stall] {node [derived, stalled] {Other\\\texttt{PM\_CMPLU\_STALL\_OTHER\_CMPL}}}
				child [stall] {node  [stalled] {LSU\\\texttt{PM\_CMPLU\_STALL\_LSU}}
					child [stall lsu] {node [derived, stalled] {Other}}
					child [stall lsu] {node [stalled] {Dcache Miss\\\texttt{PM\_CMPLU\_STALL\_DCACHE\_MISS}}
						child [stall lsu dcache miss] {node [stalled] {L3 Miss\\\texttt{PM\_CMPLU\_STALL\_DMISS\_L3MISS}}
							child [stall lsu dcache miss l3 miss] {node [derived, stalled] {Off-Node Memory}}
							child [stall lsu dcache miss l3 miss] {node [stalled] {Off-Chip Memory\\\texttt{PM\_CMPLU\_STALL\_DMISS\_REMOTE}}}
							child [stall lsu dcache miss l3 miss] {node [stalled] {On-Chip Memory\\\texttt{PM\_CMPLU\_STALL\_DMISS\_LMEM}}}
							child [stall lsu dcache miss l3 miss] {node [stalled] {On-Chip L2/L3\\\texttt{PM\_CMPLU\_STALL\_DMISS\_L21\_L31}}}
						}
						child [stall lsu dcache miss] {node [stalled] {L2/L3 Hit\\\texttt{PM\_CMPLU\_STALL\_DMISS\_L2L3}}
							child [stall lsu dcache miss l2l3 hit] {node [derived, stalled] {L2/L3 Hit\\w/ No Conflict}}
							child [stall lsu dcache miss l2l3 hit] {node [stalled] {L2/L3 Hit\\with Conflict\\\texttt{PM\_CMPLU\_STALL\_DMISS\_L2L3\_CONFLICT}}}
						}
					}
					child [stall lsu] {node [stalled] {Store Forward\\\texttt{PM\_CMPLU\_STALL\_ST\_FWD}}}
					child [stall lsu] {node [stalled] {Load Finish\\\texttt{PM\_CMPLU\_STALL\_LOAD\_FINISH}}}
					child [stall lsu] {node [stalled] {LSU Reject\\\texttt{PM\_CMPLU\_STALL\_REJECT}}
						child [stall lsu reject, yshift=0.2cm] {node [derived, stalled] {Other}}
						child [stall lsu reject] {node [stalled] {LMQ Full\\\texttt{PM\_CMPLU\_STALL\_REJ\_LMQ\_FULL}}}
						child [stall lsu reject] {node [stalled] {ERAT Miss\\\texttt{PM\_CMPLU\_STALL\_ERAT\_MIS}}}
						child [stall lsu reject] {node [stalled] {Load-Hit-Store\\\texttt{PM\_CMPLU\_STALL\_REJECT\_LHS}}}
					}
					child [stall lsu] {node [stalled] {Store Finish\\\texttt{PM\_CMPLU\_STALL\_STORE}}}
				}
				child [stall] {node [stalled] {NOPs\\\texttt{PM\_CMPLU\_STALL\_NO\_NTF}}}
				child [stall] {node [stalled] {Next-to-Complete Flush\\\texttt{PM\_CMPLU\_STALL\_NTCG\_FLUSH}}}
				child [stall, sibling distance=1.6cm] {node [stalled] {VSU\\\texttt{PM\_CMPLU\_ST ALL\_VSU}}
					child [sibling distance=0.6cm] {node [derived, stalled]{Other}}
					child {node [stalled]{Scalar\\\texttt{PM\_CMPLU\_STALL\_SCALAR}}
						child {node [derived, stalled] {Scalar (other)}}
						child {node [stalled] {Scalar Long\\\texttt{PM\_CMPLU\_STALL\_SCALAR\_LONG}}}
					}
					child {node [stalled] {Vector\\\texttt{PM\_CMPLU\_STALL\_VECTOR}}
						child {node [derived] [stalled] {Vector (other)}}
						child {node [stalled] {Vector Long\\\texttt{PM\_CMPLU\_STALL\_VECTOR\_LONG}}}
					}
				}
				child [stall, sibling distance=2.2cm] {node [stalled] {Fixed-Point\\\texttt{PM\_CMPLU\_STALL\_FXU}}
					child [sibling distance=0.7cm] {node [derived, stalled] {Fixed-Point (Other)}}
					child [sibling distance=0.7cm] {node [stalled] {Fixed-Point Long\\\texttt{PM\_CMPLU\_STALL\_FXLONG}}}
				}
				child [stall, sibling distance=2.0cm] {node  [stalled] {BR or CR\\\texttt{PM\_CMPLU\_STALL\_BRU\_CRU}}
					child [sibling distance=0.7cm] {node [derived, stalled] {CR}}
					child [sibling distance=0.7cm] {node [stalled] {Branch\\\texttt{PM\_CMPLU\_STALL\_BRU}}}
				}
			}
			child {node (wait to complete) {Group Waiting\\to Complete\\\texttt{PM\_NTCG\_ALL\_FIN}}}
			child [sibling distance=4.6cm] {node (thread blocked) [blocked, solid] {Thread Blocked\\\texttt{PM\_CMPLU\_STALL\_THRD}}
				child [thread, yshift=0.2cm] {node [blocked, derived] {Other}}
				child [thread] {node [blocked] {COQ Full\\\texttt{PM\_CMPLU\_STALL\_COQ\_FULL}}}
				child [thread] {node [blocked] {Other Thread's Flush\\\texttt{PM\_CMPLU\_STALL\_FLUSH}}}
				child [thread] {node [blocked] {ECC Delay\\\texttt{PM\_CMPLU\_STALL\_MEM\_ECC\_DELAY}}}
				child [thread] {node [blocked] {HWSYNC\\\texttt{PM\_CMPLU\_STALL\_HWSYNC}}}
				child [thread] {node [blocked] {LWSYNC\\\texttt{PM\_CMPLU\_STALL\_LWSYNC}}}
			}
		;
		\coordinate (glossary base) at ([xshift=-15pt, yshift=-10pt]current bounding box.north east);
		\node (glossary) [
			anchor=north east, 
			inner sep=5pt, 
			outer sep=5pt, 
			text width=3.5cm, 
			align=center, 
			font=\relsize{-2},
			fill=solarized-blue!5,
			draw=solarized-blue!50
		] at (glossary base) {
			\textbf{\larger Glossary}
			\vspace*{-1ex}
			\begin{description}
				\itemsep0pt
				\item[BRU] Branching Unit
				\item[CR] Conditional Register
				\item[FXU] Fixed-Point Unit
				\item[VSU] Vector-Scalar Unit
				\item[LSU] Load-Store Unit
				\item[LMQ] Load Miss Queue
				\item[ERAT] Effective to Real Address Translation
				\item[LWSYNC] Lightweight Synchronize
				\item[HWSYNC] Heavyweight Synchronize
				\item[ECC] Error Correcting Code
			\end{description}
		};
		\node (derived) [below=2mm of glossary, derived] {Derived Quantity};
		\node (stall) [below=2mm of derived, stall] {\emph{Stall due to\dots}};
		\node (blocked) [below=2mm of stall, blocked] {\emph{Thread blocked due to\dots}};
		\node (empty) [below=2mm of blocked, empty] {\emph{Nothing to dispatch due to\dots}};
	\end{tikzpicture}
\end{document}
