Lab 6: The Scheduler
Feb. 14/15, 2007
[153 Home]     [SCHED_GFS]     [Timeslice Calculation]     [/proc]     [How to test?]     [Resources]

How to add a new policy SCHED_GFS?
  1. In <include/linux/sched.h>:
    • Add a macro definition for SCHED_GFS;
    • Change the macro definition has_rt_policy (because SCHED_GFS is not a real-time policy).

  2. In <kernel/sched.c>:
    • In sched_fork() where p is forked by current:
      if (current->gid >= 1024), set p->policy to SCHED_GFS, p->static_prio/normal_prio/prio to MAX_PRIO - 1, and p->time_slice to precalculated values based on gid, also insert p into the corresponding group queues created by yourself.
    • In sched_exit() where p is exiting:
      if p->policy is SCHED_GFS, delete it from your group queues.
    • In __normal_prio() where dynamic priority is calculated:
      if p->policy is SCHED_GFS, simply return p->static_prio.
    • In scheduler_tick() where p->time_slice is changed:
      if p->policy is SCHED_GFS, decrement p->time_slice, and reassign its value based on gid.
    • In schedule() where next is chosen to replace current:
      if next->policy is SCHED_GFS, jump to your own scheduler function and pick a task as next.

Timeslice Calculation
Share the CPU among six tasks in three Groups, each group is assigned 100ms:
Group 1024: P1(250ms), P2(100ms), P3(100ms); Group 1025: P4 (260ms); Group 1026: P5(200ms), P6(180ms)

Schedule Sequence: P1 P4 P5 P2 P4 P6 P3 P4 P5 P1 P6 P1
Time Quantum (ms): 100 100 100 100 100 100 100 60 100 100 80 50

Essentially, you do a round robin algorithm between the queues and each time you select the process at the head of the queue to execute. When the time slice expires and if the process has not finished, you stop executing the process and move the process at the end of the queue and you move to the next queue.


The /proc File System
We can use the /proc file system to collect information about processes in the system. A file in /proc or one of its subdirectories is actually a program that reads kernel variables and reports them as ASCII strings.

In Linux, each file in /proc reads one or more kernel variables, for example, /proc/meminfo provides memory statistics; each subdirectory with numeric names contains pseudo files to read information about the process whose PID is the same as the directory name, for example, /proc/1/status gives a lot of general information about the init process, including its name (init), current status (sleeping), PID (1), UID (0), and other information.

Files in /proc are read just like ordinary ASCII files. You can read them by typing cat /proc/cpuinfo to the shell, or you can write a C/C++ program and then use standard library routines such as fgets() or fscanf() to read them.

In lab exercise: write a small program to read /proc/PID/stat based on the example here. In the example, PID = 1, i.e., we get the status information about the init process, while in your program, you should be able to read the stat file for the current running process, i.e., PID = getpid().



How to test?
  1. Write a program to create N processes, which belong to at least three different groups. Use my example as a start point. In my program, I have only one group, after you set the gid of the parent process to be 1024, all child processes created by the parent will have the same gid. In your program, you should create an array of group IDs, and an array of processes each group should create.
  2. Add some CPU-bound tasks in the child process, for example, a dummy loop with arithmatic calculations, +/-/*/pow, ...
  3. Before each child process exits, read status information for that child from the /proc file system, and report how much time it spent running.
  4. Try to change the number of groups and number of processes inside each group, and compare the performance difference under your SCHED_GFS policy.
  5. Run the program with different scheduling policy, your SCHED_GFS v.s. the default SCHED_NORMAL, and compare the performance difference.
Important: Compile the program in the host OS, but run the program inside the UML.


Resources
  1. Online Source Code (2.6.18): Cross-Referencing Linux
  2. Online Book: Understanding the Linux Kernel, 3rd Edition
  3. Lab Manual: p.24-31, p.55-65, p.155-166
  4. User Mode Linux
  5. The DamnSmallLinux Filesystem Image
  6. The Kernel Source Code
  7. man proc