Blame view
lib/ratelimit.c
1.42 KB
5f97a5a87
|
1 2 3 4 5 |
/* * ratelimit.c - Do something with rate limit. * * Isolated from kernel/printk.c by Dave Young <hidave.darkstar@gmail.com> * |
717115e1a
|
6 7 8 |
* 2008-05-01 rewrite the function and use a ratelimit_state data struct as * parameter. Now every user can use their own standalone ratelimit_state. * |
5f97a5a87
|
9 |
* This file is released under the GPLv2. |
5f97a5a87
|
10 |
*/ |
3fff4c42b
|
11 |
#include <linux/ratelimit.h> |
5f97a5a87
|
12 13 14 15 16 |
#include <linux/jiffies.h> #include <linux/module.h> /* * __ratelimit - rate limiting |
717115e1a
|
17 |
* @rs: ratelimit_state data |
5f97a5a87
|
18 |
* |
717115e1a
|
19 20 |
* This enforces a rate limit: not more than @rs->ratelimit_burst callbacks * in every @rs->ratelimit_jiffies |
5f97a5a87
|
21 |
*/ |
5c8287133
|
22 |
int ___ratelimit(struct ratelimit_state *rs, const char *func) |
5f97a5a87
|
23 |
{ |
4d9c377c8
|
24 |
unsigned long flags; |
979f693de
|
25 |
int ret; |
4d9c377c8
|
26 |
|
717115e1a
|
27 28 |
if (!rs->interval) return 1; |
5f97a5a87
|
29 |
|
edaac8e31
|
30 31 32 33 34 35 36 37 |
/* * If we contend on this state's lock then almost * by definition we are too busy to print a message, * in addition to the one that will be printed by * the entity that is holding the lock already: */ if (!spin_trylock_irqsave(&rs->lock, flags)) return 1; |
717115e1a
|
38 39 |
if (!rs->begin) rs->begin = jiffies; |
5f97a5a87
|
40 |
|
717115e1a
|
41 42 43 44 |
if (time_is_before_jiffies(rs->begin + rs->interval)) { if (rs->missed) printk(KERN_WARNING "%s: %d callbacks suppressed ", |
5c8287133
|
45 |
func, rs->missed); |
979f693de
|
46 |
rs->begin = 0; |
717115e1a
|
47 |
rs->printed = 0; |
979f693de
|
48 |
rs->missed = 0; |
5f97a5a87
|
49 |
} |
979f693de
|
50 51 52 53 54 55 56 57 |
if (rs->burst && rs->burst > rs->printed) { rs->printed++; ret = 1; } else { rs->missed++; ret = 0; } spin_unlock_irqrestore(&rs->lock, flags); |
717115e1a
|
58 |
|
979f693de
|
59 |
return ret; |
5f97a5a87
|
60 |
} |
5c8287133
|
61 |
EXPORT_SYMBOL(___ratelimit); |