Your Logo Here

Viller Hsiao <villerhsiao@gmail.com>

F9 Microkernel ktimer
Rights to Copy

LOGO

copyright © 2014 Viller Hsiao

Attribution – ShareAlike 3.0
You are free
●

to copy, distribute, display, and perform the work

●

to make derivative works

●

Corrections, suggestions,
contributions and
translations are welcome!

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

Place, 04/02/14

villerhsiao
F9 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

●

●

LOGO

Flexible development environment

Repository
●

https://github.com/f9micro

Place, 04/02/14

villerhsiao
Agenda

●

●

LOGO

Concept about operating system kernel
time subsystem
F9 Microkernel ktimer introduction

Place, 04/02/14

villerhsiao
LOGO

Concept about Operating System Kernel
Time Subsystem

Place, 04/02/14

villerhsiao
OS Kernel Time Subsystem

●

Current Time
●

System time, UTC/GMP time, … etc

●

Alarm, the time event

●

Process timeslice

●

Timeout

●

Time delay

Place, 04/02/14

villerhsiao

LOGO
System Time and Tick

●

LOGO

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.”

Place, 04/02/14

villerhsiao
How Is Tick Implemented?
●

LOGO

Hardware timer device
●

Assert interrupt after a programmable inteval
–

Handling tick stuff in Timeout Interrupt Service
Routine (ISR)

HW
Timer

CPU
Adjust system time
Handle timeout event
….

Timeout interrupt

Control
Read Counts
Setup timeout value

Place, 04/02/14

villerhsiao
How Is Tick Implemented? (Cont.)
●

Type 1: Incremental timer
●

Example: Cortex-A9 MP Global Timer

Place, 04/02/14

villerhsiao

LOGO
How Is Tick Implemented? (Cont.)
●

LOGO

Incremental timer functionality

1

Current Value

+

0x12abc
=

Incremental value
0x36000

Compare Value

+

0x36000

Y

Place, 04/02/14

Y

Auto
Increament?
villerhsiao

Interrupt
How Is Tick Implemented? (Cont.)
●

Type 2: Counting down timer
●

Example: Cortex-M4 SysTick

Place, 04/02/14

villerhsiao

LOGO
How Is Tick Implemented? (Cont.)
●

LOGO

Counting down timer functionality
0

=

1

-

Reload value

=

0x36000

Current Value
0x12abc

Y

Place, 04/02/14

Y

Auto
Load?
villerhsiao

Interrupt
How Is Tick Implemented? (Cont.)
●

Prescaler
●

Tweak HW tick period
Timer Module

clk

Place, 04/02/14

Prescaler
val

clk/val

villerhsiao

Counting Part

LOGO
How Is Tick Implemented? (cont.)

●

Timeout ISR
●

Increase system ticks

●

Execute handler of timeout event

●

Re-schedule if required

Place, 04/02/14

villerhsiao

LOGO
Simple Graph of CPU Operating States

LOGO

INT

Processes
Threads
Tasks

ISR
Softirq
CTX
INT

CTX
CTX
Idle thread

CTX
INT

INT

T
sleep
T
Deep sleep

Place, 04/02/14

villerhsiao

INT : interrupt
CTX: context switch
T : after a while
Time Diagram of Legacy Ticks

event1

event2
event3

LOGO

event4

HW
Timer
interrupt

CPU
activities

Place, 04/02/14

villerhsiao
Where Is the Problem?
●

LOGO

Context switch overhead
INT

Processes
Threads
Tasks

ISR
Softirq
CTX
INT

CTX
CTX

Idle thread
T
sleep
T
Deep sleep

Place, 04/02/14

villerhsiao

CTX
INT

INT
Where Is the Problem?
●

LOGO

Unecessary power consumed
INT

Processes
Threads
Tasks

ISR
Softirq
CTX
INT

CTX
CTX

Idle thread
T
sleep
T
Deep sleep

Place, 04/02/14

villerhsiao

CTX
INT

INT
Where Is the Problem?

●

Tick resolution
●

