\documentclass[xcolor=dvipsnames]{beamer}

\input{../ely05preamble.tex}
%\setbeamertemplate{background}[grid][step=1cm]

\title[PerfTools] % (optional, use only with long paper titles)
{Tools to Improve Your Software's Performance}

\subtitle
{Two Tools, One Name: PerfTools}


\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\logo{\pgfuseimage{bnl-logo}}

\begin{frame}
  \frametitle{Outline}
  \tableofcontents[]
\end{frame}


% How perftools cpu profiler works
% MinosSoft PerfTools vs. google-perftools, the similarities and differences
% author credits
% Example output of both
% Other things available with GPT

\section{The Profiling Method}

\begin{frame}
  \tableofcontents[currentsection,hideothersubsections]
\end{frame}

\subsection{Overview}

\begin{frame}
  \frametitle{How they work}
  
  Both profilers work the same way:
  \begin{enumerate}
  \item They insinuate code into your program that periodically (many
    times a second) takes a snapshot of the currently executing call
    stack.
  \item This data is post processed to produce a weighted, directed
    graph.
  \item GraphViz is then used to produce graphical output.
  \end{enumerate}
  The Graph:
  \begin{itemize}
  \item Each node represents a procedure (function/method) call and
    shows the frequency that the procedure was:
    \begin{itemize}
    \item anywhere in the call stack
    \item at the top of the call stack
    \end{itemize}
  \item Each edge represents one procedure calling another and shows
    the frequency of at which this call was seen.
  \end{itemize}
\end{frame}

\subsection{Interpreting The Results}

\begin{frame}
  \frametitle{Using the Graphs to Find CPU ``Hot Spots''}
  
  The graph not only shows what procedure is using CPU but also
  exactly how that procedure came to be called.  Finding possible
  areas of improvement is an iterative process:

  \begin{enumerate}
  \item Find where the code is using CPU most (traditional profiling)
    \begin{itemize}
    \item If the procedures using the most CPU can be improved, the
      whole program is made faster
    \item However, these procedures are often already very well
      optimized (eg. lowlevel STL operations are typically implicated)
    \item Instead, they are using the most CPU, not because they are
      slow, but because they are called often, likely by many
      different procedures.
    \end{itemize}
  \item Go up the graph to find what procedures call these popular
    procedures most frequently
    \begin{itemize}
    \item See if it is possibe to avoid calling these procedures in
      the first place.
    \item If not, can one avoid calling the procedures that called
      \textbf{these} procedures?
    \item Etc.
    \end{itemize}
  \end{enumerate}


\end{frame}

\section{Minos PerfTools (MPT)}

\begin{frame}
  \tableofcontents[currentsection,hideothersubsections]
\end{frame}

\subsection{Overview}

\begin{frame}
  \frametitle{Overview of Minos PerfTools (MPT)}
  \begin{itemize}
  \item Initially written by Jim Kowalkowski of FNAL.  Jim provided
    all the fundamental functionality.
  \item I wrote a unified script to automate the post processing and
    to provide an interactive display to view and prune the resulting
    graph.
  \item PerfTools was incorporated into Minos CVS Sept 2003.
  \item External Requirements:
    \begin{enumerate}
    \item The ELFIO library is needed for profile data production.
    \item Python is needed for Jim's post processing scripts and
      my unified post-processor/display.
    \item GraphViz and Python-Tk is needed for the display.
    \item SWIG is needed only to regenerate the GraphViz Python wrappers.
    \end{enumerate}
  \end{itemize}
\end{frame}

\subsection{Examples}

\begin{frame}[fragile]
  \frametitle{Example running}
  {\small
  \begin{center}
    \begin{cmdline}{shell\$}{ptrun.py -c ./cpuhog -l log}
\begin{verbatim}
Running LD_PRELOAD=libProfLogger.so ./cpuhog > log 2>&1
return code: 0
\end{verbatim}
    \end{cmdline}

    \begin{cmdline}{shell\$}{ptrun.py -n guess -b cpuhog}\vspace{-3mm}
\begin{verbatim}
got -n guess
guessing pid
using 24689
Running ProfParse prof_libs.out.24689 prof.out.24689 \
                 cpuhog. >> cpuhog.postproc-log 2>&1
\end{verbatim}
    \end{cmdline}

    \begin{cmdline}{shell\$}{ptrun.py -g -b cpuhog}\vspace{-3mm}
\begin{verbatim}
Setting initial path cutoff to 0
Loading cpuhog.fullnames and cpuhog.paths
Added 57 names
Added 265 paths
Number of snapshots taken is 3276
\end{verbatim}
    \end{cmdline}
  \end{center}}
  The 3 steps can be combined into one single command.

\end{frame}

\begin{frame}
  \frametitle{Example output, Full Call Graph}
  \begin{center}
  \pgfimage[height=0.85\textheight,width=0.90\textwidth]{full-mpt}    
  \end{center}
\end{frame}

\begin{frame}
  \frametitle{Example output, After Focus on Node 29}

% ### use columns see page 53 of user_guide talk!

  \begin{columns}
    \begin{column}{0.6\textwidth}
      \pgfimage[height=0.85\textheight,width=\textwidth]{node-29-mpt}%
    \end{column}
    \begin{column}{0.4\textwidth}
    Focusing on Node \#29
    \begin{enumerate}
    \item Mouse-over a node displays function name
    \item Click-drag pans
    \item Buttons 4/5 (mouse wheel) will zoom
    \item Enter cuts on number of up/down stream nodes and number of
      calls.
    \end{enumerate}
    \end{column}
  \end{columns}

\end{frame}


\section{Google Perftools (GPT)}

\begin{frame}
  \tableofcontents[currentsection,hideothersubsections]
\end{frame}

\subsection{Overview}

\begin{frame}
  \frametitle{Overview of Google PerfTools (GPT)}
  \begin{itemize}
  \item Initially released to the public March 2005.
  \item Free Software (BSD licence)
  \item Despite that the name and the CPU profiling mechansim are
    essentially identical, GPT share no MPT code.
  \item Small patch may needed to fix problem when running with loon.
    Under investigation by Google and myself.
  \item Libraries can be (optionally) permanently linked in to our
    code w/no ill effects and ``woken up'' for use by just setting
    environment variables.
  \item External Requirements, only ``standard'' system packages:
    \begin{enumerate}
    \item Perl for post processing of the data
    \item GraphViz executables to generate final results
    \item Postscript viewer.
    \item (independent from ELFIO)
    \end{enumerate}
  \end{itemize}
\end{frame}

\subsection{Examples}

\cmdlinesize[0.96\textwidth]
\begin{frame}
  \frametitle{Example Running}
  
  The profiling library must be linked into the executable.  This can
  be done in one of two ways:
  \begin{enumerate}
  \item Explicitly by adding \texttt{-lprofiler} to the link line
  \item At run time by setting \texttt{LD\_PRELOAD=/usr/lib/libprofiler.so.0}
  \end{enumerate}
  
  The profile output file is set via \texttt{CPUPROFILE=prof.out}.
  Then run the program normally.
  {\small
  \begin{cmdline}{shell\$}{./cpuhog}\\
    ...program output...\\
    PROFILE: interrupts/evictions/bytes = 1043/86/52896
  \end{cmdline}}
  Postprocess:
  {\small
  \begin{cmdline}{shell\$}{pprof --ps ./cpuhog prof.out > prof.ps}\\
    ...output from /usr/bin/nm calls...\\
    Dropping nodes with <= 5 samples; edges with <= 1 abs(samples)
  \end{cmdline}
  \prompt{shell\$}{gv prof.ps}}
\end{frame}
\cmdlinesize{}

\begin{frame}
  \frametitle{Example Output}

  %\pgfsetlinewidth{1pt}
  %\pgfgrid[step={\pgfpoint{0.5cm}{0.5cm}}]{\pgfxy(0.0,0.0)}{\pgfxy(10.0,-6.0)}{}
  
  \framezoom<1><2>[border](3.2cm,0.cm)(1.75cm,0.75cm)
  \framezoom<1><3>[border](4.0cm,1.cm)(2.0cm,2.cm)
  
  \pgfimage[height=0.9\textheight]{prof}

\end{frame}

\subsection{Other Profilers in Google Perftools}

\begin{frame}
  \frametitle{Heap Memory}

  Leak Checker:
  \begin{enumerate}
  \item Use the \texttt{tcmalloc} library as you would the \texttt{profiler} lib.
  \item Set \texttt{HEAPCHECK=normal}, \texttt{strict} or \texttt{draconian}.
  \end{enumerate}

  Memory Profiler (who uses how much memory)
  \begin{enumerate}
  \item Use the \texttt{tcmalloc} library as you would the \texttt{profiler} lib.
  \item Set \texttt{HEAPPROFILE=heap}
  \end{enumerate}
  
  Both can be alternatively turned on/off via function calls in your
  code.

\end{frame}

\section*{Summary}

\begin{frame}
  \frametitle{Summary}

  \begin{itemize}
  \item We have two similar profilers available.
  \item Each have benefits and deficits w.r.t. the other.
  \item When this method of profiling has been applied to MinosSoft
    code factors of 2-3x speed ups have been found.
  \item We (core and physics programmers alike) should profile our
    code regularly after new, significant development has been done.
  \end{itemize}
  Personally I much prefer Google's PT.  After some of the bugs in GPT
  are worked out, I will consider our PT no longer supported.
\end{frame}

\appendix
\section<presentation>*{\appendixname}
\subsection<presentation>*{For Further Information}

\begin{frame}[allowframebreaks]
  \frametitle<presentation>{For Further Information}
    
  \begin{itemize}
  \item Google Perftools page
    \url{http://goog-perftools.sourceforge.net/}
  \item Minos PerfTools page
    \url{http://minos.phy.bnl.gov/software/prof/PerfTools/doc/}
  \item GraphViz (recently became Free Software!)
    \url{http://www.graphviz.org/}
  \item Simplified Wrapper and Interface Generator (SWIG)
    \url{http://www.swig.org/}
  \end{itemize}

\end{frame}

\end{document}


