45
Your Logo Here F9 Microkernel ktimer Viller Hsiao <[email protected]>

f9-microkernel-ktimer

Embed Size (px)

DESCRIPTION

Introduction about F9 Microkernel ktimer subsystem

Citation preview

Page 1: f9-microkernel-ktimer

Your Logo Here

F9 Microkernel ktimer

Viller Hsiao <[email protected]>

Page 2: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGORights to Copycopyright © 2014 Viller Hsiao

Attribution – ShareAlike 3.0

You are free● to copy, distribute, display, and perform the work● to make derivative works● to make commercial use of the work

Under the following conditions

– Attribution. You must give the original author credit.– Share Alike. If you alter, transform, or build upon this work, you may

distribute the resulting work only under a license identical to this one.For any reuse or distribution, you must make clear to others the license terms of this work.● Any of these conditions can be waived if you get permission from the copyright holder.

Your fair use and other rights are in no way affected by the above.

License text: http://creativecommons.org/licenses/by-sa/3.0/legalcode

Corrections, suggestions, contributions and translations are welcome!

Page 3: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOF9 Microkernel [1]

● New open source implementation built from scratch, which deploys modern kernel techniques, derived from L4 microkernel designs, to deep embedded devices.● BSD License

● Characteristics of F9 microkernel● Efficiency: performance + power consumption

– Tickless● Security: memory protection + isolated execution● Flexible development environment

● Repository● https://github.com/f9micro

Page 4: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOAgenda

● Concept about operating system kernel time subsystem

● F9 Microkernel ktimer introduction

Page 5: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGO

Concept about Operating System Kernel Time Subsystem

Page 6: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOOS Kernel Time Subsystem

● Current Time● System time, UTC/GMP time, … etc

● Alarm, the time event● Process timeslice● Timeout● Time delay

Page 7: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOSystem Time and Tick

● From Wikipedia [2],“System time represents a computer system's notion of the passing of time.“

“System time is measured by a system clock, which is typically implemented as a simple count of the number of ticks that have transpired since some arbitrary starting date, called the epoch.”

Page 8: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOHow Is Tick Implemented?

● Hardware timer device● Assert interrupt after a programmable inteval

– Handling tick stuff in Timeout Interrupt Service Routine (ISR)

Timeout interrupt

HWTimer CPU

ControlRead Counts

Setup timeout value

Adjust system timeHandle timeout event

….

Page 9: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOHow Is Tick Implemented? (Cont.)

● Type 1: Incremental timer● Example: Cortex-A9 MP Global Timer

Page 10: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOHow Is Tick Implemented? (Cont.)

● Incremental timer functionality

Current Value0x12abc

AutoIncreament?

Incremental value0x36000

Compare Value0x36000

+

Interrupt

1 +

Y

=Y

Page 11: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOHow Is Tick Implemented? (Cont.)

● Type 2: Counting down timer● Example: Cortex-M4 SysTick

Page 12: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOHow Is Tick Implemented? (Cont.)

● Counting down timer functionality

AutoLoad?

Reload value0x36000

Current Value0x12abc=

Interrupt

1 -

0

=

Y

Y

Page 13: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOHow Is Tick Implemented? (Cont.)

● Prescaler● Tweak HW tick period

Counting PartPrescaler

valclk/val

Timer Module

clk

Page 14: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOHow Is Tick Implemented? (cont.)

● Timeout ISR● Increase system ticks● Execute handler of timeout event● Re-schedule if required

Page 15: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOSimple Graph of CPU Operating States

INT : interruptCTX: context switchT : after a while

ProcessesThreads

Tasks

Idle thread

ISRSoftirq

sleep

Deep sleep

INT

CTX

INT

INTINT

CTX

CTXCTX

T

T

Page 16: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOTime Diagram of Legacy Ticks

event1 event3

event2

event4

HWTimerinterrupt

CPUactivities

Page 17: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOWhere Is the Problem?

● Context switch overhead

ProcessesThreadsTasks

Idle thread

ISRSoftirq

sleep

Deep sleep

INT

CTXINT

INTINT

CTX

CTXCTX

T

T

Page 18: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOWhere Is the Problem?

● Unecessary power consumed

ProcessesThreadsTasks

Idle thread

ISRSoftirq

sleep

Deep sleep

INT

CTXINT

INTINT

CTX

CTXCTX

T

T

Page 19: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOWhere Is the Problem?

● Tick resolution● Performance, power v.s. timer precision

Page 20: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOTime Diagram of Legacy Ticks

event1 event3

event2

event4

HWTimerinterrupt

CPUactivities

CPU waken up for timekeeping only

Page 21: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOSolution: Tickless

event1 event3

event2

event4

Timerinterrupt

NewCPUactivities

PreviousCPUactivities

Page 22: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGODrawback of Tickless

● Tickless is not free [3],

“It increases the number of instructions executed on the path to and from the idle loop.”

