License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 22:07:57 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Implement CPU time clocks for the POSIX clock interface.
|
|
|
|
*/
|
|
|
|
|
2017-02-09 01:51:30 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2017-02-05 18:48:36 +08:00
|
|
|
#include <linux/sched/cputime.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/posix-timers.h>
|
|
|
|
#include <linux/errno.h>
|
2008-05-01 19:34:31 +08:00
|
|
|
#include <linux/math64.h>
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2008-09-13 00:54:39 +08:00
|
|
|
#include <linux/kernel_stat.h>
|
2009-08-10 10:52:30 +08:00
|
|
|
#include <trace/events/timer.h>
|
2013-04-18 07:31:13 +08:00
|
|
|
#include <linux/tick.h>
|
|
|
|
#include <linux/workqueue.h>
|
2017-06-07 16:42:31 +08:00
|
|
|
#include <linux/compat.h>
|
2017-12-12 19:10:24 +08:00
|
|
|
#include <linux/sched/deadline.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-05-31 05:15:41 +08:00
|
|
|
#include "posix-timers.h"
|
|
|
|
|
2017-05-31 05:15:47 +08:00
|
|
|
static void posix_cpu_timer_rearm(struct k_itimer *timer);
|
|
|
|
|
2019-08-22 03:09:06 +08:00
|
|
|
void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit)
|
|
|
|
{
|
|
|
|
posix_cputimers_init(pct);
|
2019-08-22 03:09:24 +08:00
|
|
|
if (cpu_limit != RLIM_INFINITY) {
|
2019-08-27 02:22:24 +08:00
|
|
|
pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC;
|
2019-08-22 03:09:24 +08:00
|
|
|
pct->timers_active = true;
|
|
|
|
}
|
2019-08-22 03:09:06 +08: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-13 00:54:39 +08:00
|
|
|
/*
|
2010-03-12 06:04:37 +08:00
|
|
|
* Called after updating RLIMIT_CPU to run cpu timer and update
|
2019-08-27 02:22:24 +08:00
|
|
|
* tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if
|
|
|
|
* necessary. Needs siglock protection since other code may update the
|
2019-08-22 03:09:06 +08:00
|
|
|
* 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-13 00:54:39 +08:00
|
|
|
*/
|
2009-08-28 20:05:12 +08: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-13 00:54:39 +08:00
|
|
|
{
|
2017-01-31 11:09:35 +08:00
|
|
|
u64 nsecs = rlim_new * NSEC_PER_SEC;
|
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-13 00:54:39 +08:00
|
|
|
|
2009-08-28 20:05:12 +08:00
|
|
|
spin_lock_irq(&task->sighand->siglock);
|
2017-01-31 11:09:35 +08:00
|
|
|
set_process_cpu_timer(task, CPUCLOCK_PROF, &nsecs, NULL);
|
2009-08-28 20:05:12 +08:00
|
|
|
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-13 00:54:39 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:08:48 +08:00
|
|
|
/*
|
|
|
|
* Functions for validating access to tasks.
|
|
|
|
*/
|
2020-04-27 20:55:07 +08:00
|
|
|
static struct pid *pid_for_clock(const clockid_t clock, bool gettime)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-04-27 20:55:07 +08:00
|
|
|
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
|
|
|
|
const pid_t upid = CPUCLOCK_PID(clock);
|
|
|
|
struct pid *pid;
|
|
|
|
|
|
|
|
if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
|
|
|
|
return NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-09-06 05:15:08 +08:00
|
|
|
/*
|
|
|
|
* If the encoded PID is 0, then the timer is targeted at current
|
|
|
|
* or the process to which current belongs.
|
|
|
|
*/
|
2020-04-27 20:55:07 +08:00
|
|
|
if (upid == 0)
|
|
|
|
return thread ? task_pid(current) : task_tgid(current);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-04-27 20:55:07 +08:00
|
|
|
pid = find_vpid(upid);
|
|
|
|
if (!pid)
|
|
|
|
return NULL;
|
2019-09-06 05:15:08 +08:00
|
|
|
|
2020-04-27 20:55:07 +08:00
|
|
|
if (thread) {
|
|
|
|
struct task_struct *tsk = pid_task(pid, PIDTYPE_PID);
|
|
|
|
return (tsk && same_thread_group(tsk, current)) ? pid : NULL;
|
|
|
|
}
|
2019-09-06 05:15:08 +08:00
|
|
|
|
2020-04-29 02:00:39 +08:00
|
|
|
/*
|
2020-04-27 20:55:07 +08:00
|
|
|
* For clock_gettime(PROCESS) allow finding the process by
|
|
|
|
* with the pid of the current task. The code needs the tgid
|
|
|
|
* of the process so that pid_task(pid, PIDTYPE_TGID) can be
|
|
|
|
* used to find the process.
|
2020-04-29 02:00:39 +08:00
|
|
|
*/
|
2020-04-27 20:55:07 +08:00
|
|
|
if (gettime && (pid == task_pid(current)))
|
|
|
|
return task_tgid(current);
|
2019-09-06 05:15:08 +08:00
|
|
|
|
|
|
|
/*
|
2020-04-27 20:55:07 +08:00
|
|
|
* For processes require that pid identifies a process.
|
2019-09-06 05:15:08 +08:00
|
|
|
*/
|
2020-04-27 20:55:07 +08:00
|
|
|
return pid_has_task(pid, PIDTYPE_TGID) ? pid : NULL;
|
2019-08-22 03:08:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int validate_clock_permissions(const clockid_t clock)
|
|
|
|
{
|
2020-04-26 07:38:54 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2020-04-27 20:55:07 +08:00
|
|
|
ret = pid_for_clock(clock, false) ? 0 : -EINVAL;
|
2020-04-26 07:38:54 +08:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2020-04-27 22:38:29 +08:00
|
|
|
static inline enum pid_type clock_pid_type(const clockid_t clock)
|
2020-02-29 01:11:06 +08:00
|
|
|
{
|
2020-04-27 22:38:29 +08:00
|
|
|
return CPUCLOCK_PERTHREAD(clock) ? PIDTYPE_PID : PIDTYPE_TGID;
|
2020-02-29 01:11:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct task_struct *cpu_timer_task_rcu(struct k_itimer *timer)
|
|
|
|
{
|
2020-04-27 22:38:29 +08:00
|
|
|
return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock));
|
2020-02-29 01:11:06 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Update expiry time from increment, and increase overrun count,
|
|
|
|
* given the current clock sample.
|
|
|
|
*/
|
2019-08-28 03:31:02 +08:00
|
|
|
static u64 bump_cpu_timer(struct k_itimer *timer, u64 now)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-28 03:31:02 +08:00
|
|
|
u64 delta, incr, expires = timer->it.cpu.node.expires;
|
2005-04-17 06:20:36 +08:00
|
|
|
int i;
|
|
|
|
|
2019-01-11 21:33:17 +08:00
|
|
|
if (!timer->it_interval)
|
2019-08-28 03:31:02 +08:00
|
|
|
return expires;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
if (now < expires)
|
|
|
|
return expires;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-01-11 21:33:17 +08:00
|
|
|
incr = timer->it_interval;
|
2019-08-28 03:31:02 +08:00
|
|
|
delta = now + incr - expires;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-06-28 08:06:42 +08: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;
|
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
timer->it.cpu.node.expires += incr;
|
2018-06-26 21:21:32 +08:00
|
|
|
timer->it_overrun += 1LL << i;
|
2013-06-28 08:06:42 +08:00
|
|
|
delta -= incr;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2019-08-28 03:31:02 +08:00
|
|
|
return timer->it.cpu.node.expires;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:09:19 +08:00
|
|
|
/* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */
|
|
|
|
static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct)
|
2013-04-19 22:17:38 +08:00
|
|
|
{
|
2019-08-22 03:09:19 +08:00
|
|
|
return !(~pct->bases[CPUCLOCK_PROF].nextevt |
|
|
|
|
~pct->bases[CPUCLOCK_VIRT].nextevt |
|
|
|
|
~pct->bases[CPUCLOCK_SCHED].nextevt);
|
2013-04-19 22:17:38 +08:00
|
|
|
}
|
|
|
|
|
2011-02-01 21:52:12 +08:00
|
|
|
static int
|
2017-03-27 03:04:15 +08:00
|
|
|
posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:08:48 +08:00
|
|
|
int error = validate_clock_permissions(which_clock);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
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 21:52:12 +08:00
|
|
|
static int
|
2019-08-22 03:08:48 +08:00
|
|
|
posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:08:48 +08:00
|
|
|
int error = validate_clock_permissions(clock);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* You can never reset a CPU clock, but we check for other errors
|
|
|
|
* in the call before failing with EPERM.
|
|
|
|
*/
|
2019-08-22 03:08:48 +08:00
|
|
|
return error ? : -EPERM;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-08-22 03:09:00 +08:00
|
|
|
* Sample a per-thread clock for the given task. clkid is validated.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-08-22 03:09:01 +08:00
|
|
|
static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:09:03 +08:00
|
|
|
u64 utime, stime;
|
|
|
|
|
|
|
|
if (clkid == CPUCLOCK_SCHED)
|
|
|
|
return task_sched_runtime(p);
|
|
|
|
|
|
|
|
task_cputime(p, &utime, &stime);
|
|
|
|
|
2019-08-22 03:09:00 +08:00
|
|
|
switch (clkid) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case CPUCLOCK_PROF:
|
2019-08-22 03:09:03 +08:00
|
|
|
return utime + stime;
|
2005-04-17 06:20:36 +08:00
|
|
|
case CPUCLOCK_VIRT:
|
2019-08-22 03:09:03 +08:00
|
|
|
return utime;
|
2019-08-22 03:09:00 +08:00
|
|
|
default:
|
|
|
|
WARN_ON_ONCE(1);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2019-08-22 03:09:01 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:09:12 +08:00
|
|
|
static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime)
|
|
|
|
{
|
|
|
|
samples[CPUCLOCK_PROF] = stime + utime;
|
|
|
|
samples[CPUCLOCK_VIRT] = utime;
|
|
|
|
samples[CPUCLOCK_SCHED] = rtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void task_sample_cputime(struct task_struct *p, u64 *samples)
|
|
|
|
{
|
|
|
|
u64 stime, utime;
|
|
|
|
|
|
|
|
task_cputime(p, &utime, &stime);
|
|
|
|
store_samples(samples, stime, utime, p->se.sum_exec_runtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void proc_sample_cputime_atomic(struct task_cputime_atomic *at,
|
|
|
|
u64 *samples)
|
|
|
|
{
|
|
|
|
u64 stime, utime, rtime;
|
|
|
|
|
|
|
|
utime = atomic64_read(&at->utime);
|
|
|
|
stime = atomic64_read(&at->stime);
|
|
|
|
rtime = atomic64_read(&at->sum_exec_runtime);
|
|
|
|
store_samples(samples, stime, utime, rtime);
|
|
|
|
}
|
|
|
|
|
2015-04-29 04:00:22 +08:00
|
|
|
/*
|
|
|
|
* Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
|
|
|
|
* to avoid race conditions with concurrent updates to cputime.
|
|
|
|
*/
|
|
|
|
static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
|
2009-02-11 18:30:27 +08:00
|
|
|
{
|
2015-04-29 04:00:22 +08:00
|
|
|
u64 curr_cputime;
|
|
|
|
retry:
|
|
|
|
curr_cputime = atomic64_read(cputime);
|
|
|
|
if (sum_cputime > curr_cputime) {
|
|
|
|
if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 18:30:27 +08:00
|
|
|
|
2019-08-22 03:09:16 +08:00
|
|
|
static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
|
|
|
|
struct task_cputime *sum)
|
2015-04-29 04:00:22 +08:00
|
|
|
{
|
2015-04-29 04:00:24 +08:00
|
|
|
__update_gt_cputime(&cputime_atomic->utime, sum->utime);
|
|
|
|
__update_gt_cputime(&cputime_atomic->stime, sum->stime);
|
|
|
|
__update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime);
|
2015-04-29 04:00:22 +08:00
|
|
|
}
|
2009-02-11 18:30:27 +08:00
|
|
|
|
2019-08-22 03:08:51 +08:00
|
|
|
/**
|
|
|
|
* thread_group_sample_cputime - Sample cputime for a given task
|
|
|
|
* @tsk: Task for which cputime needs to be started
|
2019-10-21 15:44:12 +08:00
|
|
|
* @samples: Storage for time samples
|
2019-08-22 03:08:51 +08:00
|
|
|
*
|
|
|
|
* Called from sys_getitimer() to calculate the expiry time of an active
|
|
|
|
* timer. That means group cputime accounting is already active. Called
|
|
|
|
* with task sighand lock held.
|
|
|
|
*
|
|
|
|
* Updates @times with an uptodate sample of the thread group cputimes.
|
|
|
|
*/
|
2019-08-22 03:09:16 +08:00
|
|
|
void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples)
|
2019-08-22 03:08:51 +08:00
|
|
|
{
|
|
|
|
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
|
2019-08-22 03:09:24 +08:00
|
|
|
struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
|
2019-08-22 03:08:51 +08:00
|
|
|
|
2019-08-22 03:09:24 +08:00
|
|
|
WARN_ON_ONCE(!pct->timers_active);
|
2019-08-22 03:08:51 +08:00
|
|
|
|
2019-08-22 03:09:16 +08:00
|
|
|
proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
|
2019-08-22 03:08:51 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:08:54 +08:00
|
|
|
/**
|
|
|
|
* thread_group_start_cputime - Start cputime and return a sample
|
|
|
|
* @tsk: Task for which cputime needs to be started
|
2019-08-22 03:09:16 +08:00
|
|
|
* @samples: Storage for time samples
|
2019-08-22 03:08:54 +08:00
|
|
|
*
|
|
|
|
* The thread group cputime accouting is avoided when there are no posix
|
|
|
|
* CPU timers armed. Before starting a timer it's required to check whether
|
|
|
|
* the time accounting is active. If not, a full update of the atomic
|
|
|
|
* accounting store needs to be done and the accounting enabled.
|
|
|
|
*
|
|
|
|
* Updates @times with an uptodate sample of the thread group cputimes.
|
|
|
|
*/
|
2019-08-22 03:09:16 +08:00
|
|
|
static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)
|
2009-02-11 18:30:27 +08:00
|
|
|
{
|
|
|
|
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
|
2019-08-22 03:09:24 +08:00
|
|
|
struct posix_cputimers *pct = &tsk->signal->posix_cputimers;
|
2009-02-11 18:30:27 +08:00
|
|
|
|
2015-04-29 04:00:22 +08:00
|
|
|
/* Check if cputimer isn't running. This is accessed without locking. */
|
2019-08-22 03:09:24 +08:00
|
|
|
if (!READ_ONCE(pct->timers_active)) {
|
2019-08-22 03:09:16 +08:00
|
|
|
struct task_cputime sum;
|
|
|
|
|
2009-02-11 18:30:27 +08:00
|
|
|
/*
|
|
|
|
* The POSIX timer interface allows for absolute time expiry
|
|
|
|
* values through the TIMER_ABSTIME flag, therefore we have
|
2015-04-29 04:00:22 +08:00
|
|
|
* to synchronize the timer to the clock every time we start it.
|
2009-02-11 18:30:27 +08:00
|
|
|
*/
|
2017-01-31 11:09:34 +08:00
|
|
|
thread_group_cputime(tsk, &sum);
|
2015-04-29 04:00:24 +08:00
|
|
|
update_gt_cputime(&cputimer->cputime_atomic, &sum);
|
2015-04-29 04:00:22 +08:00
|
|
|
|
|
|
|
/*
|
2019-08-22 03:09:24 +08:00
|
|
|
* We're setting timers_active without a lock. Ensure this
|
|
|
|
* only gets written to in one operation. We set it after
|
|
|
|
* update_gt_cputime() as a small optimization, but
|
|
|
|
* barriers are not required because update_gt_cputime()
|
2015-04-29 04:00:22 +08:00
|
|
|
* can handle concurrent updates.
|
|
|
|
*/
|
2019-08-22 03:09:24 +08:00
|
|
|
WRITE_ONCE(pct->timers_active, true);
|
2015-04-29 04:00:22 +08:00
|
|
|
}
|
2019-08-22 03:09:16 +08:00
|
|
|
proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __thread_group_cputime(struct task_struct *tsk, u64 *samples)
|
|
|
|
{
|
|
|
|
struct task_cputime ct;
|
|
|
|
|
|
|
|
thread_group_cputime(tsk, &ct);
|
|
|
|
store_samples(samples, ct.stime, ct.utime, ct.sum_exec_runtime);
|
2009-02-11 18:30:27 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2019-08-22 03:08:55 +08:00
|
|
|
* Sample a process (thread group) clock for the given task clkid. If the
|
|
|
|
* group's cputime accounting is already enabled, read the atomic
|
2020-02-29 01:08:45 +08:00
|
|
|
* store. Otherwise a full update is required. clkid is already validated.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-08-22 03:09:01 +08:00
|
|
|
static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
|
|
|
|
bool start)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:08:55 +08:00
|
|
|
struct thread_group_cputimer *cputimer = &p->signal->cputimer;
|
2019-08-22 03:09:24 +08:00
|
|
|
struct posix_cputimers *pct = &p->signal->posix_cputimers;
|
2019-08-22 03:09:16 +08:00
|
|
|
u64 samples[CPUCLOCK_MAX];
|
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-13 00:54:39 +08:00
|
|
|
|
2019-08-22 03:09:24 +08:00
|
|
|
if (!READ_ONCE(pct->timers_active)) {
|
2019-08-22 03:08:55 +08:00
|
|
|
if (start)
|
2019-08-22 03:09:16 +08:00
|
|
|
thread_group_start_cputime(p, samples);
|
2019-08-22 03:08:55 +08:00
|
|
|
else
|
2019-08-22 03:09:16 +08:00
|
|
|
__thread_group_cputime(p, samples);
|
2019-08-22 03:08:55 +08:00
|
|
|
} else {
|
2019-08-22 03:09:16 +08:00
|
|
|
proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples);
|
2019-08-22 03:08:55 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:09:16 +08:00
|
|
|
return samples[clkid];
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:08:49 +08:00
|
|
|
static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
|
2013-10-11 23:41:11 +08:00
|
|
|
{
|
2019-08-22 03:08:49 +08:00
|
|
|
const clockid_t clkid = CPUCLOCK_WHICH(clock);
|
|
|
|
struct task_struct *tsk;
|
|
|
|
u64 t;
|
2013-10-11 23:41:11 +08:00
|
|
|
|
2020-04-26 07:38:54 +08:00
|
|
|
rcu_read_lock();
|
2020-04-27 20:55:07 +08:00
|
|
|
tsk = pid_task(pid_for_clock(clock, true), clock_pid_type(clock));
|
2020-04-26 07:38:54 +08:00
|
|
|
if (!tsk) {
|
|
|
|
rcu_read_unlock();
|
2019-08-22 03:08:49 +08:00
|
|
|
return -EINVAL;
|
2020-04-26 07:38:54 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-22 03:08:49 +08:00
|
|
|
if (CPUCLOCK_PERTHREAD(clock))
|
2019-08-22 03:09:01 +08:00
|
|
|
t = cpu_clock_sample(clkid, tsk);
|
2019-08-22 03:08:49 +08:00
|
|
|
else
|
2019-08-22 03:09:01 +08:00
|
|
|
t = cpu_clock_sample_group(clkid, tsk, false);
|
2020-04-26 07:38:54 +08:00
|
|
|
rcu_read_unlock();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-22 03:08:49 +08:00
|
|
|
*tp = ns_to_timespec64(t);
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the clockid_t for a new CPU-clock timer, and initialize the timer.
|
2009-11-18 06:14:13 +08:00
|
|
|
* This is called from sys_timer_create() and do_cpu_nanosleep() with the
|
|
|
|
* new timer already all-zeros initialized.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2011-02-01 21:52:12 +08:00
|
|
|
static int posix_cpu_timer_create(struct k_itimer *new_timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-04-27 20:55:07 +08:00
|
|
|
struct pid *pid;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-04-26 07:38:54 +08:00
|
|
|
rcu_read_lock();
|
2020-04-27 20:55:07 +08:00
|
|
|
pid = pid_for_clock(new_timer->it_clock, false);
|
|
|
|
if (!pid) {
|
2020-04-26 07:38:54 +08:00
|
|
|
rcu_read_unlock();
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
2020-04-26 07:38:54 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-05-31 05:15:44 +08:00
|
|
|
new_timer->kclock = &clock_posix_cpu;
|
2019-08-28 03:31:02 +08:00
|
|
|
timerqueue_init(&new_timer->it.cpu.node);
|
2020-04-27 20:55:07 +08:00
|
|
|
new_timer->it.cpu.pid = get_pid(pid);
|
2020-04-26 07:38:54 +08:00
|
|
|
rcu_read_unlock();
|
2019-08-22 03:08:50 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 21:52:12 +08:00
|
|
|
static int posix_cpu_timer_del(struct k_itimer *timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-28 03:31:02 +08:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
2013-10-11 23:41:11 +08:00
|
|
|
struct sighand_struct *sighand;
|
2020-02-29 01:11:06 +08:00
|
|
|
struct task_struct *p;
|
2019-08-28 03:31:02 +08:00
|
|
|
unsigned long flags;
|
|
|
|
int ret = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-02-29 01:11:06 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p)
|
|
|
|
goto out;
|
2005-10-24 00:25:39 +08:00
|
|
|
|
2013-10-11 23:41:11 +08:00
|
|
|
/*
|
|
|
|
* Protect against sighand release/switch in exit/exec and process/
|
|
|
|
* thread timer list entry concurrent read/writes.
|
|
|
|
*/
|
|
|
|
sighand = lock_task_sighand(p, &flags);
|
|
|
|
if (unlikely(sighand == NULL)) {
|
2013-10-11 06:37:39 +08:00
|
|
|
/*
|
2019-08-28 03:31:02 +08:00
|
|
|
* This raced with the reaping of the task. The exit cleanup
|
|
|
|
* should have removed this timer from the timer queue.
|
2013-10-11 06:37:39 +08:00
|
|
|
*/
|
2019-08-28 03:31:02 +08:00
|
|
|
WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node));
|
2013-10-11 06:37:39 +08:00
|
|
|
} else {
|
|
|
|
if (timer->it.cpu.firing)
|
|
|
|
ret = TIMER_RETRY;
|
|
|
|
else
|
2019-08-28 03:31:02 +08:00
|
|
|
cpu_timer_dequeue(ctmr);
|
2013-10-11 23:41:11 +08:00
|
|
|
|
|
|
|
unlock_task_sighand(p, &flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-10-11 06:37:39 +08:00
|
|
|
|
2020-02-29 01:11:06 +08:00
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
2013-10-11 06:37:39 +08:00
|
|
|
if (!ret)
|
2020-02-29 01:11:06 +08:00
|
|
|
put_pid(ctmr->pid);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-10-24 00:25:39 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
static void cleanup_timerqueue(struct timerqueue_head *head)
|
2013-06-28 08:06:42 +08:00
|
|
|
{
|
2019-08-28 03:31:02 +08:00
|
|
|
struct timerqueue_node *node;
|
|
|
|
struct cpu_timer *ctmr;
|
2013-06-28 08:06:42 +08:00
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
while ((node = timerqueue_getnext(head))) {
|
|
|
|
timerqueue_del(head, node);
|
|
|
|
ctmr = container_of(node, struct cpu_timer, node);
|
|
|
|
ctmr->head = NULL;
|
|
|
|
}
|
2013-06-28 08:06:42 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2019-08-19 22:31:45 +08:00
|
|
|
* Clean out CPU timers which are still armed when a thread exits. The
|
|
|
|
* timers are only removed from the list. No other updates are done. The
|
|
|
|
* corresponding posix timers are still accessible, but cannot be rearmed.
|
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* This must be called with the siglock held.
|
|
|
|
*/
|
2019-08-22 03:09:04 +08:00
|
|
|
static void cleanup_timers(struct posix_cputimers *pct)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-28 03:31:02 +08:00
|
|
|
cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead);
|
|
|
|
cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead);
|
|
|
|
cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead);
|
2005-04-17 06:20:36 +08: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)
|
|
|
|
{
|
2019-08-22 03:09:04 +08:00
|
|
|
cleanup_timers(&tsk->posix_cputimers);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
void posix_cpu_timers_exit_group(struct task_struct *tsk)
|
|
|
|
{
|
2019-08-22 03:09:04 +08:00
|
|
|
cleanup_timers(&tsk->signal->posix_cputimers);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert the timer on the appropriate list before any timers that
|
2013-10-12 00:56:49 +08:00
|
|
|
* expire later. This must be called with the sighand lock held.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2020-02-29 01:09:46 +08:00
|
|
|
static void arm_timer(struct k_itimer *timer, struct task_struct *p)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:09:08 +08:00
|
|
|
int clkidx = CPUCLOCK_WHICH(timer->it_clock);
|
2019-08-28 03:31:02 +08:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
|
|
|
u64 newexp = cpu_timer_getexpires(ctmr);
|
2019-08-27 02:22:24 +08:00
|
|
|
struct posix_cputimer_base *base;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-27 02:22:24 +08:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
|
|
|
base = p->posix_cputimers.bases + clkidx;
|
|
|
|
else
|
|
|
|
base = p->signal->posix_cputimers.bases + clkidx;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
if (!cpu_timer_enqueue(&base->tqhead, ctmr))
|
2019-08-22 03:09:08 +08:00
|
|
|
return;
|
2010-03-12 06:04:38 +08:00
|
|
|
|
2019-08-22 03:09:08 +08: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.
|
|
|
|
*/
|
2019-08-22 03:09:19 +08:00
|
|
|
if (newexp < base->nextevt)
|
2019-08-27 02:22:24 +08:00
|
|
|
base->nextevt = newexp;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-22 03:09:08 +08:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
|
|
|
tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER);
|
|
|
|
else
|
|
|
|
tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The timer is locked, fire it and arrange for its reload.
|
|
|
|
*/
|
|
|
|
static void cpu_timer_fire(struct k_itimer *timer)
|
|
|
|
{
|
2019-08-28 03:31:02 +08:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
|
|
|
|
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 06:04:41 +08:00
|
|
|
if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) {
|
|
|
|
/*
|
|
|
|
* User don't want any signal.
|
|
|
|
*/
|
2019-08-28 03:31:02 +08:00
|
|
|
cpu_timer_setexpires(ctmr, 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 06:04:41 +08:00
|
|
|
} else if (unlikely(timer->sigq == NULL)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* This a special case for clock_nanosleep,
|
|
|
|
* not a normal timer from sys_timer_create.
|
|
|
|
*/
|
|
|
|
wake_up_process(timer->it_process);
|
2019-08-28 03:31:02 +08:00
|
|
|
cpu_timer_setexpires(ctmr, 0);
|
2019-01-11 21:33:17 +08:00
|
|
|
} else if (!timer->it_interval) {
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* One-shot timer. Clear it as soon as it's fired.
|
|
|
|
*/
|
|
|
|
posix_timer_event(timer, 0);
|
2019-08-28 03:31:02 +08:00
|
|
|
cpu_timer_setexpires(ctmr, 0);
|
2005-04-17 06:20:36 +08: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.
|
|
|
|
*/
|
2017-05-31 05:15:47 +08:00
|
|
|
posix_cpu_timer_rearm(timer);
|
2017-05-31 05:15:42 +08:00
|
|
|
++timer->it_requeue_pending;
|
2005-04-17 06:20:36 +08: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.)
|
|
|
|
*/
|
2013-10-12 00:56:49 +08:00
|
|
|
static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
|
2017-03-27 03:04:17 +08:00
|
|
|
struct itimerspec64 *new, struct itimerspec64 *old)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:08:56 +08:00
|
|
|
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
|
2017-01-31 11:09:34 +08:00
|
|
|
u64 old_expires, new_expires, old_incr, val;
|
2019-08-28 03:31:02 +08:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
2019-08-22 03:08:56 +08:00
|
|
|
struct sighand_struct *sighand;
|
2020-02-29 01:11:06 +08:00
|
|
|
struct task_struct *p;
|
2019-08-22 03:08:56 +08:00
|
|
|
unsigned long flags;
|
2019-08-28 03:31:02 +08:00
|
|
|
int ret = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-02-29 01:11:06 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p) {
|
|
|
|
/*
|
|
|
|
* If p has just been reaped, we can no
|
|
|
|
* longer get any information about it at all.
|
|
|
|
*/
|
|
|
|
rcu_read_unlock();
|
|
|
|
return -ESRCH;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-06-20 23:37:36 +08:00
|
|
|
/*
|
|
|
|
* Use the to_ktime conversion because that clamps the maximum
|
|
|
|
* value to KTIME_MAX and avoid multiplication overflows.
|
|
|
|
*/
|
|
|
|
new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
2013-10-12 00:56:49 +08:00
|
|
|
* Protect against sighand release/switch in exit/exec and p->cpu_timers
|
|
|
|
* and p->signal->cpu_timers read/write in arm_timer()
|
|
|
|
*/
|
|
|
|
sighand = lock_task_sighand(p, &flags);
|
|
|
|
/*
|
|
|
|
* If p has just been reaped, we can no
|
2005-04-17 06:20:36 +08:00
|
|
|
* longer get any information about it at all.
|
|
|
|
*/
|
2020-02-29 01:11:06 +08:00
|
|
|
if (unlikely(sighand == NULL)) {
|
|
|
|
rcu_read_unlock();
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ESRCH;
|
2020-02-29 01:11:06 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disarm any old timer after extracting its expiry time.
|
|
|
|
*/
|
2019-01-11 21:33:17 +08:00
|
|
|
old_incr = timer->it_interval;
|
2019-08-28 03:31:02 +08:00
|
|
|
old_expires = cpu_timer_getexpires(ctmr);
|
|
|
|
|
2005-10-24 22:29:58 +08:00
|
|
|
if (unlikely(timer->it.cpu.firing)) {
|
|
|
|
timer->it.cpu.firing = -1;
|
|
|
|
ret = TIMER_RETRY;
|
2019-08-28 03:31:02 +08:00
|
|
|
} else {
|
|
|
|
cpu_timer_dequeue(ctmr);
|
|
|
|
}
|
2005-04-17 06:20:36 +08: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.
|
|
|
|
*/
|
2019-08-22 03:09:01 +08:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
|
|
|
val = cpu_clock_sample(clkid, p);
|
|
|
|
else
|
|
|
|
val = cpu_clock_sample_group(clkid, p, true);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (old) {
|
2013-06-28 08:06:42 +08:00
|
|
|
if (old_expires == 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
old->it_value.tv_sec = 0;
|
|
|
|
old->it_value.tv_nsec = 0;
|
|
|
|
} else {
|
|
|
|
/*
|
2019-08-28 03:31:02 +08:00
|
|
|
* 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.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-08-28 03:31:02 +08:00
|
|
|
u64 exp = bump_cpu_timer(timer, val);
|
|
|
|
|
|
|
|
if (val < exp) {
|
|
|
|
old_expires = exp - val;
|
2017-03-27 03:04:17 +08:00
|
|
|
old->it_value = ns_to_timespec64(old_expires);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
old->it_value.tv_nsec = 1;
|
|
|
|
old->it_value.tv_sec = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-24 22:29:58 +08:00
|
|
|
if (unlikely(ret)) {
|
2005-04-17 06:20:36 +08: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).
|
|
|
|
*/
|
2013-10-12 00:56:49 +08:00
|
|
|
unlock_task_sighand(p, &flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-10-12 00:56:49 +08:00
|
|
|
if (new_expires != 0 && !(timer_flags & TIMER_ABSTIME)) {
|
2013-06-28 08:06:42 +08:00
|
|
|
new_expires += val;
|
2005-04-17 06:20:36 +08: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).
|
|
|
|
*/
|
2019-08-28 03:31:02 +08:00
|
|
|
cpu_timer_setexpires(ctmr, new_expires);
|
2013-06-28 08:06:42 +08:00
|
|
|
if (new_expires != 0 && val < new_expires) {
|
2020-02-29 01:09:46 +08:00
|
|
|
arm_timer(timer, p);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-10-12 00:56:49 +08:00
|
|
|
unlock_task_sighand(p, &flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Install the new reload setting, and
|
|
|
|
* set up the signal and overrun bookkeeping.
|
|
|
|
*/
|
2019-01-11 21:33:17 +08:00
|
|
|
timer->it_interval = timespec64_to_ktime(new->it_interval);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 08:06:42 +08:00
|
|
|
if (new_expires != 0 && !(val < new_expires)) {
|
2005-04-17 06:20:36 +08: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:
|
2020-02-29 01:11:06 +08:00
|
|
|
rcu_read_unlock();
|
2017-01-31 11:09:34 +08:00
|
|
|
if (old)
|
2017-03-27 03:04:17 +08:00
|
|
|
old->it_interval = ns_to_timespec64(old_incr);
|
2015-07-18 04:25:49 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-27 03:04:17 +08:00
|
|
|
static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:08:57 +08:00
|
|
|
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
|
2019-08-28 03:31:02 +08:00
|
|
|
struct cpu_timer *ctmr = &timer->it.cpu;
|
|
|
|
u64 now, expires = cpu_timer_getexpires(ctmr);
|
2020-02-29 01:11:06 +08:00
|
|
|
struct task_struct *p;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-02-29 01:11:06 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p)
|
|
|
|
goto out;
|
2013-10-11 06:37:39 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Easy part: convert the reload time.
|
|
|
|
*/
|
2019-01-11 21:33:17 +08:00
|
|
|
itp->it_interval = ktime_to_timespec64(timer->it_interval);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
if (!expires)
|
2020-02-29 01:11:06 +08:00
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sample the clock to take the difference with the expiry time.
|
|
|
|
*/
|
2020-02-29 01:09:19 +08:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
2019-08-22 03:09:01 +08:00
|
|
|
now = cpu_clock_sample(clkid, p);
|
2020-02-29 01:09:19 +08:00
|
|
|
else
|
|
|
|
now = cpu_clock_sample_group(clkid, p, false);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
if (now < expires) {
|
|
|
|
itp->it_value = ns_to_timespec64(expires - now);
|
2005-04-17 06:20:36 +08:00
|
|
|
} 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;
|
|
|
|
}
|
2020-02-29 01:11:06 +08:00
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
#define MAX_COLLECTED 20
|
2013-06-28 08:06:43 +08:00
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
static u64 collect_timerqueue(struct timerqueue_head *head,
|
|
|
|
struct list_head *firing, u64 now)
|
|
|
|
{
|
|
|
|
struct timerqueue_node *next;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while ((next = timerqueue_getnext(head))) {
|
|
|
|
struct cpu_timer *ctmr;
|
|
|
|
u64 expires;
|
|
|
|
|
|
|
|
ctmr = container_of(next, struct cpu_timer, node);
|
|
|
|
expires = cpu_timer_getexpires(ctmr);
|
|
|
|
/* Limit the number of timers to expire at once */
|
|
|
|
if (++i == MAX_COLLECTED || now < expires)
|
|
|
|
return expires;
|
|
|
|
|
|
|
|
ctmr->firing = 1;
|
|
|
|
cpu_timer_dequeue(ctmr);
|
|
|
|
list_add_tail(&ctmr->elist, firing);
|
2013-06-28 08:06:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:09:19 +08:00
|
|
|
return U64_MAX;
|
2013-06-28 08:06:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-28 03:31:02 +08:00
|
|
|
static void collect_posix_cputimers(struct posix_cputimers *pct, u64 *samples,
|
|
|
|
struct list_head *firing)
|
2019-08-22 03:09:20 +08:00
|
|
|
{
|
|
|
|
struct posix_cputimer_base *base = pct->bases;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CPUCLOCK_MAX; i++, base++) {
|
2019-08-28 03:31:02 +08:00
|
|
|
base->nextevt = collect_timerqueue(&base->tqhead, firing,
|
|
|
|
samples[i]);
|
2019-08-22 03:09:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 19:10:24 +08:00
|
|
|
static inline void check_dl_overrun(struct task_struct *tsk)
|
|
|
|
{
|
|
|
|
if (tsk->dl.dl_overrun) {
|
|
|
|
tsk->dl.dl_overrun = 0;
|
|
|
|
__group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 03:09:23 +08:00
|
|
|
static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard)
|
|
|
|
{
|
|
|
|
if (time < limit)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (print_fatal_signals) {
|
|
|
|
pr_info("%s Watchdog Timeout (%s): %s[%d]\n",
|
|
|
|
rt ? "RT" : "CPU", hard ? "hard" : "soft",
|
|
|
|
current->comm, task_pid_nr(current));
|
|
|
|
}
|
|
|
|
__group_send_sig_info(signo, SEND_SIG_PRIV, current);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08: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)
|
|
|
|
{
|
2019-08-22 03:09:20 +08:00
|
|
|
struct posix_cputimers *pct = &tsk->posix_cputimers;
|
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2010-03-06 05:42:53 +08:00
|
|
|
unsigned long soft;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-12-12 19:10:24 +08:00
|
|
|
if (dl_task(tsk))
|
|
|
|
check_dl_overrun(tsk);
|
|
|
|
|
2019-08-22 03:09:20 +08:00
|
|
|
if (expiry_cache_is_inactive(pct))
|
2015-10-15 03:07:54 +08:00
|
|
|
return;
|
|
|
|
|
2019-08-22 03:09:20 +08:00
|
|
|
task_sample_cputime(tsk, samples);
|
|
|
|
collect_posix_cputimers(pct, samples, firing);
|
2008-01-26 04:08:27 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the special case thread timers.
|
|
|
|
*/
|
2017-07-06 01:25:48 +08:00
|
|
|
soft = task_rlimit(tsk, RLIMIT_RTTIME);
|
2010-03-06 05:42:53 +08:00
|
|
|
if (soft != RLIM_INFINITY) {
|
2019-08-22 03:09:21 +08:00
|
|
|
/* Task RT timeout is accounted in jiffies. RTTIME is usec */
|
2019-08-22 03:09:23 +08:00
|
|
|
unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ);
|
2017-07-06 01:25:48 +08:00
|
|
|
unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME);
|
2008-01-26 04:08:27 +08:00
|
|
|
|
2019-08-22 03:09:23 +08:00
|
|
|
/* At the hard limit, send SIGKILL. No further action. */
|
|
|
|
if (hard != RLIM_INFINITY &&
|
|
|
|
check_rlimit(rttime, hard, SIGKILL, true, true))
|
2008-01-26 04:08:27 +08:00
|
|
|
return;
|
2019-08-22 03:09:22 +08:00
|
|
|
|
2019-08-22 03:09:23 +08:00
|
|
|
/* At the soft limit, send a SIGXCPU every second */
|
|
|
|
if (check_rlimit(rttime, soft, SIGXCPU, true, false)) {
|
2019-08-22 03:09:22 +08:00
|
|
|
soft += USEC_PER_SEC;
|
|
|
|
tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft;
|
2008-01-26 04:08:27 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 03:09:10 +08:00
|
|
|
|
2019-08-22 03:09:20 +08:00
|
|
|
if (expiry_cache_is_inactive(pct))
|
2015-07-18 04:25:49 +08:00
|
|
|
tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-04-29 04:00:22 +08:00
|
|
|
static inline void stop_process_timers(struct signal_struct *sig)
|
2009-02-10 23:37:31 +08:00
|
|
|
{
|
2019-08-22 03:09:24 +08:00
|
|
|
struct posix_cputimers *pct = &sig->posix_cputimers;
|
2009-02-10 23:37:31 +08:00
|
|
|
|
2019-08-22 03:09:24 +08:00
|
|
|
/* Turn off the active flag. This is done without locking. */
|
|
|
|
WRITE_ONCE(pct->timers_active, false);
|
2015-07-18 04:25:49 +08:00
|
|
|
tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER);
|
2009-02-10 23:37:31 +08:00
|
|
|
}
|
|
|
|
|
2009-07-29 18:15:26 +08:00
|
|
|
static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
|
2017-01-31 11:09:34 +08:00
|
|
|
u64 *expires, u64 cur_time, int signo)
|
2009-07-29 18:15:26 +08:00
|
|
|
{
|
2011-12-15 21:56:09 +08:00
|
|
|
if (!it->expires)
|
2009-07-29 18:15:26 +08:00
|
|
|
return;
|
|
|
|
|
2017-01-31 11:09:35 +08:00
|
|
|
if (cur_time >= it->expires) {
|
|
|
|
if (it->incr)
|
2011-12-15 21:56:09 +08:00
|
|
|
it->expires += it->incr;
|
2017-01-31 11:09:35 +08:00
|
|
|
else
|
2011-12-15 21:56:09 +08:00
|
|
|
it->expires = 0;
|
2009-07-29 18:15:26 +08:00
|
|
|
|
2009-08-10 10:52:30 +08:00
|
|
|
trace_itimer_expire(signo == SIGPROF ?
|
|
|
|
ITIMER_PROF : ITIMER_VIRTUAL,
|
2017-06-04 17:32:13 +08:00
|
|
|
task_tgid(tsk), cur_time);
|
2009-07-29 18:15:26 +08:00
|
|
|
__group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
|
|
|
|
}
|
|
|
|
|
2019-08-22 03:09:19 +08:00
|
|
|
if (it->expires && it->expires < *expires)
|
2017-01-31 11:09:35 +08:00
|
|
|
*expires = it->expires;
|
2009-07-29 18:15:26 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08: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;
|
2019-08-22 03:09:20 +08:00
|
|
|
struct posix_cputimers *pct = &sig->posix_cputimers;
|
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2010-03-06 05:42:53 +08:00
|
|
|
unsigned long soft;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-10-15 03:07:54 +08:00
|
|
|
/*
|
2019-08-22 03:09:24 +08:00
|
|
|
* If there are no active process wide timers (POSIX 1.b, itimers,
|
2019-08-29 18:52:28 +08:00
|
|
|
* RLIMIT_CPU) nothing to check. Also skip the process wide timer
|
|
|
|
* processing when there is already another task handling them.
|
2015-10-15 03:07:54 +08:00
|
|
|
*/
|
2019-08-29 18:52:28 +08:00
|
|
|
if (!READ_ONCE(pct->timers_active) || pct->expiry_active)
|
2015-10-15 03:07:54 +08:00
|
|
|
return;
|
|
|
|
|
2019-08-29 18:52:28 +08:00
|
|
|
/*
|
2015-10-15 03:07:56 +08:00
|
|
|
* Signify that a thread is checking for process timers.
|
|
|
|
* Write access to this field is protected by the sighand lock.
|
|
|
|
*/
|
2019-08-29 18:52:28 +08:00
|
|
|
pct->expiry_active = true;
|
2015-10-15 03:07:56 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2019-08-22 03:08:53 +08:00
|
|
|
* Collect the current process totals. Group accounting is active
|
|
|
|
* so the sample can be taken directly.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-08-22 03:09:16 +08:00
|
|
|
proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples);
|
2019-08-22 03:09:20 +08:00
|
|
|
collect_posix_cputimers(pct, samples, firing);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the special case process timers.
|
|
|
|
*/
|
2019-08-22 03:09:20 +08:00
|
|
|
check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF],
|
|
|
|
&pct->bases[CPUCLOCK_PROF].nextevt,
|
2019-08-22 03:09:16 +08:00
|
|
|
samples[CPUCLOCK_PROF], SIGPROF);
|
2019-08-22 03:09:20 +08:00
|
|
|
check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT],
|
|
|
|
&pct->bases[CPUCLOCK_VIRT].nextevt,
|
|
|
|
samples[CPUCLOCK_VIRT], SIGVTALRM);
|
2019-08-22 03:09:16 +08:00
|
|
|
|
2017-07-06 01:25:48 +08:00
|
|
|
soft = task_rlimit(tsk, RLIMIT_CPU);
|
2010-03-06 05:42:53 +08:00
|
|
|
if (soft != RLIM_INFINITY) {
|
2019-08-22 03:09:21 +08:00
|
|
|
/* RLIMIT_CPU is in seconds. Samples are nanoseconds */
|
2017-07-06 01:25:48 +08:00
|
|
|
unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU);
|
2019-08-22 03:09:21 +08:00
|
|
|
u64 ptime = samples[CPUCLOCK_PROF];
|
|
|
|
u64 softns = (u64)soft * NSEC_PER_SEC;
|
|
|
|
u64 hardns = (u64)hard * NSEC_PER_SEC;
|
2019-08-22 03:09:16 +08:00
|
|
|
|
2019-08-22 03:09:23 +08:00
|
|
|
/* At the hard limit, send SIGKILL. No further action. */
|
|
|
|
if (hard != RLIM_INFINITY &&
|
|
|
|
check_rlimit(ptime, hardns, SIGKILL, false, true))
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
2019-08-22 03:09:22 +08:00
|
|
|
|
2019-08-22 03:09:23 +08:00
|
|
|
/* At the soft limit, send a SIGXCPU every second */
|
|
|
|
if (check_rlimit(ptime, softns, SIGXCPU, false, false)) {
|
2019-08-22 03:09:22 +08:00
|
|
|
sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1;
|
|
|
|
softns += NSEC_PER_SEC;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2019-08-22 03:09:21 +08:00
|
|
|
|
|
|
|
/* Update the expiry cache */
|
2019-08-22 03:09:20 +08:00
|
|
|
if (softns < pct->bases[CPUCLOCK_PROF].nextevt)
|
|
|
|
pct->bases[CPUCLOCK_PROF].nextevt = softns;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 03:09:20 +08:00
|
|
|
if (expiry_cache_is_inactive(pct))
|
2010-04-28 05:12:15 +08:00
|
|
|
stop_process_timers(sig);
|
2015-10-15 03:07:56 +08:00
|
|
|
|
2019-08-22 03:09:24 +08:00
|
|
|
pct->expiry_active = false;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-05-31 05:15:46 +08:00
|
|
|
* This is called from the signal code (via posixtimer_rearm)
|
2005-04-17 06:20:36 +08:00
|
|
|
* when the last timer signal was delivered and we have to reload the timer.
|
|
|
|
*/
|
2017-05-31 05:15:47 +08:00
|
|
|
static void posix_cpu_timer_rearm(struct k_itimer *timer)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-22 03:08:58 +08:00
|
|
|
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
|
2020-02-29 01:11:06 +08:00
|
|
|
struct task_struct *p;
|
2013-10-12 00:56:49 +08:00
|
|
|
struct sighand_struct *sighand;
|
|
|
|
unsigned long flags;
|
2017-01-31 11:09:34 +08:00
|
|
|
u64 now;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-02-29 01:11:06 +08:00
|
|
|
rcu_read_lock();
|
|
|
|
p = cpu_timer_task_rcu(timer);
|
|
|
|
if (!p)
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch the current sample and update the timer's expiry time.
|
|
|
|
*/
|
2020-02-29 01:09:19 +08:00
|
|
|
if (CPUCLOCK_PERTHREAD(timer->it_clock))
|
2019-08-22 03:09:01 +08:00
|
|
|
now = cpu_clock_sample(clkid, p);
|
2020-02-29 01:09:19 +08:00
|
|
|
else
|
2019-08-22 03:09:01 +08:00
|
|
|
now = cpu_clock_sample_group(clkid, p, true);
|
2020-02-29 01:09:19 +08:00
|
|
|
|
|
|
|
bump_cpu_timer(timer, now);
|
|
|
|
|
|
|
|
/* Protect timer list r/w in arm_timer() */
|
|
|
|
sighand = lock_task_sighand(p, &flags);
|
|
|
|
if (unlikely(sighand == NULL))
|
2020-02-29 01:11:06 +08:00
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now re-arm for the new expiry time.
|
|
|
|
*/
|
2020-02-29 01:09:46 +08:00
|
|
|
arm_timer(timer, p);
|
2013-10-12 00:56:49 +08:00
|
|
|
unlock_task_sighand(p, &flags);
|
2020-02-29 01:11:06 +08:00
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
2005-04-17 06:20:36 +08: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-13 00:54:39 +08:00
|
|
|
/**
|
2019-08-27 02:22:24 +08:00
|
|
|
* task_cputimers_expired - Check whether posix CPU timers are expired
|
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-13 00:54:39 +08:00
|
|
|
*
|
2019-08-22 03:09:13 +08:00
|
|
|
* @samples: Array of current samples for the CPUCLOCK clocks
|
2019-08-27 02:22:24 +08:00
|
|
|
* @pct: Pointer to a posix_cputimers container
|
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-13 00:54:39 +08:00
|
|
|
*
|
2019-08-27 02:22:24 +08:00
|
|
|
* Returns true if any member of @samples is greater than the corresponding
|
|
|
|
* member of @pct->bases[CLK].nextevt. False otherwise
|
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-13 00:54:39 +08:00
|
|
|
*/
|
2019-08-27 02:22:24 +08:00
|
|
|
static inline bool
|
2019-10-21 15:44:12 +08:00
|
|
|
task_cputimers_expired(const u64 *samples, struct posix_cputimers *pct)
|
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-13 00:54:39 +08:00
|
|
|
{
|
2019-08-22 03:09:13 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CPUCLOCK_MAX; i++) {
|
2019-10-21 15:44:12 +08:00
|
|
|
if (samples[i] >= pct->bases[i].nextevt)
|
2019-08-22 03:09:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
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-13 00:54:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fastpath_timer_check - POSIX CPU timers fast path.
|
|
|
|
*
|
|
|
|
* @tsk: The task (thread) being checked.
|
|
|
|
*
|
2008-09-13 00:54:39 +08: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-13 00:54:39 +08:00
|
|
|
*/
|
2019-08-22 03:09:13 +08:00
|
|
|
static inline bool 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-13 00:54:39 +08:00
|
|
|
{
|
2019-08-22 03:09:24 +08:00
|
|
|
struct posix_cputimers *pct = &tsk->posix_cputimers;
|
2008-11-17 22:39:47 +08:00
|
|
|
struct signal_struct *sig;
|
2008-09-13 00:54:39 +08:00
|
|
|
|
2019-08-22 03:09:24 +08:00
|
|
|
if (!expiry_cache_is_inactive(pct)) {
|
2019-08-22 03:09:13 +08:00
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2008-09-13 00:54:39 +08:00
|
|
|
|
2019-08-22 03:09:13 +08:00
|
|
|
task_sample_cputime(tsk, samples);
|
2019-08-22 03:09:24 +08:00
|
|
|
if (task_cputimers_expired(samples, pct))
|
2019-08-22 03:09:13 +08:00
|
|
|
return true;
|
2008-09-13 00:54:39 +08:00
|
|
|
}
|
2008-11-17 22:39:47 +08:00
|
|
|
|
|
|
|
sig = tsk->signal;
|
2019-08-22 03:09:24 +08:00
|
|
|
pct = &sig->posix_cputimers;
|
2015-10-15 03:07:56 +08:00
|
|
|
/*
|
2019-08-22 03:09:24 +08:00
|
|
|
* Check if thread group timers expired when timers are active and
|
|
|
|
* no other thread in the group is already handling expiry for
|
|
|
|
* thread group cputimers. These fields are read without the
|
|
|
|
* sighand lock. However, this is fine because this is meant to be
|
|
|
|
* a fastpath heuristic to determine whether we should try to
|
|
|
|
* acquire the sighand lock to handle timer expiry.
|
2015-10-15 03:07:56 +08:00
|
|
|
*
|
2019-08-22 03:09:24 +08:00
|
|
|
* In the worst case scenario, if concurrently timers_active is set
|
|
|
|
* or expiry_active is cleared, but the current thread doesn't see
|
|
|
|
* the change yet, the timer checks are delayed until the next
|
|
|
|
* thread in the group gets a scheduler interrupt to handle the
|
|
|
|
* timer. This isn't an issue in practice because these types of
|
|
|
|
* delays with signals actually getting sent are expected.
|
2015-10-15 03:07:56 +08:00
|
|
|
*/
|
2019-08-22 03:09:24 +08:00
|
|
|
if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) {
|
2019-08-22 03:09:13 +08:00
|
|
|
u64 samples[CPUCLOCK_MAX];
|
2008-09-13 00:54:39 +08:00
|
|
|
|
2019-08-22 03:09:13 +08:00
|
|
|
proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic,
|
|
|
|
samples);
|
2010-06-12 02:04:46 +08:00
|
|
|
|
2019-08-22 03:09:24 +08:00
|
|
|
if (task_cputimers_expired(samples, pct))
|
2019-08-22 03:09:13 +08:00
|
|
|
return true;
|
2008-09-13 00:54:39 +08:00
|
|
|
}
|
2009-03-24 03:34:11 +08:00
|
|
|
|
2017-12-12 19:10:24 +08:00
|
|
|
if (dl_task(tsk) && tsk->dl.dl_overrun)
|
2019-08-22 03:09:13 +08:00
|
|
|
return true;
|
2017-12-12 19:10:24 +08:00
|
|
|
|
2019-08-22 03:09:13 +08:00
|
|
|
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-13 00:54:39 +08:00
|
|
|
}
|
|
|
|
|
2020-07-30 18:14:05 +08:00
|
|
|
static void __run_posix_cpu_timers(struct task_struct *tsk)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct k_itimer *timer, *next;
|
2010-06-11 07:10:18 +08:00
|
|
|
unsigned long flags;
|
2019-08-19 22:31:47 +08:00
|
|
|
LIST_HEAD(firing);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2020-07-30 18:14:05 +08:00
|
|
|
if (!lock_task_sighand(tsk, &flags))
|
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-13 00:54:39 +08:00
|
|
|
return;
|
2008-09-14 23:11:46 +08:00
|
|
|
|
2008-09-13 00:54:39 +08: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);
|
2015-10-15 03:07:54 +08:00
|
|
|
|
|
|
|
check_process_timers(tsk, &firing);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-09-13 00:54:39 +08: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 07:10:18 +08:00
|
|
|
unlock_task_sighand(tsk, &flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that all the timers on our list have the firing flag,
|
2011-03-31 09:57:33 +08:00
|
|
|
* no one will touch their list entries but us. We'll take
|
2005-04-17 06:20:36 +08:00
|
|
|
* each timer's lock before clearing its firing flag, so no
|
|
|
|
* timer call will interfere.
|
|
|
|
*/
|
2019-08-28 03:31:02 +08:00
|
|
|
list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) {
|
2009-04-30 07:14:32 +08:00
|
|
|
int cpu_firing;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock(&timer->it_lock);
|
2019-08-28 03:31:02 +08:00
|
|
|
list_del_init(&timer->it.cpu.elist);
|
2009-04-30 07:14:32 +08:00
|
|
|
cpu_firing = timer->it.cpu.firing;
|
2005-04-17 06:20:36 +08: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 07:14:32 +08:00
|
|
|
if (likely(cpu_firing >= 0))
|
2005-04-17 06:20:36 +08:00
|
|
|
cpu_timer_fire(timer);
|
|
|
|
spin_unlock(&timer->it_lock);
|
|
|
|
}
|
2020-07-30 18:14:05 +08: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(void)
|
|
|
|
{
|
|
|
|
struct task_struct *tsk = current;
|
|
|
|
|
|
|
|
lockdep_assert_irqs_disabled();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The fast path checks that there are no expired thread or thread
|
|
|
|
* group timers. If that's so, just return.
|
|
|
|
*/
|
|
|
|
if (!fastpath_timer_check(tsk))
|
|
|
|
return;
|
|
|
|
|
|
|
|
lockdep_posixtimer_enter();
|
|
|
|
__run_posix_cpu_timers(tsk);
|
2020-03-21 19:26:04 +08:00
|
|
|
lockdep_posixtimer_exit();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-03-12 06:04:37 +08: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-13 00:54:39 +08:00
|
|
|
* The tsk->sighand->siglock must be held by the caller.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-08-22 03:09:09 +08:00
|
|
|
void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
|
2017-01-31 11:09:35 +08:00
|
|
|
u64 *newval, u64 *oldval)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-27 02:22:24 +08:00
|
|
|
u64 now, *nextevt;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-22 03:09:09 +08:00
|
|
|
if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED))
|
2019-08-19 22:31:46 +08:00
|
|
|
return;
|
|
|
|
|
2019-08-27 02:22:24 +08:00
|
|
|
nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt;
|
2019-08-22 03:09:09 +08:00
|
|
|
now = cpu_clock_sample_group(clkid, tsk, true);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-22 03:08:59 +08:00
|
|
|
if (oldval) {
|
2010-03-12 06:04:37 +08: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 21:56:09 +08:00
|
|
|
if (*oldval) {
|
2017-01-31 11:09:35 +08:00
|
|
|
if (*oldval <= now) {
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Just about to fire. */
|
2017-01-31 11:09:35 +08:00
|
|
|
*oldval = TICK_NSEC;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2017-01-31 11:09:35 +08:00
|
|
|
*oldval -= now;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 21:56:09 +08:00
|
|
|
if (!*newval)
|
2015-07-18 04:25:49 +08:00
|
|
|
return;
|
2017-01-31 11:09:35 +08:00
|
|
|
*newval += now;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-08-22 03:09:09 +08:00
|
|
|
* Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
|
|
|
|
* expiry cache is also used by RLIMIT_CPU!.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2019-08-22 03:09:19 +08:00
|
|
|
if (*newval < *nextevt)
|
2019-08-27 02:22:24 +08:00
|
|
|
*nextevt = *newval;
|
2015-07-18 04:25:49 +08:00
|
|
|
|
|
|
|
tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-09-29 17:00:29 +08:00
|
|
|
static int do_cpu_nanosleep(const clockid_t which_clock, int flags,
|
2017-06-14 05:29:14 +08:00
|
|
|
const struct timespec64 *rqtp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2017-06-07 16:42:26 +08:00
|
|
|
struct itimerspec64 it;
|
2017-06-14 05:29:14 +08:00
|
|
|
struct k_itimer timer;
|
|
|
|
u64 expires;
|
2005-04-17 06:20:36 +08:00
|
|
|
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;
|
2019-08-28 03:31:02 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!error) {
|
2017-03-27 03:04:17 +08:00
|
|
|
static struct itimerspec64 zero_it;
|
2017-06-07 16:42:31 +08:00
|
|
|
struct restart_block *restart;
|
2006-09-29 17:00:29 +08:00
|
|
|
|
2017-06-07 16:42:31 +08:00
|
|
|
memset(&it, 0, sizeof(it));
|
2017-06-07 16:42:26 +08:00
|
|
|
it.it_value = *rqtp;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
spin_lock_irq(&timer.it_lock);
|
2017-06-07 16:42:26 +08:00
|
|
|
error = posix_cpu_timer_set(&timer, flags, &it, NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (error) {
|
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!signal_pending(current)) {
|
2019-08-28 03:31:02 +08:00
|
|
|
if (!cpu_timer_getexpires(&timer.it.cpu)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2013-02-15 18:08:11 +08:00
|
|
|
* Our timer fired and was reset, below
|
|
|
|
* deletion can not fail.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2013-02-15 18:08:11 +08:00
|
|
|
posix_cpu_timer_del(&timer);
|
2005-04-17 06:20:36 +08: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.
|
|
|
|
*/
|
2019-08-28 03:31:02 +08:00
|
|
|
expires = cpu_timer_getexpires(&timer.it.cpu);
|
2017-06-07 16:42:26 +08:00
|
|
|
error = posix_cpu_timer_set(&timer, 0, &zero_it, &it);
|
2013-02-15 18:08:11 +08:00
|
|
|
if (!error) {
|
|
|
|
/*
|
|
|
|
* Timer is now unarmed, deletion can not fail.
|
|
|
|
*/
|
|
|
|
posix_cpu_timer_del(&timer);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock_irq(&timer.it_lock);
|
|
|
|
|
2013-02-15 18:08:11 +08: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);
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:42:26 +08:00
|
|
|
if ((it.it_value.tv_sec | it.it_value.tv_nsec) == 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* It actually did fire already.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-29 17:00:29 +08:00
|
|
|
error = -ERESTART_RESTARTBLOCK;
|
2017-06-07 16:42:26 +08:00
|
|
|
/*
|
|
|
|
* Report back to the user the time still remaining.
|
|
|
|
*/
|
2017-06-07 16:42:31 +08:00
|
|
|
restart = ¤t->restart_block;
|
2017-06-14 05:29:14 +08:00
|
|
|
restart->nanosleep.expires = expires;
|
2017-06-25 02:45:06 +08:00
|
|
|
if (restart->nanosleep.type != TT_NONE)
|
|
|
|
error = nanosleep_copyout(restart, &it.it_value);
|
2006-09-29 17:00:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 21:52:12 +08:00
|
|
|
static long posix_cpu_nsleep_restart(struct restart_block *restart_block);
|
|
|
|
|
|
|
|
static int posix_cpu_nsleep(const clockid_t which_clock, int flags,
|
2017-06-14 05:34:33 +08:00
|
|
|
const struct timespec64 *rqtp)
|
2006-09-29 17:00:29 +08:00
|
|
|
{
|
2015-02-13 07:01:14 +08:00
|
|
|
struct restart_block *restart_block = ¤t->restart_block;
|
2006-09-29 17:00:29 +08:00
|
|
|
int error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Diagnose required errors first.
|
|
|
|
*/
|
|
|
|
if (CPUCLOCK_PERTHREAD(which_clock) &&
|
|
|
|
(CPUCLOCK_PID(which_clock) == 0 ||
|
2017-04-13 23:32:16 +08:00
|
|
|
CPUCLOCK_PID(which_clock) == task_pid_vnr(current)))
|
2006-09-29 17:00:29 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2017-06-07 16:42:26 +08:00
|
|
|
error = do_cpu_nanosleep(which_clock, flags, rqtp);
|
2006-09-29 17:00:29 +08:00
|
|
|
|
|
|
|
if (error == -ERESTART_RESTARTBLOCK) {
|
|
|
|
|
2011-02-01 21:51:20 +08:00
|
|
|
if (flags & TIMER_ABSTIME)
|
2006-09-29 17:00:29 +08:00
|
|
|
return -ERESTARTNOHAND;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-09-29 17:00:28 +08:00
|
|
|
restart_block->fn = posix_cpu_nsleep_restart;
|
2011-05-20 19:05:15 +08:00
|
|
|
restart_block->nanosleep.clockid = which_clock;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2011-02-01 21:52:12 +08:00
|
|
|
static long posix_cpu_nsleep_restart(struct restart_block *restart_block)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2011-05-20 19:05:15 +08:00
|
|
|
clockid_t which_clock = restart_block->nanosleep.clockid;
|
2017-03-27 03:04:18 +08:00
|
|
|
struct timespec64 t;
|
2006-01-10 12:52:37 +08:00
|
|
|
|
2017-03-27 03:04:18 +08:00
|
|
|
t = ns_to_timespec64(restart_block->nanosleep.expires);
|
2006-01-10 12:52:37 +08:00
|
|
|
|
2017-06-07 16:42:26 +08:00
|
|
|
return do_cpu_nanosleep(which_clock, TIMER_ABSTIME, &t);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2017-12-29 11:11:36 +08:00
|
|
|
#define PROCESS_CLOCK make_process_cpuclock(0, CPUCLOCK_SCHED)
|
|
|
|
#define THREAD_CLOCK make_thread_cpuclock(0, CPUCLOCK_SCHED)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-10 12:52:27 +08:00
|
|
|
static int process_cpu_clock_getres(const clockid_t which_clock,
|
2017-03-27 03:04:15 +08:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_getres(PROCESS_CLOCK, tp);
|
|
|
|
}
|
2006-01-10 12:52:27 +08:00
|
|
|
static int process_cpu_clock_get(const clockid_t which_clock,
|
2017-03-27 03:04:14 +08:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 06:20:36 +08: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 12:52:27 +08:00
|
|
|
static int process_cpu_nsleep(const clockid_t which_clock, int flags,
|
2017-06-14 05:34:33 +08:00
|
|
|
const struct timespec64 *rqtp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2017-06-07 16:42:30 +08:00
|
|
|
return posix_cpu_nsleep(PROCESS_CLOCK, flags, rqtp);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-01-10 12:52:27 +08:00
|
|
|
static int thread_cpu_clock_getres(const clockid_t which_clock,
|
2017-03-27 03:04:15 +08:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
return posix_cpu_clock_getres(THREAD_CLOCK, tp);
|
|
|
|
}
|
2006-01-10 12:52:27 +08:00
|
|
|
static int thread_cpu_clock_get(const clockid_t which_clock,
|
2017-03-27 03:04:14 +08:00
|
|
|
struct timespec64 *tp)
|
2005-04-17 06:20:36 +08: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);
|
|
|
|
}
|
|
|
|
|
2017-05-26 17:03:11 +08:00
|
|
|
const struct k_clock clock_posix_cpu = {
|
2019-11-12 09:26:54 +08:00
|
|
|
.clock_getres = posix_cpu_clock_getres,
|
|
|
|
.clock_set = posix_cpu_clock_set,
|
|
|
|
.clock_get_timespec = posix_cpu_clock_get,
|
|
|
|
.timer_create = posix_cpu_timer_create,
|
|
|
|
.nsleep = posix_cpu_nsleep,
|
|
|
|
.timer_set = posix_cpu_timer_set,
|
|
|
|
.timer_del = posix_cpu_timer_del,
|
|
|
|
.timer_get = posix_cpu_timer_get,
|
|
|
|
.timer_rearm = posix_cpu_timer_rearm,
|
2011-02-01 21:51:06 +08:00
|
|
|
};
|
|
|
|
|
2017-05-26 17:03:11 +08:00
|
|
|
const struct k_clock clock_process = {
|
2019-11-12 09:26:54 +08:00
|
|
|
.clock_getres = process_cpu_clock_getres,
|
|
|
|
.clock_get_timespec = process_cpu_clock_get,
|
|
|
|
.timer_create = process_cpu_timer_create,
|
|
|
|
.nsleep = process_cpu_nsleep,
|
2017-05-26 17:03:11 +08:00
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-05-26 17:03:11 +08:00
|
|
|
const struct k_clock clock_thread = {
|
2019-11-12 09:26:54 +08:00
|
|
|
.clock_getres = thread_cpu_clock_getres,
|
|
|
|
.clock_get_timespec = thread_cpu_clock_get,
|
|
|
|
.timer_create = thread_cpu_timer_create,
|
2017-05-26 17:03:11 +08:00
|
|
|
};
|