《Understanding the Linux operating system.ppt》由会员分享,可在线阅读,更多相关《Understanding the Linux operating system.ppt(221页珍藏版)》请在三一办公上搜索。
1、Understanding the Linuxoperating system,2023/3/2,Linux Performance and Tuning,page:2,ProcessMemoryIONetwork,2023/3/2,Linux Performance and Tuning,page:3,Schematic interaction of different performance components,Linux process management,2023/3/2,Linux Performance and Tuning,page:5,timeslice:30ms10msc
2、ell,page frame,2023/3/2,Linux Performance and Tuning,page:6,What is a process?,A process is an instance of execution that runs on a processorFrom the kernels point of view,the purpose of a process is to act as an entity to which system resources(CPU time,memory,etc.)are allocatedThey include a set o
3、f resources such as open files and pending signals,internal kernel data,processor state,a memory address space with one or more memory mappings,one or more threads of execution,and a data section containing global variables,2023/3/2,Linux Performance and Tuning,page:7,Process Descriptor and the Task
4、 Structure,To manage processes,the kernel must have a clear picture of what each process is doing The kernel stores the list of processes in a circular doubly linked list called the task listAll processes running on Linux operating system are managed by the task_struct structure,which is also called
5、 a process descriptorThe task_struct is a relatively large data structure,at around 1.7 kilobytes on a 32-bit machine,2023/3/2,Linux Performance and Tuning,page:8,The process descriptor and task list,链表,2023/3/2,Linux Performance and Tuning,page:9,2023/3/2,Linux Performance and Tuning,page:10,The Li
6、nux process descriptor,2023/3/2,Linux Performance and Tuning,page:11,Process State,TASK_RUNNINGThe process is either executing on a CPU or waiting to be executedTASK_INTERRUPTIBLEThe process is suspended(sleeping)until some condition becomes true.Raising a hardware interrupt,releasing a system resou
7、rce the process is waiting for,or delivering a signal are examples of conditions that might wake up the process(put its state back to TASK_RUNNING)A typical example of a TASK_INTERRUPTIBLE process is a process waiting for keyboard interrupt,2023/3/2,Linux Performance and Tuning,page:12,TASK_UNINTERR
8、UPTIBLELike TASK_INTERRUPTIBLE,except that delivering a signal to the sleeping process leaves its state unchangedA typical example of a TASK_UNINTERRUPTIBLE process is a process waiting for disk I/O operationTASK_STOPPEDProcess execution has been stopped;the process enters this state after receiving
9、 a SIGSTOP,SIGTSTP,SIGTTIN,or SIGTTOU signal,2023/3/2,Linux Performance and Tuning,page:13,TASK_TRACEDProcess execution has been stopped by a debuggerWhen a process is being monitored by another(such as when a debugger executes a ptrace()system call to monitor a test program),each signal may put the
10、 process in the TASK_TRACED stateTASK_ZOMBIEProcess execution is terminated,but the parent process has not yet issued a wait4()or waitpid()system call to return information about the dead process,2023/3/2,Linux Performance and Tuning,page:14,Process State,2023/3/2,Linux Performance and Tuning,page:1
11、5,Life cycle of a process,Every process has its own life cycle such as creation,execution,termination,and removal,2023/3/2,Linux Performance and Tuning,page:16,When a process creates a new process,the creating process(parent process)issues a fork()system callWhen a fork()system call is issued,it get
12、s a process descriptor for the newly created process(child process)and sets a new process idIt copies the values of the parent process process descriptor to the childschilds.At this time the entire address space of the parent process is not copied;both processes share the same address space,2023/3/2
13、,Linux Performance and Tuning,page:17,The exec()system call copies the new program to the address space of the child processBecause both processes share the same address space,writing new program data causes a page fault exceptionAt this point,the kernel assigns the new physical page to the child pr
14、ocessThis deferred operation is called the Copy On Write,2023/3/2,Linux Performance and Tuning,page:18,COW:Copy On Writing写时复制,fork,slab,2023/3/2,Linux Performance and Tuning,page:19,When program execution has completed,the child process terminates with an exit()system callThe exit()system call rele
15、ases most of the data structure of the process and notifies the parent process of the termination sending a signalAt this time,the process is called a zombie process,2023/3/2,Linux Performance and Tuning,page:20,The child process will not be completely removed until the parent process knows of the t
16、ermination of its child process by the wait()system callAs soon as the parent process is notified of the child process termination,it removes all the data structure of the child process and release the process descriptor,2023/3/2,Linux Performance and Tuning,page:21,Thread,A thread is an execution u
17、nit generated in a single processIt runs parallel with other threads in the same processThey can share the same resources such as memory,address space,open files,and so onThey can access the same set of application dataA thread is also called Light Weight Process(LWP)Because they share resources,eac
18、h thread should not change their shared resources at the same timeThe implementation of mutual exclusion,locking,serialization,and so on,are the user applications responsibility,2023/3/2,Linux Performance and Tuning,page:22,2023/3/2,Linux Performance and Tuning,page:23,From the performance perspecti
19、ve,thread creation is less expensive than process creation because a thread does not need to copy resources on creation,2023/3/2,Linux Performance and Tuning,page:24,In current Linux implementations,a thread is supported with the Portable Operating System Interface for UNIX(POSIX)compliant library(p
20、thread)Several thread implementations are available in the Linux operating system:Linux ThreadsNative POSIX Thread Library(NPTL)Next Generation POSIX Thread(NGPT)Using the LD_ASSUME_KERNEL environment variable,you can choose which threads library the application should use,2023/3/2,Linux Performance
21、 and Tuning,page:25,缓存抖动,a,2023/3/2,Linux Performance and Tuning,page:26,Context switching,To control the execution of processes,the kernel must be able to suspend the execution of the process running on the CPU and resume the execution of some other process previously suspendedThis activity goes va
22、riously by the names process switch,task switch,or context switch While each process can have its own address space,all processes have to share the CPU registersSo before resuming the execution of a process,the kernel must ensure that each such register is loaded with the value it had when the proce
23、ss was suspended The process descriptor and the area called kernel mode stack are used to store the context,2023/3/2,Linux Performance and Tuning,page:27,Context switching,2023/3/2,Linux Performance and Tuning,page:28,Process memory segments,A process uses its own memory area to perform workThe work
24、 varies depending on the situation and process usageA process can have different workload characteristics and different data size requirementsThe process has to handle a of variety of data sizesTo satisfy this requirement,the Linux kernel uses a dynamic memory allocation mechanism for each process,2
25、023/3/2,Linux Performance and Tuning,page:29,32bit,4G,2023/3/2,Linux Performance and Tuning,page:30,Process address space,2023/3/2,Linux Performance and Tuning,page:31,Classes of processes,Interactive processesThese interact constantly with their users,and therefore spend a lot of time waiting for k
26、eypresses and mouse operations Batch processesThese do not need user interaction,and hence they often run in the background Real-time processesThese have very strong scheduling requirements Such processes should never be blocked by lower-priority processes,they should have a short response time and,
27、most important,such response time should have a minimum variance Typical real-time programs are video and sound applications,robot controllers,and programs that collect data from physical sensors,2023/3/2,Linux Performance and Tuning,page:32,Process Scheduling,MultitaskingMultitasking operating syst
28、ems come in two flavors:cooperative multitasking and preemptive multitaskingLinux,like all Unix variants and most modern operating systems,implements preemptive multitaskingIn preemptive multitasking,the scheduler,one of the kernel subsystem,decides when a process is to cease running and a new proce
29、ss is to begin runningThe act of involuntarily suspending a running process is called preemption,2023/3/2,Linux Performance and Tuning,page:33,O(1)O(log n)O(n)O(n2)O(2n),2023/3/2,Linux Performance and Tuning,page:34,Linuxs Process Scheduler,O(1)schedulerIt scales well with the number of runnable pro
30、cesses,because it selects the process to run in constant time,independently of the number of runnable processes It also scales well with the number of processors because each CPU has its own queue of runnable processes The new algorithm does a better job of distinguishing interactive processes and b
31、atch processes But,O(1)scheduler had several pathological failures related to scheduling latency-sensitive applicationsThus,although the O(1)scheduler was ideal for large server workloadswhich lack interactive processesit performed below par on desktop systems,where interactive applications are the
32、raison dtre,2023/3/2,Linux Performance and Tuning,page:35,Linux kernel 2.6 O(1)scheduler,2023/3/2,Linux Performance and Tuning,page:36,Linux scheduling is based on the time sharing techniqueSeveral processes run in time multiplexing because the CPU time is divided into slices,one for each runnable p
33、rocessTime sharing relies on timer interrupts and is thus transparent to processes The scheduling policy is also based on ranking processes according to their priority,2023/3/2,Linux Performance and Tuning,page:37,Process Preemption,If a process enters the TASK_RUNNING state,the kernel checks whethe
34、r its dynamic priority is greater than the priority of the currently running processIf it is,the execution ofcurrentis interrupted and the scheduler is invoked to select another process to run a process may also be preempted when its time quantum expires when this occurs,theneed_reschedfield of the
35、current process is set,so the scheduler is invoked when the timer interrupt handler terminates,2023/3/2,Linux Performance and Tuning,page:38,Preempting the current process,Standard preemption rulesCPU receives a hard interruptProcess requests IOProcess voluntarily surrenders the CPU via sched_yieldS
36、cheduler algorithm determines that process should be preemptedViewing scheduler policy and prioritychrt-p pidps axo pid,comm,rtprio,policytopThe init process starts with SCHED_OHTEREach process inherits parents scheduling policy and priority at creatation time,2023/3/2,Linux Performance and Tuning,p
37、age:39,Scheduling Policy,Policy is the behavior of the scheduler that determines what runs whenA schedulers policy often determines the overall feel of a system and is responsible for optimally utilizing processor timeThe scheduling algorithm of traditional Unix operating systems must fulfill severa
38、l conflicting objectives:fast process response timegood throughput for background jobsavoidance of process starvationreconciliation of the needs of lowhigh-priority processesand so onThe set of rules used to determine when and how to select a new process to run is called scheduling policy,2023/3/2,L
39、inux Performance and Tuning,page:40,Characterizing processes,When speaking about scheduling,processes are traditionally classified as I/O-bound or CPU-boundThe former is characterized as a process that spends much of its time submitting and waiting on I/O requestsConversely,processor-bound processes
40、 spend much of their time executing codeA scheduler policy for processor-bound processes tends to run such processes less frequently but for longer durationsLinux,aiming to provide good interactive response and desktop performance,optimizes for process response(low latency),thus favoring I/O-bound p
41、rocesses over processor-bound processors,2023/3/2,Linux Performance and Tuning,page:41,Process Priority,In Linux,process priority is dynamicThe scheduler keeps track of what processes are doing and adjusts their priorities periodicallyProcesses that have been denied the use of a CPU for a long time
42、interval are boosted by dynamically increasing their priorityProcesses running for a long time are penalized by decreasing their priority,2023/3/2,Linux Performance and Tuning,page:42,Static priority,Every conventional process has its own static priority,which is a value used by the scheduler to rat
43、e the process with respect to the other conventional processes in the systemThe kernel represents the static priority of a conventional process with a number ranging from 100(highest priority)to 139(lowest priority)New process always inherits the static priority of its parentHowever,a user can chang
44、e the static priority of the processes that he owns by passing some nice values,2023/3/2,Linux Performance and Tuning,page:43,real-time priority,0-99higher real-time priority values correspond to a greater priorityAll real-time processes are at a higher priority than normal processes;that is,the rea
45、l-time priority and nice value are in disjoint value spacesps-eo state,uid,pid,ppid,rtprio,time,comm,2023/3/2,Linux Performance and Tuning,page:44,Timeslice,The timeslice is the numeric value that represents how long a task can run until it is preemptedThe scheduler policy must dictate a default tim
46、eslice,which is not a trivial exercise,2023/3/2,Linux Performance and Tuning,page:45,The Scheduling Algorithm,Scheduler ClassesThe Linux scheduler is modular,enabling different algorithms to schedule different types of processesThis modularity is called scheduler classesScheduler classes enable diff
47、erent,pluggable algorithms to coexist,scheduling their own types of processesEach scheduler class has a priority.The highest priority scheduler class that has a runnable process wins,selecting who runs next,2023/3/2,Linux Performance and Tuning,page:46,Scheduling classes,Every Linux process is alway
48、s scheduled according to one of the following scheduling classes:SCHED_FIFO 1-99A First-In,First-Out real-time process SCHED_RR A Round Robin real-time process SCHED_NORMAL(100-139)A conventional,time-shared processAlso named by SCHED_OTHERFor normal processes,2023/3/2,Linux Performance and Tuning,p
49、age:47,SCHED_OTHER,Priority may varyProcesses with equal priority can preempt current process every 20ms to prevent CPU starvationCPU bound processes receive a+5 priority penalty after preemptionInteractive tasks spend time waiting for IOScheduler tracks time spent waiting for IO for each process an
50、d calculates a sleep averageHigh sleep average indicates interactive processInteractive processes may be re-inserted into the active queueIf not,receive a-5 priority boost and move to expired queue,2023/3/2,Linux Performance and Tuning,page:48,Tuning scheduler policy,SCHED_FIFOchrt-f 1-99/path/to/pr