3.3. Advanced Windows Synchronization

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Tutorial 3 - Linux Interrupt Handling -
 A quantum is the amount of time a thread gets to run before Windows checks.  Length: Windows 2000 / XP: 2 clock intervals Windows Server systems: 12.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Budapesti Műszaki és Gazdaságtudományi Egyetem Méréstechnika és Információs Rendszerek Tanszék Scheduling in Windows Zoltan Micskei
General System Architecture and I/O.  I/O devices and the CPU can execute concurrently.  Each device controller is in charge of a particular device.
Chapter 8 Windows Outline Programming Windows 2000 System structure Processes and threads in Windows 2000 Memory management The Windows 2000 file.
Windows OS Internals - Copyright © 2005 David A. Solomon, Mark E. Russinovich, and Andreas Polze Unit OS4: Scheduling and Dispatch 4.4. Windows Thread.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS3: Concurrency 3.5. Lab Slides & Lab Manual.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS6: Device Management 6.1. Principles of I/O.
Windows NT and Real-Time? Reading: “Inside Microsoft Windows 2000”, (Solomon, Russinovich, Microsoft Programming Series) “Real-Time Systems and Microsoft.
Unit OS9: Real-Time and Embedded Systems
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
Windows 2000 System Mechanisms Computing Department, Lancaster University, UK.
CE Operating Systems Lecture 11 Windows – Object manager and process management.
NT Kernel CS Spring Overview Interrupts and Exceptions: Trap Handler Interrupt Request Levels and IRT DPC’s, and APC’s System Service Dispatching.
Unit OS11: Performance Evaluation Lab Manual.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 2 Computer-System Structures Slide 1 Chapter 2 Computer-System Structures.
System Components ● There are three main protected modules of the System  The Hardware Abstraction Layer ● A virtual machine to configure all devices.
Silberschatz, Galvin and Gagne  Applied Operating System Concepts Chapter 2: Computer-System Structures Computer System Architecture and Operation.
Processes, Threads, and Process States. Programs and Processes  Program: an executable file (before/after compilation)  Process: an instance of a program.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
1/9/ :46 1 Priority Model Real-time class Idle Above Normal Normal Below Normal Lowest Highest 31 Time-critical Dynamic classes.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS3: Concurrency 3.3. Advanced Windows Synchronization.
3.2. Windows Trap Dispatching, Interrupts, Synchronization
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Chapter 13: I/O Systems.
Module 12: I/O Systems I/O hardware Application I/O Interface
Chapter 2: Computer-System Structures(Hardware)
Chapter 2: Computer-System Structures
Processes and threads.
CS 6560: Operating Systems Design
Crash Dump Analysis - Santosh Kumar Singh.
CSCS 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
Day 08 Processes.
Day 09 Processes.
Unit OS9: Real-Time and Embedded Systems
Unit OS4: Scheduling and Dispatch
Unit OS2: Operating System Principles
Unit OSB: Comparing the Linux and Windows Kernels
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 2: Computer-System Structures Computer System Operation I/O Structure Storage.
Chapter 3: Windows7 Part 2.
CSCI 511 Operating Systems Ch3, 4 (Part B) Further Understanding Threads Dr. Frank Li Skipped sections 3.5 & 3.6 
Computer-System Architecture
Module 2: Computer-System Structures
Processor Fundamentals
Chapter 3: Windows7 Part 2.
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
TDC 311 Process Scheduling.
Process Description and Control
Lecture Topics: 11/1 General Operating System Concepts Processes
Process Description and Control
Unit OS5: Memory Management
Lecture 2 Part 2 Process Synchronization
CSE 451: Operating Systems Spring 2008 Module 15 I/O
CSE 451: Operating Systems Spring 2007 Module 15 I/O
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
Module 2: Computer-System Structures
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Chapter 2: Computer-System Structures
Chapter 2: Computer-System Structures
Module 2: Computer-System Structures
Module 2: Computer-System Structures
CS703 – Advanced Operating Systems
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

3.3. Advanced Windows Synchronization Unit OS3: Concurrency 3.3. Advanced Windows Synchronization Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze

