Docsity

Dynamic Priority Scheduling in Nachos: A Realistic Approach - Prof. Prasad Kulkarni, Assignments of Operating Systems

Toggle sidebar
bg1
EECS 678: Intro to Operating Systems
Programming Assignment 2-2: Dynamic Priority with Aging
As in PA2-1 you may do this assignment in groups of two. To be sure you start with an error
free source tree, begin this assignment with this Nachos TAR file. If you do not use this newer TAR
file to implement this project, I will not grade your project.
This is a big project description. Thus, here is a table of contents:
1. Background
2. Project Overview
3. Histogram Overview
4. Histogram Discussion
5. Histogram Generation
6. Implementation Details
7. Validation
8. Grading
1. Scheduling Background and Motivation
Most modern operating systems use a scheduling algorithm that is significantly more complex
than simple round robin or priority-based round robin scheduling. In this project, you will implement a
(somewhat) more realistic scheduler for Nachos that includes features supporting a plausible approach
to time-sharing among Nachos user processes.
The dynamic priority scheme you will implement was inspired by the scheme the Linux scheduler used
at one time. You can examine the real Linux scheduling code from the Linux kernel source of the time
(after extracting the archive, look in kernel/sched.c). The names we have chosen and some aspects of
the algorithms used were drawn from this version of the Linux scheduler. The names and conventions
can be a bit confusing, but that is because the issues are complex, and the need to figure it out is quite
realistic. The most challenging part of many system programming projects is to determine the real
meaning of code written by others. The decision of how to modify it is often quite simple after you
have figured out the previous authors' policy goals and how they used specific implementation
mechanisms to pursue those goals.
Using the algorithmic ideas of the Linux scheduler has the advantage of being realistic, but this does
introduce a conflict among terms from three sources: the textbook (Silberschatz, Galvin, & Gagne),
Linux, and Nachos. The ideas involved are straightforward and are listed under the Silberschatz,
Galvin, & Gagne terminology:
Priority
A term used flexibly by everyone. In this assignment, we distinguish between the static and
dynamic priorities of a process. The static priority is assigned when a process is created and
remains unchanged during execution, unless it is modified using the Nice system call. The static
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Dynamic Priority Scheduling in Nachos: A Realistic Approach - Prof. Prasad Kulkarni and more Assignments Operating Systems in PDF only on Docsity!

EECS 678: Intro to Operating Systems

Programming Assignment 2-2: Dynamic Priority with Aging

As in PA2-1 you may do this assignment in groups of two. To be sure you start with an error free source tree, begin this assignment with this Nachos TAR file. If you do not use this newer TAR file to implement this project, I will not grade your project. This is a big project description. Thus, here is a table of contents:

1. Background

2. Project Overview

3. Histogram Overview

4. Histogram Discussion

5. Histogram Generation

6. Implementation Details

7. Validation

8. Grading

1. Scheduling Background and Motivation

Most modern operating systems use a scheduling algorithm that is significantly more complex than simple round robin or priority-based round robin scheduling. In this project, you will implement a (somewhat) more realistic scheduler for Nachos that includes features supporting a plausible approach to time-sharing among Nachos user processes. The dynamic priority scheme you will implement was inspired by the scheme the Linux scheduler used at one time. You can examine the real Linux scheduling code from the Linux kernel source of the time (after extracting the archive, look in kernel/sched.c). The names we have chosen and some aspects of the algorithms used were drawn from this version of the Linux scheduler. The names and conventions can be a bit confusing, but that is because the issues are complex, and the need to figure it out is quite realistic. The most challenging part of many system programming projects is to determine the real meaning of code written by others. The decision of how to modify it is often quite simple after you have figured out the previous authors' policy goals and how they used specific implementation mechanisms to pursue those goals. Using the algorithmic ideas of the Linux scheduler has the advantage of being realistic, but this does introduce a conflict among terms from three sources: the textbook (Silberschatz, Galvin, & Gagne), Linux, and Nachos. The ideas involved are straightforward and are listed under the Silberschatz, Galvin, & Gagne terminology: Priority A term used flexibly by everyone. In this assignment, we distinguish between the static and dynamic priorities of a process. The static priority is assigned when a process is created and remains unchanged during execution, unless it is modified using the Nice system call. The static

