Linux Headquarters
[ Register ]
[ About us ] [ Home Page ]

Advertisement
[ Kernel ] [ Documentation ] [ Links ] [ Books ]

Advertisement

Kernel v2.6.26-rc1 /lib/ratelimit.c

Filename:/lib/ratelimit.c
Lines Added:51
Lines Deleted:0
Also changed in: (Previous) 2.6.25-git20  2.6.25-git19  2.6.25-git18  2.6.25-git17  2.6.25-git16  2.6.25-git15 
(Following) 2.6.26-rc2  2.6.26-rc3  2.6.26-rc4  2.6.26-rc5  2.6.26-rc6  2.6.26-rc7 

Location
[  2.6.26-rc1
  [  lib
     o  ratelimit.c

Patch

diff --git a/lib/ratelimit.c b/lib/ratelimit.c
new file mode 100644
index 0000000..485e304
--- /dev/null
+++ b/lib/ratelimit.c
@@ -0,0 +1,51 @@
+/*
+ * ratelimit.c - Do something with rate limit.
+ *
+ * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com>
+ *
+ * This file is released under the GPLv2.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+
+/*
+ * __ratelimit - rate limiting
+ * @ratelimit_jiffies: minimum time in jiffies between two callbacks
+ * @ratelimit_burst: number of callbacks we do before ratelimiting
+ *
+ * This enforces a rate limit: not more than @ratelimit_burst callbacks
+ * in every ratelimit_jiffies
+ */
+int __ratelimit(int ratelimit_jiffies, int ratelimit_burst)
+{
+   static DEFINE_SPINLOCK(ratelimit_lock);
+   static unsigned toks = 10 * 5 * HZ;
+   static unsigned long last_msg;
+   static int missed;
+   unsigned long flags;
+   unsigned long now = jiffies;
+
+   spin_lock_irqsave(&ratelimit_lock, flags);
+   toks += now - last_msg;
+   last_msg = now;
+   if (toks > (ratelimit_burst * ratelimit_jiffies))
+      toks = ratelimit_burst * ratelimit_jiffies;
+   if (toks >= ratelimit_jiffies) {
+      int lost = missed;
+
+      missed = 0;
+      toks -= ratelimit_jiffies;
+      spin_unlock_irqrestore(&ratelimit_lock, flags);
+      if (lost)
+         printk(KERN_WARNING "%s: %d messages suppressed\n",
+            __func__, lost);
+      return 1;
+   }
+   missed++;
+   spin_unlock_irqrestore(&ratelimit_lock, flags);
+   return 0;
+}
+EXPORT_SYMBOL(__ratelimit);


Comments: webmaster (at) linuxhq.com.
Advertising: banners (at) linuxhq.com.
Compilation ©1998-2008 Linux Headquarters, Inc.