% Created 2025-05-30 Fri 16:23
% Intended LaTeX compiler: pdflatex
\documentclass[usenames,dvipsnames,colorlinks,aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{color}
\usepackage{listings}
\usepackage{pdfpages}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{xspace}
\usepackage{siunitx}
\usepackage{libertine}
\usepackage{fontawesome}
\mode<beamer>{\usetheme{Boadilla}}
\setbeamertemplate{navigation symbols}{}
\setbeamerfont{alerted text}{series=\bfseries}
\setbeamercolor{alerted text}{fg=blue}
\usecolortheme{default}
\usefonttheme{serif}
\usefonttheme{professionalfonts}
\lstloadlanguages{[ISO]C++}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.50,0,0.82}
\definecolor{codeorange}{rgb}{0.95,0.3,0.1}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
commentstyle=\color{codegreen},
keywordstyle=\color{codeorange},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
\usetheme{default}
\author{Brett Viren}
\date{\today}
\title{Profiling memory and CPU usage}
\subtitle{with Google PerfTools and Valgrind}
\hypersetup{
 pdfauthor={Brett Viren},
 pdftitle={Profiling memory and CPU usage},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.2 (Org mode 9.8-pre)},
 pdflang={English}}

% Setup for code blocks [1/2]

\usepackage{fvextra}

\fvset{%
  commandchars=\\\{\},
  highlightcolor=white!95!black!80!blue,
  breaklines=true,
  breaksymbol=\color{white!60!black}\tiny\ensuremath{\hookrightarrow}}

% Make line numbers smaller and grey.
\renewcommand\theFancyVerbLine{\footnotesize\color{black!40!white}\arabic{FancyVerbLine}}

\usepackage{xcolor}

% In case engrave-faces-latex-gen-preamble has not been run.
\providecolor{EfD}{HTML}{f7f7f7}
\providecolor{EFD}{HTML}{28292e}