Copyright Notice © 2000-2005 David A. Solomon and Mark Russinovich These materials are part of the Windows Operating System Internals Curriculum Development Kit, developed by David A. Solomon and Mark E. Russinovich with Andreas Polze Microsoft has licensed these materials from David Solomon Expert Seminars, Inc. for distribution to academic organizations solely for use in academic environments (and not for commercial use)

Roadmap for Section 3.3. Deferred and Asynchronous Procedure Calls IRQLs and CPU Time Accounting Wait Queues & Dispatcher Objects This Section covers Deferred and Asynchronous Procedure Calls (DPCs and APCs), Interrupt Request Levels (IRQLs) and their role in CPU time accounting, as well as wait queues and dispatcher objects. DPCs provide the operating system with the capability to generate an interrupt and execute a system function in kernel mode. The kernel uses DPCs to process timer expiration (and release threads waiting for the timers) and to reschedule the processor after a thread’s quantum expires. Device drivers use DPCs to complete I/O requests. To provide timely service for hardware interrupts, Windows—with the cooperation of device drivers—attempts to keep the IRQL below device IRQL levels. One way that this goal is achieved is for device driver ISRs to perform the minimal work necessary to acknowledge their device, save volatile interrupt state, and defer data transfer or other less time-critical interrupt processing activity for execution in a DPC at DPC/dispatch IRQL.

Deferred Procedure Calls (DPCs) Used to defer processing from higher (device) interrupt level to a lower (dispatch) level Also used for quantum end and timer expiration Driver (usually ISR) queues request One queue per CPU. DPCs are normally queued to the current processor, but can be targeted to other CPUs Executes specified procedure at dispatch IRQL (or “dispatch level”, also “DPC level”) when all higher-IRQL work (interrupts) completed Maximum times recommended: ISR: 10 usec, DPC: 25 usec See http://www.microsoft.com/whdc/driver/perform/mmdrv.mspx NOTES: Technically every CPU has its own DPC queue head and normally DPCs are queued to the CPU from which they’re requested. However the scheduler idle loop “peeks” at other CPUs’ DPC queues, so if one CPU is busy with high-IRQL code its DPCs may be processed by other CPUs. Also, KiSetTargetCpuDpc (or something like that) can be used to direct the DPC object to a particular CPU. Nor is the queue always FIFO; KiSetImportanceDpc allows a DPC object to be set to high priority, which means that it goes at the head of the queue instead of the tail. LAB: Perfmon: Interrupts/sec, %Interrupt time, %DPC time, other DPC counters queue head DPC object DPC object DPC object

Interrupt dispatch table Delivering a DPC 1. Timer expires, kernel queues DPC that will release all waiting threads Kernel requests SW int. DPC routines can‘t assume what process address space is currently mapped DPC Interrupt dispatch table high Power failure 2. DPC interrupt occurs when IRQL drops below dispatch/DPC level 3. After DPC interrupt, control transfers to thread dispatcher DPC DPC DPC Dispatch/DPC dispatcher DPC queue APC Low The kernel always raises the processor‘s IRQL to DPC/dispatch level or above when it need to synchronize access to shared kernel structures. This disables additional software interrupts and thread dispatching. When the kernel detects that dispatching should occur, it requests a DPC/dispatch level interrupt. But because the IRQL is at or above that level, the processor holds the interrupt in check. When the kernel completes its current activity, it sees that it‘s going to lower the IRQL below DPC/dispatch level and checks to see whether any dispatch interrupts are pending. If there are, the IRQL drops to DPC/dispatch level and the dispatch interrupts are processed. DPC routines can call kernel functions but can‘t call system services, generate page faults, or create or wait on objects 4. Dispatcher executes each DPC routine in DPC queue

Asynchronous Procedure Calls (APCs) Execute code in context of a particular user thread APC routines can acquire resources (objects), incur page faults, call system services APC queue is thread-specific User mode & kernel mode APCs Permission required for user mode APCs Executive uses APCs to complete work in thread space Wait for asynchronous I/O operation Emulate delivery of POSIX signals Make threads suspend/terminate itself (env. subsystems) APCs are delivered when thread is in alertable wait state WaitForMultipleObjectsEx(), SleepEx()

