Вы находитесь на странице: 1из 33

Why Linux is not an RTOS:

porting hints

Chris Simmonds 2net Limited Embedded Systems Conference UK. 2009 Copyright 2009, 2net Limited

Overview

Linux is a popular choice as an embedded OS ost pro!ects evolve "rom previous pro!ects o"ten based on an #$OS %ow to get "rom point & '#$OS( to point ) 'Linux(*

+s Linux an #$OS**

Chris Simmonds 2net Ltd

-orting options
Application Application

Application

Application

RTOS emulation library (*)

RTOS

Linux

Linux

Virtual machine monitor

Linux

(*) for example v2lin [ ! or "apu#oft


Chris Simmonds 2net Ltd ,

$he porting dilemma

%ow much existing code can + /eep* %ow much e""ort is re0uired to port my application to Linux* 1hat should + loo/ out "or* 1hat are the gains*

Chris Simmonds 2net Ltd

1hy Linux is not an #$OS

&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*

Chris Simmonds 2net Ltd

6i""erent memory models


RTOS* all one memory #pace Linux memory #pace# Application

(#er #pace

Application

' library

)ernel #pace

RTOS

Linux

$ar%&are
Chris Simmonds 2net Ltd

$ar%&are
9

6evice drivers

Linux

%o-,R.()

+evice %river

,SR

$ar%&are

Chris Simmonds 2net Ltd

1hy Linux is not an #$OS

&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*

Chris Simmonds 2net Ltd

<verything is a "ile
3or example4 to rea% character# from fir#t #erial port4 open %evice no%e (file) /%ev/ttyS0
f = open (/dev/ttyS0, O_RDWR); l = read (f, buf, len);

/%ev/ttyS0

"a5or number 1 "inor number 21

Linux

(ART %river 1*21


9

Chris Simmonds 2net Ltd

y device doesn>t loo/ li/e a "ile?

@ile read A write operations wor/ well with byte streams 8 such as a serial port %ow about a robot arm* $he ioctl "unction allows any interaction you want
stru t robot_ ontrol_blo ! r ; f = open (/dev/robot, O_RDWR); """ #o tl (f, S$%_RO&O%_'(R()%$RS, *r ); """

Chris Simmonds 2net Ltd

=0

%int =

+denti"y all code that accesses hardware directly 6esign a "ile8based inter"ace

Bse ioctl "or things that do not naturally "it the "ile concept

a/e this into a device driver #ememberC /eep device drivers as simple as possible

&ll the complicated stu"" should be in the application

Chris Simmonds 2net Ltd

==

CheatingC user space drivers

mmap allows an application direct access to device memory

)ut, cannot handle interrupts Do control o" C-B cache or instruction 0ueue Dot the 3Linux way4

$he Linux Bser +O subsystem uses mmap to provide a "lexible "ramewor/ "or user space drivers

B+O E2F
=2

Chris Simmonds 2net Ltd

mmap example
+#n lude ,sys/--an"./ +def#ne 0O_'12S_(DDR$SS 0340500000 +def#ne (R)_'OS 030067 +def#ne (R)_)O%0O8 030069 #nt -a#n (#nt ar: , .ar ;ar:v<=) > uns#:ned led_dat; #nt -.; .ar ;ptr; -. = open (?/dev/-e-?, O_RDWR); ptr = --ap (0, 03@000, 'RO%_R$(D A 'RO%_WR0%$, )('_S1(R$D, -., 0O_'12S_(DDR$SS); """ ;(uns#:ned #nt;)(ptr B (R)_'OS) = neC_pos; C.#le (;(uns#:ned #nt;)(ptr B (R)_)O%0O8) D= 0) sleep (@); """
Chris Simmonds 2net Ltd =,

1hy Linux is not an #$OS

&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*

Chris Simmonds 2net Ltd

=.

-rocesses
6,+ /bin/init 6,+ 7 /bin/http% 6,+ 08 /bin/controler T T2 T8

6roce## 9 a%%re## #pace : pro;ram : threa% of execution Some proce## have < threa%

' library

Linux

Chris Simmonds 2net Ltd

=2

-ros and cons o" processes

-ro

-rotected memory space #esources 'memory, open "iles( released when exit <asy to re8start a "ailed process

Con

Communication between processes 0uite slow A cumbersome

Chris Simmonds 2net Ltd

=9

-ros and cons o" threads

-ro

<asy communication using shared variables, mutexes and condition variables Similar memory model to #$OS tas/s

Con

Do memory protection between threads in the same process

Chris Simmonds 2net Ltd

=:

1hy Linux is not an #$OS

&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*

Chris Simmonds 2net Ltd

=;

Scheduling policies

SC%<6GO$%<#

$ime shareC "airnessH -riority set by scheduler

SC%<6G@+@O

@ixed priority '= to 99(I preempts SC%<6GO$%<# Bse this "or real8time activities

SC%<6G##

&s SC%<6G@+@O but tas/s o" same priority time8slice 6e"ault 0uantum is =00 ms

Chris Simmonds 2net Ltd

=9

%int 2

a/e closely8coupled groups o" #$OS tas/s into -OS+7 threads within a process Separate #$OS tas/s with little interaction into separate processes a/e real time threads SC%<6G@+@O

Bse #ate onotonic &nalysis or similar to choose priorities E,F

&ll non real8time threads should be SC%<6GO$%<#


20

Chris Simmonds 2net Ltd

1hy Linux is not an #$OS

&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*

Chris Simmonds 2net Ltd

2=

-OS+7

-Ortable Operating System +nter"ace

+<<< standard =00,H=

ost #$OS "unctions map to -OS+7 one8to8one

$as/s to -OS+7 threads Semaphores and mutexes to -OS+7 semaphores, mutexes and condition variables essage 0ueues to -OS+7 message 0ueues 1atchdog timers to -OS+7 cloc/s and timers

Chris Simmonds 2net Ltd

22

Loo/ out "or

-OS+7 $hreads

$hreads run immediately they are created Dot possible to terminate an arbitrary thread

-OS+7 semaphores and mutexes

-OS+7 has many types o" mutex, including priority inheritanceH See E.F -OS+7 does have semaphores but they are not much usedH See E2F "or a discussion on mutexes vs semaphores

Chris Simmonds 2net Ltd

2,

Library and /ernel versions

@or "ull -OS+7 compliance in Linux you need current versions o" Linux and the C library

Jernel KL 2H9H22 MDB C library KL 2H2

)eware uClibc E9F

