2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Implement CPU time clocks for the POSIX clock interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/posix-timers.h>
|
|
|
|
#include <linux/errno.h>
|
2008-05-01 15:34:31 +04:00
|
|
|
#include <linux/math64.h>
|
|
|
|
#include <asm/uaccess.h>
|
2008-09-12 20:54:39 +04:00
|
|
|
#include <linux/kernel_stat.h>
|
2009-08-10 06:52:30 +04:00
|
|
|
#include <trace/events/timer.h>
|
2012-12-17 07:18:11 +04:00
|
|
|
#include <linux/random.h>
|
2013-04-18 03:31:13 +04:00
|
|
|
#include <linux/tick.h>
|
|
|
|
#include <linux/workqueue.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
/*
|
2010-03-12 01:04:37 +03:00
|
|
|
* Called after updating RLIMIT_CPU to run cpu timer and update
|
|
|
|
* tsk->signal->cputime_expires expiration cache if necessary. Needs
|
|
|
|
* siglock protection since other code may update expiration cache as
|
|
|
|
* well.
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
*/
|
2009-08-28 16:05:12 +04:00
|
|
|
void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
{
|
2009-07-29 14:15:26 +04:00
|
|
|
cputime_t cputime = secs_to_cputime(rlim_new);
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
|
2009-08-28 16:05:12 +04:00
|
|
|
spin_lock_irq(&task->sighand->siglock);
|
|
|
|
set_process_cpu_timer(task, CPUCLOCK_PROF, &cputime, NULL);
|
|
|
|
spin_unlock_irq(&task->sighand->siglock);
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
}
|
|
|
|
|
2006-01-10 07:52:27 +03:00
|
|
|
static int check_clock(const clockid_t which_clock)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
struct task_struct *p;
|
|
|
|
const pid_t pid = CPUCLOCK_PID(which_clock);
|
|
|
|
|
|
|
|
if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
return 0;
|
|
|
|
|
2010-11-03 19:52:56 +03:00
|
|
|
rcu_read_lock();
|
2008-02-08 15:21:52 +03:00
|
|
|
p = find_task_by_vpid(pid);
|
2007-10-19 10:40:18 +04:00
|
|
|
if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ?
|
2010-11-03 19:52:56 +03:00
|
|
|
same_thread_group(p, current) : has_group_leader_pid(p))) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = -EINVAL;
|
|
|
|
}
|
2010-11-03 19:52:56 +03:00
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
static inline unsigned long long
|
2006-01-10 07:52:27 +03:00
|
|
|
timespec_to_sample(const clockid_t which_clock, const struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long ret;
|
|
|
|
|
|
|
|
ret = 0; /* high half always zero when .cpu used */
|
2005-04-17 02:20:36 +04:00
|
|
|
if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
|
2013-06-28 04:06:42 +04:00
|
|
|
ret = (unsigned long long)tp->tv_sec * NSEC_PER_SEC + tp->tv_nsec;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
2013-06-28 04:06:42 +04:00
|
|
|
ret = cputime_to_expires(timespec_to_cputime(tp));
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-01-10 07:52:27 +03:00
|
|
|
static void sample_to_timespec(const clockid_t which_clock,
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long expires,
|
2005-04-17 02:20:36 +04:00
|
|
|
struct timespec *tp)
|
|
|
|
{
|
2008-05-01 15:34:31 +04:00
|
|
|
if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED)
|
2013-06-28 04:06:42 +04:00
|
|
|
*tp = ns_to_timespec(expires);
|
2008-05-01 15:34:31 +04:00
|
|
|
else
|
2013-06-28 04:06:42 +04:00
|
|
|
cputime_to_timespec((__force cputime_t)expires, tp);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update expiry time from increment, and increase overrun count,
|
|
|
|
* given the current clock sample.
|
|
|
|
*/
|
2005-10-26 20:26:53 +04:00
|
|
|
static void bump_cpu_timer(struct k_itimer *timer,
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long now)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
int i;
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long delta, incr;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
if (timer->it.cpu.incr == 0)
|
2005-04-17 02:20:36 +04:00
|
|
|
return;
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
if (now < timer->it.cpu.expires)
|
|
|
|
return;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
incr = timer->it.cpu.incr;
|
|
|
|
delta = now + incr - timer->it.cpu.expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
/* Don't use (incr*2 < delta), incr*2 might overflow. */
|
|
|
|
for (i = 0; incr < delta - incr; i++)
|
|
|
|
incr = incr << 1;
|
|
|
|
|
|
|
|
for (; i >= 0; incr >>= 1, i--) {
|
|
|
|
if (delta < incr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
timer->it.cpu.expires += incr;
|
|
|
|
timer->it_overrun += 1 << i;
|
|
|
|
delta -= incr;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 18:17:38 +04:00
|
|
|
/**
|
|
|
|
* task_cputime_zero - Check a task_cputime struct for all zero fields.
|
|
|
|
*
|
|
|
|
* @cputime: The struct to compare.
|
|
|
|
*
|
|
|
|
* Checks @cputime to see if all fields are zero. Returns true if all fields
|
|
|
|
* are zero, false if any field is nonzero.
|
|
|
|
*/
|
|
|
|
static inline int task_cputime_zero(const struct task_cputime *cputime)
|
|
|
|
{
|
|
|
|
if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
static inline unsigned long long prof_ticks(struct task_struct *p)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2012-11-13 17:20:55 +04:00
|
|
|
cputime_t utime, stime;
|
|
|
|
|
|
|
|
task_cputime(p, &utime, &stime);
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
return cputime_to_expires(utime + stime);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2013-06-28 04:06:42 +04:00
|
|
|
static inline unsigned long long virt_ticks(struct task_struct *p)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2012-11-13 17:20:55 +04:00
|
|
|
cputime_t utime;
|
|
|
|
|
|
|
|
task_cputime(p, &utime, NULL);
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
return cputime_to_expires(utime);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static int
|
|
|
|
posix_cpu_clock_getres(const clockid_t which_clock, struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
int error = check_clock(which_clock);
|
|
|
|
if (!error) {
|
|
|
|
tp->tv_sec = 0;
|
|
|
|
tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ);
|
|
|
|
if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) {
|
|
|
|
/*
|
|
|
|
* If sched_clock is using a cycle counter, we
|
|
|
|
* don't have any idea of its true resolution
|
|
|
|
* exported, but it is much more than 1s/HZ.
|
|
|
|
*/
|
|
|
|
tp->tv_nsec = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static int
|
|
|
|
posix_cpu_clock_set(const clockid_t which_clock, const struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* You can never reset a CPU clock, but we check for other errors
|
|
|
|
* in the call before failing with EPERM.
|
|
|
|
*/
|
|
|
|
int error = check_clock(which_clock);
|
|
|
|
if (error == 0) {
|
|
|
|
error = -EPERM;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sample a per-thread clock for the given task.
|
|
|
|
*/
|
2006-01-10 07:52:27 +03:00
|
|
|
static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long *sample)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
switch (CPUCLOCK_WHICH(which_clock)) {
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
case CPUCLOCK_PROF:
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = prof_ticks(p);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_VIRT:
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = virt_ticks(p);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_SCHED:
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = task_sched_runtime(p);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-02-11 13:30:27 +03:00
|
|
|
static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b)
|
|
|
|
{
|
2011-12-15 17:56:09 +04:00
|
|
|
if (b->utime > a->utime)
|
2009-02-11 13:30:27 +03:00
|
|
|
a->utime = b->utime;
|
|
|
|
|
2011-12-15 17:56:09 +04:00
|
|
|
if (b->stime > a->stime)
|
2009-02-11 13:30:27 +03:00
|
|
|
a->stime = b->stime;
|
|
|
|
|
|
|
|
if (b->sum_exec_runtime > a->sum_exec_runtime)
|
|
|
|
a->sum_exec_runtime = b->sum_exec_runtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times)
|
|
|
|
{
|
|
|
|
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
|
|
|
|
struct task_cputime sum;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!cputimer->running) {
|
|
|
|
/*
|
|
|
|
* The POSIX timer interface allows for absolute time expiry
|
|
|
|
* values through the TIMER_ABSTIME flag, therefore we have
|
|
|
|
* to synchronize the timer to the clock every time we start
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
thread_group_cputime(tsk, &sum);
|
Merge branch 'core-locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
* 'core-locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (27 commits)
rtmutex: Add missing rcu_read_unlock() in debug_rt_mutex_print_deadlock()
lockdep: Comment all warnings
lib: atomic64: Change the type of local lock to raw_spinlock_t
locking, lib/atomic64: Annotate atomic64_lock::lock as raw
locking, x86, iommu: Annotate qi->q_lock as raw
locking, x86, iommu: Annotate irq_2_ir_lock as raw
locking, x86, iommu: Annotate iommu->register_lock as raw
locking, dma, ipu: Annotate bank_lock as raw
locking, ARM: Annotate low level hw locks as raw
locking, drivers/dca: Annotate dca_lock as raw
locking, powerpc: Annotate uic->lock as raw
locking, x86: mce: Annotate cmci_discover_lock as raw
locking, ACPI: Annotate c3_lock as raw
locking, oprofile: Annotate oprofilefs lock as raw
locking, video: Annotate vga console lock as raw
locking, latencytop: Annotate latency_lock as raw
locking, timer_stats: Annotate table_lock as raw
locking, rwsem: Annotate inner lock as raw
locking, semaphores: Annotate inner lock as raw
locking, sched: Annotate thread_group_cputimer as raw
...
Fix up conflicts in kernel/posix-cpu-timers.c manually: making
cputimer->cputime a raw lock conflicted with the ABBA fix in commit
bcd5cff7216f ("cputimer: Cure lock inversion").
2011-10-26 18:17:32 +04:00
|
|
|
raw_spin_lock_irqsave(&cputimer->lock, flags);
|
2011-10-17 13:50:30 +04:00
|
|
|
cputimer->running = 1;
|
2009-02-11 13:30:27 +03:00
|
|
|
update_gt_cputime(&cputimer->cputime, &sum);
|
2011-10-17 13:50:30 +04:00
|
|
|
} else
|
Merge branch 'core-locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
* 'core-locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (27 commits)
rtmutex: Add missing rcu_read_unlock() in debug_rt_mutex_print_deadlock()
lockdep: Comment all warnings
lib: atomic64: Change the type of local lock to raw_spinlock_t
locking, lib/atomic64: Annotate atomic64_lock::lock as raw
locking, x86, iommu: Annotate qi->q_lock as raw
locking, x86, iommu: Annotate irq_2_ir_lock as raw
locking, x86, iommu: Annotate iommu->register_lock as raw
locking, dma, ipu: Annotate bank_lock as raw
locking, ARM: Annotate low level hw locks as raw
locking, drivers/dca: Annotate dca_lock as raw
locking, powerpc: Annotate uic->lock as raw
locking, x86: mce: Annotate cmci_discover_lock as raw
locking, ACPI: Annotate c3_lock as raw
locking, oprofile: Annotate oprofilefs lock as raw
locking, video: Annotate vga console lock as raw
locking, latencytop: Annotate latency_lock as raw
locking, timer_stats: Annotate table_lock as raw
locking, rwsem: Annotate inner lock as raw
locking, semaphores: Annotate inner lock as raw
locking, sched: Annotate thread_group_cputimer as raw
...
Fix up conflicts in kernel/posix-cpu-timers.c manually: making
cputimer->cputime a raw lock conflicted with the ABBA fix in commit
bcd5cff7216f ("cputimer: Cure lock inversion").
2011-10-26 18:17:32 +04:00
|
|
|
raw_spin_lock_irqsave(&cputimer->lock, flags);
|
2009-02-11 13:30:27 +03:00
|
|
|
*times = cputimer->cputime;
|
2009-07-25 20:56:56 +04:00
|
|
|
raw_spin_unlock_irqrestore(&cputimer->lock, flags);
|
2009-02-11 13:30:27 +03:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Sample a process (thread group) clock for the given group_leader task.
|
|
|
|
* Must be called with tasklist_lock held for reading.
|
|
|
|
*/
|
2008-09-12 20:54:39 +04:00
|
|
|
static int cpu_clock_sample_group(const clockid_t which_clock,
|
|
|
|
struct task_struct *p,
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long *sample)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
struct task_cputime cputime;
|
|
|
|
|
2008-11-24 17:46:31 +03:00
|
|
|
switch (CPUCLOCK_WHICH(which_clock)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
case CPUCLOCK_PROF:
|
2009-03-31 11:56:03 +04:00
|
|
|
thread_group_cputime(p, &cputime);
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = cputime_to_expires(cputime.utime + cputime.stime);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_VIRT:
|
2009-03-31 11:56:03 +04:00
|
|
|
thread_group_cputime(p, &cputime);
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = cputime_to_expires(cputime.utime);
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_SCHED:
|
posix-cpu-timers: Cure SMP wobbles
David reported:
Attached below is a watered-down version of rt/tst-cpuclock2.c from
GLIBC. Just build it with "gcc -o test test.c -lpthread -lrt" or
similar.
Run it several times, and you will see cases where the main thread
will measure a process clock difference before and after the nanosleep
which is smaller than the cpu-burner thread's individual thread clock
difference. This doesn't make any sense since the cpu-burner thread
is part of the top-level process's thread group.
I've reproduced this on both x86-64 and sparc64 (using both 32-bit and
64-bit binaries).
For example:
[davem@boricha build-x86_64-linux]$ ./test
process: before(0.001221967) after(0.498624371) diff(497402404)
thread: before(0.000081692) after(0.498316431) diff(498234739)
self: before(0.001223521) after(0.001240219) diff(16698)
[davem@boricha build-x86_64-linux]$
The diff of 'process' should always be >= the diff of 'thread'.
I make sure to wrap the 'thread' clock measurements the most tightly
around the nanosleep() call, and that the 'process' clock measurements
are the outer-most ones.
---
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
static pthread_barrier_t barrier;
static void *chew_cpu(void *arg)
{
pthread_barrier_wait(&barrier);
while (1)
__asm__ __volatile__("" : : : "memory");
return NULL;
}
int main(void)
{
clockid_t process_clock, my_thread_clock, th_clock;
struct timespec process_before, process_after;
struct timespec me_before, me_after;
struct timespec th_before, th_after;
struct timespec sleeptime;
unsigned long diff;
pthread_t th;
int err;
err = clock_getcpuclockid(0, &process_clock);
if (err)
return 1;
err = pthread_getcpuclockid(pthread_self(), &my_thread_clock);
if (err)
return 1;
pthread_barrier_init(&barrier, NULL, 2);
err = pthread_create(&th, NULL, chew_cpu, NULL);
if (err)
return 1;
err = pthread_getcpuclockid(th, &th_clock);
if (err)
return 1;
pthread_barrier_wait(&barrier);
err = clock_gettime(process_clock, &process_before);
if (err)
return 1;
err = clock_gettime(my_thread_clock, &me_before);
if (err)
return 1;
err = clock_gettime(th_clock, &th_before);
if (err)
return 1;
sleeptime.tv_sec = 0;
sleeptime.tv_nsec = 500000000;
nanosleep(&sleeptime, NULL);
err = clock_gettime(th_clock, &th_after);
if (err)
return 1;
err = clock_gettime(my_thread_clock, &me_after);
if (err)
return 1;
err = clock_gettime(process_clock, &process_after);
if (err)
return 1;
diff = process_after.tv_nsec - process_before.tv_nsec;
printf("process: before(%lu.%.9lu) after(%lu.%.9lu) diff(%lu)\n",
process_before.tv_sec, process_before.tv_nsec,
process_after.tv_sec, process_after.tv_nsec, diff);
diff = th_after.tv_nsec - th_before.tv_nsec;
printf("thread: before(%lu.%.9lu) after(%lu.%.9lu) diff(%lu)\n",
th_before.tv_sec, th_before.tv_nsec,
th_after.tv_sec, th_after.tv_nsec, diff);
diff = me_after.tv_nsec - me_before.tv_nsec;
printf("self: before(%lu.%.9lu) after(%lu.%.9lu) diff(%lu)\n",
me_before.tv_sec, me_before.tv_nsec,
me_after.tv_sec, me_after.tv_nsec, diff);
return 0;
}
This is due to us using p->se.sum_exec_runtime in
thread_group_cputime() where we iterate the thread group and sum all
data. This does not take time since the last schedule operation (tick
or otherwise) into account. We can cure this by using
task_sched_runtime() at the cost of having to take locks.
This also means we can (and must) do away with
thread_group_sched_runtime() since the modified thread_group_cputime()
is now more accurate and would deadlock when called from
thread_group_sched_runtime().
Aside of that it makes the function safe on 32 bit systems. The old
code added t->se.sum_exec_runtime unprotected. sum_exec_runtime is a
64bit value and could be changed on another cpu at the same time.
Reported-by: David Miller <davem@davemloft.net>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: stable@kernel.org
Link: http://lkml.kernel.org/r/1314874459.7945.22.camel@twins
Tested-by: David Miller <davem@davemloft.net>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2011-09-01 14:42:04 +04:00
|
|
|
thread_group_cputime(p, &cputime);
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = cputime.sum_exec_runtime;
|
2005-04-17 02:20:36 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
const pid_t pid = CPUCLOCK_PID(which_clock);
|
|
|
|
int error = -EINVAL;
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long rtn;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
/*
|
|
|
|
* Special case constant value for our own clocks.
|
|
|
|
* We don't have to do any lookup to find ourselves.
|
|
|
|
*/
|
|
|
|
if (CPUCLOCK_PERTHREAD(which_clock)) {
|
|
|
|
/*
|
|
|
|
* Sampling just ourselves we can do with no locking.
|
|
|
|
*/
|
|
|
|
error = cpu_clock_sample(which_clock,
|
|
|
|
current, &rtn);
|
|
|
|
} else {
|
|
|
|
read_lock(&tasklist_lock);
|
|
|
|
error = cpu_clock_sample_group(which_clock,
|
|
|
|
current, &rtn);
|
|
|
|
read_unlock(&tasklist_lock);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Find the given PID, and validate that the caller
|
|
|
|
* should be able to see it.
|
|
|
|
*/
|
|
|
|
struct task_struct *p;
|
2007-02-16 12:28:22 +03:00
|
|
|
rcu_read_lock();
|
2008-02-08 15:21:52 +03:00
|
|
|
p = find_task_by_vpid(pid);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (p) {
|
|
|
|
if (CPUCLOCK_PERTHREAD(which_clock)) {
|
2007-10-19 10:40:18 +04:00
|
|
|
if (same_thread_group(p, current)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
error = cpu_clock_sample(which_clock,
|
|
|
|
p, &rtn);
|
|
|
|
}
|
2007-02-16 12:28:22 +03:00
|
|
|
} else {
|
|
|
|
read_lock(&tasklist_lock);
|
2010-05-27 01:43:13 +04:00
|
|
|
if (thread_group_leader(p) && p->sighand) {
|
2007-02-16 12:28:22 +03:00
|
|
|
error =
|
|
|
|
cpu_clock_sample_group(which_clock,
|
|
|
|
p, &rtn);
|
|
|
|
}
|
|
|
|
read_unlock(&tasklist_lock);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
2007-02-16 12:28:22 +03:00
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
sample_to_timespec(which_clock, rtn, tp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
|
2009-11-18 01:14:13 +03:00
|
|
|
* This is called from sys_timer_create() and do_cpu_nanosleep() with the
|
|
|
|
* new timer already all-zeros initialized.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2011-02-01 16:52:12 +03:00
|
|
|
static int posix_cpu_timer_create(struct k_itimer *new_timer)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
const pid_t pid = CPUCLOCK_PID(new_timer->it_clock);
|
|
|
|
struct task_struct *p;
|
|
|
|
|
|
|
|
if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&new_timer->it.cpu.entry);
|
|
|
|
|
2010-11-03 19:52:56 +03:00
|
|
|
rcu_read_lock();
|
2005-04-17 02:20:36 +04:00
|
|
|
if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) {
|
|
|
|
if (pid == 0) {
|
|
|
|
p = current;
|
|
|
|
} else {
|
2008-02-08 15:21:52 +03:00
|
|
|
p = find_task_by_vpid(pid);
|
2007-10-19 10:40:18 +04:00
|
|
|
if (p && !same_thread_group(p, current))
|
2005-04-17 02:20:36 +04:00
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pid == 0) {
|
|
|
|
p = current->group_leader;
|
|
|
|
} else {
|
2008-02-08 15:21:52 +03:00
|
|
|
p = find_task_by_vpid(pid);
|
2010-11-03 19:52:56 +03:00
|
|
|
if (p && !has_group_leader_pid(p))
|
2005-04-17 02:20:36 +04:00
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_timer->it.cpu.task = p;
|
|
|
|
if (p) {
|
|
|
|
get_task_struct(p);
|
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2010-11-03 19:52:56 +03:00
|
|
|
rcu_read_unlock();
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up a CPU-clock timer that is about to be destroyed.
|
|
|
|
* This is called from timer deletion with the timer already locked.
|
|
|
|
* If we return TIMER_RETRY, it's necessary to release the timer's lock
|
|
|
|
* and try again. (This happens when the timer is in the middle of firing.)
|
|
|
|
*/
|
2011-02-01 16:52:12 +03:00
|
|
|
static int posix_cpu_timer_del(struct k_itimer *timer)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct task_struct *p = timer->it.cpu.task;
|
2005-10-23 20:25:39 +04:00
|
|
|
int ret = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2005-10-23 20:25:39 +04:00
|
|
|
if (likely(p != NULL)) {
|
2005-10-22 02:36:00 +04:00
|
|
|
read_lock(&tasklist_lock);
|
2010-05-27 01:43:13 +04:00
|
|
|
if (unlikely(p->sighand == NULL)) {
|
2005-10-22 02:36:00 +04:00
|
|
|
/*
|
|
|
|
* We raced with the reaping of the task.
|
|
|
|
* The deletion should have cleared us off the list.
|
|
|
|
*/
|
|
|
|
BUG_ON(!list_empty(&timer->it.cpu.entry));
|
|
|
|
} else {
|
|
|
|
spin_lock(&p->sighand->siglock);
|
2005-10-23 20:25:39 +04:00
|
|
|
if (timer->it.cpu.firing)
|
|
|
|
ret = TIMER_RETRY;
|
|
|
|
else
|
|
|
|
list_del(&timer->it.cpu.entry);
|
2005-10-22 02:36:00 +04:00
|
|
|
spin_unlock(&p->sighand->siglock);
|
|
|
|
}
|
|
|
|
read_unlock(&tasklist_lock);
|
2005-10-23 20:25:39 +04:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
put_task_struct(p);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2005-10-23 20:25:39 +04:00
|
|
|
return ret;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
static void cleanup_timers_list(struct list_head *head,
|
|
|
|
unsigned long long curr)
|
|
|
|
{
|
|
|
|
struct cpu_timer_list *timer, *next;
|
|
|
|
|
posix_timers: fix racy timer delta caching on task exit
When a task exits, we perform a caching of the remaining cputime delta
before expiring of its timers.
This is done from the following places:
* When the task is reaped. We iterate through its list of
posix cpu timers and store the remaining timer delta to
the timer struct instead of the absolute value.
(See posix_cpu_timers_exit() / posix_cpu_timers_exit_group() )
* When we call posix_cpu_timer_get() or posix_cpu_timer_schedule().
If the timer's task is considered dying when watched from these
places, the same conversion from absolute to relative expiry time
is performed. Then the given task's reference is released.
(See clear_dead_task() ).
The relevance of this caching is questionable but this is another
and deeper debate.
The big issue here is that these two sources of caching don't mix
up very well together.
More specifically, the caching can easily be done twice, resulting
in a wrong delta as it gets spuriously substracted a second time by
the elapsed clock. This can happen in the following scenario:
1) The task exits and gets reaped: we call posix_cpu_timers_exit()
and the absolute timer expiry values are converted to a relative
delta.
2) timer_gettime() -> posix_cpu_timer_get() is called and relies on
clear_dead_task() because tsk->exit_state == EXIT_DEAD.
The delta gets substracted again by the elapsed clock and we return
a wrong result.
To fix this, just remove the caching done on task reaping time. It
doesn't bring much value on its own. The caching done from
posix_cpu_timer_get/schedule is enough.
And it would also be hard to get it really right: we could make it put and
clear the target task in the timer struct so that readers know if they are
dealing with a relative cached of absolute value. But it would be racy.
The only safe way to do it would be to lock the itimer->it_lock so that we
know nobody reads the cputime expiry value while we modify it and its
target task reference. Doing so would involve some funny workarounds to
avoid circular lock against the sighand lock. There is just no reason to
maintain this.
The user visible effect of this patch can be observed by running the
following code: it creates a subthread that launches a posix cputimer
which expires after 10 seconds. But then the subthread only busy loops for 2
seconds and exits. The parent reaps the subthread and read the timer value.
Its expected value should the be the initial timer's expiration value
minus the cputime elapsed in the subthread. Roughly 10 - 2 = 8 seconds:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
static timer_t id;
static struct itimerspec val = { .it_value.tv_sec = 10, }, new;
static void *thread(void *unused)
{
int err;
struct timeval start, end, diff;
timer_create(CLOCK_THREAD_CPUTIME_ID, NULL, &id);
if (err < 0) {
perror("Can't create timer\n");
return NULL;
}
/* Arm 10 sec timer */
err = timer_settime(id, 0, &val, NULL);
if (err < 0) {
perror("Can't set timer\n");
return NULL;
}
/* Exit after 2 seconds of execution */
gettimeofday(&start, NULL);
do {
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
} while (diff.tv_sec < 2);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t pthread;
int err;
err = pthread_create(&pthread, NULL, thread, NULL);
if (err) {
perror("Can't create thread\n");
return -1;
}
pthread_join(pthread, NULL);
/* Just wait a little bit to make sure the child got reaped */
sleep(1);
err = timer_gettime(id, &new);
if (err)
perror("Can't get timer value\n");
printf("%d %ld\n", new.it_value.tv_sec, new.it_value.tv_nsec);
return 0;
}
Before the patch:
$ ./posix_cpu_timers
6 2278074
After the patch:
$ ./posix_cpu_timers
8 1158766
Before the patch, the elapsed time got two more seconds spuriously accounted.
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
Cc: Olivier Langlois <olivier@trillion01.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2013-06-28 04:06:43 +04:00
|
|
|
list_for_each_entry_safe(timer, next, head, entry)
|
2013-06-28 04:06:42 +04:00
|
|
|
list_del_init(&timer->entry);
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Clean out CPU timers still ticking when a thread exited. The task
|
|
|
|
* pointer is cleared, and the expiry time is replaced with the residual
|
|
|
|
* time for later timer_gettime calls to return.
|
|
|
|
* This must be called with the siglock held.
|
|
|
|
*/
|
|
|
|
static void cleanup_timers(struct list_head *head,
|
|
|
|
cputime_t utime, cputime_t stime,
|
2007-07-09 20:51:58 +04:00
|
|
|
unsigned long long sum_exec_runtime)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
cputime_t ptime = utime + stime;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
cleanup_timers_list(head, cputime_to_expires(ptime));
|
|
|
|
cleanup_timers_list(++head, cputime_to_expires(utime));
|
|
|
|
cleanup_timers_list(++head, sum_exec_runtime);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are both called with the siglock held, when the current thread
|
|
|
|
* is being reaped. When the final (leader) thread in the group is reaped,
|
|
|
|
* posix_cpu_timers_exit_group will be called after posix_cpu_timers_exit.
|
|
|
|
*/
|
|
|
|
void posix_cpu_timers_exit(struct task_struct *tsk)
|
|
|
|
{
|
2012-11-13 17:20:55 +04:00
|
|
|
cputime_t utime, stime;
|
|
|
|
|
2012-12-17 07:18:11 +04:00
|
|
|
add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
|
|
|
|
sizeof(unsigned long long));
|
2012-11-13 17:20:55 +04:00
|
|
|
task_cputime(tsk, &utime, &stime);
|
2005-04-17 02:20:36 +04:00
|
|
|
cleanup_timers(tsk->cpu_timers,
|
2012-11-13 17:20:55 +04:00
|
|
|
utime, stime, tsk->se.sum_exec_runtime);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
void posix_cpu_timers_exit_group(struct task_struct *tsk)
|
|
|
|
{
|
2009-08-07 03:03:30 +04:00
|
|
|
struct signal_struct *const sig = tsk->signal;
|
2012-11-13 17:20:55 +04:00
|
|
|
cputime_t utime, stime;
|
2005-10-24 14:36:28 +04:00
|
|
|
|
2012-11-13 17:20:55 +04:00
|
|
|
task_cputime(tsk, &utime, &stime);
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
cleanup_timers(tsk->signal->cpu_timers,
|
2012-11-13 17:20:55 +04:00
|
|
|
utime + sig->utime, stime + sig->stime,
|
2009-08-07 03:03:30 +04:00
|
|
|
tsk->se.sum_exec_runtime + sig->sum_sched_runtime);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
posix_timers: fix racy timer delta caching on task exit
When a task exits, we perform a caching of the remaining cputime delta
before expiring of its timers.
This is done from the following places:
* When the task is reaped. We iterate through its list of
posix cpu timers and store the remaining timer delta to
the timer struct instead of the absolute value.
(See posix_cpu_timers_exit() / posix_cpu_timers_exit_group() )
* When we call posix_cpu_timer_get() or posix_cpu_timer_schedule().
If the timer's task is considered dying when watched from these
places, the same conversion from absolute to relative expiry time
is performed. Then the given task's reference is released.
(See clear_dead_task() ).
The relevance of this caching is questionable but this is another
and deeper debate.
The big issue here is that these two sources of caching don't mix
up very well together.
More specifically, the caching can easily be done twice, resulting
in a wrong delta as it gets spuriously substracted a second time by
the elapsed clock. This can happen in the following scenario:
1) The task exits and gets reaped: we call posix_cpu_timers_exit()
and the absolute timer expiry values are converted to a relative
delta.
2) timer_gettime() -> posix_cpu_timer_get() is called and relies on
clear_dead_task() because tsk->exit_state == EXIT_DEAD.
The delta gets substracted again by the elapsed clock and we return
a wrong result.
To fix this, just remove the caching done on task reaping time. It
doesn't bring much value on its own. The caching done from
posix_cpu_timer_get/schedule is enough.
And it would also be hard to get it really right: we could make it put and
clear the target task in the timer struct so that readers know if they are
dealing with a relative cached of absolute value. But it would be racy.
The only safe way to do it would be to lock the itimer->it_lock so that we
know nobody reads the cputime expiry value while we modify it and its
target task reference. Doing so would involve some funny workarounds to
avoid circular lock against the sighand lock. There is just no reason to
maintain this.
The user visible effect of this patch can be observed by running the
following code: it creates a subthread that launches a posix cputimer
which expires after 10 seconds. But then the subthread only busy loops for 2
seconds and exits. The parent reaps the subthread and read the timer value.
Its expected value should the be the initial timer's expiration value
minus the cputime elapsed in the subthread. Roughly 10 - 2 = 8 seconds:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
static timer_t id;
static struct itimerspec val = { .it_value.tv_sec = 10, }, new;
static void *thread(void *unused)
{
int err;
struct timeval start, end, diff;
timer_create(CLOCK_THREAD_CPUTIME_ID, NULL, &id);
if (err < 0) {
perror("Can't create timer\n");
return NULL;
}
/* Arm 10 sec timer */
err = timer_settime(id, 0, &val, NULL);
if (err < 0) {
perror("Can't set timer\n");
return NULL;
}
/* Exit after 2 seconds of execution */
gettimeofday(&start, NULL);
do {
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
} while (diff.tv_sec < 2);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t pthread;
int err;
err = pthread_create(&pthread, NULL, thread, NULL);
if (err) {
perror("Can't create thread\n");
return -1;
}
pthread_join(pthread, NULL);
/* Just wait a little bit to make sure the child got reaped */
sleep(1);
err = timer_gettime(id, &new);
if (err)
perror("Can't get timer value\n");
printf("%d %ld\n", new.it_value.tv_sec, new.it_value.tv_nsec);
return 0;
}
Before the patch:
$ ./posix_cpu_timers
6 2278074
After the patch:
$ ./posix_cpu_timers
8 1158766
Before the patch, the elapsed time got two more seconds spuriously accounted.
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
Cc: Olivier Langlois <olivier@trillion01.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2013-06-28 04:06:43 +04:00
|
|
|
static void clear_dead_task(struct k_itimer *itimer, unsigned long long now)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
posix_timers: fix racy timer delta caching on task exit
When a task exits, we perform a caching of the remaining cputime delta
before expiring of its timers.
This is done from the following places:
* When the task is reaped. We iterate through its list of
posix cpu timers and store the remaining timer delta to
the timer struct instead of the absolute value.
(See posix_cpu_timers_exit() / posix_cpu_timers_exit_group() )
* When we call posix_cpu_timer_get() or posix_cpu_timer_schedule().
If the timer's task is considered dying when watched from these
places, the same conversion from absolute to relative expiry time
is performed. Then the given task's reference is released.
(See clear_dead_task() ).
The relevance of this caching is questionable but this is another
and deeper debate.
The big issue here is that these two sources of caching don't mix
up very well together.
More specifically, the caching can easily be done twice, resulting
in a wrong delta as it gets spuriously substracted a second time by
the elapsed clock. This can happen in the following scenario:
1) The task exits and gets reaped: we call posix_cpu_timers_exit()
and the absolute timer expiry values are converted to a relative
delta.
2) timer_gettime() -> posix_cpu_timer_get() is called and relies on
clear_dead_task() because tsk->exit_state == EXIT_DEAD.
The delta gets substracted again by the elapsed clock and we return
a wrong result.
To fix this, just remove the caching done on task reaping time. It
doesn't bring much value on its own. The caching done from
posix_cpu_timer_get/schedule is enough.
And it would also be hard to get it really right: we could make it put and
clear the target task in the timer struct so that readers know if they are
dealing with a relative cached of absolute value. But it would be racy.
The only safe way to do it would be to lock the itimer->it_lock so that we
know nobody reads the cputime expiry value while we modify it and its
target task reference. Doing so would involve some funny workarounds to
avoid circular lock against the sighand lock. There is just no reason to
maintain this.
The user visible effect of this patch can be observed by running the
following code: it creates a subthread that launches a posix cputimer
which expires after 10 seconds. But then the subthread only busy loops for 2
seconds and exits. The parent reaps the subthread and read the timer value.
Its expected value should the be the initial timer's expiration value
minus the cputime elapsed in the subthread. Roughly 10 - 2 = 8 seconds:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
static timer_t id;
static struct itimerspec val = { .it_value.tv_sec = 10, }, new;
static void *thread(void *unused)
{
int err;
struct timeval start, end, diff;
timer_create(CLOCK_THREAD_CPUTIME_ID, NULL, &id);
if (err < 0) {
perror("Can't create timer\n");
return NULL;
}
/* Arm 10 sec timer */
err = timer_settime(id, 0, &val, NULL);
if (err < 0) {
perror("Can't set timer\n");
return NULL;
}
/* Exit after 2 seconds of execution */
gettimeofday(&start, NULL);
do {
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
} while (diff.tv_sec < 2);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t pthread;
int err;
err = pthread_create(&pthread, NULL, thread, NULL);
if (err) {
perror("Can't create thread\n");
return -1;
}
pthread_join(pthread, NULL);
/* Just wait a little bit to make sure the child got reaped */
sleep(1);
err = timer_gettime(id, &new);
if (err)
perror("Can't get timer value\n");
printf("%d %ld\n", new.it_value.tv_sec, new.it_value.tv_nsec);
return 0;
}
Before the patch:
$ ./posix_cpu_timers
6 2278074
After the patch:
$ ./posix_cpu_timers
8 1158766
Before the patch, the elapsed time got two more seconds spuriously accounted.
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
Cc: Olivier Langlois <olivier@trillion01.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2013-06-28 04:06:43 +04:00
|
|
|
struct cpu_timer_list *timer = &itimer->it.cpu;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* That's all for this thread or process.
|
|
|
|
* We leave our residual in expires to be reported.
|
|
|
|
*/
|
posix_timers: fix racy timer delta caching on task exit
When a task exits, we perform a caching of the remaining cputime delta
before expiring of its timers.
This is done from the following places:
* When the task is reaped. We iterate through its list of
posix cpu timers and store the remaining timer delta to
the timer struct instead of the absolute value.
(See posix_cpu_timers_exit() / posix_cpu_timers_exit_group() )
* When we call posix_cpu_timer_get() or posix_cpu_timer_schedule().
If the timer's task is considered dying when watched from these
places, the same conversion from absolute to relative expiry time
is performed. Then the given task's reference is released.
(See clear_dead_task() ).
The relevance of this caching is questionable but this is another
and deeper debate.
The big issue here is that these two sources of caching don't mix
up very well together.
More specifically, the caching can easily be done twice, resulting
in a wrong delta as it gets spuriously substracted a second time by
the elapsed clock. This can happen in the following scenario:
1) The task exits and gets reaped: we call posix_cpu_timers_exit()
and the absolute timer expiry values are converted to a relative
delta.
2) timer_gettime() -> posix_cpu_timer_get() is called and relies on
clear_dead_task() because tsk->exit_state == EXIT_DEAD.
The delta gets substracted again by the elapsed clock and we return
a wrong result.
To fix this, just remove the caching done on task reaping time. It
doesn't bring much value on its own. The caching done from
posix_cpu_timer_get/schedule is enough.
And it would also be hard to get it really right: we could make it put and
clear the target task in the timer struct so that readers know if they are
dealing with a relative cached of absolute value. But it would be racy.
The only safe way to do it would be to lock the itimer->it_lock so that we
know nobody reads the cputime expiry value while we modify it and its
target task reference. Doing so would involve some funny workarounds to
avoid circular lock against the sighand lock. There is just no reason to
maintain this.
The user visible effect of this patch can be observed by running the
following code: it creates a subthread that launches a posix cputimer
which expires after 10 seconds. But then the subthread only busy loops for 2
seconds and exits. The parent reaps the subthread and read the timer value.
Its expected value should the be the initial timer's expiration value
minus the cputime elapsed in the subthread. Roughly 10 - 2 = 8 seconds:
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
static timer_t id;
static struct itimerspec val = { .it_value.tv_sec = 10, }, new;
static void *thread(void *unused)
{
int err;
struct timeval start, end, diff;
timer_create(CLOCK_THREAD_CPUTIME_ID, NULL, &id);
if (err < 0) {
perror("Can't create timer\n");
return NULL;
}
/* Arm 10 sec timer */
err = timer_settime(id, 0, &val, NULL);
if (err < 0) {
perror("Can't set timer\n");
return NULL;
}
/* Exit after 2 seconds of execution */
gettimeofday(&start, NULL);
do {
gettimeofday(&end, NULL);
timersub(&end, &start, &diff);
} while (diff.tv_sec < 2);
return NULL;
}
int main(int argc, char **argv)
{
pthread_t pthread;
int err;
err = pthread_create(&pthread, NULL, thread, NULL);
if (err) {
perror("Can't create thread\n");
return -1;
}
pthread_join(pthread, NULL);
/* Just wait a little bit to make sure the child got reaped */
sleep(1);
err = timer_gettime(id, &new);
if (err)
perror("Can't get timer value\n");
printf("%d %ld\n", new.it_value.tv_sec, new.it_value.tv_nsec);
return 0;
}
Before the patch:
$ ./posix_cpu_timers
6 2278074
After the patch:
$ ./posix_cpu_timers
8 1158766
Before the patch, the elapsed time got two more seconds spuriously accounted.
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
Cc: Olivier Langlois <olivier@trillion01.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2013-06-28 04:06:43 +04:00
|
|
|
put_task_struct(timer->task);
|
|
|
|
timer->task = NULL;
|
|
|
|
if (timer->expires < now) {
|
|
|
|
timer->expires = 0;
|
|
|
|
} else {
|
|
|
|
timer->expires -= now;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2009-07-29 14:15:28 +04:00
|
|
|
static inline int expires_gt(cputime_t expires, cputime_t new_exp)
|
|
|
|
{
|
2011-12-15 17:56:09 +04:00
|
|
|
return expires == 0 || expires > new_exp;
|
2009-07-29 14:15:28 +04:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Insert the timer on the appropriate list before any timers that
|
|
|
|
* expire later. This must be called with the tasklist_lock held
|
2010-03-12 01:04:42 +03:00
|
|
|
* for reading, interrupts disabled and p->sighand->siglock taken.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2010-03-12 01:04:38 +03:00
|
|
|
static void arm_timer(struct k_itimer *timer)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct task_struct *p = timer->it.cpu.task;
|
|
|
|
struct list_head *head, *listpos;
|
2010-03-12 01:04:38 +03:00
|
|
|
struct task_cputime *cputime_expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
struct cpu_timer_list *const nt = &timer->it.cpu;
|
|
|
|
struct cpu_timer_list *next;
|
|
|
|
|
2010-03-12 01:04:38 +03:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
|
|
|
|
head = p->cpu_timers;
|
|
|
|
cputime_expires = &p->cputime_expires;
|
|
|
|
} else {
|
|
|
|
head = p->signal->cpu_timers;
|
|
|
|
cputime_expires = &p->signal->cputime_expires;
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
head += CPUCLOCK_WHICH(timer->it_clock);
|
|
|
|
|
|
|
|
listpos = head;
|
2010-03-12 01:04:38 +03:00
|
|
|
list_for_each_entry(next, head, entry) {
|
2013-06-28 04:06:42 +04:00
|
|
|
if (nt->expires < next->expires)
|
2010-03-12 01:04:38 +03:00
|
|
|
break;
|
|
|
|
listpos = &next->entry;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
list_add(&nt->entry, listpos);
|
|
|
|
|
|
|
|
if (listpos == head) {
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long exp = nt->expires;
|
2010-03-12 01:04:38 +03:00
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2010-03-12 01:04:38 +03:00
|
|
|
* We are the new earliest-expiring POSIX 1.b timer, hence
|
|
|
|
* need to update expiration cache. Take into account that
|
|
|
|
* for process timers we share expiration cache with itimers
|
|
|
|
* and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
|
2010-03-12 01:04:38 +03:00
|
|
|
switch (CPUCLOCK_WHICH(timer->it_clock)) {
|
|
|
|
case CPUCLOCK_PROF:
|
2013-06-28 04:06:42 +04:00
|
|
|
if (expires_gt(cputime_expires->prof_exp, expires_to_cputime(exp)))
|
|
|
|
cputime_expires->prof_exp = expires_to_cputime(exp);
|
2010-03-12 01:04:38 +03:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_VIRT:
|
2013-06-28 04:06:42 +04:00
|
|
|
if (expires_gt(cputime_expires->virt_exp, expires_to_cputime(exp)))
|
|
|
|
cputime_expires->virt_exp = expires_to_cputime(exp);
|
2010-03-12 01:04:38 +03:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_SCHED:
|
|
|
|
if (cputime_expires->sched_exp == 0 ||
|
2013-06-28 04:06:42 +04:00
|
|
|
cputime_expires->sched_exp > exp)
|
|
|
|
cputime_expires->sched_exp = exp;
|
2010-03-12 01:04:38 +03:00
|
|
|
break;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The timer is locked, fire it and arrange for its reload.
|
|
|
|
*/
|
|
|
|
static void cpu_timer_fire(struct k_itimer *timer)
|
|
|
|
{
|
cpu-timers: Change SIGEV_NONE timer implementation
When user sets up a timer without associated signal and process does
not use any other cpu timers and does not exit, tsk->signal->cputimer
is enabled and running forever.
Avoid running the timer for no reason.
I used below program to check patch does not break current user space
visible behavior.
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
void consume_cpu(void)
{
int i = 0;
int count = 0;
for(i=0; i<100000000; i++)
count++;
}
int main(void)
{
int i;
struct sigaction act;
struct sigevent evt = { };
timer_t tid;
struct itimerspec spec = { };
evt.sigev_notify = SIGEV_NONE;
assert(timer_create(CLOCK_PROCESS_CPUTIME_ID, &evt, &tid) == 0);
spec.it_value.tv_sec = 10;
assert(timer_settime(tid, 0, &spec, NULL) == 0);
for (i = 0; i < 30; i++) {
consume_cpu();
memset(&spec, 0, sizeof(spec));
assert(timer_gettime(tid, &spec) == 0);
printf("%lu.%09lu\n",
(unsigned long) spec.it_value.tv_sec,
(unsigned long) spec.it_value.tv_nsec);
}
assert(timer_delete(tid) == 0);
return 0;
}
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2010-03-12 01:04:41 +03:00
|
|
|
if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
|
|
|
|
/*
|
|
|
|
* User don't want any signal.
|
|
|
|
*/
|
2013-06-28 04:06:42 +04:00
|
|
|
timer->it.cpu.expires = 0;
|
cpu-timers: Change SIGEV_NONE timer implementation
When user sets up a timer without associated signal and process does
not use any other cpu timers and does not exit, tsk->signal->cputimer
is enabled and running forever.
Avoid running the timer for no reason.
I used below program to check patch does not break current user space
visible behavior.
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
void consume_cpu(void)
{
int i = 0;
int count = 0;
for(i=0; i<100000000; i++)
count++;
}
int main(void)
{
int i;
struct sigaction act;
struct sigevent evt = { };
timer_t tid;
struct itimerspec spec = { };
evt.sigev_notify = SIGEV_NONE;
assert(timer_create(CLOCK_PROCESS_CPUTIME_ID, &evt, &tid) == 0);
spec.it_value.tv_sec = 10;
assert(timer_settime(tid, 0, &spec, NULL) == 0);
for (i = 0; i < 30; i++) {
consume_cpu();
memset(&spec, 0, sizeof(spec));
assert(timer_gettime(tid, &spec) == 0);
printf("%lu.%09lu\n",
(unsigned long) spec.it_value.tv_sec,
(unsigned long) spec.it_value.tv_nsec);
}
assert(timer_delete(tid) == 0);
return 0;
}
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2010-03-12 01:04:41 +03:00
|
|
|
} else if (unlikely(timer->sigq == NULL)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* This a special case for clock_nanosleep,
|
|
|
|
* not a normal timer from sys_timer_create.
|
|
|
|
*/
|
|
|
|
wake_up_process(timer->it_process);
|
2013-06-28 04:06:42 +04:00
|
|
|
timer->it.cpu.expires = 0;
|
|
|
|
} else if (timer->it.cpu.incr == 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* One-shot timer. Clear it as soon as it's fired.
|
|
|
|
*/
|
|
|
|
posix_timer_event(timer, 0);
|
2013-06-28 04:06:42 +04:00
|
|
|
timer->it.cpu.expires = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else if (posix_timer_event(timer, ++timer->it_requeue_pending)) {
|
|
|
|
/*
|
|
|
|
* The signal did not get queued because the signal
|
|
|
|
* was ignored, so we won't get any callback to
|
|
|
|
* reload the timer. But we need to keep it
|
|
|
|
* ticking in case the signal is deliverable next time.
|
|
|
|
*/
|
|
|
|
posix_cpu_timer_schedule(timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-12 17:00:52 +03:00
|
|
|
/*
|
|
|
|
* Sample a process (thread group) timer for the given group_leader task.
|
|
|
|
* Must be called with tasklist_lock held for reading.
|
|
|
|
*/
|
|
|
|
static int cpu_timer_sample_group(const clockid_t which_clock,
|
|
|
|
struct task_struct *p,
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long *sample)
|
2009-02-12 17:00:52 +03:00
|
|
|
{
|
|
|
|
struct task_cputime cputime;
|
|
|
|
|
|
|
|
thread_group_cputimer(p, &cputime);
|
|
|
|
switch (CPUCLOCK_WHICH(which_clock)) {
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
case CPUCLOCK_PROF:
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = cputime_to_expires(cputime.utime + cputime.stime);
|
2009-02-12 17:00:52 +03:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_VIRT:
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = cputime_to_expires(cputime.utime);
|
2009-02-12 17:00:52 +03:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_SCHED:
|
2013-06-28 04:06:42 +04:00
|
|
|
*sample = cputime.sum_exec_runtime + task_delta_exec(p);
|
2009-02-12 17:00:52 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-18 03:31:13 +04:00
|
|
|
#ifdef CONFIG_NO_HZ_FULL
|
|
|
|
static void nohz_kick_work_fn(struct work_struct *work)
|
|
|
|
{
|
|
|
|
tick_nohz_full_kick_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
static DECLARE_WORK(nohz_kick_work, nohz_kick_work_fn);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need the IPIs to be sent from sane process context.
|
|
|
|
* The posix cpu timers are always set with irqs disabled.
|
|
|
|
*/
|
|
|
|
static void posix_cpu_timer_kick_nohz(void)
|
|
|
|
{
|
|
|
|
schedule_work(&nohz_kick_work);
|
|
|
|
}
|
2013-04-19 18:17:38 +04:00
|
|
|
|
|
|
|
bool posix_cpu_timers_can_stop_tick(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (!task_cputime_zero(&tsk->cputime_expires))
|
2013-04-21 22:28:38 +04:00
|
|
|
return false;
|
2013-04-19 18:17:38 +04:00
|
|
|
|
|
|
|
if (tsk->signal->cputimer.running)
|
2013-04-21 22:28:38 +04:00
|
|
|
return false;
|
2013-04-19 18:17:38 +04:00
|
|
|
|
2013-04-21 22:28:38 +04:00
|
|
|
return true;
|
2013-04-19 18:17:38 +04:00
|
|
|
}
|
2013-04-18 03:31:13 +04:00
|
|
|
#else
|
|
|
|
static inline void posix_cpu_timer_kick_nohz(void) { }
|
|
|
|
#endif
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Guts of sys_timer_settime for CPU timers.
|
|
|
|
* This is called with the timer locked and interrupts disabled.
|
|
|
|
* If we return TIMER_RETRY, it's necessary to release the timer's lock
|
|
|
|
* and try again. (This happens when the timer is in the middle of firing.)
|
|
|
|
*/
|
2011-02-01 16:52:12 +03:00
|
|
|
static int posix_cpu_timer_set(struct k_itimer *timer, int flags,
|
|
|
|
struct itimerspec *new, struct itimerspec *old)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct task_struct *p = timer->it.cpu.task;
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long old_expires, new_expires, old_incr, val;
|
2005-04-17 02:20:36 +04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (unlikely(p == NULL)) {
|
|
|
|
/*
|
|
|
|
* Timer refers to a dead task's clock.
|
|
|
|
*/
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_expires = timespec_to_sample(timer->it_clock, &new->it_value);
|
|
|
|
|
|
|
|
read_lock(&tasklist_lock);
|
|
|
|
/*
|
|
|
|
* We need the tasklist_lock to protect against reaping that
|
2010-05-27 01:43:13 +04:00
|
|
|
* clears p->sighand. If p has just been reaped, we can no
|
2005-04-17 02:20:36 +04:00
|
|
|
* longer get any information about it at all.
|
|
|
|
*/
|
2010-05-27 01:43:13 +04:00
|
|
|
if (unlikely(p->sighand == NULL)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
read_unlock(&tasklist_lock);
|
|
|
|
put_task_struct(p);
|
|
|
|
timer->it.cpu.task = NULL;
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disarm any old timer after extracting its expiry time.
|
|
|
|
*/
|
|
|
|
BUG_ON(!irqs_disabled());
|
2005-10-24 18:29:58 +04:00
|
|
|
|
|
|
|
ret = 0;
|
cpu-timers: Return correct previous timer reload value
According POSIX we need to correctly set old timer it_interval value when
user request that in timer_settime(). Tested using below program.
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
int main(void)
{
struct sigaction act;
struct sigevent evt = { };
timer_t tid;
struct itimerspec spec, u_spec, k_spec;
evt.sigev_notify = SIGEV_SIGNAL;
evt.sigev_signo = SIGPROF;
assert(timer_create(CLOCK_PROCESS_CPUTIME_ID, &evt, &tid) == 0);
spec.it_value.tv_sec = 1;
spec.it_value.tv_nsec = 2;
spec.it_interval.tv_sec = 3;
spec.it_interval.tv_nsec = 4;
u_spec = spec;
assert(timer_settime(tid, 0, &spec, NULL) == 0);
spec.it_value.tv_sec = 5;
spec.it_value.tv_nsec = 6;
spec.it_interval.tv_sec = 7;
spec.it_interval.tv_nsec = 8;
assert(timer_settime(tid, 0, &spec, &k_spec) == 0);
#define PRT(val) printf(#val ":\t%d/%d\n", (int) u_spec.val, (int) k_spec.val)
PRT(it_value.tv_sec);
PRT(it_value.tv_nsec);
PRT(it_interval.tv_sec);
PRT(it_interval.tv_nsec);
return 0;
}
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2010-03-12 01:04:39 +03:00
|
|
|
old_incr = timer->it.cpu.incr;
|
2005-04-17 02:20:36 +04:00
|
|
|
spin_lock(&p->sighand->siglock);
|
|
|
|
old_expires = timer->it.cpu.expires;
|
2005-10-24 18:29:58 +04:00
|
|
|
if (unlikely(timer->it.cpu.firing)) {
|
|
|
|
timer->it.cpu.firing = -1;
|
|
|
|
ret = TIMER_RETRY;
|
|
|
|
} else
|
|
|
|
list_del_init(&timer->it.cpu.entry);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to sample the current value to convert the new
|
|
|
|
* value from to relative and absolute, and to convert the
|
|
|
|
* old value from absolute to relative. To set a process
|
|
|
|
* timer, we need a sample to balance the thread expiry
|
|
|
|
* times (in arm_timer). With an absolute time, we must
|
|
|
|
* check if it's already passed. In short, we need a sample.
|
|
|
|
*/
|
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
|
|
|
|
cpu_clock_sample(timer->it_clock, p, &val);
|
|
|
|
} else {
|
2009-02-12 17:00:52 +03:00
|
|
|
cpu_timer_sample_group(timer->it_clock, p, &val);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old) {
|
2013-06-28 04:06:42 +04:00
|
|
|
if (old_expires == 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
old->it_value.tv_sec = 0;
|
|
|
|
old->it_value.tv_nsec = 0;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Update the timer in case it has
|
|
|
|
* overrun already. If it has,
|
|
|
|
* we'll report it as having overrun
|
|
|
|
* and with the next reloaded timer
|
|
|
|
* already ticking, though we are
|
|
|
|
* swallowing that pending
|
|
|
|
* notification here to install the
|
|
|
|
* new setting.
|
|
|
|
*/
|
|
|
|
bump_cpu_timer(timer, val);
|
2013-06-28 04:06:42 +04:00
|
|
|
if (val < timer->it.cpu.expires) {
|
|
|
|
old_expires = timer->it.cpu.expires - val;
|
2005-04-17 02:20:36 +04:00
|
|
|
sample_to_timespec(timer->it_clock,
|
|
|
|
old_expires,
|
|
|
|
&old->it_value);
|
|
|
|
} else {
|
|
|
|
old->it_value.tv_nsec = 1;
|
|
|
|
old->it_value.tv_sec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-24 18:29:58 +04:00
|
|
|
if (unlikely(ret)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* We are colliding with the timer actually firing.
|
|
|
|
* Punt after filling in the timer's old value, and
|
|
|
|
* disable this firing since we are already reporting
|
|
|
|
* it as an overrun (thanks to bump_cpu_timer above).
|
|
|
|
*/
|
2010-03-12 01:04:42 +03:00
|
|
|
spin_unlock(&p->sighand->siglock);
|
2005-04-17 02:20:36 +04:00
|
|
|
read_unlock(&tasklist_lock);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
if (new_expires != 0 && !(flags & TIMER_ABSTIME)) {
|
|
|
|
new_expires += val;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Install the new expiry time (or zero).
|
|
|
|
* For a timer with no notification action, we don't actually
|
|
|
|
* arm the timer (we'll just fake it for timer_gettime).
|
|
|
|
*/
|
|
|
|
timer->it.cpu.expires = new_expires;
|
2013-06-28 04:06:42 +04:00
|
|
|
if (new_expires != 0 && val < new_expires) {
|
2010-03-12 01:04:38 +03:00
|
|
|
arm_timer(timer);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2010-03-12 01:04:42 +03:00
|
|
|
spin_unlock(&p->sighand->siglock);
|
2005-04-17 02:20:36 +04:00
|
|
|
read_unlock(&tasklist_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Install the new reload setting, and
|
|
|
|
* set up the signal and overrun bookkeeping.
|
|
|
|
*/
|
|
|
|
timer->it.cpu.incr = timespec_to_sample(timer->it_clock,
|
|
|
|
&new->it_interval);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This acts as a modification timestamp for the timer,
|
|
|
|
* so any automatic reload attempt will punt on seeing
|
|
|
|
* that we have reset the timer manually.
|
|
|
|
*/
|
|
|
|
timer->it_requeue_pending = (timer->it_requeue_pending + 2) &
|
|
|
|
~REQUEUE_PENDING;
|
|
|
|
timer->it_overrun_last = 0;
|
|
|
|
timer->it_overrun = -1;
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
if (new_expires != 0 && !(val < new_expires)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* The designated time already passed, so we notify
|
|
|
|
* immediately, even if the thread never runs to
|
|
|
|
* accumulate more time on this clock.
|
|
|
|
*/
|
|
|
|
cpu_timer_fire(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
if (old) {
|
|
|
|
sample_to_timespec(timer->it_clock,
|
cpu-timers: Return correct previous timer reload value
According POSIX we need to correctly set old timer it_interval value when
user request that in timer_settime(). Tested using below program.
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
int main(void)
{
struct sigaction act;
struct sigevent evt = { };
timer_t tid;
struct itimerspec spec, u_spec, k_spec;
evt.sigev_notify = SIGEV_SIGNAL;
evt.sigev_signo = SIGPROF;
assert(timer_create(CLOCK_PROCESS_CPUTIME_ID, &evt, &tid) == 0);
spec.it_value.tv_sec = 1;
spec.it_value.tv_nsec = 2;
spec.it_interval.tv_sec = 3;
spec.it_interval.tv_nsec = 4;
u_spec = spec;
assert(timer_settime(tid, 0, &spec, NULL) == 0);
spec.it_value.tv_sec = 5;
spec.it_value.tv_nsec = 6;
spec.it_interval.tv_sec = 7;
spec.it_interval.tv_nsec = 8;
assert(timer_settime(tid, 0, &spec, &k_spec) == 0);
#define PRT(val) printf(#val ":\t%d/%d\n", (int) u_spec.val, (int) k_spec.val)
PRT(it_value.tv_sec);
PRT(it_value.tv_nsec);
PRT(it_interval.tv_sec);
PRT(it_interval.tv_nsec);
return 0;
}
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
Cc: Balbir Singh <balbir@in.ibm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
2010-03-12 01:04:39 +03:00
|
|
|
old_incr, &old->it_interval);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2013-04-18 03:31:13 +04:00
|
|
|
if (!ret)
|
|
|
|
posix_cpu_timer_kick_nohz();
|
2005-04-17 02:20:36 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec *itp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long now;
|
2005-04-17 02:20:36 +04:00
|
|
|
struct task_struct *p = timer->it.cpu.task;
|
|
|
|
int clear_dead;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Easy part: convert the reload time.
|
|
|
|
*/
|
|
|
|
sample_to_timespec(timer->it_clock,
|
|
|
|
timer->it.cpu.incr, &itp->it_interval);
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
if (timer->it.cpu.expires == 0) { /* Timer not armed at all. */
|
2005-04-17 02:20:36 +04:00
|
|
|
itp->it_value.tv_sec = itp->it_value.tv_nsec = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(p == NULL)) {
|
|
|
|
/*
|
|
|
|
* This task already died and the timer will never fire.
|
|
|
|
* In this case, expires is actually the dead value.
|
|
|
|
*/
|
|
|
|
dead:
|
|
|
|
sample_to_timespec(timer->it_clock, timer->it.cpu.expires,
|
|
|
|
&itp->it_value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sample the clock to take the difference with the expiry time.
|
|
|
|
*/
|
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
|
|
|
|
cpu_clock_sample(timer->it_clock, p, &now);
|
|
|
|
clear_dead = p->exit_state;
|
|
|
|
} else {
|
|
|
|
read_lock(&tasklist_lock);
|
2010-05-27 01:43:13 +04:00
|
|
|
if (unlikely(p->sighand == NULL)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* The process has been reaped.
|
|
|
|
* We can't even collect a sample any more.
|
|
|
|
* Call the timer disarmed, nothing else to do.
|
|
|
|
*/
|
|
|
|
put_task_struct(p);
|
|
|
|
timer->it.cpu.task = NULL;
|
2013-06-28 04:06:42 +04:00
|
|
|
timer->it.cpu.expires = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
read_unlock(&tasklist_lock);
|
|
|
|
goto dead;
|
|
|
|
} else {
|
2009-02-12 17:00:52 +03:00
|
|
|
cpu_timer_sample_group(timer->it_clock, p, &now);
|
2005-04-17 02:20:36 +04:00
|
|
|
clear_dead = (unlikely(p->exit_state) &&
|
|
|
|
thread_group_empty(p));
|
|
|
|
}
|
|
|
|
read_unlock(&tasklist_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(clear_dead)) {
|
|
|
|
/*
|
|
|
|
* We've noticed that the thread is dead, but
|
|
|
|
* not yet reaped. Take this opportunity to
|
|
|
|
* drop our task ref.
|
|
|
|
*/
|
|
|
|
clear_dead_task(timer, now);
|
|
|
|
goto dead;
|
|
|
|
}
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
if (now < timer->it.cpu.expires) {
|
2005-04-17 02:20:36 +04:00
|
|
|
sample_to_timespec(timer->it_clock,
|
2013-06-28 04:06:42 +04:00
|
|
|
timer->it.cpu.expires - now,
|
2005-04-17 02:20:36 +04:00
|
|
|
&itp->it_value);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* The timer should have expired already, but the firing
|
|
|
|
* hasn't taken place yet. Say it's just about to expire.
|
|
|
|
*/
|
|
|
|
itp->it_value.tv_nsec = 1;
|
|
|
|
itp->it_value.tv_sec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 04:06:43 +04:00
|
|
|
static unsigned long long
|
|
|
|
check_timers_list(struct list_head *timers,
|
|
|
|
struct list_head *firing,
|
|
|
|
unsigned long long curr)
|
|
|
|
{
|
|
|
|
int maxfire = 20;
|
|
|
|
|
|
|
|
while (!list_empty(timers)) {
|
|
|
|
struct cpu_timer_list *t;
|
|
|
|
|
|
|
|
t = list_first_entry(timers, struct cpu_timer_list, entry);
|
|
|
|
|
|
|
|
if (!--maxfire || curr < t->expires)
|
|
|
|
return t->expires;
|
|
|
|
|
|
|
|
t->firing = 1;
|
|
|
|
list_move_tail(&t->entry, firing);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Check for any per-thread CPU timers that have fired and move them off
|
|
|
|
* the tsk->cpu_timers[N] list onto the firing list. Here we update the
|
|
|
|
* tsk->it_*_expires values to reflect the remaining thread CPU timers.
|
|
|
|
*/
|
|
|
|
static void check_thread_timers(struct task_struct *tsk,
|
|
|
|
struct list_head *firing)
|
|
|
|
{
|
|
|
|
struct list_head *timers = tsk->cpu_timers;
|
2008-01-25 23:08:27 +03:00
|
|
|
struct signal_struct *const sig = tsk->signal;
|
2013-06-28 04:06:43 +04:00
|
|
|
struct task_cputime *tsk_expires = &tsk->cputime_expires;
|
|
|
|
unsigned long long expires;
|
2010-03-06 00:42:53 +03:00
|
|
|
unsigned long soft;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:43 +04:00
|
|
|
expires = check_timers_list(timers, firing, prof_ticks(tsk));
|
|
|
|
tsk_expires->prof_exp = expires_to_cputime(expires);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:43 +04:00
|
|
|
expires = check_timers_list(++timers, firing, virt_ticks(tsk));
|
|
|
|
tsk_expires->virt_exp = expires_to_cputime(expires);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:43 +04:00
|
|
|
tsk_expires->sched_exp = check_timers_list(++timers, firing,
|
|
|
|
tsk->se.sum_exec_runtime);
|
2008-01-25 23:08:27 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the special case thread timers.
|
|
|
|
*/
|
2010-03-06 00:42:54 +03:00
|
|
|
soft = ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_cur);
|
2010-03-06 00:42:53 +03:00
|
|
|
if (soft != RLIM_INFINITY) {
|
2010-03-06 00:42:54 +03:00
|
|
|
unsigned long hard =
|
|
|
|
ACCESS_ONCE(sig->rlim[RLIMIT_RTTIME].rlim_max);
|
2008-01-25 23:08:27 +03:00
|
|
|
|
2008-01-25 23:08:32 +03:00
|
|
|
if (hard != RLIM_INFINITY &&
|
|
|
|
tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) {
|
2008-01-25 23:08:27 +03:00
|
|
|
/*
|
|
|
|
* At the hard limit, we just die.
|
|
|
|
* No need to calculate anything else now.
|
|
|
|
*/
|
|
|
|
__group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
|
|
|
|
return;
|
|
|
|
}
|
2010-03-06 00:42:53 +03:00
|
|
|
if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) {
|
2008-01-25 23:08:27 +03:00
|
|
|
/*
|
|
|
|
* At the soft limit, send a SIGXCPU every second.
|
|
|
|
*/
|
2010-03-06 00:42:53 +03:00
|
|
|
if (soft < hard) {
|
|
|
|
soft += USEC_PER_SEC;
|
|
|
|
sig->rlim[RLIMIT_RTTIME].rlim_cur = soft;
|
2008-01-25 23:08:27 +03:00
|
|
|
}
|
2008-05-16 06:42:49 +04:00
|
|
|
printk(KERN_INFO
|
|
|
|
"RT Watchdog Timeout: %s[%d]\n",
|
|
|
|
tsk->comm, task_pid_nr(tsk));
|
2008-01-25 23:08:27 +03:00
|
|
|
__group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
|
|
|
|
}
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2010-03-12 01:04:31 +03:00
|
|
|
static void stop_process_timers(struct signal_struct *sig)
|
2009-02-10 18:37:31 +03:00
|
|
|
{
|
2010-03-12 01:04:31 +03:00
|
|
|
struct thread_group_cputimer *cputimer = &sig->cputimer;
|
2009-02-10 18:37:31 +03:00
|
|
|
unsigned long flags;
|
|
|
|
|
2009-07-25 20:56:56 +04:00
|
|
|
raw_spin_lock_irqsave(&cputimer->lock, flags);
|
2009-02-10 18:37:31 +03:00
|
|
|
cputimer->running = 0;
|
2009-07-25 20:56:56 +04:00
|
|
|
raw_spin_unlock_irqrestore(&cputimer->lock, flags);
|
2009-02-10 18:37:31 +03:00
|
|
|
}
|
|
|
|
|
2009-07-29 14:15:27 +04:00
|
|
|
static u32 onecputick;
|
|
|
|
|
2009-07-29 14:15:26 +04:00
|
|
|
static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long *expires,
|
|
|
|
unsigned long long cur_time, int signo)
|
2009-07-29 14:15:26 +04:00
|
|
|
{
|
2011-12-15 17:56:09 +04:00
|
|
|
if (!it->expires)
|
2009-07-29 14:15:26 +04:00
|
|
|
return;
|
|
|
|
|
2011-12-15 17:56:09 +04:00
|
|
|
if (cur_time >= it->expires) {
|
|
|
|
if (it->incr) {
|
|
|
|
it->expires += it->incr;
|
2009-07-29 14:15:27 +04:00
|
|
|
it->error += it->incr_error;
|
|
|
|
if (it->error >= onecputick) {
|
2011-12-15 17:56:09 +04:00
|
|
|
it->expires -= cputime_one_jiffy;
|
2009-07-29 14:15:27 +04:00
|
|
|
it->error -= onecputick;
|
|
|
|
}
|
2009-08-10 06:52:30 +04:00
|
|
|
} else {
|
2011-12-15 17:56:09 +04:00
|
|
|
it->expires = 0;
|
2009-08-10 06:52:30 +04:00
|
|
|
}
|
2009-07-29 14:15:26 +04:00
|
|
|
|
2009-08-10 06:52:30 +04:00
|
|
|
trace_itimer_expire(signo == SIGPROF ?
|
|
|
|
ITIMER_PROF : ITIMER_VIRTUAL,
|
|
|
|
tsk->signal->leader_pid, cur_time);
|
2009-07-29 14:15:26 +04:00
|
|
|
__group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
|
|
|
|
}
|
|
|
|
|
2011-12-15 17:56:09 +04:00
|
|
|
if (it->expires && (!*expires || it->expires < *expires)) {
|
2009-07-29 14:15:26 +04:00
|
|
|
*expires = it->expires;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Check for any per-thread CPU timers that have fired and move them
|
|
|
|
* off the tsk->*_timers list onto the firing list. Per-thread timers
|
|
|
|
* have already been taken off.
|
|
|
|
*/
|
|
|
|
static void check_process_timers(struct task_struct *tsk,
|
|
|
|
struct list_head *firing)
|
|
|
|
{
|
|
|
|
struct signal_struct *const sig = tsk->signal;
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long utime, ptime, virt_expires, prof_expires;
|
2007-07-09 20:51:58 +04:00
|
|
|
unsigned long long sum_sched_runtime, sched_expires;
|
2005-04-17 02:20:36 +04:00
|
|
|
struct list_head *timers = sig->cpu_timers;
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
struct task_cputime cputime;
|
2010-03-06 00:42:53 +03:00
|
|
|
unsigned long soft;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect the current process totals.
|
|
|
|
*/
|
2009-02-05 14:24:16 +03:00
|
|
|
thread_group_cputimer(tsk, &cputime);
|
2013-06-28 04:06:42 +04:00
|
|
|
utime = cputime_to_expires(cputime.utime);
|
|
|
|
ptime = utime + cputime_to_expires(cputime.stime);
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
sum_sched_runtime = cputime.sum_exec_runtime;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-06-28 04:06:43 +04:00
|
|
|
prof_expires = check_timers_list(timers, firing, ptime);
|
|
|
|
virt_expires = check_timers_list(++timers, firing, utime);
|
|
|
|
sched_expires = check_timers_list(++timers, firing, sum_sched_runtime);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the special case process timers.
|
|
|
|
*/
|
2009-07-29 14:15:26 +04:00
|
|
|
check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime,
|
|
|
|
SIGPROF);
|
|
|
|
check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime,
|
|
|
|
SIGVTALRM);
|
2010-03-06 00:42:54 +03:00
|
|
|
soft = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
|
2010-03-06 00:42:53 +03:00
|
|
|
if (soft != RLIM_INFINITY) {
|
2005-04-17 02:20:36 +04:00
|
|
|
unsigned long psecs = cputime_to_secs(ptime);
|
2010-03-06 00:42:54 +03:00
|
|
|
unsigned long hard =
|
|
|
|
ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_max);
|
2005-04-17 02:20:36 +04:00
|
|
|
cputime_t x;
|
2010-03-06 00:42:53 +03:00
|
|
|
if (psecs >= hard) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* At the hard limit, we just die.
|
|
|
|
* No need to calculate anything else now.
|
|
|
|
*/
|
|
|
|
__group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk);
|
|
|
|
return;
|
|
|
|
}
|
2010-03-06 00:42:53 +03:00
|
|
|
if (psecs >= soft) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* At the soft limit, send a SIGXCPU every second.
|
|
|
|
*/
|
|
|
|
__group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
|
2010-03-06 00:42:53 +03:00
|
|
|
if (soft < hard) {
|
|
|
|
soft++;
|
|
|
|
sig->rlim[RLIMIT_CPU].rlim_cur = soft;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
2010-03-06 00:42:53 +03:00
|
|
|
x = secs_to_cputime(soft);
|
2011-12-15 17:56:09 +04:00
|
|
|
if (!prof_expires || x < prof_expires) {
|
2005-04-17 02:20:36 +04:00
|
|
|
prof_expires = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 04:06:42 +04:00
|
|
|
sig->cputime_expires.prof_exp = expires_to_cputime(prof_expires);
|
|
|
|
sig->cputime_expires.virt_exp = expires_to_cputime(virt_expires);
|
2010-04-28 01:12:15 +04:00
|
|
|
sig->cputime_expires.sched_exp = sched_expires;
|
|
|
|
if (task_cputime_zero(&sig->cputime_expires))
|
|
|
|
stop_process_timers(sig);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called from the signal code (via do_schedule_next_timer)
|
|
|
|
* when the last timer signal was delivered and we have to reload the timer.
|
|
|
|
*/
|
|
|
|
void posix_cpu_timer_schedule(struct k_itimer *timer)
|
|
|
|
{
|
|
|
|
struct task_struct *p = timer->it.cpu.task;
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long now;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (unlikely(p == NULL))
|
|
|
|
/*
|
|
|
|
* The task was cleaned up already, no future firings.
|
|
|
|
*/
|
2005-10-31 02:03:13 +03:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch the current sample and update the timer's expiry time.
|
|
|
|
*/
|
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock)) {
|
|
|
|
cpu_clock_sample(timer->it_clock, p, &now);
|
|
|
|
bump_cpu_timer(timer, now);
|
|
|
|
if (unlikely(p->exit_state)) {
|
|
|
|
clear_dead_task(timer, now);
|
2005-10-31 02:03:13 +03:00
|
|
|
goto out;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
read_lock(&tasklist_lock); /* arm_timer needs it. */
|
2010-03-12 01:04:42 +03:00
|
|
|
spin_lock(&p->sighand->siglock);
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
|
|
|
read_lock(&tasklist_lock);
|
2010-05-27 01:43:13 +04:00
|
|
|
if (unlikely(p->sighand == NULL)) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* The process has been reaped.
|
|
|
|
* We can't even collect a sample any more.
|
|
|
|
*/
|
|
|
|
put_task_struct(p);
|
|
|
|
timer->it.cpu.task = p = NULL;
|
2013-06-28 04:06:42 +04:00
|
|
|
timer->it.cpu.expires = 0;
|
2005-10-31 02:03:13 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else if (unlikely(p->exit_state) && thread_group_empty(p)) {
|
|
|
|
/*
|
|
|
|
* We've noticed that the thread is dead, but
|
|
|
|
* not yet reaped. Take this opportunity to
|
|
|
|
* drop our task ref.
|
|
|
|
*/
|
2013-06-28 04:06:43 +04:00
|
|
|
cpu_timer_sample_group(timer->it_clock, p, &now);
|
2005-04-17 02:20:36 +04:00
|
|
|
clear_dead_task(timer, now);
|
2005-10-31 02:03:13 +03:00
|
|
|
goto out_unlock;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2010-03-12 01:04:42 +03:00
|
|
|
spin_lock(&p->sighand->siglock);
|
2009-02-12 17:00:52 +03:00
|
|
|
cpu_timer_sample_group(timer->it_clock, p, &now);
|
2005-04-17 02:20:36 +04:00
|
|
|
bump_cpu_timer(timer, now);
|
|
|
|
/* Leave the tasklist_lock locked for the call below. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now re-arm for the new expiry time.
|
|
|
|
*/
|
2010-03-12 01:04:42 +03:00
|
|
|
BUG_ON(!irqs_disabled());
|
2010-03-12 01:04:38 +03:00
|
|
|
arm_timer(timer);
|
2010-03-12 01:04:42 +03:00
|
|
|
spin_unlock(&p->sighand->siglock);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2005-10-31 02:03:13 +03:00
|
|
|
out_unlock:
|
2005-04-17 02:20:36 +04:00
|
|
|
read_unlock(&tasklist_lock);
|
2005-10-31 02:03:13 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
timer->it_overrun_last = timer->it_overrun;
|
|
|
|
timer->it_overrun = -1;
|
|
|
|
++timer->it_requeue_pending;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
/**
|
|
|
|
* task_cputime_expired - Compare two task_cputime entities.
|
|
|
|
*
|
|
|
|
* @sample: The task_cputime structure to be checked for expiration.
|
|
|
|
* @expires: Expiration times, against which @sample will be checked.
|
|
|
|
*
|
|
|
|
* Checks @sample against @expires to see if any field of @sample has expired.
|
|
|
|
* Returns true if any field of the former is greater than the corresponding
|
|
|
|
* field of the latter if the latter field is set. Otherwise returns false.
|
|
|
|
*/
|
|
|
|
static inline int task_cputime_expired(const struct task_cputime *sample,
|
|
|
|
const struct task_cputime *expires)
|
|
|
|
{
|
2011-12-15 17:56:09 +04:00
|
|
|
if (expires->utime && sample->utime >= expires->utime)
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
return 1;
|
2011-12-15 17:56:09 +04:00
|
|
|
if (expires->stime && sample->utime + sample->stime >= expires->stime)
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
return 1;
|
|
|
|
if (expires->sum_exec_runtime != 0 &&
|
|
|
|
sample->sum_exec_runtime >= expires->sum_exec_runtime)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fastpath_timer_check - POSIX CPU timers fast path.
|
|
|
|
*
|
|
|
|
* @tsk: The task (thread) being checked.
|
|
|
|
*
|
2008-09-12 20:54:39 +04:00
|
|
|
* Check the task and thread group timers. If both are zero (there are no
|
|
|
|
* timers set) return false. Otherwise snapshot the task and thread group
|
|
|
|
* timers and compare them with the corresponding expiration times. Return
|
|
|
|
* true if a timer has expired, else return false.
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
*/
|
2008-09-12 20:54:39 +04:00
|
|
|
static inline int fastpath_timer_check(struct task_struct *tsk)
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
{
|
2008-11-17 17:39:47 +03:00
|
|
|
struct signal_struct *sig;
|
2012-11-13 17:20:55 +04:00
|
|
|
cputime_t utime, stime;
|
|
|
|
|
|
|
|
task_cputime(tsk, &utime, &stime);
|
2008-09-12 20:54:39 +04:00
|
|
|
|
|
|
|
if (!task_cputime_zero(&tsk->cputime_expires)) {
|
|
|
|
struct task_cputime task_sample = {
|
2012-11-13 17:20:55 +04:00
|
|
|
.utime = utime,
|
|
|
|
.stime = stime,
|
2008-09-12 20:54:39 +04:00
|
|
|
.sum_exec_runtime = tsk->se.sum_exec_runtime
|
|
|
|
};
|
|
|
|
|
|
|
|
if (task_cputime_expired(&task_sample, &tsk->cputime_expires))
|
|
|
|
return 1;
|
|
|
|
}
|
2008-11-17 17:39:47 +03:00
|
|
|
|
|
|
|
sig = tsk->signal;
|
2010-04-28 01:12:15 +04:00
|
|
|
if (sig->cputimer.running) {
|
2008-09-12 20:54:39 +04:00
|
|
|
struct task_cputime group_sample;
|
|
|
|
|
2009-07-25 20:56:56 +04:00
|
|
|
raw_spin_lock(&sig->cputimer.lock);
|
2010-06-11 22:04:46 +04:00
|
|
|
group_sample = sig->cputimer.cputime;
|
2009-07-25 20:56:56 +04:00
|
|
|
raw_spin_unlock(&sig->cputimer.lock);
|
2010-06-11 22:04:46 +04:00
|
|
|
|
2008-09-12 20:54:39 +04:00
|
|
|
if (task_cputime_expired(&group_sample, &sig->cputime_expires))
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-23 22:34:11 +03:00
|
|
|
|
2010-03-12 01:04:37 +03:00
|
|
|
return 0;
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
}
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* This is called from the timer interrupt handler. The irq handler has
|
|
|
|
* already updated our counts. We need to check if any timers fire now.
|
|
|
|
* Interrupts are disabled.
|
|
|
|
*/
|
|
|
|
void run_posix_cpu_timers(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
LIST_HEAD(firing);
|
|
|
|
struct k_itimer *timer, *next;
|
2010-06-11 03:10:18 +04:00
|
|
|
unsigned long flags;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
BUG_ON(!irqs_disabled());
|
|
|
|
|
|
|
|
/*
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
* The fast path checks that there are no expired thread or thread
|
2008-09-12 20:54:39 +04:00
|
|
|
* group timers. If that's so, just return.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2008-09-12 20:54:39 +04:00
|
|
|
if (!fastpath_timer_check(tsk))
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
return;
|
2008-09-14 19:11:46 +04:00
|
|
|
|
2010-06-11 03:10:18 +04:00
|
|
|
if (!lock_task_sighand(tsk, &flags))
|
|
|
|
return;
|
2008-09-12 20:54:39 +04:00
|
|
|
/*
|
|
|
|
* Here we take off tsk->signal->cpu_timers[N] and
|
|
|
|
* tsk->cpu_timers[N] all the timers that are firing, and
|
|
|
|
* put them on the firing list.
|
|
|
|
*/
|
|
|
|
check_thread_timers(tsk, &firing);
|
2010-04-28 01:12:15 +04:00
|
|
|
/*
|
|
|
|
* If there are any active process wide timers (POSIX 1.b, itimers,
|
|
|
|
* RLIMIT_CPU) cputimer must be running.
|
|
|
|
*/
|
|
|
|
if (tsk->signal->cputimer.running)
|
|
|
|
check_process_timers(tsk, &firing);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-09-12 20:54:39 +04:00
|
|
|
/*
|
|
|
|
* We must release these locks before taking any timer's lock.
|
|
|
|
* There is a potential race with timer deletion here, as the
|
|
|
|
* siglock now protects our private firing list. We have set
|
|
|
|
* the firing flag in each timer, so that a deletion attempt
|
|
|
|
* that gets the timer lock before we do will give it up and
|
|
|
|
* spin until we've taken care of that timer below.
|
|
|
|
*/
|
2010-06-11 03:10:18 +04:00
|
|
|
unlock_task_sighand(tsk, &flags);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that all the timers on our list have the firing flag,
|
2011-03-31 05:57:33 +04:00
|
|
|
* no one will touch their list entries but us. We'll take
|
2005-04-17 02:20:36 +04:00
|
|
|
* each timer's lock before clearing its firing flag, so no
|
|
|
|
* timer call will interfere.
|
|
|
|
*/
|
|
|
|
list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) {
|
2009-04-30 03:14:32 +04:00
|
|
|
int cpu_firing;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
spin_lock(&timer->it_lock);
|
|
|
|
list_del_init(&timer->it.cpu.entry);
|
2009-04-30 03:14:32 +04:00
|
|
|
cpu_firing = timer->it.cpu.firing;
|
2005-04-17 02:20:36 +04:00
|
|
|
timer->it.cpu.firing = 0;
|
|
|
|
/*
|
|
|
|
* The firing flag is -1 if we collided with a reset
|
|
|
|
* of the timer, which already reported this
|
|
|
|
* almost-firing as an overrun. So don't generate an event.
|
|
|
|
*/
|
2009-04-30 03:14:32 +04:00
|
|
|
if (likely(cpu_firing >= 0))
|
2005-04-17 02:20:36 +04:00
|
|
|
cpu_timer_fire(timer);
|
|
|
|
spin_unlock(&timer->it_lock);
|
|
|
|
}
|
2013-04-18 03:31:13 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In case some timers were rescheduled after the queue got emptied,
|
|
|
|
* wake up full dynticks CPUs.
|
|
|
|
*/
|
|
|
|
if (tsk->signal->cputimer.running)
|
|
|
|
posix_cpu_timer_kick_nohz();
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-03-12 01:04:37 +03:00
|
|
|
* Set one of the process-wide special case CPU timers or RLIMIT_CPU.
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
* The tsk->sighand->siglock must be held by the caller.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
|
|
|
void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx,
|
|
|
|
cputime_t *newval, cputime_t *oldval)
|
|
|
|
{
|
2013-06-28 04:06:42 +04:00
|
|
|
unsigned long long now;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
BUG_ON(clock_idx == CPUCLOCK_SCHED);
|
2009-02-05 14:24:16 +03:00
|
|
|
cpu_timer_sample_group(clock_idx, tsk, &now);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
if (oldval) {
|
2010-03-12 01:04:37 +03:00
|
|
|
/*
|
|
|
|
* We are setting itimer. The *oldval is absolute and we update
|
|
|
|
* it to be relative, *newval argument is relative and we update
|
|
|
|
* it to be absolute.
|
|
|
|
*/
|
2011-12-15 17:56:09 +04:00
|
|
|
if (*oldval) {
|
2013-06-28 04:06:42 +04:00
|
|
|
if (*oldval <= now) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/* Just about to fire. */
|
2009-07-29 14:15:29 +04:00
|
|
|
*oldval = cputime_one_jiffy;
|
2005-04-17 02:20:36 +04:00
|
|
|
} else {
|
2013-06-28 04:06:42 +04:00
|
|
|
*oldval -= now;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 17:56:09 +04:00
|
|
|
if (!*newval)
|
2013-04-18 03:31:13 +04:00
|
|
|
goto out;
|
2013-06-28 04:06:42 +04:00
|
|
|
*newval += now;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-03-12 01:04:37 +03:00
|
|
|
* Update expiration cache if we are the earliest timer, or eventually
|
|
|
|
* RLIMIT_CPU limit is earlier than prof_exp cpu timer expire.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2010-03-12 01:04:37 +03:00
|
|
|
switch (clock_idx) {
|
|
|
|
case CPUCLOCK_PROF:
|
|
|
|
if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval))
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
tsk->signal->cputime_expires.prof_exp = *newval;
|
2010-03-12 01:04:37 +03:00
|
|
|
break;
|
|
|
|
case CPUCLOCK_VIRT:
|
|
|
|
if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval))
|
timers: fix itimer/many thread hang
Overview
This patch reworks the handling of POSIX CPU timers, including the
ITIMER_PROF, ITIMER_VIRT timers and rlimit handling. It was put together
with the help of Roland McGrath, the owner and original writer of this code.
The problem we ran into, and the reason for this rework, has to do with using
a profiling timer in a process with a large number of threads. It appears
that the performance of the old implementation of run_posix_cpu_timers() was
at least O(n*3) (where "n" is the number of threads in a process) or worse.
Everything is fine with an increasing number of threads until the time taken
for that routine to run becomes the same as or greater than the tick time, at
which point things degrade rather quickly.
This patch fixes bug 9906, "Weird hang with NPTL and SIGPROF."
Code Changes
This rework corrects the implementation of run_posix_cpu_timers() to make it
run in constant time for a particular machine. (Performance may vary between
one machine and another depending upon whether the kernel is built as single-
or multiprocessor and, in the latter case, depending upon the number of
running processors.) To do this, at each tick we now update fields in
signal_struct as well as task_struct. The run_posix_cpu_timers() function
uses those fields to make its decisions.
We define a new structure, "task_cputime," to contain user, system and
scheduler times and use these in appropriate places:
struct task_cputime {
cputime_t utime;
cputime_t stime;
unsigned long long sum_exec_runtime;
};
This is included in the structure "thread_group_cputime," which is a new
substructure of signal_struct and which varies for uniprocessor versus
multiprocessor kernels. For uniprocessor kernels, it uses "task_cputime" as
a simple substructure, while for multiprocessor kernels it is a pointer:
struct thread_group_cputime {
struct task_cputime totals;
};
struct thread_group_cputime {
struct task_cputime *totals;
};
We also add a new task_cputime substructure directly to signal_struct, to
cache the earliest expiration of process-wide timers, and task_cputime also
replaces the it_*_expires fields of task_struct (used for earliest expiration
of thread timers). The "thread_group_cputime" structure contains process-wide
timers that are updated via account_user_time() and friends. In the non-SMP
case the structure is a simple aggregator; unfortunately in the SMP case that
simplicity was not achievable due to cache-line contention between CPUs (in
one measured case performance was actually _worse_ on a 16-cpu system than
the same test on a 4-cpu system, due to this contention). For SMP, the
thread_group_cputime counters are maintained as a per-cpu structure allocated
using alloc_percpu(). The timer functions update only the timer field in
the structure corresponding to the running CPU, obtained using per_cpu_ptr().
We define a set of inline functions in sched.h that we use to maintain the
thread_group_cputime structure and hide the differences between UP and SMP
implementations from the rest of the kernel. The thread_group_cputime_init()
function initializes the thread_group_cputime structure for the given task.
The thread_group_cputime_alloc() is a no-op for UP; for SMP it calls the
out-of-line function thread_group_cputime_alloc_smp() to allocate and fill
in the per-cpu structures and fields. The thread_group_cputime_free()
function, also a no-op for UP, in SMP frees the per-cpu structures. The
thread_group_cputime_clone_thread() function (also a UP no-op) for SMP calls
thread_group_cputime_alloc() if the per-cpu structures haven't yet been
allocated. The thread_group_cputime() function fills the task_cputime
structure it is passed with the contents of the thread_group_cputime fields;
in UP it's that simple but in SMP it must also safely check that tsk->signal
is non-NULL (if it is it just uses the appropriate fields of task_struct) and,
if so, sums the per-cpu values for each online CPU. Finally, the three
functions account_group_user_time(), account_group_system_time() and
account_group_exec_runtime() are used by timer functions to update the
respective fields of the thread_group_cputime structure.
Non-SMP operation is trivial and will not be mentioned further.
The per-cpu structure is always allocated when a task creates its first new
thread, via a call to thread_group_cputime_clone_thread() from copy_signal().
It is freed at process exit via a call to thread_group_cputime_free() from
cleanup_signal().
All functions that formerly summed utime/stime/sum_sched_runtime values from
from all threads in the thread group now use thread_group_cputime() to
snapshot the values in the thread_group_cputime structure or the values in
the task structure itself if the per-cpu structure hasn't been allocated.
Finally, the code in kernel/posix-cpu-timers.c has changed quite a bit.
The run_posix_cpu_timers() function has been split into a fast path and a
slow path; the former safely checks whether there are any expired thread
timers and, if not, just returns, while the slow path does the heavy lifting.
With the dedicated thread group fields, timers are no longer "rebalanced" and
the process_timer_rebalance() function and related code has gone away. All
summing loops are gone and all code that used them now uses the
thread_group_cputime() inline. When process-wide timers are set, the new
task_cputime structure in signal_struct is used to cache the earliest
expiration; this is checked in the fast path.
Performance
The fix appears not to add significant overhead to existing operations. It
generally performs the same as the current code except in two cases, one in
which it performs slightly worse (Case 5 below) and one in which it performs
very significantly better (Case 2 below). Overall it's a wash except in those
two cases.
I've since done somewhat more involved testing on a dual-core Opteron system.
Case 1: With no itimer running, for a test with 100,000 threads, the fixed
kernel took 1428.5 seconds, 513 seconds more than the unfixed system,
all of which was spent in the system. There were twice as many
voluntary context switches with the fix as without it.
Case 2: With an itimer running at .01 second ticks and 4000 threads (the most
an unmodified kernel can handle), the fixed kernel ran the test in
eight percent of the time (5.8 seconds as opposed to 70 seconds) and
had better tick accuracy (.012 seconds per tick as opposed to .023
seconds per tick).
Case 3: A 4000-thread test with an initial timer tick of .01 second and an
interval of 10,000 seconds (i.e. a timer that ticks only once) had
very nearly the same performance in both cases: 6.3 seconds elapsed
for the fixed kernel versus 5.5 seconds for the unfixed kernel.
With fewer threads (eight in these tests), the Case 1 test ran in essentially
the same time on both the modified and unmodified kernels (5.2 seconds versus
5.8 seconds). The Case 2 test ran in about the same time as well, 5.9 seconds
versus 5.4 seconds but again with much better tick accuracy, .013 seconds per
tick versus .025 seconds per tick for the unmodified kernel.
Since the fix affected the rlimit code, I also tested soft and hard CPU limits.
Case 4: With a hard CPU limit of 20 seconds and eight threads (and an itimer
running), the modified kernel was very slightly favored in that while
it killed the process in 19.997 seconds of CPU time (5.002 seconds of
wall time), only .003 seconds of that was system time, the rest was
user time. The unmodified kernel killed the process in 20.001 seconds
of CPU (5.014 seconds of wall time) of which .016 seconds was system
time. Really, though, the results were too close to call. The results
were essentially the same with no itimer running.
Case 5: With a soft limit of 20 seconds and a hard limit of 2000 seconds
(where the hard limit would never be reached) and an itimer running,
the modified kernel exhibited worse tick accuracy than the unmodified
kernel: .050 seconds/tick versus .028 seconds/tick. Otherwise,
performance was almost indistinguishable. With no itimer running this
test exhibited virtually identical behavior and times in both cases.
In times past I did some limited performance testing. those results are below.
On a four-cpu Opteron system without this fix, a sixteen-thread test executed
in 3569.991 seconds, of which user was 3568.435s and system was 1.556s. On
the same system with the fix, user and elapsed time were about the same, but
system time dropped to 0.007 seconds. Performance with eight, four and one
thread were comparable. Interestingly, the timer ticks with the fix seemed
more accurate: The sixteen-thread test with the fix received 149543 ticks
for 0.024 seconds per tick, while the same test without the fix received 58720
for 0.061 seconds per tick. Both cases were configured for an interval of
0.01 seconds. Again, the other tests were comparable. Each thread in this
test computed the primes up to 25,000,000.
I also did a test with a large number of threads, 100,000 threads, which is
impossible without the fix. In this case each thread computed the primes only
up to 10,000 (to make the runtime manageable). System time dominated, at
1546.968 seconds out of a total 2176.906 seconds (giving a user time of
629.938s). It received 147651 ticks for 0.015 seconds per tick, still quite
accurate. There is obviously no comparable test without the fix.
Signed-off-by: Frank Mayhar <fmayhar@google.com>
Cc: Roland McGrath <roland@redhat.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
2008-09-12 20:54:39 +04:00
|
|
|
tsk->signal->cputime_expires.virt_exp = *newval;
|
2010-03-12 01:04:37 +03:00
|
|
|
break;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2013-04-18 03:31:13 +04:00
|
|
|
out:
|
|
|
|
posix_cpu_timer_kick_nohz();
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2006-09-29 13:00:29 +04:00
|
|
|
static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
|
|
|
|
struct timespec *rqtp, struct itimerspec *it)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
struct k_itimer timer;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up a temporary timer and then wait for it to go off.
|
|
|
|
*/
|
|
|
|
memset(&timer, 0, sizeof timer);
|
|
|
|
spin_lock_init(&timer.it_lock);
|
|
|
|
timer.it_clock = which_clock;
|
|
|
|
timer.it_overrun = -1;
|
|
|
|
error = posix_cpu_timer_create(&timer);
|
|
|
|
timer.it_process = current;
|
|
|
|
if (!error) {
|
|
|
|
static struct itimerspec zero_it;
|
2006-09-29 13:00:29 +04:00
|
|
|
|
|
|
|
memset(it, 0, sizeof *it);
|
|
|
|
it->it_value = *rqtp;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
|
|
|
spin_lock_irq(&timer.it_lock);
|
2006-09-29 13:00:29 +04:00
|
|
|
error = posix_cpu_timer_set(&timer, flags, it, NULL);
|
2005-04-17 02:20:36 +04:00
|
|
|
if (error) {
|
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!signal_pending(current)) {
|
2013-06-28 04:06:42 +04:00
|
|
|
if (timer.it.cpu.expires == 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2013-02-15 14:08:11 +04:00
|
|
|
* Our timer fired and was reset, below
|
|
|
|
* deletion can not fail.
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2013-02-15 14:08:11 +04:00
|
|
|
posix_cpu_timer_del(&timer);
|
2005-04-17 02:20:36 +04:00
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Block until cpu_timer_fire (or a signal) wakes us.
|
|
|
|
*/
|
|
|
|
__set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
schedule();
|
|
|
|
spin_lock_irq(&timer.it_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We were interrupted by a signal.
|
|
|
|
*/
|
|
|
|
sample_to_timespec(which_clock, timer.it.cpu.expires, rqtp);
|
2013-02-15 14:08:11 +04:00
|
|
|
error = posix_cpu_timer_set(&timer, 0, &zero_it, it);
|
|
|
|
if (!error) {
|
|
|
|
/*
|
|
|
|
* Timer is now unarmed, deletion can not fail.
|
|
|
|
*/
|
|
|
|
posix_cpu_timer_del(&timer);
|
|
|
|
}
|
2005-04-17 02:20:36 +04:00
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
|
2013-02-15 14:08:11 +04:00
|
|
|
while (error == TIMER_RETRY) {
|
|
|
|
/*
|
|
|
|
* We need to handle case when timer was or is in the
|
|
|
|
* middle of firing. In other cases we already freed
|
|
|
|
* resources.
|
|
|
|
*/
|
|
|
|
spin_lock_irq(&timer.it_lock);
|
|
|
|
error = posix_cpu_timer_del(&timer);
|
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
}
|
|
|
|
|
2006-09-29 13:00:29 +04:00
|
|
|
if ((it->it_value.tv_sec | it->it_value.tv_nsec) == 0) {
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* It actually did fire already.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-29 13:00:29 +04:00
|
|
|
error = -ERESTART_RESTARTBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
|
|
|
|
|
|
|
|
static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
|
|
|
|
struct timespec *rqtp, struct timespec __user *rmtp)
|
2006-09-29 13:00:29 +04:00
|
|
|
{
|
|
|
|
struct restart_block *restart_block =
|
2011-02-01 16:51:20 +03:00
|
|
|
¤t_thread_info()->restart_block;
|
2006-09-29 13:00:29 +04:00
|
|
|
struct itimerspec it;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Diagnose required errors first.
|
|
|
|
*/
|
|
|
|
if (CPUCLOCK_PERTHREAD(which_clock) &&
|
|
|
|
(CPUCLOCK_PID(which_clock) == 0 ||
|
|
|
|
CPUCLOCK_PID(which_clock) == current->pid))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
error = do_cpu_nanosleep(which_clock, flags, rqtp, &it);
|
|
|
|
|
|
|
|
if (error == -ERESTART_RESTARTBLOCK) {
|
|
|
|
|
2011-02-01 16:51:20 +03:00
|
|
|
if (flags & TIMER_ABSTIME)
|
2006-09-29 13:00:29 +04:00
|
|
|
return -ERESTARTNOHAND;
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2011-02-01 16:51:20 +03:00
|
|
|
* Report back to the user the time still remaining.
|
|
|
|
*/
|
|
|
|
if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
|
2005-04-17 02:20:36 +04:00
|
|
|
return -EFAULT;
|
|
|
|
|
2006-09-29 13:00:28 +04:00
|
|
|
restart_block->fn = posix_cpu_nsleep_restart;
|
2011-05-20 15:05:15 +04:00
|
|
|
restart_block->nanosleep.clockid = which_clock;
|
2011-02-01 16:51:20 +03:00
|
|
|
restart_block->nanosleep.rmtp = rmtp;
|
|
|
|
restart_block->nanosleep.expires = timespec_to_ns(rqtp);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:52:12 +03:00
|
|
|
static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2011-05-20 15:05:15 +04:00
|
|
|
clockid_t which_clock = restart_block->nanosleep.clockid;
|
2006-01-10 07:52:37 +03:00
|
|
|
struct timespec t;
|
2006-09-29 13:00:29 +04:00
|
|
|
struct itimerspec it;
|
|
|
|
int error;
|
2006-01-10 07:52:37 +03:00
|
|
|
|
2011-02-01 16:51:20 +03:00
|
|
|
t = ns_to_timespec(restart_block->nanosleep.expires);
|
2006-01-10 07:52:37 +03:00
|
|
|
|
2006-09-29 13:00:29 +04:00
|
|
|
error = do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t, &it);
|
|
|
|
|
|
|
|
if (error == -ERESTART_RESTARTBLOCK) {
|
2011-02-01 16:51:20 +03:00
|
|
|
struct timespec __user *rmtp = restart_block->nanosleep.rmtp;
|
2006-09-29 13:00:29 +04:00
|
|
|
/*
|
2011-02-01 16:51:20 +03:00
|
|
|
* Report back to the user the time still remaining.
|
|
|
|
*/
|
|
|
|
if (rmtp && copy_to_user(rmtp, &it.it_value, sizeof *rmtp))
|
2006-09-29 13:00:29 +04:00
|
|
|
return -EFAULT;
|
|
|
|
|
2011-02-01 16:51:20 +03:00
|
|
|
restart_block->nanosleep.expires = timespec_to_ns(&t);
|
2006-09-29 13:00:29 +04:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PROCESS_CLOCK MAKE_PROCESS_CPUCLOCK(0, CPUCLOCK_SCHED)
|
|
|
|
#define THREAD_CLOCK MAKE_THREAD_CPUCLOCK(0, CPUCLOCK_SCHED)
|
|
|
|
|
2006-01-10 07:52:27 +03:00
|
|
|
static int process_cpu_clock_getres(const clockid_t which_clock,
|
|
|
|
struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
|
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int process_cpu_clock_get(const clockid_t which_clock,
|
|
|
|
struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_get(PROCESS_CLOCK, tp);
|
|
|
|
}
|
|
|
|
static int process_cpu_timer_create(struct k_itimer *timer)
|
|
|
|
{
|
|
|
|
timer->it_clock = PROCESS_CLOCK;
|
|
|
|
return posix_cpu_timer_create(timer);
|
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int process_cpu_nsleep(const clockid_t which_clock, int flags,
|
2006-01-10 07:52:37 +03:00
|
|
|
struct timespec *rqtp,
|
|
|
|
struct timespec __user *rmtp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2006-01-10 07:52:37 +03:00
|
|
|
return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp, rmtp);
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
2006-09-29 13:00:28 +04:00
|
|
|
static long process_cpu_nsleep_restart(struct restart_block *restart_block)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int thread_cpu_clock_getres(const clockid_t which_clock,
|
|
|
|
struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_getres(THREAD_CLOCK, tp);
|
|
|
|
}
|
2006-01-10 07:52:27 +03:00
|
|
|
static int thread_cpu_clock_get(const clockid_t which_clock,
|
|
|
|
struct timespec *tp)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_get(THREAD_CLOCK, tp);
|
|
|
|
}
|
|
|
|
static int thread_cpu_timer_create(struct k_itimer *timer)
|
|
|
|
{
|
|
|
|
timer->it_clock = THREAD_CLOCK;
|
|
|
|
return posix_cpu_timer_create(timer);
|
|
|
|
}
|
|
|
|
|
2011-02-01 16:51:06 +03:00
|
|
|
struct k_clock clock_posix_cpu = {
|
|
|
|
.clock_getres = posix_cpu_clock_getres,
|
|
|
|
.clock_set = posix_cpu_clock_set,
|
|
|
|
.clock_get = posix_cpu_clock_get,
|
|
|
|
.timer_create = posix_cpu_timer_create,
|
|
|
|
.nsleep = posix_cpu_nsleep,
|
|
|
|
.nsleep_restart = posix_cpu_nsleep_restart,
|
|
|
|
.timer_set = posix_cpu_timer_set,
|
|
|
|
.timer_del = posix_cpu_timer_del,
|
|
|
|
.timer_get = posix_cpu_timer_get,
|
|
|
|
};
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
static __init int init_posix_cpu_timers(void)
|
|
|
|
{
|
|
|
|
struct k_clock process = {
|
2011-02-01 16:51:03 +03:00
|
|
|
.clock_getres = process_cpu_clock_getres,
|
|
|
|
.clock_get = process_cpu_clock_get,
|
|
|
|
.timer_create = process_cpu_timer_create,
|
|
|
|
.nsleep = process_cpu_nsleep,
|
|
|
|
.nsleep_restart = process_cpu_nsleep_restart,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
|
|
|
struct k_clock thread = {
|
2011-02-01 16:51:03 +03:00
|
|
|
.clock_getres = thread_cpu_clock_getres,
|
|
|
|
.clock_get = thread_cpu_clock_get,
|
|
|
|
.timer_create = thread_cpu_timer_create,
|
2005-04-17 02:20:36 +04:00
|
|
|
};
|
2009-07-29 14:15:27 +04:00
|
|
|
struct timespec ts;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2011-02-02 14:10:09 +03:00
|
|
|
posix_timers_register_clock(CLOCK_PROCESS_CPUTIME_ID, &process);
|
|
|
|
posix_timers_register_clock(CLOCK_THREAD_CPUTIME_ID, &thread);
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2009-07-29 14:15:29 +04:00
|
|
|
cputime_to_timespec(cputime_one_jiffy, &ts);
|
2009-07-29 14:15:27 +04:00
|
|
|
onecputick = ts.tv_nsec;
|
|
|
|
WARN_ON(ts.tv_sec != 0);
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__initcall(init_posix_cpu_timers);
|