8 virtual memory - university of california, riversidehtseng/classes/cs202_2020wi/...lottery...

44
Process/Thread Scheduling (II) & Virtual memory (I) Hung-Wei Tseng

Upload: others

Post on 05-Apr-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Process/Thread Scheduling (II) & Virtual memory (I)

Hung-Wei Tseng

Page 2: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Recap: What happens when creating a process

!2

Virtual memory

heap

stack

Dynamic allocated data: malloc()

Local variables, arguments

code

static data

program

code

static data

Linux contains a .bss section for uninitialized global variables

Page 3: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

The Machine

Previously, we talked about virtualization

!3

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtually, every process seems to have a processor/memory space, but

only a few of them are physically executing/using the installed DRAM.

Page 4: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• The mechanism • Non-preemptive/cooperative: the run process itself initiate context switches — by

using system calls • Preemptive: the OS kernel can actively incur context switches — by using hardware

(timer) interrupts • The policy

• Non-preemptive • First Come First Serve • Shortest job first: SJF

• Preemptive • Round robin • Shortest Time-to-completion • Multi-level scheduling algorithm

!4

Recap: Virtualizing the processor

Page 5: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Place new process in the one of the queue • Depending on the program size

• Schedule processes in one of N queues • Start in initially assigned queue n • Run for 2n quanta (where n is current depth) • If not complete, move to a higher queue (e.g. n +1)

• Level m is run only when levels 0 to m-1 are empty!5

Recap: The Multilevel Scheduling Algorithm

wp is the program memory size — smaller ones are assigned to lower numbered queues

• Smaller tasks are given higher priority in the beginningWhy?

• Larger process will execute longer before switch

• Smaller process, newer process are given higher priority

Page 6: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Lottery scheduling • Scheduler activation • Why virtualize your memory • Start with the basic proposal — segmentation • Demand paging

!6

Outline

Page 7: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Lottery Scheduling: Flexible Proportional-Share Resource Management

Carl A. Waldspurger and William E. Weihl

!7

Page 8: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Why Lottery

!8

We want Quality of Service

Most approaches are not flexible, responsive

The overhead of running those algorithms are high!

No body knows how they work…

Page 9: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Solution — Lottery and Tickets

!9

Page 10: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Each process hold a certain number of lottery tickets • Randomize to generate a lottery • If a process wants to have higher priority

• Obtain more tickets!

!14

What lottery proposed?

Page 11: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• How many of the following can the ticket abstraction in the lottery paper promote? က: Proportional fairness က< Machine-independent implementation of the scheduling policy က> Generic scheduling policy across different devices က@ Starvation free A. 0 B. 1 C. 2 D. 3 E. 4

!15

What do ticket abstraction promote?

The ticket abstraction can be independent of machine speeds or details

Tickets represent the share of a process should receive from a resource

You may use tickets on everything you would like to share Eventually every process with a ticket gets to runIt’s also state-free — reduce the overhead

Page 12: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Ticket transfers • Ticket inflation • Ticket currencies • Compensation tickets

!16

Ticket economics

Page 13: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• The overhead is not too bad • 1000 instructions ~ less than 500 ns on a 2

GHz processor • Fairness

• Figure 5: average ratio in proportion to the ticket allocation

• Flexibility • Allows Monte-Carlo

algorithm to dynamically inflate its tickets

• Ticket transfer • Client-server setup

!17

How good is lottery?

Page 14: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Data center scheduling • You buy “times” • Lottery scheduling of your virtual machine

!18

The impact of “lottery”

Page 15: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Will it be good for • Event-driven application • Real-time application • GUI-based system

• Is randomization a good idea? • The authors later developed a deterministic stride-scheduling

!19

Will you use lottery for your system?

Page 16: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Scheduler Activations: Effective Kernel Support for the User-level Management of Parallelism

Thomas E. Anderson, Brian N. Bershad, Edward D. Lazowska and Henry M. LevyUniversity of Washington

!20

Page 17: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

User-level v.s kernel threads

!25

user-level threads

Kernel

privilege boundary

user-level

kernelmode

Process

runtimelibrary

thread list

process list

The process is a virtual processor

kernel threads

Process

Kernel

process list

thread list

threadthread

• The OS kernel is unaware of user-level threads • Switching threads does not require kernel mode operations • A thread can block other threads within the same process

• The kernel can control threads directly • Thread switch requires kernel/user mode switch and system calls • Thread works individually