priority thus expresses a process' importance relative to others on the system on a long term basis. The dynamic priority is modified as the process executes to reflect its history and OS policy goals. Different scheduling policies will use a wide variety of mechanisms to adjust priority dynamically. Note that the scheduler uses the dynamic value when deciding which process to select from the ready list. Dynamic Process Priority This is the dynamically updated priority maintained for each process. Under the Linux scheduler this is maintained by the variable counter and this is what we suggest you call it in your new Nachos scheduler. This value is the one used by the scheduler to make the decision about what process to run next. Manipulation of this value in various ways is the mechanism used to implement policies about how to balance the demands of various processes for CPU time. Also, this is the value that is modified by the various aging mechanisms in the system. Clock Tick The basic tick of the system clock gives the OS its sense of time and is usually implemented as a periodic interrupt from a hardware clock. The typical period is 10 milliseconds (although newer Linux kernels have reduced it to 1 millisecond and then raised it to 2.5 milliseconds). In older UNIX systems this was often called a tick. Under Linux this happens to be called a jiffy. Nachos keeps track of two concepts, using the word tick to mean a unit of time at the simulated MIPS CPU level and the word quantum to mean the basic unit of Nachos OS time sense. The quantum under Nachos is thus the jiffy under Linux. Here we suggest you use the word quantum (plural quanta ) as the basic unit of time your scheduler considers when managing processes, since that is the basic unit of Nachos' time sense. Note that the TimerInterruptHandler routine in threads/system.cc is called periodically, and currently forces the current thread to yield the processor. This is both the place where you can take actions to give your schedulers a sense of time passing, but also perhaps take actions to change the policy of always yielding the CPU on each system clock tick. The header file machine/stats.h is where the statistics structure and several relevant defined constants are specified. Quantum Unfortunately Silberschatz, Galvin & Gagne use this term to describe the number of OS time units a user process is permitted to run when CPU bound before another process is given a chance to run by invoking the scheduler for a decision. Note that this is of particular importance in round robin scheduling. This particular idea is not used as part of the scheduling policy you are supposed to implement, but we thought it prudent to mention this to help you avoid terminology based confusion. Also, the enforced yield of the CPU on every tick in TimerInterruptHandler in threads/system.cc is worth considering in relation to the scheduler execution behavior of the threads that you want to enforce. Aging Silberschatz, Galvin & Gagne use this as a general term to describe any of several ways in which dynamic priorities of processes are changed as time passes and events occur. Dynamic priority is often made worse as a process runs and made better as it spends time ready but not running. This is the most simple and obvious of the many possible approaches to dynamic priority. In the scheduler you are supposed to write use the counter variable to hold the current value of the dynamic priority of a process.

number of times a measured sample fell within the bucket's range. The configuration of a histogram accumulating a distribution is thus crucial; the number of buckets, the range of values represented by each bucket, etc. We have provided all of the histogram generation code (even some scripts that create PDFs). You will need to discuss these histograms and interpret them when validating your implementation of the dynamic priority: use them to argue why your code achieves the stated policy goal.

3. Histogram Overview

The following page shows the two types of histograms you will be generating for this assignment. These histograms show the lengths of the execution (activity) and idle (inactivity) intervals for the processes are derived from data describing system events. Specifically, the system records a timestamped event whenever a thread changes state (e.g. READY->RUNNING, or RUNNING-> READY) or when a context switch among processes occurs. From this information it is possible to calculate the periods during which a thread was running on the CPU and when it was not. The histogram data in the graphs above are the result of those episode duration measurements for inactive episodes and active episodes (active == running). These particular histograms were gathered using the static priority scheduler that comes with the starter Nachos tarball.

4. Histogram Discussion

In this assignment, we require you to generate histograms from your solution and discuss the results using the histograms. Because students always ask what a “good” interpretation looks like, we provide an example interpretation of the static priority inactivity histogram shown above. Also, we know the bucket values on the histograms above are impossible to see. When you generate your own, you should be able to see bucket values by zooming in to the maximum percentage in your viewer.

An Example Interpretation

Recall that thread A has a static priority of 20, B has 25, C has 30, and D has 35. From this graph we can see a striking correlation between the static priority of a thread and the longest episode of inactivity it had to endure. The interpretation is straight-forward: we know that with static priorities, the best thread runs until it's finished. Thus A did not suffer a long episode of inactivity. But while A was running, B had to wait. C had it worse because it had to wait for A and B. D waited for all three. The spacing between various threads' longest inactive episodes is regular because our threads all have equivalent execution times (about 13,000 ticks). The few brief inactive episodes at the left of the graph correspond to the start-up phase of the run where the threads are being created and setting their priorities. Excluding the start-up phase, each thread has one inactive episode because once it does obtain the CPU it runs until it completes. This histogram therefore indicates that the static priority mechanism in the handout-code correctly achieves its policy of simply choosing the thread with the best priority on the system. NOTE: There appears to be an anomalous longer period of inactivity for the process printing 'A' in this histogram. If anyone has a good explanation, please let me know, because I ran out of ideas at the time of this writing. Also note, none of the other histograms generated by your solution will contain any anomalies. Your interpretation of these histograms will be straightforward.

