Week 5.a CS6640 02/03 2023 https://naizhengtan.github.io/26spring/ 1. Interrupt handler in egos-2k+ 2. Review: OS scheduling 3. Processes in egos-2k+ 4. Kernel scheduler --- Admin: - Lab2 done - Lab3 released 1. Interrupt handler in egos-2k+ [show slides] * Timer interrupt from last time (1) register handlers (2) enable timer interrupt (3) set timer We will see where egos-2k+ does these. CPU provides a set of registers: 'mtvec', 'mstatus', 'mie', 'mtime', 'mtimecmp' EGOS: Timer handling [see earth/cpu_intr.c and earth/cpu_timer.c] * Timer Interrupt is one way to trap into kernel. Q: how many ways to trap to kernel? OS basics: there are three ways to trap into kernel (1) interrupt (2) exception (3) syscall Revisit RISC-V exception handling [see handout, panel 1] Q: with gdb and these exception CSRs, how to tell where the egos-2k+'s handler is? Exploration: where is the interrupt handler in egos-2k+? * in gdb, by "p/x $mtvec"; we will see mtvec = 0x80000020 * by checking the memory layout, we found it is in egos's code * by checking build/debug/earth.lst, we see the handler named "trap_entry" in file grass/kernel.s * CSRs: briefly introduce CSRs [handout panel 1] 2. Review: OS scheduling High-level problem: operating system has to decide which process (or thread) to run. A. When scheduling decisions happen: [show process transition diagram] exit (iv) [PROC_LOADING] +-------->[PROC_UNUSED] | (iii) interrupt | +->[PROC_READY/PROC_RUNNABLE] <------ [PROC_RUNNING] ^ ------> | syscall msg \ proc_yeild() | or wake up (ii) \ | \ / [PROC_PENDING_SYSCALL/ <-/ syscall or sleep (i) PROC_SLEEPING] scheduling decisions take place when a process: (i) Switches from running to waiting state (ii) Switches from waiting to ready (iii) Switches from running to ready state (iv) Exits preemptive scheduling: at all four points nonpreemptive scheduling: at points (i), (iv), only (this is the definition of nonpreemptive scheduling) B. what are the metrics and criteria? timeline ----v---------v-------v------v-----v----> arrival 1st exec yield 2nd completion --turnaround time time for each process to complete (completion - arrival) --waiting/response/output time. variations on this definition, but they all capture the time spent waiting for something to happen rather than the time from launch to exit. --time between when job enters system and starts executing; following the class's textbook, we will call this "response time" (1st execution - arrival) [some texts call this "waiting time"] --time from request to first response (e.g., key press to character echo) we will call this "output time" [some texts call this "response time"] --CPU time: how much CPU time the process actually consumed (this is the time when pc points to the code of this process) 3. Processes in egos-2k+ A. Process control block in egos-2k+ [see handout] B. Process life cycle in egos-2k+ [see handout] C. Calculating scheduling metrics Q: how to calculate turnaround time? Q: how to calculate response time? Q: how to calculate CPU time? 4. Kernel scheduler A. Now [read grass/scheduler.c] Try "loop &" and "ls", which is super slow. Q: why this is the case? Because the loop which runs for a long time has the same priority as system services and short-lived jobs, so it takes at least one QUANTUM to hear back from system service. B. Multi-level feedback queue [a brief intro; see OSTEP Ch8]