Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know linux scheduler time slice?

I'm looking for the value of the time slice (or quantum) of my Linux kernel.

Specific Questions:

  • Is there a /proc file which expose such an information ?
  • (Or) Is it well-defined in the Linux header of my distributions ?
  • (Or) Is there a C function of the Linux API (maybe sysinfo) that expose this value ?
like image 281
backlash Avatar asked May 06 '13 14:05

backlash


People also ask

Why do you need to schedule the CPU time slice?

Time slice : The period of each time slice can be very significant and crucial to balance CPUs performance and responsiveness. If time slice is quite short, scheduler will take more processing time. In contrast, if the time slice is too long, scheduler will again take more processing time.

What happens when a time slice is over?

If a process does not complete or get blocked because of an I/O operation within the time slice, the time slice expires and the process is preempted. This preempted process is placed at the back of the run queue where it must wait for all the processes that were already in the queue to cycle through the CPU.

What is CPU time slicing?

Answer: Time slicing is a scheduling mechanism/way used in time sharing systems. It is also termed as Round Robin scheduling. The aim of Round Robin scheduling or time slicing scheduling is to give all processes an equal opportunity to use CPU.

What is the current Linux scheduler?

Linux uses a Completely Fair Scheduling (CFS) algorithm, which is an implementation of weighted fair queueing (WFQ). Imagine a single CPU system to start with: CFS time-slices the CPU among running threads.


2 Answers

The quantum allocated for a particular process may vary:

You can tune "slice" by adjusting sched_latency_ns and sched_min_granularity_ns, but note that "slice" is not a fixed quantum. Also note that CFS preemption decisions are based upon instantaneous state. A task may have received a full (variable) "slice" of CPU time, but preemption will be triggered only if a more deserving task is available, so a "slice" is not the "max uninterrupted CPU time" that you may expect it to be.. but it is somewhat similar.

This is because the Completely Fair Scheduler, the default Linux scheduler, assigns a proportion of the processor to a process rather than a fixed timeslice. That means the timeslice for each process is proportional to the current load and weighted by the process' priority value.

For special-purpose realtime processes which use SCHED_RR, the default timeslice is defined in the Linux kernel as RR_TIMESLICE in include/linux/sched/rt.h.

/*  * default timeslice is 100 msecs (used only for SCHED_RR tasks).  * Timeslices get refilled after they expire.  */ #define RR_TIMESLICE            (100 * HZ / 1000) 

You can use sched_rr_get_interval() to get the SCHED_RR interval for a specific SCHED_RR process.

like image 123
Vilhelm Gray Avatar answered Sep 22 '22 21:09

Vilhelm Gray


CFS (which is default scheduler for processes) has no fixed timeslice, it is calculated at runtime depending of targeted latency (sysctl_sched_latency) and number of running processes. Timeslice could never be less than minimum granularity (sysctl_sched_min_granularity).

Timeslice will be always between sysctl_sched_min_granularity and sysctl_sched_latency, which are defaults to 0.75 ms and 6 ms respectively and defined in kernel/sched/fair.c.

But actual timeslice isn't exported to user-space.

like image 38
Alexey Shmalko Avatar answered Sep 19 '22 21:09

Alexey Shmalko