% Define a Code environment to prettily wrap the fontified code.
\usepackage[breakable,xparse]{tcolorbox}
\DeclareTColorBox[]{Code}{o}%
{colback=EfD!98!EFD, colframe=EfD!95!EFD,
  fontupper=\footnotesize\setlength{\fboxsep}{0pt},
  colupper=EFD,
  IfNoValueTF={#1}%
  {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
    boxrule=0.5pt, left=2pt}%
  {boxsep=2.5pt, arc=0pt, outer arc=0pt,
    boxrule=0pt, leftrule=1.5pt, left=0.5pt},
  right=2pt, top=1pt, bottom=0.5pt,
  breakable}

% Support listings with captions
\usepackage{float}
\floatstyle{plain}
\newfloat{listing}{htbp}{lst}
\newcommand{\listingsname}{Listing}
\floatname{listing}{\listingsname}
\newcommand{\listoflistingsname}{List of Listings}
\providecommand{\listoflistings}{\listof{listing}{\listoflistingsname}}


% Setup for code blocks [2/2]: syntax highlighting colors

\newcommand\efstrut{\vrule height 2.1ex depth 0.8ex width 0pt}
\definecolor{EFD}{HTML}{000000}
\definecolor{EfD}{HTML}{ffffff}
\newcommand{\EFD}[1]{\textcolor{EFD}{#1}} % default
\definecolor{EFvp}{HTML}{000000}
\newcommand{\EFvp}[1]{\textcolor{EFvp}{#1}} % variable-pitch
\definecolor{EFh}{HTML}{7f7f7f}
\newcommand{\EFh}[1]{\textcolor{EFh}{#1}} % shadow
\definecolor{EFsc}{HTML}{228b22}
\newcommand{\EFsc}[1]{\textcolor{EFsc}{\textbf{#1}}} % success
\definecolor{EFw}{HTML}{ff8e00}
\newcommand{\EFw}[1]{\textcolor{EFw}{\textbf{#1}}} % warning
\definecolor{EFe}{HTML}{ff0000}
\newcommand{\EFe}[1]{\textcolor{EFe}{\textbf{#1}}} % error
\definecolor{EFl}{HTML}{ff0000}
\newcommand{\EFl}[1]{\textcolor{EFl}{#1}} % link
\definecolor{EFlv}{HTML}{ff0000}
\newcommand{\EFlv}[1]{\textcolor{EFlv}{#1}} % link-visited
\definecolor{EFhi}{HTML}{ff0000}
\newcommand{\EFhi}[1]{\textcolor{EFhi}{#1}} % highlight
\definecolor{EFc}{HTML}{b22222}
\newcommand{\EFc}[1]{\textcolor{EFc}{#1}} % font-lock-comment-face
\definecolor{EFcd}{HTML}{b22222}
\newcommand{\EFcd}[1]{\textcolor{EFcd}{#1}} % font-lock-comment-delimiter-face
\definecolor{EFs}{HTML}{8b2252}
\newcommand{\EFs}[1]{\textcolor{EFs}{#1}} % font-lock-string-face
\definecolor{EFd}{HTML}{8b2252}
\newcommand{\EFd}[1]{\textcolor{EFd}{#1}} % font-lock-doc-face
\definecolor{EFm}{HTML}{008b8b}
\newcommand{\EFm}[1]{\textcolor{EFm}{#1}} % font-lock-doc-markup-face
\definecolor{EFk}{HTML}{9370db}
\newcommand{\EFk}[1]{\textcolor{EFk}{#1}} % font-lock-keyword-face
\definecolor{EFb}{HTML}{483d8b}
\newcommand{\EFb}[1]{\textcolor{EFb}{#1}} % font-lock-builtin-face
\definecolor{EFf}{HTML}{0000ff}
\newcommand{\EFf}[1]{\textcolor{EFf}{#1}} % font-lock-function-name-face
\definecolor{EFv}{HTML}{a0522d}
\newcommand{\EFv}[1]{\textcolor{EFv}{#1}} % font-lock-variable-name-face
\definecolor{EFt}{HTML}{228b22}
\newcommand{\EFt}[1]{\textcolor{EFt}{#1}} % font-lock-type-face
\definecolor{EFo}{HTML}{008b8b}
\newcommand{\EFo}[1]{\textcolor{EFo}{#1}} % font-lock-constant-face
\definecolor{EFwr}{HTML}{ff0000}
\newcommand{\EFwr}[1]{\textcolor{EFwr}{\textbf{#1}}} % font-lock-warning-face
\newcommand{\EFnc}[1]{#1} % font-lock-negation-char-face
\definecolor{EFpp}{HTML}{483d8b}
\newcommand{\EFpp}[1]{\textcolor{EFpp}{#1}} % font-lock-preprocessor-face
\newcommand{\EFrc}[1]{\textbf{#1}} % font-lock-regexp-grouping-construct
\newcommand{\EFrb}[1]{\textbf{#1}} % font-lock-regexp-grouping-backslash
\newcommand{\EFob}[1]{#1} % org-block
\newcommand{\EFobb}[1]{#1} % org-block-begin-line
\newcommand{\EFobe}[1]{#1} % org-block-end-line
\definecolor{EFOa}{HTML}{0000ff}
\newcommand{\EFOa}[1]{\textcolor{EFOa}{#1}} % outline-1
\definecolor{EFOb}{HTML}{a0522d}
\newcommand{\EFOb}[1]{\textcolor{EFOb}{#1}} % outline-2
\definecolor{EFOc}{HTML}{a020f0}
\newcommand{\EFOc}[1]{\textcolor{EFOc}{#1}} % outline-3
\definecolor{EFOd}{HTML}{b22222}
\newcommand{\EFOd}[1]{\textcolor{EFOd}{#1}} % outline-4
\definecolor{EFOe}{HTML}{228b22}
\newcommand{\EFOe}[1]{\textcolor{EFOe}{#1}} % outline-5
\definecolor{EFOf}{HTML}{008b8b}
\newcommand{\EFOf}[1]{\textcolor{EFOf}{#1}} % outline-6
\definecolor{EFOg}{HTML}{483d8b}
\newcommand{\EFOg}[1]{\textcolor{EFOg}{#1}} % outline-7
\definecolor{EFOh}{HTML}{8b2252}
\newcommand{\EFOh}[1]{\textcolor{EFOh}{#1}} % outline-8
\definecolor{EFhn}{HTML}{008b8b}
\newcommand{\EFhn}[1]{\textcolor{EFhn}{#1}} % highlight-numbers-number
\definecolor{EFhq}{HTML}{9370db}
\newcommand{\EFhq}[1]{\textcolor{EFhq}{#1}} % highlight-quoted-quote
\definecolor{EFhs}{HTML}{008b8b}
\newcommand{\EFhs}[1]{\textcolor{EFhs}{#1}} % highlight-quoted-symbol
\definecolor{EFrda}{HTML}{707183}
\newcommand{\EFrda}[1]{\textcolor{EFrda}{#1}} % rainbow-delimiters-depth-1-face
\definecolor{EFrdb}{HTML}{7388d6}
\newcommand{\EFrdb}[1]{\textcolor{EFrdb}{#1}} % rainbow-delimiters-depth-2-face
\definecolor{EFrdc}{HTML}{909183}
\newcommand{\EFrdc}[1]{\textcolor{EFrdc}{#1}} % rainbow-delimiters-depth-3-face
\definecolor{EFrdd}{HTML}{709870}
\newcommand{\EFrdd}[1]{\textcolor{EFrdd}{#1}} % rainbow-delimiters-depth-4-face
\definecolor{EFrde}{HTML}{907373}
\newcommand{\EFrde}[1]{\textcolor{EFrde}{#1}} % rainbow-delimiters-depth-5-face
\definecolor{EFrdf}{HTML}{6276ba}
\newcommand{\EFrdf}[1]{\textcolor{EFrdf}{#1}} % rainbow-delimiters-depth-6-face
\definecolor{EFrdg}{HTML}{858580}
\newcommand{\EFrdg}[1]{\textcolor{EFrdg}{#1}} % rainbow-delimiters-depth-7-face
\definecolor{EFrdh}{HTML}{80a880}
\newcommand{\EFrdh}[1]{\textcolor{EFrdh}{#1}} % rainbow-delimiters-depth-8-face
\definecolor{EFrdi}{HTML}{887070}
\newcommand{\EFrdi}[1]{\textcolor{EFrdi}{#1}} % rainbow-delimiters-depth-9-face
\definecolor{EFany}{HTML}{CDCD00}
\newcommand{\EFany}[1]{\textcolor{EFany}{#1}} % ansi-color-yellow
\definecolor{EFanr}{HTML}{CD0000}
\newcommand{\EFanr}[1]{\textcolor{EFanr}{#1}} % ansi-color-red
\definecolor{EFanb}{HTML}{000000}
\newcommand{\EFanb}[1]{\textcolor{EFanb}{#1}} % ansi-color-black
\definecolor{EFang}{HTML}{00CD00}
\newcommand{\EFang}[1]{\textcolor{EFang}{#1}} % ansi-color-green
\definecolor{EFanB}{HTML}{0000EE}
\newcommand{\EFanB}[1]{\textcolor{EFanB}{#1}} % ansi-color-blue
\definecolor{EFanc}{HTML}{00CDCD}
\newcommand{\EFanc}[1]{\textcolor{EFanc}{#1}} % ansi-color-cyan
\definecolor{EFanw}{HTML}{E5E5E5}
\newcommand{\EFanw}[1]{\textcolor{EFanw}{#1}} % ansi-color-white
\definecolor{EFanm}{HTML}{CD00CD}
\newcommand{\EFanm}[1]{\textcolor{EFanm}{#1}} % ansi-color-magenta
\definecolor{EFANy}{HTML}{EEEE00}
\newcommand{\EFANy}[1]{\textcolor{EFANy}{#1}} % ansi-color-bright-yellow
\definecolor{EFANr}{HTML}{EE0000}
\newcommand{\EFANr}[1]{\textcolor{EFANr}{#1}} % ansi-color-bright-red
\newcommand{\EFANb}[1]{#1} % ansi-color-bright-black
\definecolor{EFANg}{HTML}{00EE00}
\newcommand{\EFANg}[1]{\textcolor{EFANg}{#1}} % ansi-color-bright-green
\definecolor{EFANB}{HTML}{0000FF}
\newcommand{\EFANB}[1]{\textcolor{EFANB}{#1}} % ansi-color-bright-blue
\definecolor{EFANc}{HTML}{00EEEE}
\newcommand{\EFANc}[1]{\textcolor{EFANc}{#1}} % ansi-color-bright-cyan
\newcommand{\EFANw}[1]{#1} % ansi-color-bright-white
\newcommand{\EFANm}[1]{#1} % ansi-color-bright-magenta
\begin{document}

\maketitle
\begin{frame}[label={sec:org2d230fb}]{}
\begin{center}
This is meant to help the DNNROI inference optimization work. 
\end{center}
\end{frame}
\begin{frame}[label={sec:org105238e},fragile]{Overview}
 It is very difficult to understand memory and CPU usage by observing the the job
as a \alert{monolith}.
\begin{itemize}
\item Eg, by watching the program ``\texttt{top}''.
\item One can \alert{profile} the job to better understand which parts deserve ``blame''.
\end{itemize}
\begin{block}{How do you ``profile'' a job?}
\begin{itemize}
\item Run the job with some \alert{profiling tool}.
\item The tool generates some output.
\item Run a provided analysis tool to make a visualization.
\item Examine the visualization to gain understanding.
\end{itemize}
\end{block}
\begin{block}{Google's PerfTools and Valgrind}
These are both simple, commonly available and can be very useful.  
\end{block}
\end{frame}
\begin{frame}[label={sec:org0a9c85c},fragile]{Run Google PerfTools}
 For shorthand:
\footnotesize
\begin{verbatim}
$ export PROG=/path/to/your/program
\end{verbatim}
\begin{block}{CPU}
\footnotesize
\begin{verbatim}
$ export CPUPROFILE=cpu.prof
$ LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libprofiler.so.0 $PROG [args]
$ ls -l cpu.prof*
\end{verbatim}
\end{block}
\begin{block}{Memory}
\footnotesize
\begin{verbatim}
$ export HEAPPROFILE=mem.prof                          # required
$ LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc.so.4 $PROG
$ ls -l mem.prof*
\end{verbatim}
\end{block}
\end{frame}
\begin{frame}[label={sec:org92bf8a6},fragile]{Visualize PerfTools results}
 Here we use the \alert{cpu} file.  The \alert{mem} file works identically but is named like \texttt{mem.prof.heap.XXXX}.
\begin{block}{Basic graph generation}
\footnotesize
\begin{verbatim}
$ google-pprof --pdf $PROG $CPUPROFILE > cpu.pdf
\end{verbatim}
\end{block}
\begin{block}{Focus on a particular function:}
\footnotesize
\begin{verbatim}
$ google-pprof --focus 'as_pctree' --pdf $PROG $CPUPROFILE > cpu.pdf
$ google-pprof --help
\end{verbatim}
\end{block}
\begin{block}{More ways to process see:}
\footnotesize
\begin{verbatim}
$ google-pprof --help
\end{verbatim}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgf2e87ce},fragile]{Example graphs - CPU and Memory usage}
 \begin{columns}
\begin{column}{0.4\columnwidth}
\begin{itemize}
\item Nodes sized by how mcuh resource (cpu/mem) they use.
\begin{itemize}
\item Numbers give used resource directly by the function or by functions the function calls.
\end{itemize}
\item Edges show how much resource went for each (sub) function call.
\end{itemize}
Tips: Use a PDF viewer that allows easy zoom/pan and play with filtering and other \texttt{google-pprof} options.
\end{column}
\begin{column}{0.6\columnwidth}
\includegraphics[height=\textheight]{cpu.pdf}\%
\includegraphics[height=\textheight]{mem.pdf}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[label={sec:org8de721e},fragile]{How Valgrind works}
 Valgrind works similarly to Google Perftools.  It has a number of ``tools''

\begin{description}
\item[{callgrind}] makes snapshots of the call stack to make and analyze a call graph.
\item[{massif}] checks heap memory by overriding \texttt{malloc} etc and taking snapshots.  Generates \alert{profile file} for later visualization.
\item[{memcheck}] looks for memory leaks and other errors.  Generates \alert{summary report} and error messages.
\end{description}


General usage:
\footnotesize
\begin{verbatim}
valgrind --tool=TOOLNAME [tool options] your_program [program args]
\end{verbatim}
\end{frame}
\begin{frame}[label={sec:orga689480},fragile]{Visualize massif results}
 \footnotesize
\begin{verbatim}
$ valgrind --tool=massif --time-unit=ms --detailed-freq=1 --massif-out-file=valgrind.massif \
  myprogram [my program args]
$ massif-visualizer valgrind.massif
\end{verbatim}
\includegraphics[width=\textwidth]{massif-viz.png}
\end{frame}
\begin{frame}[label={sec:org704e4a3}]{Going further}
GPU profiling has its own tools, usually specific to the hardware. 
\begin{itemize}
\item NVIDIA: Nsight, nvprof, nvpp (nvidia-smi gives a ``top'' for the GPU)
\end{itemize}
Various profiling tools work at Linux kernel level.
\begin{itemize}
\item perf, eBPF, \ldots{}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgfc473ad}]{}
\begin{center}
\Huge \(\mathcal{FIN}\)
\end{center}
\end{frame}
\begin{frame}[label={sec:org65e4d12}]{}
\end{frame}

\begin{frame}[label={sec:orgf849631},fragile]{What is a call graph}
 Assume a program like:

\begin{Code}
\begin{Verbatim}
\color{EFD}\EFt{void} \EFf{c}() \{ c1\_work(); c2\_work() \}
\EFt{void} \EFf{b}() \{ c(); b\_work()  \}
\EFt{void} \EFf{a}() \{ b(); a\_work() \}
\EFt{void} \EFf{main}() \{ a(); \}
\end{Verbatim}
\end{Code}
A snapshot looks at CPU/Memory usage in any instance and records the \alert{call stack}.

Some example stack snapshots:

\begin{itemize}
\item \texttt{main() -> a() -> b() -> c() -> c1\_work()}
\item \texttt{main() -> a() -> b() -> b\_work()}
\item \texttt{main() -> a() -> a\_work()}
\end{itemize}

Google Perftools simply book keeps how much CPU and Memory is being used on each snapshot.
\end{frame}
\end{document}
