\section{Process Monitoring}

\begin{frame}
   {Process Monitoring Tools}

   \begin{itemize}

      \item\textbf{top}
      \item\textbf{uptime}
      \item\textbf{ps}
      \item\textbf{pstree}
      \item\textbf{mpstat}
      \item\textbf{iostat}
      \item\textbf{sar}
      \item\textbf{numastat}
      \item\textbf{strace}
   \end{itemize}

\end{frame}

\cprotect\note{

   To monitor processes, \textbf{Linux} administrators
   make use of many utilities, such as \textbf{ps},
   \textbf{pstree} and \textbf{top}, all of which have
   long histories in \textbf{UNIX}-like operating systems.

   Here is a list of some of the main tools for process
   monitoring:

   \begin{table}[H]
      \caption{Process and Load Monitoring Tools}
      \vspace{-15pt}
   \end{table}

   \small{\begin{tcolorbox}[tabularx={p{0.25\textwidth}|X}]
         \textbf{Tool} & \textbf{Purpose}                      \\ \hline \hline
         \textbf{top}
         &
         Process activity, dynamically updated                 \\ \hline

         \textbf{uptime}
         &
         How long the system is running and the average load   \\ \hline

         \textbf{ps}
         &
         Detailed information about processes                  \\ \hline

         \textbf{pstree}
         &
         A tree of processes and their connections             \\ \hline

         \textbf{mpstat}
         &
         Multiple processor usage                              \\ \hline

         \textbf{iostat}
         &
         CPU utilization and I/O statistics                    \\ \hline

         \textbf{sar}
         &
         Display and collect information about system activity \\ \hline

         \textbf{numastat}
         &
         Information about \textbf{NUMA} (Non-Uniform Memory
         Architecture)                                         \\ \hline

         \textbf{strace}
         &
         Information about all system calls a process makes
      \end{tcolorbox}}

   The \filelink{/proc} filesystem can also be helpful in
   monitoring processes, as well as other items, on the system.
}

\section{ps}
\begin{frame}
   {Viewing Process States with ps}

   \begin{itemize}
      \item Gets its information from \filelink{/proc}
      \item Extremely flexible in selection and presentation
      options
      \item Some common choices of options:
      \begin{cmd}
$ ps aux
$ ps -elf
$ ps -eL
$ ps -C "bash"
      \end{cmd}
   \end{itemize}

\end{frame}

\cprotect\note{

   \textbf{ps} is a workhorse for displaying process
   characteristics and statistics, garnered from the
   \filelink{/proc} directory.

   This command utility has existed in all
   \textbf{UNIX}-like operating system variants, and that
   diversity is reflected in the complicated potpourri of
   options that the \textbf{Linux} version of \textbf{ps}
   accepts, which fall into three categories:

   \begin{enumerate}
      \item
      \textbf{UNIX} options, which must be preceded by
      \verb?-?, and which may be grouped.
      \item
      \textbf{BSD} options, which must not be preceded by
      \verb?-?, and which may be grouped.
      \item
      \textbf{GNU} long options, each of which must be
      preceded by \verb?--?.
   \end{enumerate}

   Having all these possible options can make life rather
   confusing.  Most system administrators tend to use one or
   two standard combinations for their daily use.

   \begin{figure}[H]
      \includegraphics[width=7.0in]{IMAGES/psauxf}
      \caption{Using ps With BSD Options}
   \end{figure}

}


\begin{frame}
   {Customizing  the \textbf{ps} Output}

   \begin{itemize}
      \item Use the \texttt{-o} option, followed by a comma
      separated list of field identifiers to allow the
      user to print out a selected list of \texttt{ps}
      fields
      \begin{itemize}
         \item \texttt{pid}: process id
         \item \texttt{uid}: user id of process owner
         \item \texttt{cmd}: command with all  arguments
         \item \texttt{cputime}: cumulative CPU time
         \item \texttt{pmem}: ratio of the process's
         resident set size to the physical memory on
         the machine, expressed as a percentage
      \end{itemize}
      \item See \textbf{ps} man page for many other output
      options
   \end{itemize}


\end{frame}

