Nuttx RTOS Beginnings
Total Page:16
File Type:pdf, Size:1020Kb
NuttX RTOS Beginnings Gregory Nutt AboutAboutAbout MeMeMe Math/Physics Digital Signal Processing Math Neuro- Computer Science 1970 science Academia 1980 First mult-tasking, pre-emptive RTOS, 1982 Cooperative Schedulers Hewlett Packard Military Avionics Cooperative Schedulers Commercial Electronics 1990 LaserJet POSIX RTOS, 1996 Costa Rica LaserJet Firmware Architect Open Source 2000 Ridgerun.com NuttX Release to Open Source, 2007 NX-Engineering, SA GraduateGraduateGraduate SchoolSchoolSchool DaysDaysDays LINC PDP-8/i LINC-8 PDP-8/e TRS-80 Model 1 HardwareHardwareHardware ofofof FirstFirstFirst RTOSRTOSRTOS InterruptInterruptInterrupt DrivenDrivenDriven ––– OSOSOS #1#1#1 (Bare(Bare(Bare Metal)Metal)Metal) NoNo OSOS:: ExtensiveExtensive interruptinterrupt processing,processing, prioritizedprioritized Response Latency interruptsinterrupts and,and, maybe,maybe, aa mainmain loop. Single Interrupt Handler loop. Deterministic?Deterministic? NoNo MeetMeet Deadlines?Deadlines? MaybeMaybe High Priority Medium Priority Low Priority Response Latency Problems:Problems: Stacked,Stacked, CanCan loselose interrupts.interrupts. NoNo waiting,waiting, allall runrun toto completion.completion. MainMainMain LoopLoopLoop ––– OSOSOS #1#1#1 (Cont'd)(Cont'd)(Cont'd) Task A Task G Task B Task F Main, Background Task C Loop Task E Non-deterministic! ● Round-Robin ● Non-premptive ● Cooperative Scheduling Task D ● State machines ● Ad hoc strategies Non-deterministic! RealRealRealReal TimeTimeTimeTime ======== DeterministicDeterministicDeterministicDeterministic Response Latency Stimulus Response Deadline RealReal timetime doesdoes notnot meanmean “fast”“fast” RealReal timetime systemssystems havehave DeadlinesDeadlines MainMainMain LoopLoopLoop withwithwith PriorityPriorityPriority QueueQueueQueue ––– OSOSOS #2#2#2 Interrupt BriefBrief interruptinterrupt processing,processing, onlyonly queuesqueues workwork Lowest Prio Main, Background Loop Highest Prio StillStill Non-deterministic!Non-deterministic! HighHigh prioritypriority workwork stillstill hashas toto waitwait forfor workwork inin progress.progress. TaskTask X X MainMainMain LoopLoopLoop withwithwith CooperativeCooperativeCooperative Scheduler–Scheduler–Scheduler– OSOSOS #3#3#3 Task X ●● Non-premptiveNon-premptive switch (state) ● { ● CooperativeCooperative SchedulingScheduling case state A: Start event processing; state = state B; ●● DivideDivide eventevent processingprocessing upup intointo piecespieces Reschedule; ●● ManageManage withwith aa statestate machinemachine Break; ●● RescheduleReschedule toto allowallow higherhigher prioritypriority taskstasks ●● OtherOther adad hochoc strategiesstrategies Case state B: Continue event processing; State = state C; Reschedule; Break; Case state C: Finish event processing; State = state X; Break; Case state X: Break; StillStill Non-deterministic!Non-deterministic! } HighHigh prioritypriority workwork stillstill hashas toto waitwait forfor workwork inin progress.progress. ForegroundForegroundForeground /// BackgoundBackgoundBackgound MainMainMain LoopsLoopsLoops ––– OSOSOS #4#4#4 ContextContext Switch!Switch! Foreground Background Main Loop Main Loop ParitiallyParitially DeterministicDeterministic Pre-emptivePre-emptivePre-emptive OSOSOS ––– OSOSOS #5#5#5 Task Start TheThe DECDEC connectionconnection Pending (NuttX) FullyFully pre-emptiblepre-emptible ContextContext switch:switch: Ready-to-run ThinkThink setjmp/longjmpsetjmp/longjmp onon steriodssteriods TaskTask ControlControl BlockBlock (TCB)(TCB) Blocked StatesStates representedrepresented byby listslists ofof TCBsTCBs HighestHighest PriorityPriority WaitWait forfor signal,signal, Running Ready-to-runReady-to-run tasktask semaphore,semaphore, isis RunningRunning messagemessage queue,queue, pagepage fill,fill, stopped,stopped, etc.etc. RTOSRTOSRTOSRTOS InterruptInterruptInterruptInterrupt ProcessingProcessingProcessingProcessing Stimulus Response Interrupt RTOSRTOS Scheduler Scheduler Handler ReassessReassess next next ready-to-runready-to-run thread thread Signals thread via IPC Resumes thread if highest priority, ready-to-run Task Task Suspended, Suspended, Waiting for Task awakened, Waiting for event Processes interrupt related event Next event RTOSRTOSRTOSRTOS InterruptsInterruptsInterruptsInterrupts NoNo OSOS way:way: ExtensiveExtensive interruptinterrupt processing,processing, prioritizedprioritized interruptsinterrupts and,and, maybe,maybe, aa mainmain looploop.. Interrupt Interrupt Interrupt Interrupt Interrupt Signal Task C SignalSignal Task Task A A Signal Task C SignalSignal Task Task E E SignalSignal Task Task B B SignalSignal Task Task D D RTOSRTOS way:way: MinimalMinimal workwork performedperformed inin interruptinterrupt handlershandlers InterruptInterrupt handlershandlers onlyonly signalsignal eventsevents toto taskstasks RTOSRTOS schedulerscheduler managesmanages real-timereal-time behaviorbehavior PrioritizedPrioritized interruptsinterrupts replacedreplaced withwith prioritizedprioritized taskstasks NoNo benefitbenefit inin nestingnesting interruptsinterrupts (usually)(usually) SMPSMPSMP Pending Ready-to-run nn HighestHighest PriorityPriority Blocked Ready-to-runReady-to-run taskstasks areare RunningRunning SpinlocksSpinlocks AffinityAffinity RunningRunningRunning CPU 1 Running CPU CPU2 3 AssignedAssigned TaskTask ListList (not(not shown)shown) CPU 0 CPU 1 CPU 2 CPU n RateRateRate MontonicMontonicMontonic SchedulingSchedulingScheduling CanCan achieveachieve Real-TimeReal-Time behaviorbehavior underunder certaincertain circumstancescircumstances ● ● StrictStrict prioritypriority schedulingscheduling ● Threads with shorter periods/ ● Static priorities Threads with shorter periods/ Static priorities ● deadlinesdeadlines areare assignedassigned thethe ● PrioritiesPriorities assignedassigned accordingaccording toto highesthighest priorities.priorities. ● ● RateRate MonotonicMonotonic conventionsconventions And this unrealistic assumption: ● No resource sharing ● No waiting for resources ● No semaphores or locks ● No critical sections ● No disabling pre-emption ● No disabling interrupts WhyWhyWhy POSIX?POSIX?POSIX? WhyWhy not...not... VersusVersus customcustom adad hochoc OSOS interfaceinterface POSIXPOSIX devicedevice modelmodel vsvs HALHAL LikeLike simplersimpler FreeRTOS,FreeRTOS, ChibiOS,ChibiOS, Zephyr,Zephyr, mbed,mbed, RIOT,RIOT, etc.etc. AtAt thisthis pointpoint POSIXPOSIX isis thethe NuttXNuttX identityidentity PortabilityPortability LinuxLinux compatibilitycompatibility ComplexComplex buildbuild models:models: PROTECTEDPROTECTED andand KERNELKERNEL buildsbuilds WorkWork Queues Queues Interrupt Handler “Top Half” Worker Prioritized Work Thread Defer more Queue “Bottom half” extended interrupt processing to Worker Thread PriorityPriority Qeue Qeue Non-premptiveNon-premptive Non-deterministic! VeryVery high high priority priority InappropriateInappropriate for for extended processing UseUse withwith care!care! MultipleMultipleMultipleMultiple WorkWorkWorkWork QueuesQueuesQueuesQueues SingleSingle highhigh prioritypriority workwork queuequeue High Priority High Priority IntendedIntended forfor interruptinterrupt “bottom“bottom half”half” WorkWork QueueQueue ShouldShould bebe highesthighest prioritypriority P R I O R MultipleMultiple Multiple low priority work queues I Multiple low priority work queues Low Priority Low Priority Support priority inheritance T WorkHigh Queues Priority Support priority inheritance WorkHigh Queues Priority Y WorkWork High HighQueueQueue PriorityPriority UseUse toto implementimplement asynchronousasynchronous WorkWork QueueQueue I/OI/O (AIO)(AIO) ThreadThread poolpool.