6. Implementation Details (The Code You Will Write)

This discussion should provide you the details you need to implement our prescribed dynamic priority mechanism.

Tracking Process' Dynamic Priority (Counter)

Similar to adding a static priority variable to the thread PCB for the previous assignment, we also need to add an integer to the PCB for dynamic priority. This allows us access to an integer value when making scheduling decisions based on dynamic priorities. This is the counter. Looking a bit ahead, realize that there will be two main operations to perform on dynamic priorities: improving them and worsening them. Also, when the thread's static priority is initialized to 20, it's dynamic priority ought to be set to 20 as well. As we are adding a PCB field very similar to an already existing one, it would be wise to find all parts of the code that rely on the static priority and check if we should also involve the dynamic priority (e.g. Thread::Print) or replace the static with the dynamic (e.g. ThreadOrder).

Adding Threads to the Ready Queue

Each time the Nachos quantum elapses, the kernel jumps to an interrupt handler (in ./thread/system.cc ). This interrupt handler sets a flag that forces the running thread to yield the CPU to the scheduler once the kernel receives control back from the interrupt handler. The Thread::Yield member function is where the scheduler gets a chance to run. It places the current thread into the ready list (Scheduler::ReadyToRun) and then selects the best thread in the ready list (ThreadOrder and Scheduler::FindNextToRun) and starts its execution (Scheduler::Run). Whereas the scheduling decision was formerly made based on static priority, it should now be made based on dynamic priority. It is primarily in these parts of the scheduler that the dynamic priority will be implemented. According to our earlier discussion of the dynamic priority mechanism, the threads' dynamic priorities are improved after each scheduling decision if they were not chosen (hint: FindNextToRun). In our mechanism, we will reward a thread for not being chosen by decrementing its dynamic priority by 1 (remember that the lower the priority the better). The most direct way to apply a function to every ready thread is to use the List::MapCar member function of the List class. Note that, since the list is sorted and all dynamic priorities are altered in the same way, the list will still be in sorted order after the aging is done. Although it is highly unlikely to occur, be sure that the dynamic priorities do not underflow (i.e. become so negative that the 32-bit representation actually becomes positive again).

Preempting a Thread

When a thread is being removed from the CPU, its dynamic priority must be worsened because it was just running. The most immediate way to recognize when a thread is being preempted is at the bottom of the Scheduler::FindNextToRun function. The first requirement is that the current thread is in the READY state: it is only considered a preemption if the current thread still wants to run. Once we have selected the next thread (i.e. the one in the ready list with the best dynamic priority), we can check if it is the current thread. If the current thread is ready and is not the thread chosen to receive the CPU, then its dynamic priority ought to be worsened. We will worsen the dynamic priority by resetting it to

the thread's static priority value (for now: we'll make this a bit more complicated in a bit). Beware! By changing the thread's dynamic priority while it is in the ready list, which is sorted by dynamic priority, we might invalidate the ordering of the ready list. That could have some seriously unexpected consequences. To avoid this, when we reset the current thread's dynamic priority, we must remove it from the ready list, reset its dynamic priority, and then put it back into the ready list.

What Computation Behavior Should Look Like ( Pre-Validation )

Execution Order

For you to notice the trends we wish you to observe as you validate your code by looking at the histograms and other metrics of your simulator runs of nice_free , we need you to be looking at the same histograms we looked at! We don't want you ripping out your hair because you're looking at insufficient data. For that reason, we provide the example run of our solution below. We've added line- breaks so it fits on this page: nfrisby@wozniak: ../userprog/nachos -x nice_free Starting... CCCBBBAAAAAAADDDDDDDDDDDDDDDAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAABBBBBBBBBBBDDDDDDDAAAACCCCCCCCCCCCCCCCCCCAAAAAAAABBBBBBBBBBBBBBBB BBBBAAAAAAAAAAAAAAAAAAAAAAAAAAADDDDCCCCCCCCBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAABBBBBBBBBBBCCCCCCCCAAAADDDDDDDDDDDDDDDDDDDDAAAAAAAABBBBBBBBBBBBBBBBBBB BAAAAAAAACCCCCCCCCCCCCCCCCCCCAAAAAAAAAAABBBBBBBBBBBBDDDDDDDDAAAAAAAAAAAAAAAAAAAA AAAAAAAACCCCBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADDDDDDDDBBBBCCCCCCCAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBAAAAAAAACCCCCCCCDDDDDDDDDDDAAAAA AAAAAAABBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAACCCCCCCCCCCCCCCCCCCCAAAABBBBBBBBDDDDDD DDDDDDAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBCCCCCCCCCCCAAAAAAAAAAAAAAAAAAAAAAAAAAAB BBBDDDDDDDDDDDDDDDDAAAAAAAAAAAACCCCCCCCCCCCBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAABBBBBBBBDDDDCCCCCCCCAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBAAA AAAAAAAACCCCCCCCCCCCDDDDDDDAAAAAAAABBBBBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAC CCCCCCCC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DDDDD Finished Machine halting!

