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.
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;
29 unsigned long now = jiffies;
31 spin_lock_irqsave(&ratelimit_lock, flags);
32 toks += now - last_msg;
34 if (toks > (ratelimit_burst * ratelimit_jiffies))
35 toks = ratelimit_burst * ratelimit_jiffies;
36 if (toks >= ratelimit_jiffies) {
40 toks -= ratelimit_jiffies;
41 spin_unlock_irqrestore(&ratelimit_lock, flags);
43 printk(KERN_WARNING "%s: %d messages suppressed\n",
48 spin_unlock_irqrestore(&ratelimit_lock, flags);
51 EXPORT_SYMBOL(__ratelimit);