mirror of
https://github.com/edk2-porting/linux-next.git
synced 2024-12-24 13:13:57 +08:00
f06febc96b
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>
284 lines
7.4 KiB
C
284 lines
7.4 KiB
C
/*
|
|
* linux/kernel/itimer.c
|
|
*
|
|
* Copyright (C) 1992 Darren Senn
|
|
*/
|
|
|
|
/* These are all the functions necessary to implement itimers */
|
|
|
|
#include <linux/mm.h>
|
|
#include <linux/interrupt.h>
|
|
#include <linux/syscalls.h>
|
|
#include <linux/time.h>
|
|
#include <linux/posix-timers.h>
|
|
#include <linux/hrtimer.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
/**
|
|
* itimer_get_remtime - get remaining time for the timer
|
|
*
|
|
* @timer: the timer to read
|
|
*
|
|
* Returns the delta between the expiry time and now, which can be
|
|
* less than zero or 1usec for an pending expired timer
|
|
*/
|
|
static struct timeval itimer_get_remtime(struct hrtimer *timer)
|
|
{
|
|
ktime_t rem = hrtimer_get_remaining(timer);
|
|
|
|
/*
|
|
* Racy but safe: if the itimer expires after the above
|
|
* hrtimer_get_remtime() call but before this condition
|
|
* then we return 0 - which is correct.
|
|
*/
|
|
if (hrtimer_active(timer)) {
|
|
if (rem.tv64 <= 0)
|
|
rem.tv64 = NSEC_PER_USEC;
|
|
} else
|
|
rem.tv64 = 0;
|
|
|
|
return ktime_to_timeval(rem);
|
|
}
|
|
|
|
int do_getitimer(int which, struct itimerval *value)
|
|
{
|
|
struct task_struct *tsk = current;
|
|
cputime_t cinterval, cval;
|
|
|
|
switch (which) {
|
|
case ITIMER_REAL:
|
|
spin_lock_irq(&tsk->sighand->siglock);
|
|
value->it_value = itimer_get_remtime(&tsk->signal->real_timer);
|
|
value->it_interval =
|
|
ktime_to_timeval(tsk->signal->it_real_incr);
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
break;
|
|
case ITIMER_VIRTUAL:
|
|
spin_lock_irq(&tsk->sighand->siglock);
|
|
cval = tsk->signal->it_virt_expires;
|
|
cinterval = tsk->signal->it_virt_incr;
|
|
if (!cputime_eq(cval, cputime_zero)) {
|
|
struct task_cputime cputime;
|
|
cputime_t utime;
|
|
|
|
thread_group_cputime(tsk, &cputime);
|
|
utime = cputime.utime;
|
|
if (cputime_le(cval, utime)) { /* about to fire */
|
|
cval = jiffies_to_cputime(1);
|
|
} else {
|
|
cval = cputime_sub(cval, utime);
|
|
}
|
|
}
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
cputime_to_timeval(cval, &value->it_value);
|
|
cputime_to_timeval(cinterval, &value->it_interval);
|
|
break;
|
|
case ITIMER_PROF:
|
|
spin_lock_irq(&tsk->sighand->siglock);
|
|
cval = tsk->signal->it_prof_expires;
|
|
cinterval = tsk->signal->it_prof_incr;
|
|
if (!cputime_eq(cval, cputime_zero)) {
|
|
struct task_cputime times;
|
|
cputime_t ptime;
|
|
|
|
thread_group_cputime(tsk, ×);
|
|
ptime = cputime_add(times.utime, times.stime);
|
|
if (cputime_le(cval, ptime)) { /* about to fire */
|
|
cval = jiffies_to_cputime(1);
|
|
} else {
|
|
cval = cputime_sub(cval, ptime);
|
|
}
|
|
}
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
cputime_to_timeval(cval, &value->it_value);
|
|
cputime_to_timeval(cinterval, &value->it_interval);
|
|
break;
|
|
default:
|
|
return(-EINVAL);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
asmlinkage long sys_getitimer(int which, struct itimerval __user *value)
|
|
{
|
|
int error = -EFAULT;
|
|
struct itimerval get_buffer;
|
|
|
|
if (value) {
|
|
error = do_getitimer(which, &get_buffer);
|
|
if (!error &&
|
|
copy_to_user(value, &get_buffer, sizeof(get_buffer)))
|
|
error = -EFAULT;
|
|
}
|
|
return error;
|
|
}
|
|
|
|
|
|
/*
|
|
* The timer is automagically restarted, when interval != 0
|
|
*/
|
|
enum hrtimer_restart it_real_fn(struct hrtimer *timer)
|
|
{
|
|
struct signal_struct *sig =
|
|
container_of(timer, struct signal_struct, real_timer);
|
|
|
|
kill_pid_info(SIGALRM, SEND_SIG_PRIV, sig->leader_pid);
|
|
|
|
return HRTIMER_NORESTART;
|
|
}
|
|
|
|
/*
|
|
* Returns true if the timeval is in canonical form
|
|
*/
|
|
#define timeval_valid(t) \
|
|
(((t)->tv_sec >= 0) && (((unsigned long) (t)->tv_usec) < USEC_PER_SEC))
|
|
|
|
int do_setitimer(int which, struct itimerval *value, struct itimerval *ovalue)
|
|
{
|
|
struct task_struct *tsk = current;
|
|
struct hrtimer *timer;
|
|
ktime_t expires;
|
|
cputime_t cval, cinterval, nval, ninterval;
|
|
|
|
/*
|
|
* Validate the timevals in value.
|
|
*/
|
|
if (!timeval_valid(&value->it_value) ||
|
|
!timeval_valid(&value->it_interval))
|
|
return -EINVAL;
|
|
|
|
switch (which) {
|
|
case ITIMER_REAL:
|
|
again:
|
|
spin_lock_irq(&tsk->sighand->siglock);
|
|
timer = &tsk->signal->real_timer;
|
|
if (ovalue) {
|
|
ovalue->it_value = itimer_get_remtime(timer);
|
|
ovalue->it_interval
|
|
= ktime_to_timeval(tsk->signal->it_real_incr);
|
|
}
|
|
/* We are sharing ->siglock with it_real_fn() */
|
|
if (hrtimer_try_to_cancel(timer) < 0) {
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
goto again;
|
|
}
|
|
expires = timeval_to_ktime(value->it_value);
|
|
if (expires.tv64 != 0) {
|
|
tsk->signal->it_real_incr =
|
|
timeval_to_ktime(value->it_interval);
|
|
hrtimer_start(timer, expires, HRTIMER_MODE_REL);
|
|
} else
|
|
tsk->signal->it_real_incr.tv64 = 0;
|
|
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
break;
|
|
case ITIMER_VIRTUAL:
|
|
nval = timeval_to_cputime(&value->it_value);
|
|
ninterval = timeval_to_cputime(&value->it_interval);
|
|
spin_lock_irq(&tsk->sighand->siglock);
|
|
cval = tsk->signal->it_virt_expires;
|
|
cinterval = tsk->signal->it_virt_incr;
|
|
if (!cputime_eq(cval, cputime_zero) ||
|
|
!cputime_eq(nval, cputime_zero)) {
|
|
if (cputime_gt(nval, cputime_zero))
|
|
nval = cputime_add(nval,
|
|
jiffies_to_cputime(1));
|
|
set_process_cpu_timer(tsk, CPUCLOCK_VIRT,
|
|
&nval, &cval);
|
|
}
|
|
tsk->signal->it_virt_expires = nval;
|
|
tsk->signal->it_virt_incr = ninterval;
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
if (ovalue) {
|
|
cputime_to_timeval(cval, &ovalue->it_value);
|
|
cputime_to_timeval(cinterval, &ovalue->it_interval);
|
|
}
|
|
break;
|
|
case ITIMER_PROF:
|
|
nval = timeval_to_cputime(&value->it_value);
|
|
ninterval = timeval_to_cputime(&value->it_interval);
|
|
spin_lock_irq(&tsk->sighand->siglock);
|
|
cval = tsk->signal->it_prof_expires;
|
|
cinterval = tsk->signal->it_prof_incr;
|
|
if (!cputime_eq(cval, cputime_zero) ||
|
|
!cputime_eq(nval, cputime_zero)) {
|
|
if (cputime_gt(nval, cputime_zero))
|
|
nval = cputime_add(nval,
|
|
jiffies_to_cputime(1));
|
|
set_process_cpu_timer(tsk, CPUCLOCK_PROF,
|
|
&nval, &cval);
|
|
}
|
|
tsk->signal->it_prof_expires = nval;
|
|
tsk->signal->it_prof_incr = ninterval;
|
|
spin_unlock_irq(&tsk->sighand->siglock);
|
|
if (ovalue) {
|
|
cputime_to_timeval(cval, &ovalue->it_value);
|
|
cputime_to_timeval(cinterval, &ovalue->it_interval);
|
|
}
|
|
break;
|
|
default:
|
|
return -EINVAL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* alarm_setitimer - set alarm in seconds
|
|
*
|
|
* @seconds: number of seconds until alarm
|
|
* 0 disables the alarm
|
|
*
|
|
* Returns the remaining time in seconds of a pending timer or 0 when
|
|
* the timer is not active.
|
|
*
|
|
* On 32 bit machines the seconds value is limited to (INT_MAX/2) to avoid
|
|
* negative timeval settings which would cause immediate expiry.
|
|
*/
|
|
unsigned int alarm_setitimer(unsigned int seconds)
|
|
{
|
|
struct itimerval it_new, it_old;
|
|
|
|
#if BITS_PER_LONG < 64
|
|
if (seconds > INT_MAX)
|
|
seconds = INT_MAX;
|
|
#endif
|
|
it_new.it_value.tv_sec = seconds;
|
|
it_new.it_value.tv_usec = 0;
|
|
it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
|
|
|
|
do_setitimer(ITIMER_REAL, &it_new, &it_old);
|
|
|
|
/*
|
|
* We can't return 0 if we have an alarm pending ... And we'd
|
|
* better return too much than too little anyway
|
|
*/
|
|
if ((!it_old.it_value.tv_sec && it_old.it_value.tv_usec) ||
|
|
it_old.it_value.tv_usec >= 500000)
|
|
it_old.it_value.tv_sec++;
|
|
|
|
return it_old.it_value.tv_sec;
|
|
}
|
|
|
|
asmlinkage long sys_setitimer(int which,
|
|
struct itimerval __user *value,
|
|
struct itimerval __user *ovalue)
|
|
{
|
|
struct itimerval set_buffer, get_buffer;
|
|
int error;
|
|
|
|
if (value) {
|
|
if(copy_from_user(&set_buffer, value, sizeof(set_buffer)))
|
|
return -EFAULT;
|
|
} else
|
|
memset((char *) &set_buffer, 0, sizeof(set_buffer));
|
|
|
|
error = do_setitimer(which, &set_buffer, ovalue ? &get_buffer : NULL);
|
|
if (error || !ovalue)
|
|
return error;
|
|
|
|
if (copy_to_user(ovalue, &get_buffer, sizeof(get_buffer)))
|
|
return -EFAULT;
|
|
return 0;
|
|
}
|