Page 18: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Comparing user-level threads and kernel threads, please identify how many of the following statements are correct. က: The overhead of switching threads is smaller for user-level threads က< The OS scheduler can directly control the scheduling of kernel thread, but not for

user-level threads က> A user-level thread can potentially block all other threads in the same process က@ Implementing the user-level thread library can be achieved without modifying the

OS kernel A. 0 B. 1 C. 2 D. 3 E. 4

!26

User-level v.s kernel threads

— kernel threads requires kernel switch!!!

— user threads are not visible from kernel!!!

— because the OS scheduler treat all threads as the same scheduling identity, if one is doing I/O, whole process maybe switched out — how do you implement “locks” in user-level threads and kernel-level threads?

Page 19: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• User-level threads • Efficient, flexible, safer, customizable

• Kernel threads • Slower, more powerful • Better matches the multiprocessor hardware

• Problems • OS is only aware of kernel threads • OS is unaware of user-level threads as they are hidden behind each

process

!27

Why — the “dilemma” of thread implementations

Page 20: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• The OS kernel provides each user-level thread system with its own virtual multiprocessor

• Communication mechanism between kernel and user-level

!28

What does “Scheduler Activations” propose?

Page 21: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• The kernel allocates processors to an address space/process • An address space is shared by all threads within the same process • The kernel controls the number of processors to an address space

• Each process has complete control over the processor-thread allocation

• The kernel notifies the address space when the allocated number of processors changes

• The process notifies the kernel when it needs more or fewer processors

• Transparent to users/programmers!29

The virtual multiprocessor abstraction

Page 22: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Create a scheduler activation when the system create a process on a processor • Create a scheduler activation when the kernel needs to perform an “upcall”

user-level • Add a processor • Processor has been preempted • Scheduler activation has blocked • Scheduler activation has unblocked

• Downcalls — hints for kernel to perform resource management • Add more processors • This processor is idle

• Key difference from a kernel thread • Kernel never restarts user thread after it is blocked

!30

How scheduler activation works?

Page 23: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Once been implemented in NetBSD, FreeBSD, Linux • A user-level thread gets preempted whenever there is

scheduling-related event • Overhead • You may preempt a performance critical thread

• Blocking system call

!31

Will you use Scheduler activation?

Page 24: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Linux treat all schedule identities as “tasks” — context of executions

• COEs can share parts of their contexts with each • Processes share nothing • Threads share everything but the CPU states

• http://www.evanjones.ca/software/threading-linus-msg.html

!32

Linux’s thread implementation

Page 25: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Let’s talk about virtual memory

!33

Page 26: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

The Machine

Previously, we talked about virtualization

!34

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Virtual memory

heap

stack

code

static data

Previously, we’ve talked about sharing the processor.How about sharing DRAM?

Page 27: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Why Virtual Memory?

!35

Page 28: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

!36

If we expose memory directly to the processor (I)

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Memory

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 0000000800c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Data

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008 00c2e800 00000008 00c2f000 00000008

00c2e800 00000008 00c2f000 00000008

00c2f800 00000008 00c30000 00000008

? What if my program needs more memory?

Page 29: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

!37

If we expose memory directly to the processor (II)

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Memory

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Memory

?

What if my program runs on a machine

with a different memory size?

Page 30: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

!38

If we expose memory directly to the processor (III)

Memory

0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3

00c2e800 00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

?

What if both programs need to use memory?

Page 31: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

The Virtual Memory Abstraction

!39

Page 32: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Virtual memory

!40

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Program0f00bb27509cbd23 00005d24 0000bd24 2ca422a0 130020e4 00003d24 2ca4e2b3Ins

tructi

ons 00c2e800

00000008 00c2f000 00000008 00c2f800 00000008 00c30000 00000008

Data

Virtual Memory SpaceVirtual Memory Space

Memory

00c2e800 00000008 00c2f000 00000008

instruction0x0

0f00bb27509cbd23 00005d24 0000bd24

data0x80000000 instruction

0x0

0f00bb27509cbd23 00005d24 0000bd24

00c2f800 00000008 00c30000 00000008

data0x80008000data

0x80008000

00c2f800 00000008 00c30000 00000008

Page 33: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Virtual memory

!41Physical memory

Virtual memory

CPU

address mapping

0x000000000000

0xFFFFFFFFFFFF

Code Static Data

Data

Heap

Stack

Process A0x000000000000

0xFFFFFFFFFFFF

CodeStatic Data

Data

Heap

Stack

Process B