\cprotect\note{

   \begin{figure}[H]
      \includegraphics[width=7.0in]{IMAGES/psopt}
      \caption{Customizing ps Output}
   \end{figure}
   \vspace{-24pt}

   Another common options choice, \verb?-elf? :

   \begin{figure}[H]
      \includegraphics[width=7.0in]{IMAGES/pself}
      \caption{Using ps With UNIX Options}
   \end{figure}

}
\section{pstree}
\begin{frame}
   {Using pstree}

   \begin{itemize}
      \item Shows the user the relationships between
      parent-child processes
      \item Use \texttt{-p} to show process IDs
      \item Use \texttt{-H} [pid] to highlight [pid] and
      its ancestors
      \begin{cmd}
$ pstree -aAps 31478
      \end{cmd}
      \begin{response}
systemd,1 --switched-root --system --deserialize 21
  `-vmplayer,31478
      |-{vmplayer},31570
      |-{vmplayer},31593
      |-{vmplayer},32572
      `-{vmplayer},32698
      \end{response}
   \end{itemize}

\end{frame}

\cprotect\note{

   \textbf{pstree} gives a visual description of the
   process ancestry and multi-threaded applications:

   \begin{cmd}
$ pstree -aAp 2408
   \end{cmd}
   \begin{response}
  bash,2408
  |-emacs,24998 pmonitor.tex
  |   |-{emacs},25002
  |   `-{emacs},25003
  |-evince,18036 LFS201-SLIDES.pdf
  |   |-{evince},18040
  |   |-{evince},18046
  |   `-{evince},18047
   \end{response}

   Consult the \textbf{man} page for \textbf{pstree} for
   an explanation of many options; in the above we have
   chosen just to show information for \texttt{pid=2408}.

   Note that one of its child processes (\textbf{evince},
   \texttt{pid=18036}) has three children of its
   own. Another way to see that is:
   \begin{cmd}
$ ls -l /proc/18036/task
   \end{cmd}
   \begin{response}
total 0
dr-xr-xr-x 5 coop coop 0 Sep 11 07:15 18036
dr-xr-xr-x 5 coop coop 0 Sep 11 07:15 18040
dr-xr-xr-x 5 coop coop 0 Sep 11 07:15 18046
dr-xr-xr-x 5 coop coop 0 Sep 11 07:15 18047
   \end{response}
}
\section{top}
\begin{frame}
   {top}

   \begin{itemize}
      \item Used to display processes with highest CPU usage
      \item Processes are initially sorted by CPU usage
      \item If not run in secure mode (top s) user can
      signal processes
      \begin{itemize}
         \item Press the \verb?k? key
         \item Give a PID when prompted
         \item Give a signal number when prompted
      \end{itemize}
      \item
      A ton of options, an ancient utility, lots of
      interactivity
      \item Hit \verb?h? key to get list of key bindings
      \item
      Easy to control sorting options etc.

   \end{itemize}

\end{frame}

\cprotect\note{

   Press "h" for help.  Display may be sorted by a number of
   different criteria.  The display is refreshed every 5
   seconds (though this is configurable) and continues to run
   until the user interrupts it (\verb?Ctrl-C?).

   \begin{figure}[H]
      \includegraphics[width=6.5in]{IMAGES/top2}
      \caption{Using top}
   \end{figure}

}

\begin{frame}
   {More on /proc}

   \begin{itemize}
      \item The \filelink{/proc} filesystem is an interface to
      the kernel data structures
      \item \filelink{/proc} contains a subdirectory for each
      active process, named by the process id (PID)
      \item \filelink{/proc/self} is the currently executing
      process
      \item Some tunable parameters are in the
      \filelink{/proc} directories
      \item For more info, see the \texttt{proc} man page
   \end{itemize}

\end{frame}

\cprotect\note{

   In the sample output below, notice the sub-directories of
   the \filelink{/proc} filesystem which have numeric values for
   names.  These ASCII numeric strings represent the PIDs of
   all active processes on the system.  Each subdirectory
   contains additional files with information about that
   process.
   \begin{figure}[H]
      \includegraphics[width=7.25in]{IMAGES/lsproc}
      \caption{/proc Contents}
   \end{figure}

}



