isolate ratelimit from printk.c for other use
[safe/jmp/linux-2.6] / lib / ratelimit.c
1 /*
2  * ratelimit.c - Do something with rate limit.
3  *
4  * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
5  *
6  * This file is released under the GPLv2.
7  *
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/jiffies.h>
12 #include <linux/module.h>
13
14 /*
15  * __ratelimit - rate limiting
16  * @ratelimit_jiffies: minimum time in jiffies between two callbacks
17  * @ratelimit_burst: number of callbacks we do before ratelimiting
18  *
19  * This enforces a rate limit: not more than @ratelimit_burst callbacks
20  * in every ratelimit_jiffies
21  */
22 int __ratelimit(int ratelimit_jiffies, int ratelimit_burst)
23 {
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;
30
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;
38
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;
46         }
47         missed++;
48         spin_unlock_irqrestore(&ratelimit_lock, flags);
49         return 0;
50 }
51 EXPORT_SYMBOL(__ratelimit);