“On many architectures, dyntick-idle mode also increases the number of expensive clock-reprogramming operations”

● Systems with aggressive real-time response constraints often run periodic tick

Page 23: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOExample: Linux Time Subsystem

● Device level● clocksource

– jiffies● clockevent

● High resolution kernel timer● hrtimers

Page 24: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOExample: Linux Time Subsystem (Cont.)

● Tickless● (Nearly) Full tickless operation after 3.10

● dyntick-idle– CONFIG_NO_HZ_IDLE

● adaptive-tick – CONFIG_NO_HZ_FULL

● periodic– CONFIG_HZ_PERIODIC– Consumes battery power 2-3 times as fast as dyntick-idle one. [3]

Page 25: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGO

F9 Microkernel ktimer Introduction

Page 26: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOExample Hardware Device

● STM32F4Discovery board● Cortex-M4 EVB

– Use Cortex-M4 SysTick as the tick HW● 24-bit system timer● Counting down● Please refer to [4] for more detail

Page 27: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Features

● System time

● Time event

● Tickless

Page 28: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Configurations

● CONFIG_KTIMER_HEARTBEAT● HW cycles per ktimer tick

● CONFIG_KTIMER_MINTICKS● Minimum ktimer ticks unit for time event

● CONFIG_KTIMER_TICKLESS● Enable tickless

Page 29: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Event Utility

● ktimer_event_create()● Arguments

– Timeout ticks– Timeout handler– Pointer to arguments for timeout handler

● Return Non-zero to reschedule event again– Freed automatically if not be re-scheduled

Page 30: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy

● System time● ktime_now

● ktimer event list● event_queue● Managed by ktable

Page 31: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● ktable● Basic kernel library in F9 Microkernel● Fast objects pool management

– To allocate/free objects of pre-defined size and numbers easiler

ktable

ktable_free()

ktable_alloc()

ktable_init()

Used

Unused

Page 32: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Life cycle of time event● One-shot● Continuous

● How to control life cycle in ktimer?● The return value of handler is treated as next

timeout delta

Page 33: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● HW Timer ISR● __ktimer_handler()

– Increase tick– Arrange Softirq for handling timer events

Page 34: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Two-stage interrupt handling● ISR

– IRQ context● Softirq

– Thread context● Real time preemptive characteristic.● Can be scheduled like any other threads in the system

[5].

– Handled in kernel thread

Page 35: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● HW Timer ISR● __ktimer_handler()

– Increase tick– Arrange Softirq for handling timer events

Page 36: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● HW timer softirq handler● ktimer_event_handler()

– Find timeout event and executing its handler– Re-insert event if required

Page 37: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Tickless● Enter tickless right before going to CPU idle

state● Set interval of next timer interrupt as delta of

next event– Or KTIMER_MAXTICKS

● Adjust system time after waked up

Page 38: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Tickless Compensation● SysTick frequency distortion when enter/exit standby

mode

Timerinterrupt

idle idle active

handleother

interrupt

tickless

CPUActivity

active

systick

Page 39: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Tickless Compensation

Timerinterrupt

idle idle active

handleother

interrupt

tickless

CPUActivity

active

systick

Page 40: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Tickless Compensation● Pre-defined value

– CONFIG_KTIMER_TICKLESS_COMPENSATION● Compensation value before entering idle

– CONFIG_KTIMER_TICKLESS_INT_COMPENSATION● Compensation value when CPU is waked up by non-timer

interrupt.

● But how much to set these value to?

Page 41: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Tickless Compensation● But how much to set these value to?

– Get average compensation from general pupose timer

● (total diff / count of entering tickless)

SYSTICK

HW timerTIM2

Page 42: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● Tickless Compensation● How to calibrate the compensation value?

– CONFIG_KTIMER_TICKLESS_VERIFY– CONFIG_KDB

Setcompensation

To 0

Getaverage

Compensationfrom KDB

Re-define Compensation

value

Page 43: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOktimer Anotomy (Cont.)

● KDB● Kernel debugger

## KDB ## commands:K: print kernel tablese: dump ktimer eventsn: show timer (now)s: show softirqst: dump threadsm: dump memory poolsa: dump address spacesp: show samplingv: show tickless scheduling stat----------------

## KDB ##-------NOW------Now is 70993Ktimer T=1297 D=0--------------------

Page 44: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGOReference:

[1] Jim Huang (Dec 9, 2013), “F9: A Secure and Efficient Microkernel Built for Deep Embedded Systems”

[2] Wikipedia, “System time”

[3] P. E. McKenney (May 14, 2013), “NO_HZ: Reducing Scheduling-Clock Ticks”

[4] ST Mcroelectronics, STM32F3xxx and STM32F4xxx Cortex-M4 programming manual

[5] J. Altenberg, “Using the Realtime Preemption Patch on ARM CPUs”

Page 45: f9-microkernel-ktimer

Place, 04/02/14 villerhsiao

LOGO

THE END