• Anna-Maria Gleixner's avatar
    timers: Implement optimization for same expiry time in mod_timer() · f00c0afd
    Anna-Maria Gleixner authored
    The existing optimization for same expiry time in mod_timer() checks whether
    the timer expiry time is the same as the new requested expiry time. In the old
    timer wheel implementation this does not take the slack batching into account,
    neither does the new implementation evaluate whether the new expiry time will
    requeue the timer to the same bucket.
    
    To optimize that, we can calculate the resulting bucket and check if the new
    expiry time is different from the current expiry time. This calculation
    happens outside the base lock held region. If the resulting bucket is the same
    we can avoid taking the base lock and requeueing the timer.
    
    If the timer needs to be requeued then we have to check under the base lock
    whether the base time has changed between the lockless calculation and taking
    the lock. If it has changed we need to recalculate under the lock.
    
    This optimization takes effect for timers which are enqueued into the less
    granular wheel levels (1 and above). With a simple test case the functionality
    has been verified:
    
                Before        After
     Match:       5.5%        86.6%
     Requeue:    94.5%        13.4%
     Recalc:                  <0.01%
    
    In the non optimized case the timer is requeued in 94.5% of the cases. With
    the index optimization in place the requeue rate drops to 13.4%. The case
    where the lockless index calculation has to be redone is less than 0.01%.
    
    With a real world test case (networking) we observed the following changes:
    
                Before        After
     Match:      97.8%        99.7%
     Requeue:     2.2%         0.3%
     Recalc:                  <0.001%
    
    That means two percent fewer lock/requeue/unlock operations done in one of
    the hot path use cases of timers.
    Signed-off-by: default avatarAnna-Maria Gleixner <anna-maria@linutronix.de>
    Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
    Cc: Arjan van de Ven <arjan@infradead.org>
    Cc: Chris Mason <clm@fb.com>
    Cc: Eric Dumazet <edumazet@google.com>
    Cc: Frederic Weisbecker <fweisbec@gmail.com>
    Cc: George Spelvin <linux@sciencehorizons.net>
    Cc: Josh Triplett <josh@joshtriplett.org>
    Cc: Len Brown <lenb@kernel.org>
    Cc: Linus Torvalds <torvalds@linux-foundation.org>
    Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Rik van Riel <riel@redhat.com>
    Cc: rt@linutronix.de
    Link: http://lkml.kernel.org/r/20160704094342.778527749@linutronix.deSigned-off-by: default avatarIngo Molnar <mingo@kernel.org>
    f00c0afd
timer.c 54.3 KB