iwlwifi: retfactor get_temperature functions
[linux-2.6/sactl.git] / lib / ratelimit.c
blob485e3040dcd4cc7eedc1f5981fd4b8bd572572b6
1 /*
2 * ratelimit.c - Do something with rate limit.
4 * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
6 * This file is released under the GPLv2.
8 */
10 #include <linux/kernel.h>
11 #include <linux/jiffies.h>
12 #include <linux/module.h>
15 * __ratelimit - rate limiting
16 * @ratelimit_jiffies: minimum time in jiffies between two callbacks
17 * @ratelimit_burst: number of callbacks we do before ratelimiting
19 * This enforces a rate limit: not more than @ratelimit_burst callbacks
20 * in every ratelimit_jiffies
22 int __ratelimit(int ratelimit_jiffies, int ratelimit_burst)
24 static DEFINE_SPINLOCK(ratelimit_lock);
25 static unsigned toks = 10 * 5 * HZ;
26 static unsigned long last_msg;
27 static int missed;
28 unsigned long flags;
29 unsigned long now = jiffies;
31 spin_lock_irqsave(&ratelimit_lock, flags);
32 toks += now - last_msg;
33 last_msg = now;
34 if (toks > (ratelimit_burst * ratelimit_jiffies))
35 toks = ratelimit_burst * ratelimit_jiffies;
36 if (toks >= ratelimit_jiffies) {
37 int lost = missed;
39 missed = 0;
40 toks -= ratelimit_jiffies;
41 spin_unlock_irqrestore(&ratelimit_lock, flags);
42 if (lost)
43 printk(KERN_WARNING "%s: %d messages suppressed\n",
44 __func__, lost);
45 return 1;
47 missed++;
48 spin_unlock_irqrestore(&ratelimit_lock, flags);
49 return 0;
51 EXPORT_SYMBOL(__ratelimit);