Performance, power v.s. timer precision

Place, 04/02/14

villerhsiao

LOGO
Time Diagram of Legacy Ticks

event1

event2
event3

LOGO

event4

HW
Timer
interrupt

CPU
activities

CPU waken up for timekeeping only
Place, 04/02/14

villerhsiao
Solution: Tickless

event1

event2
event3

LOGO

event4

Timer
interrupt
New
CPU
activities

Previous
CPU
activities
Place, 04/02/14

villerhsiao
Drawback of Tickless

●

LOGO

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 clockreprogramming operations”

●

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

Place, 04/02/14

villerhsiao
Example: Linux Time Subsystem

●

Device level
●

clocksource
–

●

●

jiffies

clockevent

High resolution kernel timer
●

hrtimers

Place, 04/02/14

villerhsiao

LOGO
Example: Linux Time Subsystem (Cont.)

●

LOGO

Tickless
●

(Nearly) Full tickless operation after 3.10

●

dyntick-idle
–

●

adaptive-tick
–

●

CONFIG_NO_HZ_IDLE

CONFIG_NO_HZ_FULL

periodic
–

CONFIG_HZ_PERIODIC

–

Consumes battery power 2-3 times as fast as dyntick-idle one. [3]

Place, 04/02/14

villerhsiao
LOGO

F9 Microkernel ktimer Introduction

Place, 04/02/14

villerhsiao
Example Hardware Device

●

STM32F4Discovery board
●

Cortex-M4 EVB
–

Use Cortex-M4 SysTick as the tick HW
●
●
●

Place, 04/02/14

24-bit system timer
Counting down
Please refer to [4] for more detail

villerhsiao

LOGO
ktimer Features

●

System time

●

Time event

●

Tickless

Place, 04/02/14

villerhsiao

LOGO
ktimer Configurations

●

CONFIG_KTIMER_HEARTBEAT
●

●

CONFIG_KTIMER_MINTICKS
●

●

HW cycles per ktimer tick

Minimum ktimer ticks unit for time event

CONFIG_KTIMER_TICKLESS
●

Enable tickless

Place, 04/02/14

villerhsiao

LOGO
ktimer Event Utility

●

LOGO

ktimer_event_create()
●

Arguments
–
–

Timeout handler

–

●

Timeout ticks
Pointer to arguments for timeout handler

Return Non-zero to reschedule event again
–

Freed automatically if not be re-scheduled

Place, 04/02/14

villerhsiao
ktimer Anotomy

●

System time
●

●

ktime_now

ktimer event list
●

event_queue

●

Managed by ktable

Place, 04/02/14

villerhsiao

LOGO
ktimer Anotomy (Cont.)
●

LOGO

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_init()

Used
Unused
Place, 04/02/14

ktable_alloc()
villerhsiao
ktimer Anotomy (Cont.)

●

Life cycle of time event
●

One-shot

●

●

LOGO

Continuous

How to control life cycle in ktimer?
●

The return value of handler is treated as next
timeout delta

Place, 04/02/14

villerhsiao
ktimer Anotomy (Cont.)

●

HW Timer ISR
●

__ktimer_handler()
–

Increase tick

–

Arrange Softirq for handling timer events

Place, 04/02/14

villerhsiao

LOGO
ktimer Anotomy (Cont.)

●

LOGO

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

Place, 04/02/14

villerhsiao
ktimer Anotomy (Cont.)

●

HW Timer ISR
●

__ktimer_handler()
–

Increase tick

–

Arrange Softirq for handling timer events

Place, 04/02/14

villerhsiao

LOGO
ktimer Anotomy (Cont.)

●

HW timer softirq handler
●

ktimer_event_handler()
–

Find timeout event and executing its handler

–

Re-insert event if required

Place, 04/02/14

villerhsiao

LOGO
ktimer Anotomy (Cont.)

●

LOGO

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

Place, 04/02/14

villerhsiao
ktimer Anotomy (Cont.)
●

LOGO

Tickless Compensation
●

