mirror of
https://gitee.com/Vancouver2017/luban-lite.git
synced 2025-12-25 05:28:55 +00:00
178 lines
6.2 KiB
C
178 lines
6.2 KiB
C
|
|
#include <linux/types.h>
|
|
#include <linux/unaligned.h>
|
|
#include <linux/bitops.h>
|
|
#include <linux/jiffies.h>
|
|
|
|
#define raw_smp_processor_id() (0)
|
|
|
|
static unsigned long round_jiffies_common(unsigned long j, int cpu,
|
|
bool force_up)
|
|
{
|
|
int rem;
|
|
unsigned long original = j;
|
|
|
|
/*
|
|
* We don't want all cpus firing their timers at once hitting the
|
|
* same lock or cachelines, so we skew each extra cpu with an extra
|
|
* 3 jiffies. This 3 jiffies came originally from the mm/ code which
|
|
* already did this.
|
|
* The skew is done by adding 3*cpunr, then round, then subtract this
|
|
* extra offset again.
|
|
*/
|
|
j += cpu * 3;
|
|
|
|
rem = j % HZ;
|
|
|
|
/*
|
|
* If the target jiffie is just after a whole second (which can happen
|
|
* due to delays of the timer irq, long irq off times etc etc) then
|
|
* we should round down to the whole second, not up. Use 1/4th second
|
|
* as cutoff for this rounding as an extreme upper bound for this.
|
|
* But never round down if @force_up is set.
|
|
*/
|
|
if (rem < HZ/4 && !force_up) /* round down */
|
|
j = j - rem;
|
|
else /* round up */
|
|
j = j - rem + HZ;
|
|
|
|
/* now that we have rounded, subtract the extra skew again */
|
|
j -= cpu * 3;
|
|
|
|
/*
|
|
* Make sure j is still in the future. Otherwise return the
|
|
* unmodified value.
|
|
*/
|
|
return time_is_after_jiffies(j) ? j : original;
|
|
}
|
|
|
|
/**
|
|
* __round_jiffies - function to round jiffies to a full second
|
|
* @j: the time in (absolute) jiffies that should be rounded
|
|
* @cpu: the processor number on which the timeout will happen
|
|
*
|
|
* __round_jiffies() rounds an absolute time in the future (in jiffies)
|
|
* up or down to (approximately) full seconds. This is useful for timers
|
|
* for which the exact time they fire does not matter too much, as long as
|
|
* they fire approximately every X seconds.
|
|
*
|
|
* By rounding these timers to whole seconds, all such timers will fire
|
|
* at the same time, rather than at various times spread out. The goal
|
|
* of this is to have the CPU wake up less, which saves power.
|
|
*
|
|
* The exact rounding is skewed for each processor to avoid all
|
|
* processors firing at the exact same time, which could lead
|
|
* to lock contention or spurious cache line bouncing.
|
|
*
|
|
* The return value is the rounded version of the @j parameter.
|
|
*/
|
|
unsigned long __round_jiffies(unsigned long j, int cpu)
|
|
{
|
|
return round_jiffies_common(j, cpu, false);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__round_jiffies);
|
|
|
|
/**
|
|
* __round_jiffies_relative - function to round jiffies to a full second
|
|
* @j: the time in (relative) jiffies that should be rounded
|
|
* @cpu: the processor number on which the timeout will happen
|
|
*
|
|
* __round_jiffies_relative() rounds a time delta in the future (in jiffies)
|
|
* up or down to (approximately) full seconds. This is useful for timers
|
|
* for which the exact time they fire does not matter too much, as long as
|
|
* they fire approximately every X seconds.
|
|
*
|
|
* By rounding these timers to whole seconds, all such timers will fire
|
|
* at the same time, rather than at various times spread out. The goal
|
|
* of this is to have the CPU wake up less, which saves power.
|
|
*
|
|
* The exact rounding is skewed for each processor to avoid all
|
|
* processors firing at the exact same time, which could lead
|
|
* to lock contention or spurious cache line bouncing.
|
|
*
|
|
* The return value is the rounded version of the @j parameter.
|
|
*/
|
|
unsigned long __round_jiffies_relative(unsigned long j, int cpu)
|
|
{
|
|
unsigned long j0 = jiffies;
|
|
|
|
/* Use j0 because jiffies might change while we run */
|
|
return round_jiffies_common(j + j0, cpu, false) - j0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(__round_jiffies_relative);
|
|
|
|
/**
|
|
* round_jiffies - function to round jiffies to a full second
|
|
* @j: the time in (absolute) jiffies that should be rounded
|
|
*
|
|
* round_jiffies() rounds an absolute time in the future (in jiffies)
|
|
* up or down to (approximately) full seconds. This is useful for timers
|
|
* for which the exact time they fire does not matter too much, as long as
|
|
* they fire approximately every X seconds.
|
|
*
|
|
* By rounding these timers to whole seconds, all such timers will fire
|
|
* at the same time, rather than at various times spread out. The goal
|
|
* of this is to have the CPU wake up less, which saves power.
|
|
*
|
|
* The return value is the rounded version of the @j parameter.
|
|
*/
|
|
unsigned long round_jiffies(unsigned long j)
|
|
{
|
|
return round_jiffies_common(j, raw_smp_processor_id(), false);
|
|
}
|
|
EXPORT_SYMBOL_GPL(round_jiffies);
|
|
|
|
/**
|
|
* round_jiffies_relative - function to round jiffies to a full second
|
|
* @j: the time in (relative) jiffies that should be rounded
|
|
*
|
|
* round_jiffies_relative() rounds a time delta in the future (in jiffies)
|
|
* up or down to (approximately) full seconds. This is useful for timers
|
|
* for which the exact time they fire does not matter too much, as long as
|
|
* they fire approximately every X seconds.
|
|
*
|
|
* By rounding these timers to whole seconds, all such timers will fire
|
|
* at the same time, rather than at various times spread out. The goal
|
|
* of this is to have the CPU wake up less, which saves power.
|
|
*
|
|
* The return value is the rounded version of the @j parameter.
|
|
*/
|
|
unsigned long round_jiffies_relative(unsigned long j)
|
|
{
|
|
return __round_jiffies_relative(j, raw_smp_processor_id());
|
|
}
|
|
EXPORT_SYMBOL_GPL(round_jiffies_relative);
|
|
|
|
/**
|
|
* __round_jiffies_up - function to round jiffies up to a full second
|
|
* @j: the time in (absolute) jiffies that should be rounded
|
|
* @cpu: the processor number on which the timeout will happen
|
|
*
|
|
* This is the same as __round_jiffies() except that it will never
|
|
* round down. This is useful for timeouts for which the exact time
|
|
* of firing does not matter too much, as long as they don't fire too
|
|
* early.
|
|
*/
|
|
unsigned long __round_jiffies_up(unsigned long j, int cpu)
|
|
{
|
|
return round_jiffies_common(j, cpu, true);
|
|
}
|
|
EXPORT_SYMBOL_GPL(__round_jiffies_up);
|
|
|
|
/**
|
|
* round_jiffies_up - function to round jiffies up to a full second
|
|
* @j: the time in (absolute) jiffies that should be rounded
|
|
*
|
|
* This is the same as round_jiffies() except that it will never
|
|
* round down. This is useful for timeouts for which the exact time
|
|
* of firing does not matter too much, as long as they don't fire too
|
|
* early.
|
|
*/
|
|
unsigned long round_jiffies_up(unsigned long j)
|
|
{
|
|
return round_jiffies_common(j, raw_smp_processor_id(), true);
|
|
}
|
|
EXPORT_SYMBOL_GPL(round_jiffies_up);
|
|
|
|
|