Small, 3embeddable4 C library Mood "or small systems with NL =9 i) storage )B$, lac/s many -OS+7 "unctions

Chris Simmonds 2net Ltd

2.

%int ,

Oou are going to have to re8write some code 1here possible, re8"actor code around shared data

1rite accessor "unctions to hide data structure "rom rest o" the program -rotect against concurrent access using a mutex +n the literature this is called a monitor E:F 8 ma/es "uture maintenance and porting much easier

Chris Simmonds 2net Ltd

22

1hy Linux is not an #$OS

&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*

Chris Simmonds 2net Ltd

29

+s Linux real time*


6eterministic scheduler Static priorities 'SC%<6G@+@O( -riority inheritance mutexes Loc/able memory 8 stops demand paging %igh resolution timers 6eterministic interrupt response

Chris Simmonds 2net Ltd

2:

6emand paging
6ro;ram a%%re##e#

$eap

6ro;ram file

-ages o" code and data are read "rom the program "ile on demand .Ji) at a time Causes !itter in real8time programs

+ata

'o%e

%intC Oou can page in and loc/ all memory using


-lo !all ()EF_GH%HR$);

Chris Simmonds 2net Ltd

2;

+nterrupt latency
,nterrupt ,SR #tart# ,SR complete# = call# &a>e-up Ta#> i# #che%ule%

T0

T2

T8

Time

,nterrupt# %i#able%

,SR execute

6reemption latency

,nterrupt latency

Chris Simmonds 2net Ltd

29

Jernel preemption options

6e"ault 8 no preemption in /ernel mode

Mood "or throughput, bad "or real8time

-reemptible /ernel

#educes !itter in preemption latency Mood "or so"t real8time

-#<< -$G#$ E;F

#educes !itter in all three areas Mood "or 3"irm4 real8time Dot in the main line /ernel yet
,0

Chris Simmonds 2net Ltd

%int .

+n a real8time system, wor/ out what deadlines you have and how much !itter you can accept Loc/ memory in any process with real8time threads with mloc/all @or so"t real8time with !itter P millisecond enable /ernel preemption @or 3"irm4 real8time with !itter P =0>s or =00>s microseconds use the -#<< -$G#$ patch

Chris Simmonds 2net Ltd

,=

Summary

-orting to Linux will re0uire some code re"actoring

%ardware re0uires device drivers $as/s become threads in one or more processes ap #$OS "unctions onto -OS+7 Select real time model

+s Linux an #$OS*

DoC it is a complete operating system?

Chris Simmonds 2net Ltd

,2

#e"erences
E=F v2lin 8 Qx1or/s &-+ "or Linux httpC55v2linHsource"orgeHnet5 E2F B+OC user8space drivers httpC55lwnHnet5&rticles52,22:2 E,F #ate8monotonic scheduling httpC55enHwi/ipediaHorg5wi/i5#ate8monotonicGscheduling E.F E2F utex mutandisC understanding mutex types and attributes httpC55wwwHembedded8linuxHcoHu/5tutorial5mutexGmutandis utex vsH Semaphores httpC55wwwH"eabhasHcom5blog5labels5SemaphoreHhtml

E9F uClibcC & C library "or embedded Linux httpC55wwwHuclibcHorg5 E:F onitor 'synchroniRation( httpC55enHwi/ipediaHorg5wi/i5 onitorG'synchroniRation(

E;F $he -#<< -$G#$ real time patch series httpC55wwwH/ernelHorg5pub5linux5/ernel5pro!ects5rt5

Chris Simmonds 2net Ltd

,,

Вам также может понравиться