Page 34: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• An abstraction of memory space available for programs/software/programmer

• Programs execute using virtual memory address • The operating system and hardware work together to handle

the mapping between virtual memory addresses and real/physical memory addresses

• Virtual memory organizes memory locations into “pages”

!42

Virtual memory

Page 35: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Demo revisited: Virtualization

!43

double a;

int main(int argc, char *argv[]){ int cpu, status, i; int *address_from_malloc; cpu_set_t my_set; // Define your cpu_set bit mask. CPU_ZERO(&my_set); // Initialize it all to 0, i.e. no CPUs selected. CPU_SET(4, &my_set); // set the bit that represents core 7. sched_setaffinity(0, sizeof(cpu_set_t), &my_set); // Set affinity of this process to the defined mask, i.e. only 7. status = syscall(SYS_getcpu, &cpu, NULL, NULL);

if(argc < 2) { fprintf(stderr, "Usage: %s process_nickname\n",argv[0]); exit(1); }

srand((int)time(NULL)+(int)getpid()); a = rand();

fprintf(stderr, "\nProcess %s is using CPU: %d. Value of a is %lf and address of a is %p\n",argv[1], cpu, a, &a); sleep(1);

fprintf(stderr, "\nProcess %s is using CPU: %d. Value of a is %lf and address of a is %p\n",argv[1], cpu, a, &a); sleep(3);

return 0;}

getcpu system call to retrieve the executing CPU ID

create a random number

print the value of a and address of a

print the value of a and address of a again after sleep

Page 36: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Demo revisited

!44

The same processor!The same memory

address!

Different values

Different values are preserved

Page 37: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Demo revisited

!45

#define _GNU_SOURCE #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <sched.h> #include <sys/syscall.h> #include <time.h>

double a;

int main(int argc, char *argv[]) { int i, number_of_total_processes=4; number_of_total_processes = atoi(argv[1]); for(i = 0; i< number_of_total_processes-1 && fork(); i++); srand((int)time(NULL)+(int)getpid()); fprintf(stderr, "\nProcess %d is using CPU: %d. Value of a is %lf and address of a is %p\n”,getpid(), cpu, a, &a); sleep(10); fprintf(stderr, "\nProcess %d is using CPU: %d. Value of a is %lf and address of a is %p\n”,getpid(), cpu, a, &a); return 0; }

Process B

Process A&a = 0x601090

Process A’s Mapping Table

Process B’s Mapping Table

Page 38: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

How to map from virtual to physical? Let’s start from segmentation

!46

Page 39: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• The compiler generates code using virtual memory addresses • The OS works together with hardware to partition physical memory

space into segments for each running application • The hardware dynamically translates virtual addresses into physical

memory addresses

!47

Segmentation

Physical memory of the machineApplication A0

X

Application B

X

00

X

2X

Page 40: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Address translation in segmentation

!48

Physical memory of the machine0

X

2X

Virtual memory of Application

X

0Processor

PC Base

0 X+ = X

load 0x4000

X0x4000 + = X + 0x4000

Page 41: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Protection again malicious processes

!49

Physical memory of the machine0

X

2X

Virtual memory of Application

X

0Processor

PC Base

0 X+ = X

load 0x4000

0x4000 X+ = X + 0x4000what if X + 0x4000 belongs to

another process?

what if we are load a “negative offset”?

Page 42: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

Protection again malicious processes

!50

Physical memory of the machine0

X

2X

Virtual memory of Application

X

0Processor

PC Base

X = X + 0x4000

load 0x4000

0x4000 +

Bound

X Yes — proceed<0x4000

only allow the access if it’s yes

No — segmentation fault!!!

a segmentation fault (often shortened to segfault), raised by hardware with

memory protection, when the software has attempted to access a restricted area of memory (a memory access violation).

Page 43: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

What if?

!51

Application B

X

0

Y

X+Y

!51

Application A0

Y

Physical memory of the machine0

zApplication C0

XWhere can we map

Application C?

X

X

External Fragment

What if Application B only uses part of the

allocated space?

Internal Fragment

Even though we have space, we still cannot map App. C We waste some space in

the allocated segment

Page 44: 8 Virtual memory - University of California, Riversidehtseng/classes/cs202_2020wi/...Lottery Scheduling: Flexible Proportional-Share Resource Management Carl A. Waldspurger and William

• Reading quiz due next Tuesday • Project due date 3/3

• In about a month • Please come to our office hours if you need help for your projects

!104

Announcement