Skip to contentSkip to conversation

Multiple run-queues for BFS

ANNOUNCE Matthias Kohler 3 participants 3 messages Dec 15, 2012 – Dec 21, 2012

lore
Share & tools
lore
#0 lore raw

I'm doing a CPU-Scheduler based on BFS by Con Kolivas with support for multiple run-queues. BFS in itself uses only one run-queue for all CPU's. This avoids the load-balancing overhead, but does not scale well. One run-queue per CPU does scale well, but then the scheduler has load-balancing overhead. The scheduler I'm developing supports every possible run-queues configuration. You can have one single run-queue like in BFS, or you can have one run-queue per CPU, or something completely different like one run-queue every two CPU's. This, in theory would allow the scheduler to be fine-tuned to the hardware and the workload.

What state is it in?
Currently it is very unstable, CPU-Hotplug is broken, scheduling statistics are broken, support for real-time tasks is broken. Load balancing when having more than one run-queue is working, but is nothing more than keeping the load on all run-queues equal. Associating a CPU and a run-queue is currently done with a system call and there is no access right checking. The source is in a very bad state. Uni-processor build is broken.
It lacks proper Documentation.

Why allow the user to change the run-queue layout?
To optimize the scheduler to specific hardware and workloads. You could use one run-queue for all CPU's if you want low latency and low scheduling overhead.
You could use one run-queue per CPU if you want high scalability. You could use one run-queue per n CPU's is these n CPU's share cache and there is not much benefit in load balancing between them.

Benchmarks?
None, it is not stable enough to benchmark and the load balancing algorithm that is currently used, delivers very bad performance.

What advantages does it have when compared to other schedulers? It is more scalable than BFS.
It could in future have all features of BFS and of CFS, especially throughput and low latency.
It has far less lines of code than CFS.

What disadvantages does it have when compared to other schedulers? It is not stable.
It is not tested on anything else than kvm and more than 4 CPU's. Many features are not yet working or not implemented at all (good load balancing).

Implementation details:
All tasks that are runnable but not currently executing on a CPU, are queued on one of the global run-queues. Every global run-queue has its own spin-lock. When a task gets queued or dequeued this lock needs to be taken. All global run-queues are protected by one global read-write lock. When normal scheduling is done, this lock needs to be read_locked. When any change to the layout of the global run-queues is done, like adding new global run-queues or removing them, the global read-write lock needs to be write-locked.
Fair time distribution among tasks is done via the deadline mechanism of BFS.

Patch for linux-3.6.2:

diff -uprN linux-3.6.2/arch/powerpc/platforms/cell/spufs/sched.c linux-3.6.2-bfs-multi-runqueue/arch/powerpc/platforms/cell/spufs/sched.c
--- linux-3.6.2/arch/powerpc/platforms/cell/spufs/sched.c 2012-10-12
22:50:59.000000000 +0200
+++
linux-3.6.2-bfs-multi-runqueue/arch/powerpc/platforms/cell/spufs/sched.c

2012-10-25 17:13:12.578060772 +0200

@@ -63,11 +63,6 @@ static struct timer_list spusched_timer;
static struct timer_list spuloadavg_timer;

/*
- * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
- */
-#define NORMAL_PRIO 120
-
-/*
* Frequency of the spu scheduler tick. By default we do one SPU
scheduler
* tick for every 10 CPU scheduler ticks.
*/
diff -uprN linux-3.6.2/arch/x86/Kconfig linux-3.6.2-bfs-multi-runqueue/arch/x86/Kconfig
--- linux-3.6.2/arch/x86/Kconfig 2012-10-12 22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/arch/x86/Kconfig 2012-10-25
17:13:12.597060777 +0200
@@ -797,15 +797,7 @@ config SCHED_MC
increased overhead in some places. If unsure say N here.

config IRQ_TIME_ACCOUNTING
- bool "Fine granularity task level IRQ time accounting"
- default n
- ---help---

-	  Select this option to enable fine granularity task irq time
-	  accounting. This is done by reading a timestamp on each
-	  transitions between softirq and hardirq state, so there can be a
-	  small performance impact.

-
- If in doubt, say N here.
+ def_bool y

source "kernel/Kconfig.preempt"

diff -uprN linux-3.6.2/arch/x86/syscalls/syscall_64.tbl linux-3.6.2-bfs-multi-runqueue/arch/x86/syscalls/syscall_64.tbl
--- linux-3.6.2/arch/x86/syscalls/syscall_64.tbl 2012-10-12
22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/arch/x86/syscalls/syscall_64.tbl

2012-12-07 19:32:05.307937117 +0100

@@ -319,6 +319,7 @@
310 64 process_vm_readv sys_process_vm_readv
311 64 process_vm_writev sys_process_vm_writev
312 common kcmp sys_kcmp
+313 common associate_cpu_grq sys_associate_cpu_grq

#

 # x32-specific system call numbers start at 512 to avoid cache impact
diff -uprN linux-3.6.2/Documentation/scheduler/sched-BFS.txt
linux-3.6.2-bfs-multi-runqueue/Documentation/scheduler/sched-BFS.txt
--- linux-3.6.2/Documentation/scheduler/sched-BFS.txt	1970-01-01
01:00:00.000000000 +0100
+++ linux-3.6.2-bfs-multi-runqueue/Documentation/scheduler/sched-BFS.txt
2012-10-25 17:13:12.579060779 +0200
@@ -0,0 +1,347 @@
+BFS - The Brain Fuck Scheduler by Con Kolivas.
+
+Goals.
+
+The goal of the Brain Fuck Scheduler, referred to as BFS from here on,
is to
+completely do away with the complex designs of the past for the cpu
process
+scheduler and instead implement one that is very simple in basic
design.
+The main focus of BFS is to achieve excellent desktop interactivity and
+responsiveness without heuristics and tuning knobs that are difficult
to
+understand, impossible to model and predict the effect of, and when
tuned to
+one workload cause massive detriment to another.
+
+
+Design summary.
+
+BFS is best described as a single runqueue, O(n) lookup, earliest
effective
+virtual deadline first design, loosely based on EEVDF (earliest
eligible virtual
+deadline first) and my previous Staircase Deadline scheduler. Each
component
+shall be described in order to understand the significance of, and
reasoning for
+it. The codebase when the first stable version was released was
approximately
+9000 lines less code than the existing mainline linux kernel scheduler
(in
+2.6.31). This does not even take into account the removal of
documentation and
+the cgroups code that is not used.
+
+Design reasoning.
+
+The single runqueue refers to the queued but not running processes for
the
+entire system, regardless of the number of CPUs. The reason for going
back to
+a single runqueue design is that once multiple runqueues are
introduced,
+per-CPU or otherwise, there will be complex interactions as each
runqueue will
+be responsible for the scheduling latency and fairness of the tasks
only on its
+own runqueue, and to achieve fairness and low latency across multiple
CPUs, any
+advantage in throughput of having CPU local tasks causes other
disadvantages.
+This is due to requiring a very complex balancing system to at best
achieve some
+semblance of fairness across CPUs and can only maintain relatively low
latency
+for tasks bound to the same CPUs, not across them. To increase said
fairness
+and latency across CPUs, the advantage of local runqueue locking, which
makes
+for better scalability, is lost due to having to grab multiple locks.
+
+A significant feature of BFS is that all accounting is done purely
based on CPU
+used and nowhere is sleep time used in any way to determine entitlement
or
+interactivity. Interactivity "estimators" that use some kind of
sleep/run
+algorithm are doomed to fail to detect all interactive tasks, and to
falsely tag
+tasks that aren't interactive as being so. The reason for this is that
it is
+close to impossible to determine that when a task is sleeping, whether
it is
+doing it voluntarily, as in a userspace application waiting for input
in the
+form of a mouse click or otherwise, or involuntarily, because it is
waiting for
+another thread, process, I/O, kernel activity or whatever. Thus, such
an
+estimator will introduce corner cases, and more heuristics will be
required to
+cope with those corner cases, introducing more corner cases and failed
+interactivity detection and so on. Interactivity in BFS is built into
the design
+by virtue of the fact that tasks that are waking up have not used up
their quota
+of CPU time, and have earlier effective deadlines, thereby making it
very likely
+they will preempt any CPU bound task of equivalent nice level. See
below for
+more information on the virtual deadline mechanism. Even if they do not
preempt
+a running task, because the rr interval is guaranteed to have a bound
upper
+limit on how long a task will wait for, it will be scheduled within a
timeframe
+that will not cause visible interface jitter.
+
+
+Design details.
+
+Task insertion.
+
+BFS inserts tasks into each relevant queue as an O(1) insertion into a
double
+linked list. On insertion, *every* running queue is checked to see if
the newly
+queued task can run on any idle queue, or preempt the lowest running
task on the
+system. This is how the cross-CPU scheduling of BFS achieves
significantly lower
+latency per extra CPU the system has. In this case the lookup is, in
the worst
+case scenario, O(n) where n is the number of CPUs on the system.
+
+Data protection.
+
+BFS has one single lock protecting the process local data of every task
in the
+global queue. Thus every insertion, removal and modification of task
data in the
+global runqueue needs to grab the global lock. However, once a task is
taken by
+a CPU, the CPU has its own local data copy of the running process'
accounting
+information which only that CPU accesses and modifies (such as during a
+timer tick) thus allowing the accounting data to be updated lockless.
Once a
+CPU has taken a task to run, it removes it from the global queue. Thus
the
+global queue only ever has, at most,
+
+	(number of tasks requesting cpu time) - (number of logical CPUs) + 1
+
+tasks in the global queue. This value is relevant for the time taken to
look up
+tasks during scheduling. This will increase if many tasks with CPU
affinity set
+in their policy to limit which CPUs they're allowed to run on if they
outnumber
+the number of CPUs. The +1 is because when rescheduling a task, the
CPU's
+currently running task is put back on the queue. Lookup will be
described after
+the virtual deadline mechanism is explained.
+
+Virtual deadline.
+
+The key to achieving low latency, scheduling fairness, and "nice level"
+distribution in BFS is entirely in the virtual deadline mechanism. The
one
+tunable in BFS is the rr_interval, or "round robin interval". This is
the
+maximum time two SCHED_OTHER (or SCHED_NORMAL, the common scheduling
policy)
+tasks of the same nice level will be running for, or looking at it the
other
+way around, the longest duration two tasks of the same nice level will
be
+delayed for. When a task requests cpu time, it is given a quota
(time_slice)
+equal to the rr_interval and a virtual deadline. The virtual deadline
is
+offset from the current time in jiffies by this equation:
+
+	jiffies + (prio_ratio * rr_interval)
+
+The prio_ratio is determined as a ratio compared to the baseline of
nice -20
+and increases by 10% per nice level. The deadline is a virtual one only
in that
+no guarantee is placed that a task will actually be scheduled by this
time, but
+it is used to compare which task should go next. There are three
components to
+how a task is next chosen. First is time_slice expiration. If a task
runs out
+of its time_slice, it is descheduled, the time_slice is refilled, and
the
+deadline reset to that formula above. Second is sleep, where a task no
longer
+is requesting CPU for whatever reason. The time_slice and deadline are
_not_
+adjusted in this case and are just carried over for when the task is
next
+scheduled. Third is preemption, and that is when a newly waking task is
deemed
+higher priority than a currently running task on any cpu by virtue of
the fact
+that it has an earlier virtual deadline than the currently running
task. The
+earlier deadline is the key to which task is next chosen for the first
and
+second cases. Once a task is descheduled, it is put back on the queue,
and an
+O(n) lookup of all queued-but-not-running tasks is done to determine
which has
+the earliest deadline and that task is chosen to receive CPU next.
+
+The CPU proportion of different nice tasks works out to be
approximately the
+
+	(prio_ratio difference)^2
+
+The reason it is squared is that a task's deadline does not change
while it is
+running unless it runs out of time_slice. Thus, even if the time
actually
+passes the deadline of another task that is queued, it will not get CPU
time
+unless the current running task deschedules, and the time
"base" (jiffies) is
+constantly moving.
+
+Task lookup.
+
+BFS has 103 priority queues. 100 of these are dedicated to the static
priority
+of realtime tasks, and the remaining 3 are, in order of best to worst
priority,
+SCHED_ISO (isochronous), SCHED_NORMAL, and SCHED_IDLEPRIO (idle
priority
+scheduling). When a task of these priorities is queued, a bitmap of
running
+priorities is set showing which of these priorities has tasks waiting
for CPU
+time. When a CPU is made to reschedule, the lookup for the next task to
get
+CPU time is performed in the following way:
+
+First the bitmap is checked to see what static priority tasks are
queued. If
+any realtime priorities are found, the corresponding queue is checked
and the
+first task listed there is taken (provided CPU affinity is suitable)
and lookup
+is complete. If the priority corresponds to a SCHED_ISO task, they are
also
+taken in FIFO order (as they behave like SCHED_RR). If the priority
corresponds
+to either SCHED_NORMAL or SCHED_IDLEPRIO, then the lookup becomes O(n).
At this
+stage, every task in the runlist that corresponds to that priority is
checked
+to see which has the earliest set deadline, and (provided it has
suitable CPU
+affinity) it is taken off the runqueue and given the CPU. If a task has
an
+expired deadline, it is taken and the rest of the lookup aborted (as
they are
+chosen in FIFO order).
+
+Thus, the lookup is O(n) in the worst case only, where n is as
described
+earlier, as tasks may be chosen before the whole task list is looked
over.
+
+
+Scalability.
+
+The major limitations of BFS will be that of scalability, as the
separate
+runqueue designs will have less lock contention as the number of CPUs
rises.
+However they do not scale linearly even with separate runqueues as
multiple
+runqueues will need to be locked concurrently on such designs to be
able to
+achieve fair CPU balancing, to try and achieve some sort of nice-level
fairness
+across CPUs, and to achieve low enough latency for tasks on a busy CPU
when
+other CPUs would be more suited. BFS has the advantage that it requires
no
+balancing algorithm whatsoever, as balancing occurs by proxy simply
because
+all CPUs draw off the global runqueue, in priority and deadline order.
Despite
+the fact that scalability is _not_ the prime concern of BFS, it both
shows very
+good scalability to smaller numbers of CPUs and is likely a more
scalable design
+at these numbers of CPUs.
+
+It also has some very low overhead scalability features built into the
design
+when it has been deemed their overhead is so marginal that they're
worth adding.
+The first is the local copy of the running process' data to the CPU
it's running
+on to allow that data to be updated lockless where possible. Then there
is
+deference paid to the last CPU a task was running on, by trying that
CPU first
+when looking for an idle CPU to use the next time it's scheduled.
Finally there
+is the notion of "sticky" tasks that are flagged when they are
involuntarily
+descheduled, meaning they still want further CPU time. This sticky flag
is
+used to bias heavily against those tasks being scheduled on a different
CPU
+unless that CPU would be otherwise idle. When a cpu frequency governor
is used
+that scales with CPU load, such as ondemand, sticky tasks are not
scheduled
+on a different CPU at all, preferring instead to go idle. This means
the CPU
+they were bound to is more likely to increase its speed while the other
CPU
+will go idle, thus speeding up total task execution time and likely
decreasing
+power usage. This is the only scenario where BFS will allow a CPU to go
idle
+in preference to scheduling a task on the earliest available spare CPU.
+
+The real cost of migrating a task from one CPU to another is entirely
dependant
+on the cache footprint of the task, how cache intensive the task is,
how long
+it's been running on that CPU to take up the bulk of its cache, how big
the CPU
+cache is, how fast and how layered the CPU cache is, how fast a context
switch
+is... and so on. In other words, it's close to random in the real world
where we
+do more than just one sole workload. The only thing we can be sure of
is that
+it's not free. So BFS uses the principle that an idle CPU is a wasted
CPU and
+utilising idle CPUs is more important than cache locality, and cache
locality
+only plays a part after that.
+
+When choosing an idle CPU for a waking task, the cache locality is
determined
+according to where the task last ran and then idle CPUs are ranked from
best
+to worst to choose the most suitable idle CPU based on cache locality,
NUMA
+node locality and hyperthread sibling business. They are chosen in the
+following preference (if idle):
+
+* Same core, idle or busy cache, idle threads
+* Other core, same cache, idle or busy cache, idle threads.
+* Same node, other CPU, idle cache, idle threads.
+* Same node, other CPU, busy cache, idle threads.
+* Same core, busy threads.
+* Other core, same cache, busy threads.
+* Same node, other CPU, busy threads.
+* Other node, other CPU, idle cache, idle threads.
+* Other node, other CPU, busy cache, idle threads.
+* Other node, other CPU, busy threads.
+
+This shows the SMT or "hyperthread" awareness in the design as well
which will
+choose a real idle core first before a logical SMT sibling which
already has
+tasks on the physical CPU.
+
+Early benchmarking of BFS suggested scalability dropped off at the 16
CPU mark.
+However this benchmarking was performed on an earlier design that was
far less
+scalable than the current one so it's hard to know how scalable it is
in terms
+of both CPUs (due to the global runqueue) and heavily loaded machines
(due to
+O(n) lookup) at this stage. Note that in terms of scalability, the
number of
+_logical_ CPUs matters, not the number of _physical_ CPUs. Thus, a dual
(2x)
+quad core (4X) hyperthreaded (2X) machine is effectively a 16X. Newer
benchmark
+results are very promising indeed, without needing to tweak any knobs,
features
+or options. Benchmark contributions are most welcome.
+
+
+Features
+
+As the initial prime target audience for BFS was the average desktop
user, it
+was designed to not need tweaking, tuning or have features set to
obtain benefit
+from it. Thus the number of knobs and features has been kept to an
absolute
+minimum and should not require extra user input for the vast majority
of cases.
+There are precisely 2 tunables, and 2 extra scheduling policies. The
rr_interval
+and iso_cpu tunables, and the SCHED_ISO and SCHED_IDLEPRIO policies. In
addition
+to this, BFS also uses sub-tick accounting. What BFS does _not_ now
feature is
+support for CGROUPS. The average user should neither need to know what
these
+are, nor should they need to be using them to have good desktop
behaviour.
+
+rr_interval
+
+There is only one "scheduler" tunable, the round robin interval. This
can be
+accessed in
+
+	/proc/sys/kernel/rr_interval
+
+The value is in milliseconds, and the default value is set to 6ms.
Valid values
+are from 1 to 1000. Decreasing the value will decrease latencies at the
cost of
+decreasing throughput, while increasing it will improve throughput, but
at the
+cost of worsening latencies. The accuracy of the rr interval is limited
by HZ
+resolution of the kernel configuration. Thus, the worst case latencies
are
+usually slightly higher than this actual value. BFS uses "dithering" to
try and
+minimise the effect the Hz limitation has. The default value of 6 is
not an
+arbitrary one. It is based on the fact that humans can detect jitter at
+approximately 7ms, so aiming for much lower latencies is pointless
under most
+circumstances. It is worth noting this fact when comparing the latency
+performance of BFS to other schedulers. Worst case latencies being
higher than
+7ms are far worse than average latencies not being in the microsecond
range.
+Experimentation has shown that rr intervals being increased up to 300
can
+improve throughput but beyond that, scheduling noise from elsewhere
prevents
+further demonstrable throughput.
+
+Isochronous scheduling.
+
+Isochronous scheduling is a unique scheduling policy designed to
provide
+near-real-time performance to unprivileged (ie non-root) users without
the
+ability to starve the machine indefinitely. Isochronous tasks (which
means
+"same time") are set using, for example, the schedtool application like
so:
+
+	schedtool -I -e amarok
+
+This will start the audio application "amarok" as SCHED_ISO. How
SCHED_ISO works
+is that it has a priority level between true realtime tasks and
SCHED_NORMAL
+which would allow them to preempt all normal tasks, in a SCHED_RR
fashion (ie,
+if multiple SCHED_ISO tasks are running, they purely round robin at
rr_interval
+rate). However if ISO tasks run for more than a tunable finite amount
of time,
+they are then demoted back to SCHED_NORMAL scheduling. This finite
amount of
+time is the percentage of _total CPU_ available across the machine,
configurable
+as a percentage in the following "resource handling" tunable (as
opposed to a
+scheduler tunable):
+
+	/proc/sys/kernel/iso_cpu
+
+and is set to 70% by default. It is calculated over a rolling 5 second
average
+Because it is the total CPU available, it means that on a multi CPU
machine, it
+is possible to have an ISO task running as realtime scheduling
indefinitely on
+just one CPU, as the other CPUs will be available. Setting this to 100
is the
+equivalent of giving all users SCHED_RR access and setting it to 0
removes the
+ability to run any pseudo-realtime tasks.
+
+A feature of BFS is that it detects when an application tries to obtain
a
+realtime policy (SCHED_RR or SCHED_FIFO) and the caller does not have
the
+appropriate privileges to use those policies. When it detects this, it
will
+give the task SCHED_ISO policy instead. Thus it is transparent to the
user.
+Because some applications constantly set their policy as well as their
nice
+level, there is potential for them to undo the override specified by
the user
+on the command line of setting the policy to SCHED_ISO. To counter
this, once
+a task has been set to SCHED_ISO policy, it needs superuser privileges
to set
+it back to SCHED_NORMAL. This will ensure the task remains ISO and all
child
+processes and threads will also inherit the ISO policy.
+
+Idleprio scheduling.
+
+Idleprio scheduling is a scheduling policy designed to give out CPU to
a task
+_only_ when the CPU would be otherwise idle. The idea behind this is to
allow
+ultra low priority tasks to be run in the background that have
virtually no
+effect on the foreground tasks. This is ideally suited to distributed
computing
+clients (like setiathome, folding, mprime etc) but can also be used to
start
+a video encode or so on without any slowdown of other tasks. To avoid
this
+policy from grabbing shared resources and holding them indefinitely, if
it
+detects a state where the task is waiting on I/O, the machine is about
to
+suspend to ram and so on, it will transiently schedule them as
SCHED_NORMAL. As
+per the Isochronous task management, once a task has been scheduled as
IDLEPRIO,
+it cannot be put back to SCHED_NORMAL without superuser privileges.
Tasks can
+be set to start as SCHED_IDLEPRIO with the schedtool command like so:
+
+	schedtool -D -e ./mprime
+
+Subtick accounting.
+
+It is surprisingly difficult to get accurate CPU accounting, and in
many cases,
+the accounting is done by simply determining what is happening at the
precise
+moment a timer tick fires off. This becomes increasingly inaccurate as
the
+timer tick frequency (HZ) is lowered. It is possible to create an
application
+which uses almost 100% CPU, yet by being descheduled at the right time,
records
+zero CPU usage. While the main problem with this is that there are
possible
+security implications, it is also difficult to determine how much CPU a
task
+really does use. BFS tries to use the sub-tick accounting from the TSC
clock,
+where possible, to determine real CPU usage. This is not entirely
reliable, but
+is far more likely to produce accurate CPU usage data than the existing
designs
+and will not show tasks as consuming no CPU usage when they actually
are. Thus,
+the amount of CPU reported as being used by BFS will more accurately
represent
+how much CPU the task itself is using (as is shown for example by the
'time'
+application), so the reported values may be quite different to other
schedulers.
+Values reported as the 'load' are more prone to problems with this
design, but
+per process values are closer to real usage. When comparing throughput
of BFS
+to other designs, it is important to compare the actual completed work
in terms
+of total wall clock time taken and total work done, rather than the
reported
+"cpu usage".
+
+
+Con Kolivas <[email protected]> Tue, 5 Apr 2011
diff -uprN linux-3.6.2/Documentation/sysctl/kernel.txt
linux-3.6.2-bfs-multi-runqueue/Documentation/sysctl/kernel.txt
--- linux-3.6.2/Documentation/sysctl/kernel.txt	2012-10-12
22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/Documentation/sysctl/kernel.txt
2012-10-25 17:13:12.584060777 +0200
@@ -33,6 +33,7 @@ show up in /proc/sys/kernel:
 - domainname
 - hostname
 - hotplug
