From 80432a0d5d17e5091a226ca27c9b0b18ee83336f Mon Sep 17 00:00:00 2001
From: Jan Altenberg <jan@linutronix.de>
Date: Fri, 8 Oct 2010 18:24:20 +0200
Subject: RT application design.

---
 realtime/rt-app-basics/pres_rt-app-basics_en.tex | 296 +++++++++++++++++++++++
 1 file changed, 296 insertions(+)
 create mode 100644 realtime/rt-app-basics/pres_rt-app-basics_en.tex

(limited to 'realtime/rt-app-basics/pres_rt-app-basics_en.tex')

diff --git a/realtime/rt-app-basics/pres_rt-app-basics_en.tex b/realtime/rt-app-basics/pres_rt-app-basics_en.tex
new file mode 100644
index 0000000..95094cf
--- /dev/null
+++ b/realtime/rt-app-basics/pres_rt-app-basics_en.tex
@@ -0,0 +1,296 @@
+\documentclass[11pt]{beamer}
+
+%\usepackage{ngerman}
+\usepackage{times}
+\usepackage{graphicx}
+\usepackage{pgf,pgfarrows,pgfnodes,pgfautomata,pgfheaps}
+\usepackage{amsmath,amssymb}
+\usepackage[latin1]{inputenc}
+\usepackage{listings,color}
+\definecolor{lbcolor}{RGB}{255,210,150}
+\lstset{
+	language=C,
+	numbers=left,
+	stepnumber=1,
+	numbersep=5pt,
+	numberstyle=\tiny,
+	breaklines=true,
+	breakautoindent=true,
+	postbreak=\space,
+	tabsize=2,
+	basicstyle=\ttfamily\footnotesize,
+	showspaces=false,
+	showstringspaces=false,
+	extendedchars=true,
+	backgroundcolor=\color{lbcolor},
+	keywordstyle=\bf ,
+	commentstyle=\color{green},
+	stringstyle=\color{red}
+}
+
+\mode<presentation>
+{
+	\usetheme{linutronix}
+}
+
+% on the following slides, include icon in the left sidebar
+\def\lximg{/usr/share/lx/icons/fueller.png}
+
+\title{Realtime application development using Linux-RT\_PREEMPT}
+\institute{Linutronix GmbH}
+
+\begin{document}
+
+\frame{ \titlepage }
+
+% stop displaying 'fueller.png' on the following slides
+\def\lximg{none}
+
+%\AtBeginSection[]
+%{
+%	\begin{frame}<beamer>
+%		\tableofcontents[currentsection,currentsubsection]
+%	\end{frame}
+%}
+
+\AtBeginSubsection[]
+{
+	\begin{frame}<beamer>
+		\tableofcontents[currentsection,currentsubsection]
+	\end{frame}
+}
+
+\begin{frame}
+	\tableofcontents
+\end{frame}
+
+\section{Basics}
+\begin{frame}[fragile]
+\frametitle{Check sched\_rt\_runtime\_us!!}
+\begin{verbatim}
+echo -1 > \
+/proc/sys/kernel/sched_rt_runtime_us
+\end{verbatim}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Setting RT priority}
+\begin{lstlisting}
+/* Set realtime priority */
+struct sched_param param;
+param.sched_priority = 99;
+if (sched_setscheduler(0, SCHED_FIFO, &param) < 0)
+    perror("Can't set scheduling policy");
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Lock memory}
+\begin{lstlisting}
+if (mlockall(MCL_CURRENT | MCL_FUTURE))
+    perror("mlockall failed:");
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Configure malloc() behaviour}
+\begin{lstlisting}
+/* Turn off malloc trimming.*/
+mallopt(M_TRIM_THRESHOLD, -1);
+
+/* Turn off mmap usage. */
+mallopt(M_MMAP_MAX, 0);
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Reserve process memory}
+\begin{lstlisting}
+buffer = malloc(MSIZE);
+
+for (i = 0; i < MSIZE; i += sysconf(_SC_PAGESIZE))
+    buffer[i] = 0;
+
+free(buffer);
+\end{lstlisting}
+\textbf{BUT: Please avoid memory allocation from realtime context!!}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Prefault the stack (per Thread!)}
+\begin{lstlisting}
+/* Prove that this thread is behaving well */
+for (i = 0; i < SAVE_STACK_SIZE; i += sysconf(_SC_PAGESIZE))
+    buffer[i] = i;
+\end{lstlisting}
+\end{frame}
+
+\section{Clocks and Timers}
+
+\begin{frame}
+\frametitle{Clocks: Basic}
+\begin{itemize}
+\item Use POSIX!
+\item clock\_getres()
+\item clock\_gettime()
+\item clock\_settime()
+\item clock\_nanosleep()
+\end{itemize}
+\end{frame}
+
+\begin{frame}
+\frametitle{Clock types}
+\begin{itemize}
+\item CLOCK\_MONOTONIC: Clock that cannot be set and represents monotonic time
+since some unspecified starting point.
+\item CLOCK\_REALTIME: System-wide real-time clock. Can be set (by NTP, User,
+...)!
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Getting the time}
+\begin{lstlisting}
+struct timespec tv;
+struct timespec res;
+
+clock_gettime(CLOCK_MONOTONIC, &tv);
+
+clock_getres(CLOCK_MONOTONIC, &res);
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}
+\frametitle{Cyclic tasks}
+\begin{itemize}
+\item Use clock\_nanosleep!
+\item Do not use signals!
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Example: 1 / 2}
+\begin{lstlisting}
+#include <stdio.h>
+#include <time.h>
+
+#define CYCLE_TIME_NS (100 * 1000 * 1000) 
+#define NSEC_PER_SEC 1000000000
+
+void norm_ts(struct timespec *tv)
+{
+        if(tv->tv_nsec > NSEC_PER_SEC) {
+                tv->tv_sec++;
+                tv->tv_nsec -= NSEC_PER_SEC;
+        }
+}
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Example: 2 / 2}
+\begin{lstlisting}
+int main(void)
+{
+        struct timespec tv;
+
+        clock_gettime(CLOCK_MONOTONIC, &tv);
+        do {
+                /* Do the work */
+
+                /* Wait for next cycle */
+                tv.tv_nsec += CYCLE_TIME_NS;
+                norm_ts(&tv);
+                clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &tv, NULL);
+        } while(1);
+}
+\end{lstlisting}
+\end{frame}
+
+\section{Locking}
+\begin{frame}
+\frametitle{Locking}
+\begin{itemize}
+\item Use pthread\_mutexes
+\item Activate PRIO\_INHERIT
+\item Activate robustness
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{Locking example}
+\begin{lstlisting}
+pthread_mutex_t master_lock;
+pthread_mutexattr_t mattr;
+
+pthread_mutexattr_init(&mattr);
+pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
+pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+pthread_mutexattr_setrobust_np(&mattr, PTHREAD_MUTEX_ROBUST_NP);
+pthread_mutex_init(&master_lock, &mattr);
+
+pthread_mutex_lock(&master_lock);
+/* Do critical work */
+pthread_mutex_unlock(&master_lock);
+
+pthread_mutex_destroy(&master_lock);
+\end{lstlisting}
+\end{frame}
+
+\section{Signalling}
+\begin{frame}
+\frametitle{Signalling mechanisms}
+\begin{itemize}
+\item Do not use signals
+\item Use pthread\_cond\_vars
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{pthread\_cond\_var: Initialization 1 / 2}
+\begin{lstlisting}
+pthread_mutexattr_t mattr;
+pthread_condattr_t cattr;
+pthread_cond_t cond;
+pthread_mutex_t mutex;
+
+pthread_mutexattr_init (&mattr);
+pthread_mutexattr_setpshared (&mattr, PTHREAD_PROCESS_SHARED);
+pthread_mutexattr_setprotocol(&mattr, PTHREAD_PRIO_INHERIT);
+pthread_mutexattr_setrobust_np(&mattr, PTHREAD_MUTEX_ROBUST_NP);
+pthread_mutex_init (&mutex, &mattr);
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{pthread\_cond\_var: Initialization 2 / 2}
+\begin{lstlisting}
+pthread_condattr_init (&cattr);
+pthread_condattr_setpshared (&cattr, PTHREAD_PROCESS_SHARED);
+pthread_cond_init (&cond, &cattr);
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{pthread\_cond\_var: Send signal}
+\begin{lstlisting}
+/* Send signal */
+pthread_mutex_lock(&mutex);
+/* Do the homework */
+/* ... */
+pthread_cond_broadcast (&cond);
+pthread_mutex_unlock(&mutex);
+\end{lstlisting}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{pthread\_cond\_var: Wait for signal}
+\begin{lstlisting}
+/* Wait for signal */
+pthread_mutex_lock(&mutex);
+pthread_cond_wait (&cond, &mutex);
+/* -> We've been signalled */
+pthread_mutex_unlock(&mutex);
+\end{lstlisting}
+\end{frame}
+\end{document}
-- 
cgit v1.2.3

