aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ratelimit.c
diff options
context:
space:
mode:
authorDave Young <hidave.darkstar@gmail.com>2008-04-29 03:59:43 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2008-04-29 11:06:06 -0400
commit5f97a5a8799b8d7d0afdb9d68a50a4e0e8298a05 (patch)
tree03f0a35e50e9a765603fc9249e601b52e043fd0d /lib/ratelimit.c
parent762873c251b056c6c1b29e83a4dabafb064e5421 (diff)
isolate ratelimit from printk.c for other use
Due to the rcupreempt.h WARN_ON trigged, I got 2G syslog file. For some serious complaining of kernel, we need repeat the warnings, so here I isolate the ratelimit part of printk.c to a standalone file. Signed-off-by: Dave Young <hidave.darkstar@gmail.com> Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Tested-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/ratelimit.c')
-rw-r--r--lib/ratelimit.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/ratelimit.c b/lib/ratelimit.c
new file mode 100644
index 000000000000..485e3040dcd4
--- /dev/null
+++ b/lib/ratelimit.c
@@ -0,0 +1,51 @@
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 */
22int __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}
51EXPORT_SYMBOL(__ratelimit);