+- iso_cpu
 - kptr_restrict
 - kstack_depth_to_print       [ X86 only ]
 - l2cr                        [ PPC only ]
@@ -59,6 +60,7 @@ show up in /proc/sys/kernel:
 - randomize_va_space
 - real-root-dev               ==> Documentation/initrd.txt
 - reboot-cmd                  [ SPARC only ]
+- rr_interval
 - rtsig-max
 - rtsig-nr
 - sem
@@ -301,6 +303,16 @@ kernel stack.

==============================================================

+iso_cpu: (BFS CPU scheduler only).
+
+This sets the percentage cpu that the unprivileged SCHED_ISO tasks can +run effectively at realtime priority, averaged over a rolling five +seconds over the -whole- system, meaning all cpus.
+
+Set to 70 (percent) by default.
+
+==============================================================
+
l2cr: (PPC only)

This flag controls the L2 cache of G3 processor boards. If
@@ -517,6 +529,20 @@ rebooting. ???

==============================================================

+rr_interval: (BFS CPU scheduler only)
+
+This is the smallest duration that any cpu process scheduling unit +will run for. Increasing this value can increase throughput of cpu +bound tasks substantially but at the expense of increased latencies +overall. Conversely decreasing it will decrease average and maximum +latencies but at the expense of throughput. This value is in +milliseconds and the default value chosen depends on the number of +cpus available at scheduler initialisation with a minimum of 6. +
+Valid values are from 1-1000.
+
+==============================================================
+
rtsig-max & rtsig-nr:

The file rtsig-max can be used to tune the maximum number
diff -uprN linux-3.6.2/drivers/cpufreq/cpufreq.c linux-3.6.2-bfs-multi-runqueue/drivers/cpufreq/cpufreq.c
--- linux-3.6.2/drivers/cpufreq/cpufreq.c 2012-10-12 22:50:59.000000000
+0200
+++ linux-3.6.2-bfs-multi-runqueue/drivers/cpufreq/cpufreq.c 2012-10-25
17:13:12.594060777 +0200
@@ -28,6 +28,7 @@
#include <linux/cpu.h>
#include <linux/completion.h>
#include <linux/mutex.h>
+#include <linux/sched.h>
#include <linux/syscore_ops.h>

#include <trace/events/power.h>
@@ -1476,6 +1477,12 @@ int __cpufreq_driver_target(struct cpufr
target_freq, relation);
if (cpu_online(policy->cpu) && cpufreq_driver->target)
retval = cpufreq_driver->target(policy, target_freq, relation);
+ if (likely(retval != -EINVAL)) {
+ if (target_freq == policy->max)
+ cpu_nonscaling(policy->cpu);
+ else
+ cpu_scaling(policy->cpu);
+ }

return retval;
}
diff -uprN linux-3.6.2/drivers/cpufreq/cpufreq_conservative.c linux-3.6.2-bfs-multi-runqueue/drivers/cpufreq/cpufreq_conservative.c
--- linux-3.6.2/drivers/cpufreq/cpufreq_conservative.c 2012-10-12
22:50:59.000000000 +0200
+++
linux-3.6.2-bfs-multi-runqueue/drivers/cpufreq/cpufreq_conservative.c

2012-10-25 17:13:12.596060777 +0200

@@ -29,8 +29,8 @@
* It helps to keep variable names smaller, simpler */

-#define DEF_FREQUENCY_UP_THRESHOLD (80)
-#define DEF_FREQUENCY_DOWN_THRESHOLD (20)
+#define DEF_FREQUENCY_UP_THRESHOLD (63)
+#define DEF_FREQUENCY_DOWN_THRESHOLD (26)

/*
* The polling frequency of this governor depends on the capability of
diff -uprN linux-3.6.2/drivers/cpufreq/cpufreq_ondemand.c linux-3.6.2-bfs-multi-runqueue/drivers/cpufreq/cpufreq_ondemand.c
--- linux-3.6.2/drivers/cpufreq/cpufreq_ondemand.c 2012-10-12
22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/drivers/cpufreq/cpufreq_ondemand.c

2012-10-25 17:13:12.595060777 +0200

@@ -28,8 +28,8 @@
* It helps to keep variable names smaller, simpler */