SysTick frequency distortion when enter/exit standby
mode
tickless

active

idle

handle
other
interrupt

Timer
interrupt
idle

active

CPU
Activity
systick

Place, 04/02/14

villerhsiao
ktimer Anotomy (Cont.)
●

LOGO

Tickless Compensation

tickless

active

idle

handle
other
interrupt

Timer
interrupt
idle

active

CPU
Activity
systick

Place, 04/02/14

villerhsiao
ktimer Anotomy (Cont.)

●

LOGO

Tickless Compensation
●

Pre-defined value
–

CONFIG_KTIMER_TICKLESS_COMPENSATION
●

–

CONFIG_KTIMER_TICKLESS_INT_COMPENSATION
●

●

Compensation value before entering idle
Compensation value when CPU is waked up by non-timer
interrupt.

But how much to set these value to?

Place, 04/02/14

villerhsiao
ktimer Anotomy (Cont.)

●

LOGO

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 timer
TIM2

Place, 04/02/14

villerhsiao
ktimer Anotomy (Cont.)

●

LOGO

Tickless Compensation
●

How to calibrate the compensation value?
–

CONFIG_KTIMER_TICKLESS_VERIFY

–

CONFIG_KDB

Set
compensation
To 0

Place, 04/02/14

Get
average
Compensation
from KDB

villerhsiao

Re-define
Compensation
value
ktimer Anotomy (Cont.)
●

LOGO

KDB
●

Kernel debugger

## KDB ##
commands:
K: print kernel tables
e: dump ktimer events
n: show timer (now)
s: show softirqs
t: dump threads
m: dump memory pools
a: dump address spaces
p: show sampling
v: show tickless scheduling stat
----------------

Place, 04/02/14

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

villerhsiao
Reference:

LOGO