Asynchronous Procedure Calls (APCs) Special kernel APCs Run in kernel mode, at IRQL 1 Always deliverable unless thread is already at IRQL 1 or above Used for I/O completion reporting from “arbitrary thread context” Kernel-mode interface is linkable, but not documented “Ordinary” kernel APCs Always deliverable if at IRQL 0, unless explicitly disabled (disable with KeEnterCriticalRegion) User mode APCs Used for I/O completion callback routines (see ReadFileEx, WriteFileEx); also, QueueUserApc Only deliverable when thread is in “alertable wait” Thread Object K APC objects U

IRQLs and CPU Time Accounting Interval clock timer ISR keeps track of time Clock ISR time accounting: If IRQL<2, charge to thread’s user or kernel time If IRQL=2 and processing a DPC, charge to DPC time If IRQL=2 and not processing a DPC, charge to thread kernel time If IRQL>2, charge to interrupt time Since time servicing interrupts are NOT charged to interrupted thread, if system is busy but no process appears to be running, must be due to interrupt-related activity Note: time at IRQL 2 or more is charged to the current thread’s quantum (to be described) Note: if you’re at IRQL 2 the time appears as %DPC time, even though you might not be running a DPC. Similarly, if you’re at IRQL >2 the time appears as %interrupt time, even though you might not be servicing an interrupt. You might have just done a KeRaiseIrql.

Interrupt Time Accounting Task Manager includes interrupt and DPC time with the Idle process time Since interrupt activity is not charged to any thread or process, Process Explorer shows these as separate processes (not really processes) Context switches for these are really number of interrupts and DPCs

Time Accounting Quirks Looking at total CPU time for each process may not reveal where system has spent its time CPU time accounting is driven by programmable interrupt timer Normally 10 msec (15 msec on some MP Pentiums) Thread execution and context switches between clock intervals NOT accounted E.g., one or more threads run and enter a wait state before clock fires Thus threads may run but never get charged View context switch activity with Process Explorer Add Context Switch Delta column

Looking at Waiting Threads For waiting threads, user-mode utilities only display the wait reason Example: pstat To find out what a thread is waiting on, must use kernel debugger

Wait Internals 1: Dispatcher Objects Any kernel object you can wait for is a “dispatcher object” some exclusively for synchronization e.g. events, mutexes (“mutants”), semaphores, queues, timers others can be waited for as a side effect of their prime function e.g. processes, threads, file objects non-waitable kernel objects are called “control objects” All dispatcher objects have a common header All dispatcher objects are in one of two states “signaled” vs. “nonsignaled” when signalled, a wait on the object is satisfied different object types differ in terms of what changes their state wait and unwait implementation is common to all types of dispatcher objects Dispatcher Object Size Type State Wait listhead Object-type-specific data (see \ntddk\inc\ddk\ntddk.h)

Wait Internals 2: Wait Blocks Thread Objects Wait Internals 2: Wait Blocks WaitBlockList Key Type Next link List entry Object Thread Key Type Next link List entry Object Thread Key Type Next link List entry Object Thread WaitBlockList Represent a thread’s reference to something it’s waiting for (one per handle passed to WaitFor…) All wait blocks from a given wait call are chained to the waiting thread Type indicates wait for “any” or “all” Key denotes argument list position for WaitForMultipleObjects Dispatcher Objects Wait blocks Size Type State Wait listhead Object-type-specific data Size Type State Wait listhead Object-type-specific data

Further Reading Mark E. Russinovich and David A. Solomon, Microsoft Windows Internals, 4th Edition, Microsoft Press, 2004. Chapter 3 - System Mechanisms Kernel Event Tracing (from pp. 175) DPC Interrupts (from pp. 104)

Source Code References Windows Research Kernel sources \base\ntos\ke Dpcobj.c, dpcsup.c – Deferred Procedure Calls Apcobj.c, apcsup.c – Asynchronous Procedure Calls interobj.c - Interrupt Object wait.c, waitsup.c – Wait support \base\ntos\ke\i386 (similar files for amd64) Clockint.asm – Clock Interrupt Handler \base\ntos\inc\ke.h – structure/type definitions