-#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
-#define DEF_FREQUENCY_UP_THRESHOLD (80)
+#define DEF_FREQUENCY_DOWN_DIFFERENTIAL (26)
+#define DEF_FREQUENCY_UP_THRESHOLD (63)
#define DEF_SAMPLING_DOWN_FACTOR (1)
#define MAX_SAMPLING_DOWN_FACTOR (100000)
#define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
@@ -472,10 +472,10 @@ static void dbs_check_cpu(struct cpu_dbs

/*
* Every sampling_rate, we check, if current idle time is less
- * than 20% (default), then we try to increase frequency
+ * than 37% (default), then we try to increase frequency
* Every sampling_rate, we look for a the lowest
* frequency which can sustain the load while keeping idle time over
- * 30%. If such a frequency exist, we try to decrease to this
frequency.
+ * 63%. If such a frequency exist, we try to decrease to this
frequency.
*
* Any frequency increase takes it to the maximum frequency.
* Frequency reduction happens at minimum steps of
diff -uprN linux-3.6.2/fs/proc/base.c linux-3.6.2-bfs-multi-runqueue/fs/proc/base.c
--- linux-3.6.2/fs/proc/base.c 2012-10-12 22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/fs/proc/base.c 2012-10-25
17:13:12.585060777 +0200
@@ -338,7 +338,7 @@ static int proc_pid_stack(struct seq_fil
static int proc_pid_schedstat(struct task_struct *task, char *buffer) {
return sprintf(buffer, "%llu %llu %lu\n",
- (unsigned long long)task->se.sum_exec_runtime,
+ (unsigned long long)tsk_seruntime(task),
(unsigned long long)task->sched_info.run_delay, task->sched_info.pcount);
}
diff -uprN linux-3.6.2/include/linux/init_task.h linux-3.6.2-bfs-multi-runqueue/include/linux/init_task.h
--- linux-3.6.2/include/linux/init_task.h 2012-10-12 22:50:59.000000000
+0200
+++ linux-3.6.2-bfs-multi-runqueue/include/linux/init_task.h 2012-10-25
17:13:12.585060777 +0200
@@ -141,12 +141,70 @@ extern struct task_group root_task_group

 # define INIT_PERF_EVENTS(tsk)
 #endif

-#define INIT_TASK_COMM "swapper"
-
/*
* INIT_TASK is used to set up the first task table, touch at
* your own risk!. Base=0, limit=0x1fffff (=2MB)
*/
+#ifdef CONFIG_SCHED_BFS
+#define INIT_TASK_COMM "BFS"
+#define INIT_TASK(tsk) \
+{ \
+ .state = 0, \
+ .stack = &init_thread_info, \
+ .usage = ATOMIC_INIT(2), \
+ .flags = PF_KTHREAD, \
+ .prio = NORMAL_PRIO, \
+ .static_prio = MAX_PRIO-20, \
+ .normal_prio = NORMAL_PRIO, \
+ .deadline = 0, \
+ .policy = SCHED_NORMAL, \
+ .cpus_allowed = CPU_MASK_ALL, \
+ .mm = NULL, \
+ .active_mm = &init_mm, \
+ .run_list = LIST_HEAD_INIT(tsk.run_list), \
+ .time_slice = HZ, \
+ .tasks = LIST_HEAD_INIT(tsk.tasks), \
+ INIT_PUSHABLE_TASKS(tsk) \
+ .ptraced = LIST_HEAD_INIT(tsk.ptraced), \
+ .ptrace_entry = LIST_HEAD_INIT(tsk.ptrace_entry), \
+ .real_parent = &tsk, \
+ .parent = &tsk, \
+ .children = LIST_HEAD_INIT(tsk.children), \
+ .sibling = LIST_HEAD_INIT(tsk.sibling), \
+ .group_leader = &tsk, \
+ RCU_POINTER_INITIALIZER(real_cred, &init_cred), \
+ RCU_POINTER_INITIALIZER(cred, &init_cred), \
+ .comm = INIT_TASK_COMM, \
+ .thread = INIT_THREAD, \
+ .fs = &init_fs, \
+ .files = &init_files, \
+ .signal = &init_signals, \
+ .sighand = &init_sighand, \
+ .nsproxy = &init_nsproxy, \
+ .pending = { \
+ .list = LIST_HEAD_INIT(tsk.pending.list), \
+ .signal = {{0}}}, \
+ .blocked = {{0}}, \
+ .alloc_lock = __SPIN_LOCK_UNLOCKED(tsk.alloc_lock), \
+ .journal_info = NULL, \
+ .cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
+ .pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
+ .timer_slack_ns = 50000, /* 50 usec default slack */ \
+ .pids = { \
+ [PIDTYPE_PID] = INIT_PID_LINK(PIDTYPE_PID), \
+ [PIDTYPE_PGID] = INIT_PID_LINK(PIDTYPE_PGID), \
+ [PIDTYPE_SID] = INIT_PID_LINK(PIDTYPE_SID), \
+ }, \
+ INIT_IDS \
+ INIT_PERF_EVENTS(tsk) \
+ INIT_TRACE_IRQFLAGS \
+ INIT_LOCKDEP \
+ INIT_FTRACE_GRAPH \
+ INIT_TRACE_RECURSION \
+ INIT_TASK_RCU_PREEMPT(tsk) \
+}
+#else /* CONFIG_SCHED_BFS */
+#define INIT_TASK_COMM "swapper"
#define INIT_TASK(tsk) \
{ \
.state = 0, \
@@ -211,7 +269,7 @@ extern struct task_group root_task_group
INIT_TASK_RCU_PREEMPT(tsk) \
INIT_CPUSET_SEQ \
}
-
+#endif /* CONFIG_SCHED_BFS */

#define INIT_CPU_TIMERS(cpu_timers) \
{ \
diff -uprN linux-3.6.2/include/linux/ioprio.h linux-3.6.2-bfs-multi-runqueue/include/linux/ioprio.h
--- linux-3.6.2/include/linux/ioprio.h 2012-10-12 22:50:59.000000000
+0200
+++ linux-3.6.2-bfs-multi-runqueue/include/linux/ioprio.h 2012-10-25
17:13:12.585060777 +0200
@@ -52,6 +52,8 @@ enum {
*/
static inline int task_nice_ioprio(struct task_struct *task) {
+ if (iso_task(task))
+ return 0;
return (task_nice(task) + 20) / 5;
}

diff -uprN linux-3.6.2/include/linux/jiffies.h linux-3.6.2-bfs-multi-runqueue/include/linux/jiffies.h
--- linux-3.6.2/include/linux/jiffies.h 2012-10-12 22:50:59.000000000
+0200
+++ linux-3.6.2-bfs-multi-runqueue/include/linux/jiffies.h 2012-10-25
17:13:12.593060777 +0200
@@ -173,7 +173,7 @@ static inline u64 get_jiffies_64(void)
* Have the 32 bit jiffies value wrap 5 minutes after boot
* so jiffies wrap bugs show up earlier.
*/
-#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ)) +#define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-10*HZ))

/*
* Change timeval to jiffies, trying to avoid the
diff -uprN linux-3.6.2/include/linux/sched.h linux-3.6.2-bfs-multi-runqueue/include/linux/sched.h
--- linux-3.6.2/include/linux/sched.h 2012-10-12 22:50:59.000000000
+0200
+++ linux-3.6.2-bfs-multi-runqueue/include/linux/sched.h 2012-10-25
17:13:12.587060777 +0200
@@ -37,8 +37,15 @@
#define SCHED_FIFO 1
#define SCHED_RR 2
#define SCHED_BATCH 3
-/* SCHED_ISO: reserved but not implemented yet */ +/* SCHED_ISO: Implemented on BFS only */
#define SCHED_IDLE 5
+#ifdef CONFIG_SCHED_BFS
+#define SCHED_ISO 4
+#define SCHED_IDLEPRIO SCHED_IDLE
+#define SCHED_MAX (SCHED_IDLEPRIO)
+#define SCHED_RANGE(policy) ((policy) <= SCHED_MAX)
+#endif
+
/* Can be ORed in to make sure the process is reverted back to
SCHED_NORMAL on fork */
#define SCHED_RESET_ON_FORK 0x40000000

@@ -270,8 +277,6 @@ extern asmlinkage void schedule_tail(str
extern void init_idle(struct task_struct *idle, int cpu); extern void init_idle_bootup_task(struct task_struct *idle);

-extern int runqueue_is_locked(int cpu);
-
#if defined(CONFIG_SMP) && defined(CONFIG_NO_HZ) extern void select_nohz_load_balancer(int stop_tick); extern void set_cpu_sd_state_idle(void);
@@ -1240,18 +1245,35 @@ struct task_struct {

#ifdef CONFIG_SMP
struct llist_node wake_entry;
- int on_cpu;
#endif
- int on_rq;
+#if defined(CONFIG_SMP) || defined(CONFIG_SCHED_BFS)
+ bool on_cpu;
+#endif
+#ifndef CONFIG_SCHED_BFS
+ bool on_rq;
+#endif

int prio, static_prio, normal_prio;
unsigned int rt_priority;
+#ifdef CONFIG_SCHED_BFS
+ int time_slice;
+ u64 deadline;
+ struct list_head run_list;
+ u64 last_ran;
+ u64 sched_time; /* sched_clock time spent running */
+#ifdef CONFIG_SMP
+ bool sticky; /* Soft affined flag */
+#endif
+ unsigned long rt_timeout;
+#else /* CONFIG_SCHED_BFS */
const struct sched_class *sched_class;
struct sched_entity se;
struct sched_rt_entity rt;
+
#ifdef CONFIG_CGROUP_SCHED
struct task_group *sched_task_group;
#endif
+#endif

#ifdef CONFIG_PREEMPT_NOTIFIERS
/* list of struct preempt_notifier: */
@@ -1360,6 +1382,9 @@ struct task_struct {
int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */

cputime_t utime, stime, utimescaled, stimescaled;
+#ifdef CONFIG_SCHED_BFS
+ unsigned long utime_pc, stime_pc;
+#endif
cputime_t gtime;
#ifndef CONFIG_VIRT_CPU_ACCOUNTING
cputime_t prev_utime, prev_stime;
@@ -1591,6 +1616,64 @@ struct task_struct {
#endif
};

+#ifdef CONFIG_SCHED_BFS
+bool grunqueue_is_locked(void);
+void grq_unlock_wait(void);
+void cpu_scaling(int cpu);
+void cpu_nonscaling(int cpu);
+bool above_background_load(void);
+#define tsk_seruntime(t) ((t)->sched_time)
+#define tsk_rttimeout(t) ((t)->rt_timeout)
+
+static inline void tsk_cpus_current(struct task_struct *p) +{
+}
+
+static inline int runqueue_is_locked(int cpu)
+{
+ return grunqueue_is_locked();
+}
+
+void print_scheduler_version(void);
+
+static inline bool iso_task(struct task_struct *p)
+{
+ return (p->policy == SCHED_ISO);
+}
+#else /* CFS */
+extern int runqueue_is_locked(int cpu);
+static inline void cpu_scaling(int cpu)
+{
+}
+
+static inline void cpu_nonscaling(int cpu)
+{
+}
+#define tsk_seruntime(t) ((t)->se.sum_exec_runtime)
+#define tsk_rttimeout(t) ((t)->rt.timeout)
+
+static inline void tsk_cpus_current(struct task_struct *p) +{
+ p->nr_cpus_allowed = current->nr_cpus_allowed;
+}
+
+static inline void print_scheduler_version(void)
+{
+ printk(KERN_INFO"CFS CPU scheduler.\n");
+}
+
+static inline bool iso_task(struct task_struct *p)
+{
+ return false;
+}
+
+/* Anyone feel like implementing this? */
+static inline bool above_background_load(void)
+{
+ return false;
+}
+#endif /* CONFIG_SCHED_BFS */
+
/* Future-safe accessor for struct task_struct's cpus_allowed. */ #define tsk_cpus_allowed(tsk) (&(tsk)->cpus_allowed)

@@ -1608,10 +1691,20 @@ struct task_struct {
*/

#define MAX_USER_RT_PRIO 100
-#define MAX_RT_PRIO MAX_USER_RT_PRIO
+#define MAX_RT_PRIO (MAX_USER_RT_PRIO + 1)
+#define DEFAULT_PRIO (MAX_RT_PRIO + 20)

+#ifdef CONFIG_SCHED_BFS
+#define PRIO_RANGE (40)
+#define MAX_PRIO (MAX_RT_PRIO + PRIO_RANGE)
+#define ISO_PRIO (MAX_RT_PRIO)
+#define NORMAL_PRIO (MAX_RT_PRIO + 1)
+#define IDLE_PRIO (MAX_RT_PRIO + 2)
+#define PRIO_LIMIT ((IDLE_PRIO) + 1)
+#else /* CONFIG_SCHED_BFS */
#define MAX_PRIO (MAX_RT_PRIO + 40)
-#define DEFAULT_PRIO (MAX_RT_PRIO + 20)
+#define NORMAL_PRIO DEFAULT_PRIO
+#endif /* CONFIG_SCHED_BFS */

static inline int rt_prio(int prio)
{
@@ -1989,7 +2082,7 @@ extern unsigned long long
task_sched_runtime(struct task_struct *task);

/* sched_exec is called by processes performing an exec */
-#ifdef CONFIG_SMP
+#if defined(CONFIG_SMP) && !defined(CONFIG_SCHED_BFS)
extern void sched_exec(void);
#else
#define sched_exec() {}
@@ -2705,7 +2798,7 @@ static inline unsigned int task_cpu(cons
return 0;
}

-static inline void set_task_cpu(struct task_struct *p, unsigned int cpu)
+static inline void set_task_cpu(struct task_struct *p, int cpu)
{
}

diff -uprN linux-3.6.2/init/Kconfig
linux-3.6.2-bfs-multi-runqueue/init/Kconfig
--- linux-3.6.2/init/Kconfig 2012-10-12 22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/init/Kconfig 2012-10-25
17:13:12.588060777 +0200
@@ -32,6 +32,19 @@ config BUILDTIME_EXTABLE_SORT

menu "General setup"

+config SCHED_BFS
+ bool "BFS cpu scheduler"
+ ---help---
+ The Brain Fuck CPU Scheduler for excellent interactivity and
+ responsiveness on the desktop and solid scalability on normal
+ hardware. Not recommended for 4096 CPUs.
+
+ Currently incompatible with the Group CPU scheduler, and RCU TORTURE
+ TEST so these options are disabled.
+
+ Say Y here.
+ default y
+
config EXPERIMENTAL
bool "Prompt for development and/or incomplete code/drivers" ---help---
@@ -676,6 +689,7 @@ config PROC_PID_CPUSET

config CGROUP_CPUACCT
bool "Simple CPU accounting cgroup subsystem"
+ depends on !SCHED_BFS
help
Provides a simple Resource Controller for monitoring the total CPU consumed by the tasks in a cgroup.
@@ -778,6 +792,7 @@ config CGROUP_PERF

menuconfig CGROUP_SCHED
bool "Group CPU scheduler"
+ depends on !SCHED_BFS
default n
help
This feature lets CPU scheduler recognize task groups and control
CPU
@@ -1042,6 +1057,7 @@ config UIDGID_STRICT_TYPE_CHECKS

config SCHED_AUTOGROUP
bool "Automatic process group scheduling"
+ depends on !SCHED_BFS
select EVENTFD
select CGROUPS
select CGROUP_SCHED
@@ -1426,38 +1442,8 @@ config COMPAT_BRK

On non-ancient distros (post-2000 ones) N is usually a safe choice.

-choice
- prompt "Choose SLAB allocator"
- default SLUB
- help
- This option allows to select a slab allocator.
-
-config SLAB
- bool "SLAB"
- help

-	  The regular slab allocator that is established and known to work
-	  well in all environments. It organizes cache hot objects in
-	  per cpu and per node queues.

-
config SLUB
- bool "SLUB (Unqueued Allocator)"
- help

-	   SLUB is a slab allocator that minimizes cache line usage
-	   instead of managing queues of cached objects (SLAB approach).
-	   Per cpu caching is realized using slabs of objects instead
-	   of queues of objects. SLUB can use memory efficiently
-	   and has enhanced diagnostics. SLUB is the default choice for
-	   a slab allocator.

-
-config SLOB
- depends on EXPERT
- bool "SLOB (Simple Allocator)"
- help

-	   SLOB replaces the stock allocator with a drastically simpler
-	   allocator. SLOB is generally more space efficient but
-	   does not perform as well on large systems.

-
-endchoice
+ def_bool y

config MMAP_ALLOW_UNINITIALIZED
bool "Allow mmapped anonymous memory to be uninitialized"
diff -uprN linux-3.6.2/init/main.c linux-3.6.2-bfs-multi-runqueue/init/main.c
--- linux-3.6.2/init/main.c 2012-10-12 22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/init/main.c 2012-11-19
16:14:36.759575528 +0100
@@ -701,7 +701,6 @@ int __init_or_module do_one_initcall(ini
return ret;
}

-
extern initcall_t __initcall_start[];
extern initcall_t __initcall0_start[];
extern initcall_t __initcall1_start[];
@@ -809,6 +808,8 @@ static noinline int init_post(void)
current->signal->flags |= SIGNAL_UNKILLABLE; flush_delayed_fput();

+ print_scheduler_version();
+
if (ramdisk_execute_command) {
run_init_process(ramdisk_execute_command); printk(KERN_WARNING "Failed to execute %s\n",
@@ -857,10 +858,8 @@ static int __init kernel_init(void * unu
cad_pid = task_pid(current);

smp_prepare_cpus(setup_max_cpus);
-
do_pre_smp_initcalls();
lockup_detector_init();
-
smp_init();
sched_init_smp();

diff -uprN linux-3.6.2/kernel/cpu.c linux-3.6.2-bfs-multi-runqueue/kernel/cpu.c
--- linux-3.6.2/kernel/cpu.c 2012-10-12 22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/kernel/cpu.c 2012-11-22
18:10:05.921368389 +0100
@@ -285,11 +285,9 @@ static int __ref _cpu_down(unsigned int
if (err) {
/* CPU didn't die: tell everyone. Can't complain. */ cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
-
goto out_release;
}
BUG_ON(cpu_online(cpu));
-
/*
* The migration_call() CPU_DYING callback will have removed all
* runnable tasks from the cpu, there's only the idle task left now
@@ -398,7 +396,6 @@ int __cpuinit cpu_up(unsigned int cpu)
#endif
return -EINVAL;
}
-
#ifdef CONFIG_MEMORY_HOTPLUG
nid = cpu_to_node(cpu);
if (!node_online(nid)) {
@@ -406,7 +403,6 @@ int __cpuinit cpu_up(unsigned int cpu)
if (err)
return err;
}
-
pgdat = NODE_DATA(nid);
if (!pgdat) {
printk(KERN_ERR
diff -uprN linux-3.6.2/kernel/delayacct.c linux-3.6.2-bfs-multi-runqueue/kernel/delayacct.c
--- linux-3.6.2/kernel/delayacct.c 2012-10-12 22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/kernel/delayacct.c 2012-10-25
17:13:12.589060777 +0200
@@ -130,7 +130,7 @@ int __delayacct_add_tsk(struct taskstats
*/
t1 = tsk->sched_info.pcount;
t2 = tsk->sched_info.run_delay;
- t3 = tsk->se.sum_exec_runtime;
+ t3 = tsk_seruntime(tsk);

d->cpu_count += t1;

diff -uprN linux-3.6.2/kernel/exit.c linux-3.6.2-bfs-multi-runqueue/kernel/exit.c
--- linux-3.6.2/kernel/exit.c 2012-10-12 22:50:59.000000000 +0200
+++ linux-3.6.2-bfs-multi-runqueue/kernel/exit.c 2012-10-25
17:13:12.590060777 +0200
@@ -145,7 +145,7 @@ static void __exit_signal(struct task_st
sig->inblock += task_io_get_inblock(tsk); sig->oublock += task_io_get_oublock(tsk); task_io_accounting_add(&sig->ioac, &tsk->ioac);
- sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
+ sig->sum_sched_runtime += tsk_seruntime(tsk);
}

sig->nr_threads--;
diff -uprN linux-3.6.2/kernel/posix-cpu-timers.c linux-3.6.2-bfs-multi-runqueue/kernel/posix-cpu-timers.c
--- linux-3.6.2/kernel/posix-cpu-timers.c 2012-10-12 22:50:59.000000000
+0200
+++ linux-3.6.2-bfs-multi-runqueue/kernel/posix-cpu-timers.c 2012-10-25
17:13:12.591060777 +0200
@@ -495,7 +495,7 @@ static void cleanup_timers(struct list_h
void posix_cpu_timers_exit(struct task_struct *tsk) {
cleanup_timers(tsk->cpu_timers,
- tsk->utime, tsk->stime, tsk->se.sum_exec_runtime);
+ tsk->utime, tsk->stime, tsk_seruntime(tsk));

}
void posix_cpu_timers_exit_group(struct task_struct *tsk)
@@ -504,7 +504,7 @@ void posix_cpu_timers_exit_group(struct

cleanup_timers(tsk->signal->cpu_timers,
tsk->utime + sig->utime, tsk->stime + sig->stime,
- tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
+ tsk_seruntime(tsk) + sig->sum_sched_runtime);
}

static void clear_dead_task(struct k_itimer *timer, union
cpu_time_count now)
@@ -934,7 +934,7 @@ static void check_thread_timers(struct t
struct cpu_timer_list *t = list_first_entry(timers,
struct cpu_timer_list, entry);

-		if (!--maxfire || tsk->se.sum_exec_runtime < t->expires.sched) {
+		if (!--maxfire || tsk_seruntime(tsk) < t->expires.sched) {

tsk->cputime_expires.sched_exp = t->expires.sched; break;
}
@@ -951,7 +951,7 @@ static void check_thread_timers(struct t
ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);

if (hard != RLIM_INFINITY &&
- tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
+ tsk_rttimeout(tsk) > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
/*
* At the hard limit, we just die.
* No need to calculate anything else now.
@@ -959,7 +959,7 @@ static void check_thread_timers(struct t
__group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); return;
}
- if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
+ if (tsk_rttimeout(tsk) > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
/*
* At the soft limit, send a SIGXCPU every second. */
@@ -1252,7 +1252,7 @@ static inline int fastpath_timer_check(s
struct task_cputime task_sample = {
.utime = tsk->utime,
.stime = tsk->stime,
- .sum_exec_runtime = tsk->se.sum_exec_runtime
+ .sum_exec_runtime = tsk_seruntime(tsk)
};

if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
diff -uprN linux-3.6.2/kernel/sched/bfs.c linux-3.6.2-bfs-multi-runqueue/kernel/sched/bfs.c
--- linux-3.6.2/kernel/sched/bfs.c 1970-01-01 01:00:00.000000000 +0100
+++ linux-3.6.2-bfs-multi-runqueue/kernel/sched/bfs.c 2012-12-15
16:25:58.651400017 +0100
@@ -0,0 +1,8156 @@
+/*
+ * kernel/sched_bfs.c, was sched.c
+ *
+ * Kernel scheduler and related syscalls
+ *
+ * Copyright (C) 1991-2002 Linus Torvalds
+ *
+ * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
+ * make semaphores SMP safe
+ * 1998-11-19 Implemented schedule_timeout() and related stuff
+ * by Andrea Arcangeli
+ * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
+ * hybrid priority-list and round-robin design with
+ * an array-switch method of distributing timeslices
+ * and per-CPU runqueues. Cleanups and useful suggestions
+ * by Davide Libenzi, preemptible kernel bits by Robert Love.

+ *  2003-09-03	Interactivity tuning by Con Kolivas.
+ *  2004-04-02	Scheduler domains code by Nick Piggin
+ *  2007-04-15  Work begun on replacing all interactivity tuning with a
+ *              fair scheduling design by Con Kolivas.
+ *  2007-05-05  Load balancing (smp-nice) and other improvements
+ *              by Peter Williams
+ *  2007-05-06  Interactivity improvements to CFS by Mike Galbraith
+ *  2007-07-01  Group scheduling enhancements by Srivatsa Vaddagiri
+ *  2007-11-29  RT balancing improvements by Steven Rostedt, Gregory

Haskins,
+ * Thomas Gleixner, Mike Kravetz
+ * now Brainfuck deadline scheduling policy by Con Kolivas deletes
+ * a whole lot of those previous things.
+ * Support for multiple runqueues by Matthias Kohler
+ */
+
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/nmi.h>
+#include <linux/init.h>
+#include <linux/init_task.h>
+#include <asm/uaccess.h>
+#include <linux/highmem.h>
+#include <asm/mmu_context.h>
+#include <linux/interrupt.h>
+#include <linux/capability.h>
+#include <linux/completion.h>
+#include <linux/kernel_stat.h>
+#include <linux/debug_locks.h>
+#include <linux/perf_event.h>
+#include <linux/security.h>
+#include <linux/notifier.h>
+#include <linux/profile.h>
+#include <linux/freezer.h>
+#include <linux/vmalloc.h>
+#include <linux/blkdev.h>
+#include <linux/delay.h>
+#include <linux/smp.h>
+#include <linux/threads.h>
+#include <linux/timer.h>
+#include <linux/rcupdate.h>
+#include <linux/cpu.h>
+#include <linux/cpuset.h>
+#include <linux/cpumask.h>
+#include <linux/percpu.h>
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+#include <linux/syscalls.h>
+#include <linux/times.h>
+#include <linux/tsacct_kern.h>
+#include <linux/kprobes.h>
+#include <linux/delayacct.h>
+#include <linux/log2.h>
+#include <linux/bootmem.h>
+#include <linux/ftrace.h>
+#include <linux/slab.h>
+#include <linux/binfmts.h>
+
+#include <asm/switch_to.h>
+#include <asm/tlb.h>
+#include <asm/unistd.h>
+#include <asm/mutex.h>
+#ifdef CONFIG_PARAVIRT
+#include <asm/paravirt.h>
+#endif
+
+#include "cpupri.h"
+#include "../workqueue_sched.h"
+#include "../smpboot.h"
+
+#define CREATE_TRACE_POINTS
+#include <trace/events/sched.h>
+
+#define rt_prio(prio) unlikely((prio) < MAX_RT_PRIO)
+#define rt_task(p) rt_prio((p)->prio)
+#define rt_queue(rq) rt_prio((rq)->rq_prio)
+#define batch_task(p) (unlikely((p)->policy == SCHED_BATCH))
+#define is_rt_policy(policy) ((policy) == SCHED_FIFO || \
+ (policy) == SCHED_RR)
+#define has_rt_policy(p) unlikely(is_rt_policy((p)->policy))
+#define idleprio_task(p) unlikely((p)->policy == SCHED_IDLEPRIO)
+#define iso_task(p) unlikely((p)->policy == SCHED_ISO)
+#define iso_queue(rq) unlikely((rq)->rq_policy == SCHED_ISO)
+#define ISO_PERIOD ((5 * HZ * grq.noc) + 1)
+
+/*
+ * Convert user-nice values [ -20 ... 0 ... 19 ]
+ * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
+ * and back.
+ */
+#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
+#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
+#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
+
+/*
+ * 'User priority' is the nice value converted to something we
+ * can work with better when scaling various scheduler parameters,
+ * it's a [ 0 ... 39 ] range.
+ */
+#define USER_PRIO(p) ((p) - MAX_RT_PRIO)
+#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
+#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
+#define SCHED_PRIO(p) ((p) + MAX_RT_PRIO)
+#define STOP_PRIO (MAX_RT_PRIO - 1)
+
+/*
+ * Some helpers for converting to/from various scales. Use shifts to get
+ * approximate multiples of ten for less overhead.
+ */
+#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
+#define JIFFY_NS (1000000000 / HZ)
+#define HALF_JIFFY_NS (1000000000 / HZ / 2)
+#define HALF_JIFFY_US (1000000 / HZ / 2)
+#define MS_TO_NS(TIME) ((TIME) << 20)
+#define MS_TO_US(TIME) ((TIME) << 10)
+#define NS_TO_MS(TIME) ((TIME) >> 20)
+#define NS_TO_US(TIME) ((TIME) >> 10)
+
+#define RESCHED_US (100) /* Reschedule if less than this many μs left
*/
+
+void print_scheduler_version(void)
+{
+}
+
+/*
+ * This is the time all tasks within the same priority round robin.
+ * Value is in ms and set to a minimum of 6ms. Scales with number of cpus.
+ * Tunable via /proc interface.
+ */
+int rr_interval __read_mostly = 6;
+
+/*
+ * sched_iso_cpu - sysctl which determines the cpu percentage SCHED_ISO tasks
+ * are allowed to run five seconds as real time tasks. This is the total over
+ * all online cpus.
+ */
+int sched_iso_cpu __read_mostly = 70;
+
+/*
+ * The relative length of deadline for each priority(nice) level.
+ */
+static int prio_ratios[PRIO_RANGE] __read_mostly;
+
+/*
+ * The quota handed out to tasks of all priority levels when refilling their
+ * time_slice.
+ */
+static inline int timeslice(void)
+{
+ return MS_TO_US(rr_interval);
+}
+
+/*
+ * The global runqueue data that all CPUs work off. Data is protected either
+ * by the global grq lock, or the discrete lock that precedes the data in this
+ * struct.
+ */
+
+struct global_rq {
+ raw_spinlock_t lock;
+ unsigned long nr_running;
+ unsigned long nr_uninterruptible;
+ unsigned long long nr_switches;
+ struct list_head queue[PRIO_LIMIT];
+ DECLARE_BITMAP(prio_bitmap, PRIO_LIMIT + 1);
+#ifdef CONFIG_SMP
+ unsigned long qnr; /* queued not running */
+ cpumask_t cpu_idle_map;
+ bool idle_cpus;
+ cpumask_t cpu_span;
+#endif
+ int noc; /* num_online_cpus stored and updated when it changes */
+ u64 niffies; /* Nanosecond jiffies */
+ unsigned long last_jiffy; /* Last jiffy we updated niffies */
+
+ raw_spinlock_t iso_lock;
+ int iso_ticks;
+ int iso_refractory;
+
+ /*
+ * for the initial global runqueue this is set to false
+ * to ensure it does not get freed
+ */
+
+ bool is_freeable;
+};
+
+static void init_a_grq(struct global_rq *const this_grq,

+		       int const n_online_cpus,
+		       struct cpumask const *this_span,
+		       struct global_rq const *const old_grq)

+{
+ int i;
+
+ raw_spin_lock_init(&(this_grq->lock));
+ this_grq->nr_running =
+ this_grq->nr_uninterruptible =
+ this_grq->nr_switches = 0;
+ if (old_grq == NULL) {
+ this_grq->niffies = 0;
+ this_grq->last_jiffy = jiffies;
+ } else {
+ this_grq->niffies = old_grq->niffies;
+ this_grq->last_jiffy = old_grq->last_jiffy;
+ }
+ raw_spin_lock_init(&(this_grq->iso_lock));
+ this_grq->iso_ticks = this_grq->iso_refractory = 0;
+ this_grq->noc = n_online_cpus;
+#ifdef CONFIG_SMP
+ this_grq->qnr = this_grq->idle_cpus = 0;
+ cpumask_clear(&(this_grq->cpu_idle_map));
+ cpumask_copy(&this_grq->cpu_span, this_span);
+ this_grq->is_freeable = true;
+#endif
+
+ for (i = 0; i < PRIO_LIMIT; i++)
+ INIT_LIST_HEAD(this_grq->queue + i);
+ /* delimiter for bitsearch */
+ __set_bit(PRIO_LIMIT, this_grq->prio_bitmap);
+}
+
+/*
+ * The initial global runqueue, this gets never freed
+ */
+
+#ifdef CONFIG_SMP
+
+/*
+ * We add the notion of a root-domain which will be used to define per-domain
+ * variables. Each exclusive cpuset essentially defines an island domain by
+ * fully partitioning the member cpus from any other cpuset. Whenever a new
+ * exclusive cpuset is created, we also create and attach a new root-domain
+ * object.
+ *
+ */
+struct root_domain {
+ atomic_t refcount;
+ atomic_t rto_count;
+ struct rcu_head rcu;
+ cpumask_var_t span;
+ cpumask_var_t online;
+
+ /*
+ * The "RT overload" flag: it gets set if a CPU has more than
+ * one runnable RT task.
+ */
+ cpumask_var_t rto_mask;
+ struct cpupri cpupri;
+};
+
+/*
+ * By default the system creates a single root-domain with all cpus as
+ * members (mimicking the global state we have today).
+ */
+static struct root_domain def_root_domain;
+
+#endif /* CONFIG_SMP */
+
+/* There can be only one */
+
+/*
+ * A per_cpu field for fast lookup, which gloabl_runqueue is
+ * associated with which cpu
+ */
+static DEFINE_PER_CPU(struct global_rq *, global_runqueues); +
+/*
+ * This field tranlates global-runqeue numbers to pointers
+ * to them.
+ * if(per_cpu(global_runqueues, number_of_grq) == NULL) :
+ * The global runqueue with the number number_of_grq
+ * does _not_ exist.
+ * if(per_cpu(global_runqueues, number_of_grq) != NULL) :
+ * The global runqueue with the number number_of_grq
+ * does exist.
+ */
+static DEFINE_PER_CPU(struct global_rq *, grq_nr_lookup_table); +
+/*
+ * 1 is the minimal value, because there has always to be grq.
+ * We set it here to one, when the first cpu is brought up,
+ * it is incremented.
+ */
+static int nr_grq;
+
+/*
+ * When scheduling is in progress, this read-write lock gets
+ * read_locked, to ensure no one is modifying the layout
+ * of the global runqueues. This allows many accesses to
+ * the global runqueues. Each global runqueue has its own
+ * spinlock which is also taken when scheduling is in progress.
+ * If the layout of the global runqueues is changed,
+ * this lock needs to be write_locked, this ensures that
+ * nothing is accesssing the global runqueues or scheduling
+ * taken place.
+ */
+static DEFINE_RWLOCK(grq_layout_rwlock);
+
+#define grq (*__get_cpu_var(global_runqueues))
+
+/*
+ * This is the main, per-CPU runqueue data structure.
+ * This data should only be modified by the local cpu.
+ */
+struct rq {
+#ifdef CONFIG_SMP
+#ifdef CONFIG_NO_HZ
+ u64 nohz_stamp;
+ unsigned char in_nohz_recently;
+#endif
+#endif
+
+ struct task_struct *curr, *idle, *stop;
+ struct mm_struct *prev_mm;
+
+ /* Stored data about rq->curr to work outside grq lock */
+ u64 rq_deadline;
+ unsigned int rq_policy;
+ int rq_time_slice;
+ u64 rq_last_ran;
+ int rq_prio;
+ bool rq_running; /* There is a task running */
+
+ /* Accurate timekeeping data */
+ u64 timekeep_clock;
+ unsigned long user_pc, nice_pc, irq_pc, softirq_pc, system_pc,
+ iowait_pc, idle_pc;
+ long account_pc;
+ atomic_t nr_iowait;
+
+#ifdef CONFIG_SMP
+ int cpu; /* cpu of this runqueue */
+ bool online;
+ bool scaling; /* This CPU is managed by a scaling CPU freq governor */
+
+ struct root_domain *rd;
+ struct sched_domain *sd;
+ int *cpu_locality; /* CPU relative cache distance */
+#ifdef CONFIG_SCHED_SMT
+ bool (*siblings_idle)(int cpu);
+ /* See if all smt siblings are idle */
+ cpumask_t smt_siblings;
+#endif
+#ifdef CONFIG_SCHED_MC
+ bool (*cache_idle)(int cpu);
+ /* See if all cache siblings are idle */
+ cpumask_t cache_siblings;
+#endif
+ u64 last_niffy; /* Last time this RQ updated grq.niffies */
+#endif
+#ifdef CONFIG_IRQ_TIME_ACCOUNTING
+ u64 prev_irq_time;
+#endif
+#ifdef CONFIG_PARAVIRT
+ u64 prev_steal_time;
+#endif
+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
+ u64 prev_steal_time_rq;
+#endif
+
+ u64 clock, old_clock, last_tick;
+ u64 clock_task;
+ bool dither;
+
+#ifdef CONFIG_SCHEDSTATS
+
+ /* latency stats */
+ struct sched_info rq_sched_info;
+ unsigned long long rq_cpu_time;
+ /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
+
+ /* sys_sched_yield() stats */
+ unsigned int yld_count;
+
+ /* schedule() stats */
+ unsigned int sched_switch;
+ unsigned int sched_count;
+ unsigned int sched_goidle;
+
+ /* try_to_wake_up() stats */
+ unsigned int ttwu_count;
+ unsigned int ttwu_local;
+#endif
+};
+
+DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); +static DEFINE_MUTEX(sched_hotcpu_mutex);
+
+#ifdef CONFIG_SMP
+/*
+ * sched_domains_mutex serialises calls to init_sched_domains,
+ * detach_destroy_domains and partition_sched_domains.
+ */
+static DEFINE_MUTEX(sched_domains_mutex);
+
+/*
+static void grq_cpu_offline(struct global_rq *__grq, int cpu) +{
+ cpu_clear(cpu, __grq->cpu_span);
+ __grq->noc = cpumask_weight(&__grq->cpu_span);
+}*/
+
+
+/*
+ * @__grq pointer to a global runqueue that may be unused
+ * The function checks if this is possible
+ * If it is possible the function will free the global runqueue
+ * enter with the grq_layout_rwlock write_locked
+ */
+
+static struct global_rq *cpu_grq(int const cpu);
+static noinline void double_grq_lock(struct global_rq *grq1,
+ struct global_rq *grq2);
+static void double_grq_unlock(struct global_rq *grq1,
+ struct global_rq *grq2);
+static void dequeue_task(struct task_struct *p, struct global_rq *__grq);
+static void enqueue_task(struct task_struct *p, struct global_rq *__grq);
+static struct global_rq *task_grq(struct task_struct *p);
+
+static void free_grq(struct global_rq *__grq)
+{
+ int cpu_tmp;
+ int idx;
+ struct global_rq *fallback_grq;
+ struct list_head *dead_queue;
+ struct task_struct *swap;
+ struct task_struct *p, *t;
+
+ if (!__grq)
+ return;
+
+ /*
+ * Only if the cpumask_t span of global runqueue in
+ * question is empty the runqueue may be freed
+ */
+ if (!cpumask_empty(&(__grq->cpu_span)))
+ return;
+
+ /*
+ * There can not be less than one grq
+ */
+ if(nr_grq == 1)
+ return;
+
+ /*
+ * cpu0 always online? FIXME
+ */
+ fallback_grq = cpu_grq(0);
+
+ /*
+ * This is not necassary, because we hold the grq_layout_rwlock
+ * writelocked, but silences lockdep
+ */
+ double_grq_lock(__grq, fallback_grq);
+reinit:
+ idx = 0;
+ idx = find_next_bit(__grq->prio_bitmap, PRIO_LIMIT, idx);
+ if(idx >= PRIO_LIMIT)
+ goto unlock;
+
+ dead_queue = __grq->queue + idx;
+
+ list_for_each_entry(swap, dead_queue, run_list) {
+ dequeue_task(swap, __grq);
+ enqueue_task(swap, fallback_grq);
+ set_task_cpu(swap, cpumask_first(&fallback_grq->cpu_span));
+ goto reinit;
+ }
+
+ do_each_thread(t, p) {
+ if(task_grq(p) == __grq)
+ BUG();
+ } while_each_thread(t, p);
+
+unlock:
+ double_grq_unlock(__grq, fallback_grq);
+
+ kfree(__grq);
+ nr_grq--;
+ /*
+ * Make shure the grg number to grq pointer lookup table
+ * doesn't reference the dead grq any more
+ */
+ for_each_possible_cpu(cpu_tmp) {
+ if (per_cpu(grq_nr_lookup_table, cpu_tmp) == __grq)
+ per_cpu(grq_nr_lookup_table, cpu_tmp) = NULL;
+ }
+ return;
+}
+
+/*
+ * This function allocates a new global runqueue,
+ * initializes it and increments the nr_grq.
+ * Then returns a pointer to it
+ */
+
+static struct noinline global_rq * new_grq(struct global_rq const *const old_grq)
+{
+ struct global_rq *new_grq;
+ new_grq = kzalloc(sizeof(struct global_rq), GFP_ATOMIC);
+ if (!new_grq) {
+ printk(KERN_ALERT"Failed");
+ return NULL;
+ }
+
+ init_a_grq(new_grq, 1, cpu_online_mask, old_grq);
+ cpumask_clear(&(new_grq->cpu_span));
+ nr_grq++;
+ return new_grq;
+}
+
+/*
+ * @cpu: The number if the cpu from 0 to NR_CPUS
+ * @n_new_grq: The number of the global runqueue
+ * from 0 ( initial global runqueue
+ * to NR_CPUS
+ * This function does all the necasserry stuff to
+ * bind a cpu to a runqueue.
+ */
+void print_grqs(void);
+
+static noinline int associate_grq_cpu(unsigned int cpu, unsigned int n_new_grq)
+{
+ struct global_rq *_old_grq;
+ struct global_rq *_new_grq;
+
+ if (!cpu_online(cpu)) {
+ return 0;
+ }
+ if (n_new_grq > NR_CPUS) {
+ return 0;
+ }
+
+ _old_grq = per_cpu(global_runqueues, cpu);
+ _new_grq = per_cpu(grq_nr_lookup_table, n_new_grq);
+
+ if (!_new_grq) {
+ _new_grq = new_grq(_old_grq);
+ if (!_new_grq)
+ return 0;
+ per_cpu(grq_nr_lookup_table, n_new_grq) = _new_grq;
+ }
+
+
+
+ /*
+ * unset the @cpu from the old grq and
+ * actualise the noc
+ * _old_grq can be NULL when the first cpu is brought up,
+ * global_runqueues and grq_nr_lookup_table get both
+ * initialized to NULL in sched_init
+ */
+ if (_old_grq != NULL) {
+ cpu_clear(cpu, _old_grq->cpu_span);
+ _old_grq->noc = cpumask_weight(&_old_grq->cpu_span);
+ }
+ /*
+ * set @cpu in the new grq and
+ * actualise noc
+ */
+ cpu_set(cpu, _new_grq->cpu_span);
+ _new_grq->noc = cpumask_weight(&_new_grq->cpu_span);
+
+
+ /*
+ * Set the new grq in the big grq lookup table
+ */
+ per_cpu(global_runqueues, cpu) = _new_grq;
+
+
+ /* Call this to avoid mem leaks */
+ free_grq(_old_grq);
+
+ print_grqs();
+
+ return 0;
+}
+
+/*
+ * By default the system creates a single root-domain with all cpus as
+ * members (mimicking the global state we have today).
+ */
+static struct root_domain def_root_domain;
+
+int __weak arch_sd_sibling_asym_packing(void)
+{
+ return 0*SD_ASYM_PACKING;
+}
+#endif
+
+#define rcu_dereference_check_sched_domain(p) \
+ rcu_dereference_check((p), \
+ lockdep_is_held(&sched_domains_mutex))
+
+/*
+ * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
+ * See detach_destroy_domains: synchronize_sched for details.
+ *
+ * The domain tree of any CPU may only be accessed from within
+ * preempt-disabled sections.
+ */
+#define for_each_domain(cpu, __sd) \
+ for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); __sd;
__sd = __sd->parent)
+
+static void update_rq_clock(struct rq *rq);
+
+/*
+ * Sanity check should sched_clock return bogus values. We make sure it does
+ * not appear to go backwards, and use jiffies to determine the maximum and
+ * minimum it could possibly have increased, and round down to the nearest
+ * jiffy when it falls outside this.
+ */
+static inline void niffy_diff(s64 *niff_diff, int jiff_diff) +{
+ unsigned long min_diff, max_diff;
+
+ if (jiff_diff > 1)
+ min_diff = JIFFIES_TO_NS(jiff_diff - 1);
+ else
+ min_diff = 1;
+ /* Round up to the nearest tick for maximum */
+ max_diff = JIFFIES_TO_NS(jiff_diff + 1);
+
+ if (unlikely(*niff_diff < min_diff || *niff_diff > max_diff))
+ *niff_diff = min_diff;
+}
+
+#ifdef CONFIG_SMP
+#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
+#define this_rq() (&__get_cpu_var(runqueues))
+#define task_rq(p) cpu_rq(task_cpu(p))
+#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
+static inline int cpu_of(struct rq *rq)
+{
+ return rq->cpu;
+}
+
+/*
+ * Niffies are a globally increasing nanosecond counter. Whenever a runqueue
+ * clock is updated with the grq.lock held, it is an opportunity to update the
+ * niffies value. Any CPU can update it by adding how much its clock has
+ * increased since it last updated niffies, minus any added niffies by other
+ * CPUs.
+ */
+
+static struct global_rq *rq_grq(struct rq const * const rq); +
+static noinline void update_clocks(struct rq *rq, struct global_rq *__grq)
+{
+ s64 ndiff;
+ long jdiff;
+
+ update_rq_clock(rq);
+ ndiff = rq->clock - rq->old_clock;
+ /* old_clock is only updated when we are updating niffies */
+ rq->old_clock = rq->clock;
+ ndiff -= __grq->niffies - rq->last_niffy;
+ jdiff = jiffies - __grq->last_jiffy;
+ niffy_diff(&ndiff, jdiff);
+ __grq->last_jiffy += jdiff;
+ __grq->niffies += ndiff;
+ rq->last_niffy = __grq->niffies;
+}
+#else /* CONFIG_SMP */
+static struct rq *uprq;
+#define cpu_rq(cpu) (uprq)
+#define this_rq() (uprq)
+#define task_rq(p) (uprq)
+#define cpu_curr(cpu) ((uprq)->curr)
+static inline int cpu_of(struct rq *rq)
+{
+ return 0;
+}
+
+static inline void update_clocks(struct rq *rq)
+{
+ s64 ndiff;
+ long jdiff;
+
+ update_rq_clock(rq);
+ ndiff = rq->clock - rq->old_clock;
+ rq->old_clock = rq->clock;
+ jdiff = jiffies - grq.last_jiffy;
+ niffy_diff(&ndiff, jdiff);
+ grq.last_jiffy += jdiff;
+ grq.niffies += ndiff;
+}
+#endif
+#define raw_rq() (&__raw_get_cpu_var(runqueues))
+
+#include "stats.h"
+
+#ifndef prepare_arch_switch
+# define prepare_arch_switch(next) do { } while (0)
+#endif
+#ifndef finish_arch_switch
+# define finish_arch_switch(prev) do { } while (0)
+#endif
+#ifndef finish_arch_post_lock_switch
+# define finish_arch_post_lock_switch() do { } while (0)
+#endif
+
+/*
+ * All common locking functions performed on grq.lock. rq->clock is local to
+ * the CPU accessing it so it can be modified just with interrupts disabled
+ * when we're not updating niffies.
+ * Looking up task_rq must be done under grq.lock to be safe.
+ */
+static void update_rq_clock_task(struct rq *rq, s64 delta); +
+static void update_rq_clock(struct rq *rq)
+{
+ s64 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
+
+ lockdep_assert_held(&grq_layout_rwlock);
+
+ rq->clock += delta;
+ update_rq_clock_task(rq, delta);
+}
+
+static bool task_running(struct task_struct *p)
+{
+ return p->on_cpu;
+}
+
+static struct global_rq *cpu_grq(int const cpu)
+{
+ return per_cpu(global_runqueues, cpu);
+}
+
+static struct global_rq *rq_grq(struct rq const * const rq) +{
+ return cpu_grq(rq->cpu);
+}
+
+static struct global_rq *grq_number_to_grq(int const number) +{
+ return per_cpu(grq_nr_lookup_table, number);
+}
+
+static struct global_rq *task_grq(struct task_struct *p)
+{
+ return cpu_grq(task_cpu(p));
+}
+
+static void sched_set_tsk_need_resched(struct task_struct *p) +{
+ assert_raw_spin_locked(&task_grq(p)->lock);
+ lockdep_assert_held(&task_grq(p)->lock);
+ set_tsk_need_resched(p);
+}
+
+/*
+ * __task_grq_lock - lock the rq @p resides on. Doesnt lock p->pi
+ */
+static struct rq *__task_grq_lock(struct task_struct *p)
+ __acquires(__grq->lock)
+{
+ struct rq *rq;
+ struct global_rq *__grq;
+
+ lockdep_assert_held(&grq_layout_rwlock);
+ lockdep_assert_held(&p->pi_lock);
+ BUG_ON(!irqs_disabled());
+
+ for (;;) {
+ rq = task_rq(p);
+ __grq = rq_grq(rq);
+ raw_spin_lock(&__grq->lock);
+ if (likely(rq == task_rq(p)))
+ return rq;
+ raw_spin_unlock(&__grq->lock);
+ }
+}
+
+
+/*
+ * task_grq_lock - lock p->pi_lock and lock the rq @p resides on.
+ */
+static struct rq *task_grq_lock(struct task_struct *p, unsigned long *flags)
+ __acquires(p->pi_lock)
+ __acquires(__grq->lock)
+ __acquires(grq_layout_rwlock)
+{
+ struct rq *rq;
+ struct global_rq *__grq;
+
+ read_lock_irqsave(&grq_layout_rwlock, *flags);
+
+ for (;;) {
+ raw_spin_lock(&p->pi_lock);
+ rq = task_rq(p);
+ __grq = rq_grq(rq);
+ raw_spin_lock(&__grq->lock);
+ if (likely(rq == task_rq(p)))
+ return rq;
+ raw_spin_unlock(&__grq->lock);
+ raw_spin_unlock(&p->pi_lock);
+ }
+}
+
+static void
+task_grq_unlock(struct rq *rq, struct task_struct *p,
+ unsigned long *flags)
+ __releases(__grq->lock)
+ __releases(p->pi_lock)
+ __releases(grq_layout_rwlock)
+{
+ struct global_rq *__grq = rq_grq(rq);
+ raw_spin_unlock(&__grq->lock);
+ raw_spin_unlock(&p->pi_lock);
+ read_unlock_irqrestore(&grq_layout_rwlock, *flags);
+}
+
+static noinline void __cpu_grq_lock_irqsave(int cpu, unsigned long *flags)
+{
+ raw_spin_lock(&cpu_grq(cpu)->lock);
+}
+
+static noinline void __cpu_grq_unlock_irqrestore(int cpu, unsigned long *flags)
+{
+ raw_spin_unlock(&cpu_grq(cpu)->lock);
+}
+
+/*
+static struct global_rq *this_grq_lock_irq(void)
+ __acquires(this_grq->lock)
+ __acquires(grq_layout_rwlock)
+{
+ struct global_rq *this_grq;
+
+ read_lock_irq(&grq_layout_rwlock);
+ this_grq = __get_cpu_var(global_runqueues);
+ raw_spin_lock(&this_grq->lock);
+ return this_grq;
+}
+
+static void this_grq_unlock_irq(struct global_rq *this_grq)
+ __releases(this_grq->lock)
+ __releases(grq_layout_rwlock)
+{
+ raw_spin_unlock(&this_grq->lock);
+ read_unlock_irq(&grq_layout_rwlock);
+}
+
+static struct global_rq *this_grq_lock_irqsafe(unsigned long *flags)
+ __acquires(this_grq->lock)
+ __acquires(grq_layout_rwlock)
+{
+ struct global_rq *this_grq;
+
+ read_lock_irqsave(&grq_layout_rwlock, *flags);
+ this_grq = __get_cpu_var(global_runqueues);
+ raw_spin_lock(&this_grq->lock);
+ return this_grq;
+}
+
+static void this_grq_unlock_irqrestore(struct global_rq *this_grq,
+ unsigned long *flags)
+ __releases(this_grq->lock)
+ __releases(grq_layout_rwlock)
+{
+ raw_spin_unlock(&this_grq->lock);
+ read_unlock_irqrestore(&grq_layout_rwlock, *flags);
+}*/
+
+static void grq_lock(void)
+ __acquires(grq.lock)
+ __acquires(grq_layout_rwlock)
+{
+ /*
+ * the global read/write lock needs to be taken
+ * before any global runqueus lock, because taking
+ * a lock is also modifying the runqueue in question
+ */
+ read_lock(&grq_layout_rwlock);
+ raw_spin_lock(&grq.lock);
+}
+
+static void grq_unlock(void)
+ __releases(grq.lock)
+ __releases(grq_layout_rwlock)
+{
+ raw_spin_unlock(&grq.lock);
+ read_unlock(&grq_layout_rwlock);
+}
+
+static void grq_lock_irq(void)
+ __acquires(grq.lock)
+ __acquires(grq_layout_rwlock)
+{
+ read_lock_irq(&grq_layout_rwlock);
+ raw_spin_lock(&grq.lock);
+}
+
+static void grq_unlock_irq(void)
+ __releases(grq.lock)
+ __releases(grq_layout_rwlock)
+{
+ raw_spin_unlock(&grq.lock);
+ read_unlock_irq(&grq_layout_rwlock);
+}
+
+static inline void grq_lock_irqsave(unsigned long *flags)
+ __acquires(grq.lock)
+ __acquires(grq_layout_rwlock)
+{
+ read_lock_irqsave(&grq_layout_rwlock, *flags);
+ raw_spin_lock(&grq.lock);
+}
+
+static void grq_unlock_irqrestore(unsigned long *flags)
+ __releases(grq.lock)
+ __releases(grq_layout_rwlock)
+{
+ raw_spin_unlock(&grq.lock);
+ read_unlock_irqrestore(&grq_layout_rwlock, *flags);
+}
+
+static struct rq *cfs_time_task_grq_lock(struct task_struct *p,
+ unsigned long *flags)
+ __acquires(p->pi_lock)
+ __acquires(__grq->lock)
+ __acquires(grq_layout_rwlock)
+{
+ struct rq *rq;
+ struct global_rq *__grq;
+
+ read_lock_irqsave(&grq_layout_rwlock, *flags);
+
+ for (;;) {
+ raw_spin_lock(&p->pi_lock);
+ rq = task_rq(p);
+ __grq = rq_grq(rq);
+ raw_spin_lock(&__grq->lock);
+ if (likely(rq == task_rq(p)))
+ goto out_return;
+ raw_spin_unlock(&__grq->lock);
+ raw_spin_unlock(&p->pi_lock);
+ }
+out_return:
+ update_clocks(rq, rq_grq(rq));
+ return rq;
+}
+
+/*
+ * double_grq_lock - safely lock two global-runqueues
+ *
+ * Note this does not disable interrupts,
+ * you need to do so manually before calling.
+ */
+
+static noinline void double_grq_lock(struct global_rq *grq1,
+ struct global_rq *grq2)
+ __acquires(grq1->lock)
+ __acquires(grq2->lock)
+{
+ BUG_ON(!irqs_disabled());
+ lockdep_assert_held(&grq_layout_rwlock);
+ if (grq1 == grq2) {
+ raw_spin_lock(&grq1->lock);
+ //__acquire(grq2->lock); /* Fake it out ;) */
+ } else {
+ if (grq1 < grq2) {
+ raw_spin_lock(&grq1->lock);
+ raw_spin_lock_nested(&grq2->lock, SINGLE_DEPTH_NESTING);
+ } else {
+ raw_spin_lock(&grq2->lock);
+ raw_spin_lock_nested(&grq1->lock, SINGLE_DEPTH_NESTING);
+ }
+ }
+}
+
+/*
+ * double_grq_unlock - safely unlock two runqueues
+ *
+ * Note this does not restore interrupts,
+ * you need to do so manually after calling.
+ */
+static void double_grq_unlock(struct global_rq *grq1,
+ struct global_rq *grq2)
+ __releases(grq1->lock)
+ __releases(grq2->lock)
+{
+ raw_spin_unlock(&grq1->lock);
+ lockdep_assert_held(&grq_layout_rwlock);
+ if (grq1 != grq2)
+ raw_spin_unlock(&grq2->lock);
+ else
+ __release(grq2->lock);
+}
+
+
+#ifndef __ARCH_WANT_UNLOCKED_CTXSW
+static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
+{
+ next->on_cpu = true;
+}
+
+static noinline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
+{
+ smp_mb();
+ prev->on_cpu = false;
+
+#ifdef CONFIG_DEBUG_SPINLOCK
+ /* this is a valid case when another task releases the spinlock */
+ grq.lock.owner = current;
+// grq_layout_rwlock.owner = current*/
+#endif
+ /*
+ * If we are tracking spinlock dependencies then we have to
+ * fix up the runqueue lock - which gets 'carried over' from
+ * prev into current:
+ */
+
+ spin_acquire(&grq.lock.dep_map, 0, 0, _THIS_IP_);
+ rwlock_acquire_read(&grq_layout_rwlock.dep_map, 0, 0, _THIS_IP_);
+ grq_unlock_irq();
+}
+
+#else /* __ARCH_WANT_UNLOCKED_CTXSW */
+
+static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
+{
+#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
+ grq_unlock_irq();
+#else
+ grq_unlock();
+#endif
+}
+
+static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
+{
+ smp_wmb();
+#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
+ local_irq_enable();
+#endif
+}
+#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
+
+static inline bool deadline_before(u64 deadline, u64 time) +{
+ return (deadline < time);
+}
+
+static inline bool deadline_after(u64 deadline, u64 time) +{
+ return (deadline > time);
+}
+
+/*
+ * A task that is queued but not running will be on the grq run list.
+ * A task that is not running or queued will not be on the grq run list.
+ * A task that is currently running will have ->on_cpu set but not on the
+ * grq run list.
+ */
+static inline bool task_queued(struct task_struct *p)
+{
+ return !list_empty(&p->run_list);
+}
+
+/*
+ * Removing from the global runqueue. Enter with grq locked.
+ */
+static void dequeue_task(struct task_struct *p, struct global_rq *__grq)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ assert_raw_spin_locked(&(__grq->lock));
+ lockdep_assert_held(&(__grq->lock));
+
+ (__grq->qnr)--;
+ list_del_init(&p->run_list);
+ if (list_empty(__grq->queue + p->prio))
+ __clear_bit(p->prio, __grq->prio_bitmap);
+}
+
+/*
+ * To determine if it's safe for a task of SCHED_IDLEPRIO to actually run as
+ * an idle task, we ensure none of the following conditions are met.
+ */
+static bool idleprio_suitable(struct task_struct *p)
+{
+ return (!freezing(p) && !signal_pending(p) &&
+ !(task_contributes_to_load(p)) && !(p->flags & (PF_EXITING)));
+}
+
+/*
+ * To determine if a task of SCHED_ISO can run in pseudo-realtime, we check
+ * that the iso_refractory flag is not set.
+ */
+static bool isoprio_suitable(void)
+{
+ return !grq.iso_refractory;
+}
+
+/*
+ * Adding to the global runqueue. Enter with grq locked.
+ */
+
+static void enqueue_task(struct task_struct *p, struct global_rq *__grq)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ /*assert_raw_spin_locked(&(__grq->lock));
+ lockdep_assert_held(&(__grq->lock));*/
+
+ if (!rt_task(p)) {
+ /* Check it hasn't gotten rt from PI */
+ if ((idleprio_task(p) && idleprio_suitable(p)) ||
+ (iso_task(p) && isoprio_suitable()))
+ p->prio = p->normal_prio;
+ else
+ p->prio = NORMAL_PRIO;
+ }
+ (__grq->qnr)++;
+ __set_bit(p->prio, __grq->prio_bitmap);
+ list_add_tail(&p->run_list, __grq->queue + p->prio);
+ sched_info_queued(p);
+}
+
+/* Only idle task does this as a real time task*/
+static inline void enqueue_task_head(struct task_struct *p,
+ struct global_rq *__grq)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ //assert_raw_spin_locked(&(__grq->lock));
+ //lockdep_assert_held(&(__grq->lock));
+
+ (__grq->qnr)++;
+ __set_bit(p->prio, __grq->prio_bitmap);
+ list_add(&p->run_list, __grq->queue + p->prio);
+ sched_info_queued(p);
+}
+
+static void requeue_task(struct task_struct *p)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ assert_raw_spin_locked(&task_grq(p)->lock);
+ lockdep_assert_held(&task_grq(p)->lock);
+
+ sched_info_queued(p);
+}
+
+/*
+ * Returns the relative length of deadline all compared to the shortest
+ * deadline which is that of nice -20.
+ */
+static inline int task_prio_ratio(struct task_struct *p)
+{
+ return prio_ratios[TASK_USER_PRIO(p)];
+}
+
+/*
+ * task_timeslice - all tasks of all priorities get the exact same timeslice
+ * length. CPU distribution is handled by giving different deadlines to
+ * tasks of different priorities. Use 128 as the base value for fast shifts.
+ */
+static inline int task_timeslice(struct task_struct *p)
+{
+ return (rr_interval * task_prio_ratio(p) / 128);
+}
+
+#ifdef CONFIG_SMP
+/*
+ * qnr is the "queued but not running" count which is the total number of
+ * tasks on the global runqueue list waiting for cpu time but not actually
+ * currently running on a cpu.
+ */
+/*static inline void inc_qnr(void)
+{
+ grq.qnr++;
+}
+
+static inline void dec_qnr(void)
+{
+ grq.qnr--;
+}*/
+
+static inline int queued_notrunning(void)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ assert_raw_spin_locked(&grq.lock);
+ lockdep_assert_held(&grq.lock);
+ return grq.qnr;
+}
+
+/*
+ * The cpu_idle_map stores a bitmap of all the CPUs currently idle to
+ * allow easy lookup of whether any suitable idle CPUs are available.
+ * It's cheaper to maintain a binary yes/no if there are any idle CPUs on the
+ * idle_cpus variable than to do a full bitmask check when we are busy.
+ */
+static inline void set_cpuidle_map(int cpu)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ assert_raw_spin_locked(&grq.lock);
+ lockdep_assert_held(&grq.lock);
+ if (likely(cpu_online(cpu))) {
+ cpu_set(cpu, grq.cpu_idle_map);
+ grq.idle_cpus = true;
+ }
+}
+
+static inline void clear_cpuidle_map(int cpu)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ assert_raw_spin_locked(&grq.lock);
+ lockdep_assert_held(&grq.lock);
+ cpu_clear(cpu, grq.cpu_idle_map);
+ if (cpus_empty(grq.cpu_idle_map))
+ grq.idle_cpus = false;
+}
+
+static bool suitable_idle_cpus(struct task_struct *p)
+{
+ return false;
+}
+
+bool cpus_share_cache(int this_cpu, int that_cpu)
+{
+ struct rq *this_rq = cpu_rq(this_cpu);
+
+ return (this_rq->cpu_locality[that_cpu] < 2);
+}
+
+static inline void resched_suitable_idle(struct task_struct *p, struct global_rq *source)
+{
+ /*struct global_rq *aux;
+
+ dequeue_task(p, source);
+ raw_spin_unlock(&source->lock);
+ aux = cpu_grq(cpumask_first(&p->cpus_allowed));
+ double_grq_lock(aux, source);
+ //raw_spin_lock(&aux->lock);
+ enqueue_task(p, aux);
+ set_task_cpu(p, cpumask_first(&aux->cpu_span));
+ //raw_spin_unlock(&aux->lock);
+ double_grq_unlock(aux, source);
+ read_unlock(&grq_layout_rwlock);
+ sched_preempt_enable_no_resched();*/
+}
+/*
+ * Flags to tell us whether this CPU is running a CPU frequency governor that
+ * has slowed its speed or not. No locking required as the very rare wrongly
+ * read value would be harmless.
+ */
+void cpu_scaling(int cpu)
+{
+ cpu_rq(cpu)->scaling = true;
+}
+
+void cpu_nonscaling(int cpu)
+{
+ cpu_rq(cpu)->scaling = false;
+}
+
+static inline bool scaling_rq(struct rq *rq)
+{
+ return rq->scaling;
+}
+#else /* CONFIG_SMP */
+static inline void inc_qnr(void)
+{
+}
+
+static inline void dec_qnr(void)
+{
+}
+
+static inline int queued_notrunning(void)
+{
+ return grq.nr_running;
+}
+
+static inline void set_cpuidle_map(int cpu)
+{
+}
+
+static inline void clear_cpuidle_map(int cpu)
+{
+}
+
+static inline bool suitable_idle_cpus(struct task_struct *p) +{
+ return uprq->curr == uprq->idle;
+}
+
+static inline void resched_suitable_idle(struct task_struct *p) +{
+}
+
+void cpu_scaling(int __unused)
+{
+}
+
+void cpu_nonscaling(int __unused)
+{
+}
+
+/*
+ * Although CPUs can scale in UP, there is nowhere else for tasks to go so this
+ * always returns 0.
+ */
+static inline bool scaling_rq(struct rq *rq)
+{
+ return false;
+}
+#endif /* CONFIG_SMP */
+EXPORT_SYMBOL_GPL(cpu_scaling); +EXPORT_SYMBOL_GPL(cpu_nonscaling);
+
+
+int debug_cpu_tmp;
+void print_grqs(void)
+{
+ int cpu_tmp;
+ int i = 0;
+
+ printk("cpu to grq:\n");
+ for_each_possible_cpu(cpu_tmp) {
+ if (per_cpu(global_runqueues, cpu_tmp)) {
+ printk("cpu: %d, grq: %p\n", cpu_tmp,
+ per_cpu(global_runqueues, cpu_tmp));
+ }
+ }
+
+ printk("grq_number to grq\n");
+ for_each_possible_cpu(cpu_tmp) {
+ if (per_cpu(grq_nr_lookup_table, cpu_tmp)) {
+ printk("grq_number: %d, grq: %p\n", cpu_tmp,
+ per_cpu(grq_nr_lookup_table, cpu_tmp));
+ }
+ }
+
+ for_each_possible_cpu(cpu_tmp) {
+ i++;
+ }
+ debug_cpu_tmp = i;
+}
+
+/*
+ * this is the syscall for associating a cpu to a global runqueue
+ */
+asmlinkage long sys_associate_cpu_grq(unsigned int cpu, unsigned int __grq)
+{
+ unsigned long flags;
+
+ write_lock_irqsave(&grq_layout_rwlock, flags);
+
+ associate_grq_cpu(cpu, __grq);
+
+ write_unlock_irqrestore(&grq_layout_rwlock, flags);
+
+ print_grqs();
+
+ return 0;
+}
+
+struct global_rq *get_min_grq(void)
+{
+ int cpu_tmp;
+ struct global_rq *min = NULL;
+ struct global_rq *tmp_grq = NULL;
+ unsigned long min_qnr = ~0UL;
+ for_each_possible_cpu(cpu_tmp) {
+ tmp_grq = grq_number_to_grq(cpu_tmp);
+ if (tmp_grq) {
+ if (tmp_grq->qnr < min_qnr) {
+ min = tmp_grq;
+ min_qnr = tmp_grq->qnr;
+ }
+ }
+ }
+ return min;
+}
+
+struct global_rq *get_min_grq_task(struct task_struct *p) +{
+ int cpu_tmp;
+ struct global_rq *min = NULL;
+ struct global_rq *tmp_grq = NULL;
+ unsigned long min_qnr = ~0UL;
+ for_each_cpu_and(cpu_tmp, cpu_possible_mask, &p->cpus_allowed) {
+ tmp_grq = grq_number_to_grq(cpu_tmp);
+ if (tmp_grq) {
+ if (tmp_grq->qnr < min_qnr) {
+ min = tmp_grq;
+ min_qnr = tmp_grq->qnr;
+ }
+ }
+ }
+ return min;
+}
+
+
+struct global_rq *get_max_grq(void)
+{
+ int cpu_tmp;
+ struct global_rq *max = NULL;
+ struct global_rq *tmp_grq = NULL;
+ unsigned long max_qnr = 0;
+ for_each_possible_cpu(cpu_tmp) {
+ tmp_grq = grq_number_to_grq(cpu_tmp);
+ if (tmp_grq) {
+ if (tmp_grq->qnr > max_qnr) {
+ max = tmp_grq;
+ max_qnr = tmp_grq->qnr;
+ }
+ }
+ }
+ return max;
+}
+
+static void move_task(struct task_struct *p, struct global_rq *src,
+ struct global_rq *dst)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ lockdep_assert_held(&src->lock);
+ lockdep_assert_held(&dst->lock);
+
+ dequeue_task(p, src);
+ enqueue_task(p, dst);
+ set_task_cpu(p, cpumask_first_and(&dst->cpu_span, &p->cpus_allowed));
+}
+
+/*
+ * Move a task to a suitable grq, because it can not
+ * stay on @src due to changed cpu affinity
+ * @p the task to move, has to be dequeued
+ */
+static void move_task_suitable_grq(struct task_struct *p,
+ struct global_rq *src)
+{
+ struct global_rq *dst;
+
+ lockdep_assert_held(&grq_layout_rwlock);
+ lockdep_assert_held(&src->lock);
+
+ raw_spin_unlock(&src->lock);
+ dst = cpu_grq(cpumask_first(&p->cpus_allowed));
+ if(!dst)
+ goto out;
+ double_grq_lock(dst, src);
+ enqueue_task(p, dst);
+ set_task_cpu(p, cpumask_first_and(&dst->cpu_span, &p->cpus_allowed));
+ double_grq_unlock(dst, src);
+out:
+ raw_spin_lock(&src->lock);
+}
+
+void do_load_balance(void)
+{
+ struct global_rq *dst= NULL;
+ struct global_rq *src = NULL;
+ struct task_struct *swap = NULL;
+ struct list_head *queue;
+
+ BUG_ON(!irqs_disabled());
+
+ dst = get_min_grq();
+ if (!dst)
+ goto out_nounlock;
+
+ src = get_max_grq();
+ if (!src)
+ goto out_nounlock;
+
+ double_grq_lock(dst, src);
+
+ queue = src->queue + NORMAL_PRIO;
+ list_for_each_entry(swap, queue, run_list) {
+ if (!cpus_intersects(swap->cpus_allowed, dst->cpu_span))
+ continue;
+ move_task(swap, src, dst);
+ break;
+ }
+ double_grq_unlock(dst, src);
+out_nounlock:
+ return;
+}
+
+/*
+ * activate_idle_task - move idle task to the _front_ of runqueue.
+ */
+static inline void activate_idle_task(struct task_struct *p, struct rq *__rq)
+{
+ rq_grq(__rq)->nr_running++;
+}
+
+static inline int normal_prio(struct task_struct *p)
+{
+ if (has_rt_policy(p))
+ return MAX_RT_PRIO - 1 - p->rt_priority;
+ if (idleprio_task(p))
+ return IDLE_PRIO;
+ if (iso_task(p))
+ return ISO_PRIO;
+ return NORMAL_PRIO;
+}
+
+/*
+ * Calculate the current priority, i.e. the priority
+ * taken into account by the scheduler. This value might
+ * be boosted by RT tasks as it will be RT if the task got
+ * RT-boosted. If not then it returns p->normal_prio.
+ */
+static int effective_prio(struct task_struct *p)
+{
+ p->normal_prio = normal_prio(p);
+ /*
+ * If we are RT tasks or we were boosted to RT priority,
+ * keep the priority unchanged. Otherwise, update priority
+ * to the normal priority:
+ */
+ if (!rt_prio(p->prio))
+ return p->normal_prio;
+ return p->prio;
+}
+
+
+/*
+ * activate_task - move a task to the runqueue. Enter with grq locked.
+ */
+
+static noinline void activate_task_on(struct task_struct *const p,
+ struct rq * const rq,
+ struct global_rq *dst)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ lockdep_assert_held(&dst->lock);
+ assert_raw_spin_locked(&dst->lock);
+
+ update_clocks(rq, dst);
+
+ /*
+ * Sleep time is in units of nanosecs, so shift by 20 to get a
+ * milliseconds-range estimation of the amount of time that the task
+ * spent sleeping:
+ */
+ if (unlikely(prof_on == SLEEP_PROFILING)) {
+ if (p->state == TASK_UNINTERRUPTIBLE)
+ profile_hits(SLEEP_PROFILING, (void *)get_wchan(p),
+ (rq->clock - p->last_ran) >> 20);
+ }
+
+ p->prio = effective_prio(p);
+ if (task_contributes_to_load(p))
+ (dst->nr_uninterruptible)--;
+ enqueue_task(p, dst);
+
+ (dst->nr_running)++;
+}
+
+/*
+ * deactivate_task - If it's running, it's not on the grq and we can just
+ * decrement the nr_running. Enter with grq locked.
+ */
+static inline void deactivate_task(struct global_rq *__grq, struct task_struct *p)
+{
+ lockdep_assert_held(&grq_layout_rwlock);
+ /*assert_raw_spin_locked(&__grq->lock);
+ lockdep_assert_held(&__grq->lock);*/
+
+ if (task_contributes_to_load(p))
+ __grq->nr_uninterruptible++;
+ __grq->nr_running--;
+}
+
+#ifdef CONFIG_SMP
+void set_task_cpu(struct task_struct *p, unsigned int cpu) +{
+#ifdef CONFIG_LOCKDEP
+ /*
+ * The caller should hold grq lock.
+ */
+/* WARN_ON_ONCE(p->state != TASK_RUNNING &&
+ p->state != TASK_WAKING &&
+ !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));*/
+
+
+ if(p->state != TASK_RUNNING &&
+ p->state != TASK_WAKING &&
+ !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE)) {
+ printk("-------------------\n");
+ printk("comm: %s \n", p->comm);
+ printk("state %ld\n", p->state);
+ printk("preempt_cont %d\n", task_thread_info(p)->preempt_count);
+ printk("queued: %s\n", task_queued(p)?"true":"false" );
+ printk("on_cpu: %d", p->on_cpu);
+ printk("-------------------\n");
+ }
+ WARN_ON(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
+ lockdep_is_held(&task_grq(p)->lock)));
+#endif
+
+ trace_sched_migrate_task(p, cpu);
+ if (task_cpu(p) != cpu)
+ perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
+
+ /*
+ * After ->cpu is set up to a new value, task_grq_lock(p, ...) can be
+ * successfuly executed on another CPU. We must ensure that updates of
+ * per-task data have been completed by this moment.
+ */
+ smp_wmb();
+ task_thread_info(p)->cpu = cpu;
+}
+
+#endif
+
+
+/*
+ * Move a task off the global queue and take it to a cpu for it will
+ * become the running task.
+ */
+static inline void take_task(int cpu, struct task_struct *p,
+ struct global_rq *__grq)
+{
+ set_task_cpu(p, cpu);
+ dequeue_task(p, __grq);
+}
+
+/*
+ * Returns a descheduling task to the grq runqueue unless it is being
+ * deactivated.
+ */
+static inline void return_task(struct global_rq *__grq, struct task_struct *p,
+ int deactivate)
+{
+ if (deactivate)
+ deactivate_task(__grq, p);
+ else
+ enqueue_task(p, __grq);
+}
+
+/*
+ * resched_task - mark a task 'to be rescheduled now'.
+ *
+ * On UP this means the setting of the need_resched flag, on SMP it
+ * might also involve a cross-CPU call to trigger the scheduler on
+ * the target CPU.
+ */
+#ifdef CONFIG_SMP
+
+#ifndef tsk_is_polling
+#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) +#endif
+
+static void resched_task(struct task_struct *p)
+{
+ int cpu;
+
+ //assert_raw_spin_locked(&task_grq(p)->lock);
+
+ if (test_tsk_need_resched(p))
+ return;
+
+ set_tsk_need_resched(p);
+
+ cpu = task_cpu(p);
+ if (cpu == smp_processor_id())
+ return;
+
+ /* NEED_RESCHED must be visible before we test polling */
+ smp_mb();
+ if (!tsk_is_polling(p))
+ smp_send_reschedule(cpu);
+}
+
+#else
+static inline void resched_task(struct task_struct *p)
+{
+ /*assert_raw_spin_locked(&grq.lock);*/
+ set_tsk_need_resched(p);
+}
+#endif
+
+/**
+ * task_curr - is this task currently executing on a CPU?
+ * @p: the task in question.
+ */
+inline int task_curr(const struct task_struct *p)
+{
+ return cpu_curr(task_cpu(p)) == p;
+}
+
+#ifdef CONFIG_SMP
+struct migration_req {
+ struct task_struct *task;
+ int dest_cpu;
+};
+
+/*
+ * wait_task_inactive - wait for a thread to unschedule.
+ *
+ * If @match_state is nonzero, it's the @p->state value just checked and
+ * not expected to change. If it changes, i.e. @p might have woken up,
+ * then return zero. When we succeed in waiting for @p to be off its CPU,
+ * we return a positive number (its total switch count). If a second call
+ * a short while later returns the same number, the caller can be sure that
+ * @p has remained unscheduled the whole time.
+ *
+ * The caller must ensure that the task *will* unschedule sometime soon,
+ * else this function might spin for a *long* time. This function can't
+ * be called with interrupts off, or it may introduce deadlock with
+ * smp_call_function() if an IPI is sent by the same process we are
+ * waiting to become inactive.
+ */
+unsigned long wait_task_inactive(struct task_struct *p, long match_state)
+{
+ unsigned long flags;
+ bool running, on_rq;
+ unsigned long ncsw;
+ struct rq *rq;
+
+ for (;;) {
+ /*
+ * We do the initial early heuristics without holding
+ * any task-queue locks at all. We'll only try to get
+ * the runqueue lock when things look like they will
+ * work out! In the unlikely event rq is dereferenced
+ * since we're lockless, grab it again.
+ */
+#ifdef CONFIG_SMP
+retry_rq:
+ rq = task_rq(p);
+ if (unlikely(!rq))
+ goto retry_rq;
+#else /* CONFIG_SMP */
+ rq = task_rq(p);
+#endif
+ /*
+ * If the task is actively running on another CPU
+ * still, just relax and busy-wait without holding
+ * any locks.
+ *
+ * NOTE! Since we don't hold any locks, it's not
+ * even sure that "rq" stays as the right runqueue!
+ * But we don't care, since this will return false
+ * if the runqueue has changed and p is actually now
+ * running somewhere else!
+ */
+ while (task_running(p) && p == rq->curr) {
+ if (match_state && unlikely(p->state != match_state))
+ return 0;
+ cpu_relax();
+ }
+
+ /*
+ * Ok, time to look more closely! We need the grq
+ * lock now, to be *sure*. If we're wrong, we'll
+ * just go back and repeat.
+ */
+ rq = task_grq_lock(p, &flags);
+ trace_sched_wait_task(p);
+ running = task_running(p);
+ on_rq = task_queued(p);
+ ncsw = 0;
+ if (!match_state || p->state == match_state)
+ ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
+ task_grq_unlock(rq, p, &flags);
+
+ /*
+ * If it changed from the expected state, bail out now.
+ */
+ if (unlikely(!ncsw))
+ break;
+
+ /*
+ * Was it really running after all now that we
+ * checked with the proper locks actually held?
+ *
+ * Oops. Go back and try again..
+ */
+ if (unlikely(running)) {
+ cpu_relax();
+ continue;
+ }
+
+ /*
+ * It's not enough that it's not actively running,
+ * it must be off the runqueue _entirely_, and not
+ * preempted!
+ *
+ * So if it was still runnable (but just not actively
+ * running right now), it's preempted, and we should
+ * yield - it could be a while.
+ */
+ if (unlikely(on_rq)) {
+ ktime_t to = ktime_set(0, NSEC_PER_SEC / HZ);
+
+ set_current_state(TASK_UNINTERRUPTIBLE);
+ schedule_hrtimeout(&to, HRTIMER_MODE_REL);
+ continue;
+ }
+
+ /*
+ * Ahh, all good. It wasn't running, and it wasn't
+ * runnable, which means that it will never become
+ * running in the future either. We're all done!
+ */
+ break;
+ }
+
+ return ncsw;
+}
+
+/***
+ * kick_process - kick a running thread to enter/exit the kernel
+ * @p: the to-be-kicked thread
+ *
+ * Cause a process which is running on another CPU to enter
+ * kernel-mode, without any delay. (to get signals handled.)
+ *
+ * NOTE: this function doesn't have to take the runqueue lock,
+ * because all it wants to ensure is that the remote task enters
+ * the kernel. If the IPI races and the task has been migrated
+ * to another CPU then no harm is done and the purpose has been
+ * achieved as well.
+ */
+void kick_process(struct task_struct *p)
+{
+ int cpu;
+
+ preempt_disable();
+ cpu = task_cpu(p);
+ if ((cpu != smp_processor_id()) && task_curr(p))
+ smp_send_reschedule(cpu);
+ preempt_enable();
+}
+EXPORT_SYMBOL_GPL(kick_process);
+#endif
+
+#define rq_idle(rq) ((rq)->rq_prio == PRIO_LIMIT)
+
+/*
+ * RT tasks preempt purely on priority. SCHED_NORMAL tasks preempt on the
+ * basis of earlier deadlines. SCHED_IDLEPRIO don't preempt anything else or
+ * between themselves, they cooperatively multitask. An idle rq scores as
+ * prio PRIO_LIMIT so it is always preempted.
+ */
+static inline bool
+can_preempt(struct task_struct *p, int prio, u64 deadline) +{
+ /* Better static priority RT task or better policy preemption */
+ if (p->prio < prio)
+ return true;
+ if (p->prio > prio)
+ return false;
+ /* SCHED_NORMAL, BATCH and ISO will preempt based on deadline */
+ if (!deadline_before(p->deadline, deadline))
+ return false;
+ return true;
+}
+
+#ifdef CONFIG_SMP
+#define cpu_online_map (*(cpumask_t *)cpu_online_mask)
+#ifdef CONFIG_HOTPLUG_CPU
+/*
+ * Check to see if there is a task that is affined only to offline CPUs but
+ * still wants runtime. This happens to kernel threads during suspend/halt and
+ * disabling of CPUs.
+ */
+
+static inline bool online_cpus(struct task_struct *p)
+{
+ return likely(cpus_intersects(cpu_online_map, p->cpus_allowed));
+}
+#else /* CONFIG_HOTPLUG_CPU */
+/* All available CPUs are always online without hotplug. */ +static inline bool online_cpus(struct task_struct *p)
+{
+ return true;
+}
+#endif
+
+/*
+ * Check to see if p can run on cpu, and if not, whether there are any online
+ * CPUs it can run on instead.
+ */
+static inline bool needs_other_cpu(struct task_struct *p, int cpu) +{
+ if (unlikely(!cpu_isset(cpu, p->cpus_allowed)))
+ return true;
+ return false;
+}
+
+/*
+ * When all else is equal, still prefer this_rq.
+ */
+static void try_preempt(struct task_struct *p, struct rq *this_rq) +{
+ struct rq *highest_prio_rq;
+ int cpu, highest_prio;
+ u64 latest_deadline;
+ struct global_rq *__grq;
+
+ lockdep_assert_held(&grq_layout_rwlock);
+
+ __grq = rq_grq(this_rq);
+ lockdep_assert_held(&__grq->lock);
+
+ /* IDLEPRIO tasks never preempt anything */
+ if (p->policy == SCHED_IDLEPRIO)
+ return;
+
+ if(!online_cpus(p))
+ return;
+
+ highest_prio = p->prio;
+ highest_prio_rq = this_rq;
+ latest_deadline = this_rq->rq_deadline;
+
+ for_each_cpu_and(cpu, &__grq->cpu_span, &p->cpus_allowed) {
+ struct rq *rq;
+ int rq_prio;
+
+ rq = cpu_rq(cpu);
+ rq_prio = rq->rq_prio;
+ if (rq_prio < highest_prio)
+ continue;
+
+ if (rq_prio > highest_prio ||
+ deadline_after(rq->rq_deadline, latest_deadline)) {
+ latest_deadline = rq->rq_deadline;
+ highest_prio = rq_prio;
+ highest_prio_rq = rq;
+ }
+ }
+
+ if (!can_preempt(p, highest_prio, highest_prio_rq->rq_deadline))
+ return;
+
+ resched_task(highest_prio_rq->curr);
+}
+#else /* CONFIG_SMP */
+static inline bool needs_other_cpu(struct task_struct *p, int cpu) +{
+ return false;
+}
+
+static void try_preempt(struct task_struct *p, struct rq *this_rq) +{
+ if (p->policy == SCHED_IDLEPRIO)
+ return;
+ if (can_preempt(p, uprq->rq_prio, uprq->rq_deadline))
+ resched_task(uprq->curr);
+}
+#endif /* CONFIG_SMP */
+
+static void
+ttwu_stat(struct task_struct *p, int cpu, int wake_flags) +{
+#ifdef CONFIG_SCHEDSTATS
+ struct rq *rq = this_rq();
+
+#ifdef CONFIG_SMP
+ int this_cpu = smp_processor_id();
+
+ if (cpu == this_cpu)
+ schedstat_inc(rq, ttwu_local);
+ else {
+ struct sched_domain *sd;
+
+ rcu_read_lock();
+ for_each_domain(this_cpu, sd) {
+ if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
+ schedstat_inc(sd, ttwu_wake_remote);
+ break;
+ }
+ }
+ rcu_read_unlock();
+ }
+
+#endif /* CONFIG_SMP */
+
+ schedstat_inc(rq, ttwu_count);
+#endif /* CONFIG_SCHEDSTATS */
+}
+
+static void ttwu_activate(struct task_struct *p, struct rq *rq,
+ bool is_sync)
+{
+ activate_task_on(p, rq, rq_grq(rq));
+
+ /*
+ * Sync wakeups (i.e. those types of wakeups where the waker
+ * has indicated that it will leave the CPU in short order)
+ * don't trigger a preemption if there are no idle cpus,
+ * instead waiting for current to deschedule.
+ */
+ if (!is_sync || suitable_idle_cpus(p))
+ try_preempt(p, rq);
+}
+
+static inline void ttwu_post_activation(struct task_struct *p, struct rq *rq,
+ bool success)
+{
+ trace_sched_wakeup(p, success);
+ p->state = TASK_RUNNING;
+
+ /*
+ * if a worker is waking up, notify workqueue. Note that on BFS, we
+ * don't really know what cpu it will be, so we fake it for
+ * wq_worker_waking_up :/
+ */
+ if ((p->flags & PF_WQ_WORKER) && success)
+ wq_worker_waking_up(p, cpu_of(rq));
+}
+
+#ifdef CONFIG_SMP
+void scheduler_ipi(void)
+{
+ irq_enter();
+ irq_exit();
+}
+#endif /* CONFIG_SMP */
+
+/***
+ * try_to_wake_up - wake up a thread
+ * @p: the thread to be awakened
+ * @state: the mask of task states that can be woken
+ * @wake_flags: wake modifier flags (WF_*)
+ *
+ * Put it on the run-queue if it's not already there. The "current"
+ * thread is always on the run-queue (except when the actual
+ * re-schedule is in progress), and as such you're allowed to do
+ * the simpler "current->state = TASK_RUNNING" to mark yourself
+ * runnable without the overhead of this.
+ *
+ * Returns %true if @p was woken up, %false if it was already running
+ * or @state didn't match @p's state.
+ */
+static bool try_to_wake_up(struct task_struct *p, unsigned int state,
+ int wake_flags)
+{
+ bool success = false;
+ unsigned long flags;
+ struct rq *rq;
+ int cpu;
+
+ get_cpu();
+
+ /* This barrier is undocumented, probably for p->state? くそ */
+ smp_wmb();
+
+ /*
+ * No need to do time_lock_grq as we only need to update the rq clock
+ * if we activate the task
+ */
+ rq = task_grq_lock(p, &flags);
+ cpu = task_cpu(p);
+
+ /* state is a volatile long, どうして、分からない */
+ if (!((unsigned int)p->state & state))
+ goto out_unlock;
+
+ if (task_queued(p) || task_running(p))
+ goto out_running;
+
+ ttwu_activate(p, rq, wake_flags & WF_SYNC);
+ success = true;
+
+out_running:
+ ttwu_post_activation(p, rq, success);
+out_unlock:
+ task_grq_unlock(rq, p, &flags);
+
+ ttwu_stat(p, cpu, wake_flags);
+
+ put_cpu();
+
+ return success;
+}
+
+/**
+ * try_to_wake_up_local - try to wake up a local task with grq lock held
+ * @p: the thread to be awakened
+ *
+ * Put @p on the run-queue if it's not already there. The caller must
+ * ensure that grq is locked and, @p is not the current task.
+ * grq stays locked over invocation.
+ */
+static noinline void try_to_wake_up_local(struct task_struct *p) +{
+ struct rq *rq = task_rq(p);
+ bool success = false;
+
+ BUG_ON(rq != this_rq());
+ BUG_ON(p == current);
+
+ lockdep_assert_held(&grq_layout_rwlock);
+ lockdep_assert_held(&grq.lock);
+
+ if (!raw_spin_trylock(&p->pi_lock)) {

+                raw_spin_unlock(&grq.lock);
+                raw_spin_lock(&p->pi_lock);
+                raw_spin_lock(&grq.lock);

6,329 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#1 lore raw
Quoted 10 lines · Matthias Kohler

I'm doing a CPU-Scheduler based on BFS by Con Kolivas with support for multiple run-queues. BFS in itself uses only one run-queue for all CPU's. This avoids the load-balancing overhead, but does not scale well. One run-queue per CPU does scale well, but then the scheduler has load-balancing overhead. The scheduler I'm developing supports every possible run-queues configuration. You can have one single run-queue like in BFS, or you can have one run-queue per CPU, or something completely different like one run-queue every two CPU's. This, in theory would allow the scheduler to be fine-tuned to the hardware and the workload.

Cannot see the reason to install wings on horse back. Is it developed to schedule apps on advanced servers? Notebook, or smart phone?

Good Weekend
Hillf

#2 lore raw
In reply to Matthias Kohler

Hi Matthias, et al.

Quoted 2 lines · On Sat, 15 Dec 2012 18:16:43 Matthias Kohler wrote:

I'm doing a CPU-Scheduler based on BFS by Con Kolivas with support for multiple run-queues.

Nice to see you doing interesting hacking on BFS and thanks for your bugfixes previously. Well done making it this far! Multiple runqueues is something that's been on and off the boil for BFS right from the start. I've also done various experiments and some ground work along similar lines (I'll explain later), but soon lost interest since my experiments suggested I would need a heck of a lot of time to implement it the way I envisioned.

Quoted 2 lines

BFS in itself uses only one run-queue for all
CPU's. This avoids the load-balancing overhead, but does not scale well.

Actually describing it like this is a little bit of a generalisation that unfortunately has the tendency to point to the obvious culprit for scaling without really defining what the limitations are to scaling and how removing it might help. The culprit people point to is the single runqueue because of the potential for lock contention rising exponentially as the number of CPUs rises. However while some very early testing showed that scalability of -some- workloads did not match mainline once we reached 16 CPUs, it was just an assumption that it was lock contention that caused this. The most CPUs I can find someone to test BFS on previously was 16, and lock debugging actually did NOT show there to be significant contention as registering any more than mainline. This is not that surprising really since the critical sections under the global runqueue lock are kept as short as possible.

Scalability seems only affected on workloads that rely greatly on cache warmth, and very low cache requirement workloads like simple networking based tasks do not suffer at all (and some benchmarks have shown it to be better than mainline at these). This implies to me that complex balancing mechanisms designed to keep tasks cache warm is the reason mainline outperforms with these workloads. The main thrust of BFS is to offer -any- free CPU to the task that has waited the longest, thereby minimising latency at all times, and bringing it down the more CPUs you have. This approach is pretty much orthogonal to trying to keep tasks bound to their old CPU or a cache shared sibling if it means occasionally leaving a cold cache CPU free for a period instead. BFS uses only the simplest possible cache warmth approach which proves to help, but overrides it in the interests of latency. Certainly in the
- now overworked example - of the make kernel benchmarks, it performs very well up to 16x.

Which brings up the question of just how many CPUs it requires for lock contention to be a problem. I don't have the answer to that question, but I would not be surprised if it was a significant problem at 64 or more. Luckily, while the general trend is for more and more threads even on commodity hardware, I don't see this size happening any time soon (note I'm not saying never). That said, it is a valid endpoint to scale beyond that if possible in the design if one wished BFS to be used outside the commodity hardware spectrum.

Quoted 7 lines

One run-queue per CPU does scale well, but then the scheduler has load-balancing overhead. The scheduler I'm developing supports every possible run-queues configuration. You can have one single run-queue like in BFS, or you can have one run-queue per CPU, or something completely different like one run-queue every two CPU's. This, in theory would allow the scheduler to be fine-tuned to the hardware and the workload.

I agree with you this is by far the most interesting way of tackling this problem, while trying to maintain the benefits of the shared runqueue as much as possible. Given that lock contention is not a problem even up to 16 threads/CPUs, it gives scope to have clusters of CPUs benefiting from the shared runqueue aspects BFS has while still allowing it to scale beyond that. I would hazard a guess that optimal would simply be one runqueue per shared cache.

Quoted 11 lines

What state is it in?
Currently it is very unstable, CPU-Hotplug is broken, scheduling statistics are broken, support for real-time tasks is broken. Load balancing when having more than one run-queue is working, but is nothing more than keeping the load on all run-queues equal. Associating a CPU and a run-queue is currently done with a system call and there is no access right checking. The source is in a very bad state. Uni-processor build is broken.
It lacks proper Documentation.

Quoted 7 lines

Why allow the user to change the run-queue layout?
To optimize the scheduler to specific hardware and workloads. You could use one run-queue for all CPU's if you want low latency and low scheduling overhead.
You could use one run-queue per CPU if you want high scalability. You could use one run-queue per n CPU's is these n CPU's share cache and there is not much benefit in load balancing between them.

That is in agreement with what I said above :) Admittedly trying to find just the right balance would be tricky, but having infinite flexibility is an admirable quality that would allow the optimal design to be found.

Quoted 4 lines

Benchmarks?
None, it is not stable enough to benchmark and the load balancing algorithm that is currently used, delivers very bad performance.

Well this would be crucial to get right given my experience with BFS. Fixing t he locking contention would only go partway to a solution since balancing is so important with our extremely cache fat CPUs these days, and localised memory and so on in the case of NUMA.

Quoted 13 lines

What advantages does it have when compared to other schedulers? It is more scalable than BFS.
It could in future have all features of BFS and of CFS, especially throughput and low latency.
It has far less lines of code than CFS.

What disadvantages does it have when compared to other schedulers? It is not stable.
It is not tested on anything else than kvm and more than 4 CPU's. Many features are not yet working or not implemented at all (good load balancing).

Quoted 9 lines

Implementation details:
All tasks that are runnable but not currently executing on a CPU, are queued on one of the global run-queues. Every global run-queue has its own spin-lock. When a task gets queued or dequeued this lock needs to be taken. All global run-queues are protected by one global read-write lock. When normal scheduling is done, this lock needs to be read_locked. When any change to the layout of the global run-queues is done, like adding new global run-queues or removing them, the global read-write lock needs to be write-locked.

This is where the parallels between your approach and my experiments starts becoming apparent. You will note in the last few -ck releases, there are extra patches that are not applied, that I mentioned in my blog.

http://ck-hack.blogspot.com/2012/06/upgradeable-rwlocks-and-bfs.html

These patches have been updated slightly since then and the latest can be found here:
http://ck.kolivas.org/patches/3.0/3.7/3.7-ck1/patches/

The reason I point these out are the problem with using read locks is that they're not upgradeable, and favour reads over writes. This means that if you had something time critical to do under the write lock, under heavy contention you may not be able to achieve it. The upgradeable rwlock favours writes over reads, and allows an intermediate stage between read and write which is upgradeable to a write lock or downgradeable to a read lock while still allowing read access to the critical sections. I have used it in place of the current global runqueue spinlock to create more read sections, although it has not had a massive impact on the hardware that I could test it on (up to 16x again). I wonder if you might find the urwlocks helpful for your code?

However the main reason for developing the upgradeable rwlocks was not just to create more critical sections that other CPUs can have read access. Ultimately I had a pipe dream that it could be used to create multiple runqueues as you have done in your patch. However, what I didn't want to do was to create a multi runqueue design that then needed a load balancer as that took away one of the advantages of BFS needing no balancer and keeping latency as low as possible.

I've not ever put a post up about what my solution was to this problem because the logistics of actually creating it, and the work required kept putting me off since it would require many hours, and I really hate to push vapourware. Code speaks louder than rhetoric. However since you are headed down creating multi runqueue code, perhaps you might want to consider it.

What I had in mind was to create varying numbers of runqueues in a hierarchical fashion. Whenever possible, the global runqueue could be grabbed in order to find the best possible task to schedule on that CPU from the entire pool. If there was contention however on the global runqueue, it could step down in the hierarchy and just grab a runqueue effective for a numa node and schedule the best task from that. If there was contention on that it could step down and schedule the best task from a physical package, and then shared cache, then shared threads, and if all that failed only would it just grab a local CPU runqueue. The reason for doing this is it would create a load balancer by sheer virtue of the locking mechanism itself rather than there actually being a load balancer at all, thereby benefiting from the BFS approach in terms of minimising latency, finding the best global task, not requiring a load balancer, and at the same time benefit from having multiple runqueues to avoid lock contention - and in fact use that lock contention as a means to an endpoint.

Alas to implement it myself I'd have to be employed full time for months working on just this to get it working...

Quoted 3 lines

Fair time distribution among tasks is done via the deadline mechanism of BFS.

While I unfortunately do not have the time to review your code due to my own real life events, I greatly look forward to seeing how your code evolves and wish you luck.

Signature · 1 line
-ck