[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”

Place, 04/02/14

villerhsiao
LOGO

THE END

Place, 04/02/14

villerhsiao

f9-microkernel-ktimer

  • 1.
    Your Logo Here VillerHsiao <villerhsiao@gmail.com> F9 Microkernel ktimer
  • 2.
    Rights to Copy LOGO copyright© 2014 Viller Hsiao Attribution – ShareAlike 3.0 You are free ● to copy, distribute, display, and perform the work ● to make derivative works ● Corrections, suggestions, contributions and translations are welcome! 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 Place, 04/02/14 villerhsiao
  • 3.
    F9 Microkernel [1] ● Newopen 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 ● ● LOGO Flexible development environment Repository ● https://github.com/f9micro Place, 04/02/14 villerhsiao
  • 4.
    Agenda ● ● LOGO Concept about operatingsystem kernel time subsystem F9 Microkernel ktimer introduction Place, 04/02/14 villerhsiao
  • 5.
    LOGO Concept about OperatingSystem Kernel Time Subsystem Place, 04/02/14 villerhsiao
  • 6.
    OS Kernel TimeSubsystem ● Current Time ● System time, UTC/GMP time, … etc ● Alarm, the time event ● Process timeslice ● Timeout ● Time delay Place, 04/02/14 villerhsiao LOGO
  • 7.
    System Time andTick ● LOGO 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.” Place, 04/02/14 villerhsiao
  • 8.
    How Is TickImplemented? ● LOGO Hardware timer device ● Assert interrupt after a programmable inteval – Handling tick stuff in Timeout Interrupt Service Routine (ISR) HW Timer CPU Adjust system time Handle timeout event …. Timeout interrupt Control Read Counts Setup timeout value Place, 04/02/14 villerhsiao
  • 9.
    How Is TickImplemented? (Cont.) ● Type 1: Incremental timer ● Example: Cortex-A9 MP Global Timer Place, 04/02/14 villerhsiao LOGO
  • 10.
    How Is TickImplemented? (Cont.) ● LOGO Incremental timer functionality 1 Current Value + 0x12abc = Incremental value 0x36000 Compare Value + 0x36000 Y Place, 04/02/14 Y Auto Increament? villerhsiao Interrupt
  • 11.
    How Is TickImplemented? (Cont.) ● Type 2: Counting down timer ● Example: Cortex-M4 SysTick Place, 04/02/14 villerhsiao LOGO
  • 12.
    How Is TickImplemented? (Cont.) ● LOGO Counting down timer functionality 0 = 1 - Reload value = 0x36000 Current Value 0x12abc Y Place, 04/02/14 Y Auto Load? villerhsiao Interrupt
  • 13.
    How Is TickImplemented? (Cont.) ● Prescaler ● Tweak HW tick period Timer Module clk Place, 04/02/14 Prescaler val clk/val villerhsiao Counting Part LOGO
  • 14.
    How Is TickImplemented? (cont.) ● Timeout ISR ● Increase system ticks ● Execute handler of timeout event ● Re-schedule if required Place, 04/02/14 villerhsiao LOGO
  • 15.
    Simple Graph ofCPU Operating States LOGO INT Processes Threads Tasks ISR Softirq CTX INT CTX CTX Idle thread CTX INT INT T sleep T Deep sleep Place, 04/02/14 villerhsiao INT : interrupt CTX: context switch T : after a while
  • 16.
    Time Diagram ofLegacy Ticks event1 event2 event3 LOGO event4 HW Timer interrupt CPU activities Place, 04/02/14 villerhsiao
  • 17.
    Where Is theProblem? ● LOGO Context switch overhead INT Processes Threads Tasks ISR Softirq CTX INT CTX CTX Idle thread T sleep T Deep sleep Place, 04/02/14 villerhsiao CTX INT INT
  • 18.
    Where Is theProblem? ● LOGO Unecessary power consumed INT Processes Threads Tasks ISR Softirq CTX INT CTX CTX Idle thread T sleep T Deep sleep Place, 04/02/14 villerhsiao CTX INT INT
  • 19.
    Where Is theProblem? ● Tick resolution ● Performance, power v.s. timer precision Place, 04/02/14 villerhsiao LOGO
  • 20.
    Time Diagram ofLegacy Ticks event1 event2 event3 LOGO event4 HW Timer interrupt CPU activities CPU waken up for timekeeping only Place, 04/02/14 villerhsiao
  • 21.
  • 22.
    Drawback of Tickless ● LOGO Ticklessis 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 clockreprogramming operations” ● Systems with aggressive real-time response constraints often run periodic tick Place, 04/02/14 villerhsiao
  • 23.
    Example: Linux TimeSubsystem ● Device level ● clocksource – ● ● jiffies clockevent High resolution kernel timer ● hrtimers Place, 04/02/14 villerhsiao LOGO
  • 24.
    Example: Linux TimeSubsystem (Cont.) ● LOGO Tickless ● (Nearly) Full tickless operation after 3.10 ● dyntick-idle – ● adaptive-tick – ● CONFIG_NO_HZ_IDLE CONFIG_NO_HZ_FULL periodic – CONFIG_HZ_PERIODIC – Consumes battery power 2-3 times as fast as dyntick-idle one. [3] Place, 04/02/14 villerhsiao
  • 25.
    LOGO F9 Microkernel ktimerIntroduction Place, 04/02/14 villerhsiao
  • 26.
    Example Hardware Device ● STM32F4Discoveryboard ● Cortex-M4 EVB – Use Cortex-M4 SysTick as the tick HW ● ● ● Place, 04/02/14 24-bit system timer Counting down Please refer to [4] for more detail villerhsiao LOGO
  • 27.
    ktimer Features ● System time ● Timeevent ● Tickless Place, 04/02/14 villerhsiao LOGO
  • 28.
    ktimer Configurations ● CONFIG_KTIMER_HEARTBEAT ● ● CONFIG_KTIMER_MINTICKS ● ● HW cyclesper ktimer tick Minimum ktimer ticks unit for time event CONFIG_KTIMER_TICKLESS ● Enable tickless Place, 04/02/14 villerhsiao LOGO
  • 29.
    ktimer Event Utility ● LOGO ktimer_event_create() ● Arguments – – Timeouthandler – ● Timeout ticks Pointer to arguments for timeout handler Return Non-zero to reschedule event again – Freed automatically if not be re-scheduled Place, 04/02/14 villerhsiao
  • 30.
    ktimer Anotomy ● System time ● ● ktime_now ktimerevent list ● event_queue ● Managed by ktable Place, 04/02/14 villerhsiao LOGO
  • 31.
    ktimer Anotomy (Cont.) ● LOGO ktable ● Basickernel library in F9 Microkernel ● Fast objects pool management – To allocate/free objects of pre-defined size and numbers easiler ktable ktable_free() ktable_init() Used Unused Place, 04/02/14 ktable_alloc() villerhsiao
  • 32.
    ktimer Anotomy (Cont.) ● Lifecycle of time event ● One-shot ● ● LOGO Continuous How to control life cycle in ktimer? ● The return value of handler is treated as next timeout delta Place, 04/02/14 villerhsiao
  • 33.
    ktimer Anotomy (Cont.) ● HWTimer ISR ● __ktimer_handler() – Increase tick – Arrange Softirq for handling timer events Place, 04/02/14 villerhsiao LOGO
  • 34.
    ktimer Anotomy (Cont.) ● LOGO Two-stageinterrupt 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 Place, 04/02/14 villerhsiao
  • 35.
    ktimer Anotomy (Cont.) ● HWTimer ISR ● __ktimer_handler() – Increase tick – Arrange Softirq for handling timer events Place, 04/02/14 villerhsiao LOGO
  • 36.
    ktimer Anotomy (Cont.) ● HWtimer softirq handler ● ktimer_event_handler() – Find timeout event and executing its handler – Re-insert event if required Place, 04/02/14 villerhsiao LOGO
  • 37.
    ktimer Anotomy (Cont.) ● LOGO Tickless ● ● Entertickless 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 Place, 04/02/14 villerhsiao
  • 38.
    ktimer Anotomy (Cont.) ● LOGO TicklessCompensation ● SysTick frequency distortion when enter/exit standby mode tickless active idle handle other interrupt Timer interrupt idle active CPU Activity systick Place, 04/02/14 villerhsiao
  • 39.
    ktimer Anotomy (Cont.) ● LOGO TicklessCompensation tickless active idle handle other interrupt Timer interrupt idle active CPU Activity systick Place, 04/02/14 villerhsiao
  • 40.
    ktimer Anotomy (Cont.) ● LOGO TicklessCompensation ● Pre-defined value – CONFIG_KTIMER_TICKLESS_COMPENSATION ● – CONFIG_KTIMER_TICKLESS_INT_COMPENSATION ● ● Compensation value before entering idle Compensation value when CPU is waked up by non-timer interrupt. But how much to set these value to? Place, 04/02/14 villerhsiao
  • 41.
    ktimer Anotomy (Cont.) ● LOGO TicklessCompensation ● But how much to set these value to? – Get average compensation from general pupose timer ● (total diff / count of entering tickless) SYSTICK HW timer TIM2 Place, 04/02/14 villerhsiao
  • 42.
    ktimer Anotomy (Cont.) ● LOGO TicklessCompensation ● How to calibrate the compensation value? – CONFIG_KTIMER_TICKLESS_VERIFY – CONFIG_KDB Set compensation To 0 Place, 04/02/14 Get average Compensation from KDB villerhsiao Re-define Compensation value
  • 43.
    ktimer Anotomy (Cont.) ● LOGO KDB ● Kerneldebugger ## KDB ## commands: K: print kernel tables e: dump ktimer events n: show timer (now) s: show softirqs t: dump threads m: dump memory pools a: dump address spaces p: show sampling v: show tickless scheduling stat ---------------- Place, 04/02/14 ## KDB ## -------NOW-----Now is 70993 Ktimer T=1297 D=0 -------------------- villerhsiao
  • 44.
    Reference: LOGO [1] Jim Huang (Dec9, 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” Place, 04/02/14 villerhsiao
  • 45.