Blame view
lib/ratelimit.c
1.52 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 |
2a7268abc
|
18 |
* @func: name of calling function |
5f97a5a87
|
19 |
* |
2a7268abc
|
20 21 22 23 24 25 |
* This enforces a rate limit: not more than @rs->burst callbacks * in every @rs->interval * * RETURNS: * 0 means callbacks will be suppressed. * 1 means go ahead and do it. |
5f97a5a87
|
26 |
*/ |
5c8287133
|
27 |
int ___ratelimit(struct ratelimit_state *rs, const char *func) |
5f97a5a87
|
28 |
{ |
4d9c377c8
|
29 |
unsigned long flags; |
979f693de
|
30 |
int ret; |
4d9c377c8
|
31 |
|
717115e1a
|
32 33 |
if (!rs->interval) return 1; |
5f97a5a87
|
34 |
|
edaac8e31
|
35 36 37 38 39 40 41 |
/* * 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)) |
57119c34e
|
42 |
return 0; |
edaac8e31
|
43 |
|
717115e1a
|
44 45 |
if (!rs->begin) rs->begin = jiffies; |
5f97a5a87
|
46 |
|
717115e1a
|
47 48 49 50 |
if (time_is_before_jiffies(rs->begin + rs->interval)) { if (rs->missed) printk(KERN_WARNING "%s: %d callbacks suppressed ", |
5c8287133
|
51 |
func, rs->missed); |
979f693de
|
52 |
rs->begin = 0; |
717115e1a
|
53 |
rs->printed = 0; |
979f693de
|
54 |
rs->missed = 0; |
5f97a5a87
|
55 |
} |
979f693de
|
56 57 58 59 60 61 62 63 |
if (rs->burst && rs->burst > rs->printed) { rs->printed++; ret = 1; } else { rs->missed++; ret = 0; } spin_unlock_irqrestore(&rs->lock, flags); |
717115e1a
|
64 |
|
979f693de
|
65 |
return ret; |
5f97a5a87
|
66 |
} |
5c8287133
|
67 |
EXPORT_SYMBOL(___ratelimit); |