Dan Noé University Of New Hampshire / VeloBit - MIT ESP

Transcription

Dan NoéUniversity of New Hampshire / VeloBit

A review of how the CPU worksThe operating system “kernel” and when it runs User and kernel mode Device drivers Virtualization of memory Virtual memory Paging Virtualization of CPU Threads, processes, programs, tasks and Cores Virtualization of Disk Filesystems

Memory (RAM) contains instructions coded inbinary and space for data storageThe CPU has multiple “registers” fortemporary storeThe “instruction pointer” register points tothe next instruction to be executedInterrupts When a piece of hardware needs something itcauses an interrupt which makes execution jump toa predetermined location

In order to continue execution after aninterrupt the machine context is saved: Save the contents of all registers (including specialregisters) into memory: the “Machine Context” Update the program counter so the next instructionis a predetermined location (“Interrupt Handler”) If execution is interrupted and the context issaved execution can be resumed later withouteven knowing!When an interrupt occurs the interrupthandler runs in “kernel mode”

Application code runsin this modeLimited privileges Cannot execute allinstructions, memoryaccess limited Cannot transition intokernel mode withoutan interrupt This interrupt is called a“context switch”User Mode System starts in thismodeInterrupt handlers runin this modeUnlimited privileges Access all memory andhardware devices Execute any CPUinstruction Can transition intouser mode with aninstructionKernel Mode

Term for the collection of code which formsthe core of the Operating SystemKernel code runs first at system boot timeAfter the first user mode code runs, thekernel runs only as a result of interrupts“Operating System” also includes user modecomponents User Interface components: Shells, explorer System services

Kernel code that interacts with hardware devicesSends commands to device Network driver: Send data to remote computer Disk driver: Begin write or read or read operation Video driver: Display something on screen Handles interrupts by that device Network driver: Interrupt occurs when incoming dataarrives Disk driver: Interrupt occurs when operation is complete Human interface device: Keyboard press causes interrupt

But how does a user application requestsomething from the kernel?It causes an interrupt!Old way: Software interrupt instruction A software interrupt (also called a “trap”) istriggered by a special instruction Newer way: Special “sysenter” or “syscall”instruction Allows some shortcuts by the CPU and kernel forbetter performance

A key concept: Each userapplication runs on the CPU asthough it has full access to thesystem through virtualization ofresources

Each program acts as though it has universalaccess to the memory Without this life would be difficult! Virtual Memory is the solutionAddress spaces Physical addresses – size is the amount of physicalRAM Virtual addresses – size depends on the size of apointer (32 or 64 bits) 32-bit system: 4GB of virtual address space 64-bit system: 16 Exabytes!

Physical address space is divided into 4 kilobytePage FramesVirtual address space is divided into 4 kilobytePages 32 bit virtual address 0xdeadbeef Page number Offset within page A page table is maintained in memory for eachprogram. It maps virtual Page Frame - PhysicalPageFor each memory access the CPU looks up thevirtual address and translates it into a physicaladdress

The Page Fault (a trap/interrupt) A virtual memory address is accessed which doesnot have a valid or resident physical page Demand Paging and nonresident pagesFile backed pagesShared pages Copy on Write Making it fast: Caching and the TranslationLookaside Buffer (TLB)

If a page has been resident in memory andmodified then it is marked “dirty”If a dirty page is backed by a disk file then itmust be written out (flushed) eventuallyIf there are no free page frames the kernelcan old flush pages to disk and evict themmaking them nonresident.Insufficient memory prompts “thrashing” andextremely poor performance

Tasks: Threads, Processes, Programs,ApplicationsCores: Multiprocessing, Multiple Cores,MultithreadingCPU Scheduling maps Tasks to Cores Much like virtual memory maps virtual addresses tophysical! The Scheduler decides what task will run nextafter servicing an interrupt or system call

Time to execute one CPU instruction: 0.4nanosecondsTime to retrieve data from main memory: 18nanosecondsTime to retrieve data from disk: 8 milliseconds (8,000,000 nanoseconds)Time to retrieve data from California server:70 ms (70,000,000 nanoseconds)A CPU which executes one instruction persecond would take about 231 days to seekthe hard drive!

Most tasks spend most of their time waitingfor IOThe scheduler will allow CPU bound (or otherIO bound tasks) to run while waitingA typical program might only run hundreds ofinstructions before blocking on more IOIf no tasks are ready to run the CPU willinstruct the processor to save powerA timer interrupt fires regularly to interruptany CPU bound tasks

User applications cannot write directly to thedisk hardwareInstead, the OS organizes files using afilesystem and allows programs to access thedisk through system callsUser programs use a standard interface toname, read, and write “files”. The filesystemtranslates this into actual disk blockoperations.

The difference between main memory anddisk access latency means that the OS mustcache disk data aggressivelyLeverage virtual memory with a Page cache: Each file opened by a user program is loaded bymapping file backed pages Pages from the file are loaded on demand when apage fault occurs Reads and writes are memory operations. Thepages will be marked dirty if modified

Hypervisors, mobile devices,etc.

VMWare, Parallels, VirtualBox, Xen, KVM, andHyper-V are examples of “hypervisors” whichare Operating System kernels which can runother operating systemsSame concepts apply!Interrupts are initially serviced by theHypervisor then control is passed to guestoperating system kernel

Desire for low power consumption means theCPU should be powered off as much aspossibleMemory pressure means that while there maybe “multitasking” applications can be killed atany timeCooperative multitasking/state saving helps Callbacks to save state Save to persistent storage immediately Different paradigms for file storage

An Operating System is composed of the“Kernel” and “user space” utilitiesThe “Kernel” is the core code which providesvirtualization of resources. It has several keycomponents: Virtual memory managerCPU schedulerFilesystems / block layerNetworking

WikipediaLearn about Linux Kernel: LWN.net,kernelnewbies.orgInstall Linux (free) in VirtualBox (free – runson top of Windows) Make modifications to the kernel and the worst youdo is crash your virtual machine! Explore the Linux kernel source:http://lxr.linux.no

The scheduler will allow CPU bound (or other IO bound tasks) to run while waiting A typical program might only run hundreds of instructions before blocking on more IO If no tasks are ready to run the CPU will instruct the processor to save power A timer i