Note how, as its competitors die off (and the best SP on the system becomes nearer to its own), D's episodes of inactivity become shorter and shorter. If your histogram looks like this one (again, not necessarily exactly the same), that is more evidence indicating that your implementation is correct.

The Last Touch

Above, we indicated that the dynamic priority will not necessarily be reset back to the static priority. Instead, when a thread is being preempted, it will retain a fraction of its dynamic priority advantage. So the new dynamic priority is calculated like this: new DP = SP - (R * (SP - DP)) where R is the retention factor This retention factor is available in the code as the class data member Thread::dpRetentionFactor. We already put it in there because that way you don't have to go to the trouble of adding it as a command line parameter. It already exists as "-R". Try this command: ../userprog/nachos -x ../test/nice_free -R 1. Look familiar? It should look just like round-robin scheduling. Part of the validation will be explaining why this is the case. Note also, that if R=0.0, then it's the same as the design decision of reseting the dynamic priority to the static priority that we started with at every scheduling decision point.

Important Files

Files that you will need to reference or modify include:

  • threads/thread.h — Contains the definition of the Thread class.
  • threads/thread.cc — Contains the implementation of the Thread class.
  • threads/scheduler.h — Contains the definition of the Scheduler.
  • threads/scheduler.cc — Contains the implementation of the Scheduler.
  • threads/list.h — Contains the definition of the List class.
  • threads/list.cc — Contains the implementation of the List class.
  • machine/interrupt.h — Contains the definition of the Interrupt class.
  • machine/interrupt.cc — Contains the implementation of the Interrupt class.
  • threads/system.cc — Creates many of the global objects in Nachos.

7. Validation

Since we have quantifiable metrics relating to our policy, you should be able to provide empirical evidence supporting that your implementation of the mechanism is a correct one. When you go about discussing these histograms and how they correspond to your code and our policy goal, it may be helpful to consider the following strong metaphor. The kernel (including the scheduler) is a service provider for threads. Threads have certain expectation for the quality of service they will receive when working with the kernel. Considering our policy goals in this project, that quality of service is expressed by the distribution of the durations of inactivity as well as the standard thread

metrics (e.g. turnaround time, waiting time). Make an effort to discuss each of the basic metrics in your validation, but realize the focus we are encouraging is on the distribution of the durations of inactivity.

Generating the Relevant Histograms

Look into the ./nachos/dsui_tmp/makePDFs.py Python script. When this script is invoked with its first argument as the name of an empty directory (assuming a correct Nachos implementation), it will generate a number of graphs: [0] % cd dsui_tmp [1] % mkdir tmp [2] % python makePDFs.py ./tmp/ For the basic validation, when you're looking at the generated histograms, you may ignore the i parameter in the titles, and focus instead on the varying R in these 5 graphs: R0.00-i1-inactive.pdf This is the most basic. It corresponds to the first histogram we made for the static priorities except that this one has dynamic priorities. R0.*-i1-inactive.pdf The other "-inactive.pdf" graphs with R varying from 0.25 to 1.00 are graphs expressing the effect of the retention factor on the behavior of the dynamic priority scheduling. Please realize, as we've stressed, that the PDFs we try to generate automatically for you may not be completely appropriate for your particular implementation choices. If the data seems lop-sided (many occurrences in the underflow or overflow buckets), then consider either coming to see the TA to discuss possible adjustments to your code or for advice on how to adjust the histogram specification (number of buckets, width, minimum value) so that you can reason about suitable histograms.

Basic Validation

The first lesson this project targets is the effect of dynamic priorities as compared to static priorities. Compare the R0.00-i1-inactive.pdf representing your dynamic priority implementation to the histogram discussed in 4. Histogram Discussion. Note well the difference in the machine tick ranges on the x-axis. The static priority histogram ought to have much larger values.

Advanced Validation

The second lesson is the effect of the retention factor ( -R switch) on the behavior. This is the kind of mechanism abstraction that empowers general mechanisms (those that can satisfy multiple policies). Analyze the five graphs (note the changing y-axis) and try to determine the trend in the graphs. Then explain why the retention factor code you wrote would have these effects on the durations of inactivity.