Real-time Java Threads Insik Shin Real-time Systems Group University of Pennsylvania
KVM Threading System What kind of information for a thread? data structures How to maintain such information? How to support multi-threading? scheduling policy context switching mechanism Additional features for supporting threads? timer, monitor
Thread Information pointer to ‘run’ method priority virtual machine execution stack virtual machine registers (ip, fp, sp) timer & monitor state (active | wait) thread queues: active, wait, timer, monitor
Thread Information in KVM pointer to ‘run’ method priority virtual machine execution stack virtual machine registers (ip, fp, sp) timer & monitor state (active | wait) thread queues: active, wait, timer, monitor Java-level thread information implementation-level thread information
Data Structures in KVM Two separate structures THREAD internal (VM-level) structure information for implementing threading system JAVATHREAD information defined in Java class ‘Thread.java’ Advantages JAVATHREAD needs to be subclassed Implementation independent of the Java language for portability & extension reasons
JAVATHREAD in KVM JAVATHREAD COMMON_OBJECT_INFO long priority current priority THREAD VMthread pointer to internal THREAD INSTANCE target pointer to the object whose ‘run’
THREAD in KVM THREAD THREAD nextAliveThread active thread queue THREAD nextThread all thread queue JAVATHREAD javathread java-level thread informatin STACK stack execution stack BYTE* ipStore program counter FRAME fpStore frame pointer Cell* spStore execution stack pointer MONITOR monitor thread monitor queue THREAD nextAlarmThread thread timer queue Long wakeupTime[2] wakeup time Void (*wakeupCall)(THREAD) callback when timer expires state thread state
THREAD & JAVATHREAD in KVM THREAD & JAVATHREAD JAVATHREAD javathreadTHREAD VMthread
Thread Queues in KVM nextThread nextAliveThread monitor nextAlarmThread
Thread Queues in KVM nextThread nextAliveThread monitor nextAlarmThread CurrentThread RunnableThreads
Thread Context Switching in KVM IP, FP, SP VM Registers CurrentThread 1.store execution environment
Thread Context Switching in KVM IP, FP, SP VM Registers 1.store execution environment CurrentThread 2. Pick a thread from ‘RunnableThreads’ queue
Thread Context Switching in KVM IP, FP, SP VM Registers 1.store execution environment CurrentThread 2. Pick a thread from ‘RunnableThreads’ queue 3. Put the old thread into ‘RunnableThreads’ queue
Thread Context Switching in KVM IP, FP, SP VM Registers 1.store execution environment CurrentThread 2. Pick a thread from ‘RunnableThreads’ queue 4. load execution environment 3. Put the old thread into ‘RunnableThreads’ queue
Thread Scheduling in KVM Scheduling policy Round-robin with the following quantum: quantum = 500 * priority Scheduler data structure THREAD CurrentThread THREAD RunnableThreads
Thread operations in KVM Constructors & deconstructors BuildThread, DismantleThread Thread activation & deactivation initThreadBehavior, startThread, stopThread, suspendThread, resumeThread, removeFirstRunnableThread Multitasking operations switchThread storeExecutionEnvironment, loadExecutionEnvironment Timer, Queue, Monitor operations
Java API & KVM Thread.java public native void start(); KVM : nativeCore.c Java_java_lang_Thread_start() { … startThread(); … } KVM : thread.c startThread() { … }
Real-time Thread Scheduling What kind of features to be added? Real-time thread information Real-time (priority) scheduler Synchronization (monitor) Timer, Asynchrony How to extend KVM for real-time threads?
Real-time Thread Information Scheduling parameters priority, importance Release parameters cost, deadline, overrunHandler, missHandler start, period, minInterarrival Memory parameters maxMemoryArea, maxImmortal Processing group parameters Scheduler
Data Structures in KVM Two separate structures THREAD internal (VM-level) structure information for implementing threading system JAVATHREAD information defined in Java class ‘Thread.java’ Advantages JAVATHREAD needs to be subclassed Implementation independent of the Java language for portability & extension reasons
Real-time Thread Structure Two separate structures THREAD internal (VM-level) structure information for implementing threading system JAVATHREAD information defined in Java class ‘Thread.java’ RT_JAVATHREAD information defined in Java class ‘RealtimeThread.java’
Real-time Thread Structure RT_JAVATHREAD extends JAVATHREAD JAVATHREAD SchedulingParameter sp ReleaseParameter rp MemoryParameter mp ProcessingGroupParameter pgp Scheduler sch COMMON_OBJECT_INFO long priority THREAD VMthread INSTANCE target
THREAD in KVM THREAD nextAliveThread active thread queue THREAD nextThread all thread queue JAVATHREAD javathread java-level thread informatin STACK stack execution stack BYTE* ipStore program counter FRAME fpStore frame pointer Cell* spStore execution stack pointer MONITOR monitor thread monitor queue THREAD nextAlarmThread thread timer queue Long wakeupTime[2] wakeup time Void (*wakeupCall)(THREAD) callback when timer expires state thread state
THREAD in KVM THREAD nextAliveThread active thread queue THREAD nextThread all thread queue JAVATHREAD javathread java-level thread informatin STACK stack execution stack BYTE* ipStore program counter FRAME fpStore frame pointer Cell* spStore execution stack pointer MONITOR monitor thread monitor queue THREAD nextAlarmThread thread timer queue Long wakeupTime[2] wakeup time Void (*wakeupCall)(THREAD) callback when timer expires state thread state bool_t realtime
Thread Structures for Real-time Java realtime = falserealtime = true non-realtime thread realtime thread
Real-time Thread Supports We can use the same mechanisms as in KVM for the followings: thread queue managements context switching
Real-time Thread Scheduler RTSJ suggests PriorityScheduler for RTJ Each RT thread can be associated with any scheduler multiple schedulers at the same time Two-level scheduling
Two-level Scheduling Meta-level scheduler Priority scheduler Round-robin scheduler EDF scheduler
Thread Scheduling in KVM Scheduling policy Round-robin with the following quantum: quantum = 500 * priority Scheduler data structure THREAD CurrentThread THREAD RunnableThreads
Real-time Thread Scheduling Scheduling policy Meta-level scheduling policy (priority) Scheduler data structure SCHEDULER THREAD CurrentThread THREAD RunnableThreads
Java API & KVM Thread.java public native void start(); KVM : nativeCore.c Java_java_lang_Thread_start() { … startThread(); … } KVM : thread.c startThread() { … }
Real-time Java API & KVM RealtimeThread.java public native void start(); KVM : nativeCore.c Java_javax_rtgedition_RealtimeThread_start() { … startThread(); … } KVM : thread.c startThread() { … }