Linux 4.6-rc6
[cris-mirror.git] / include / linux / ratelimit.h
blob18102529254eeb8fe9fe112c9b455ca7c4e8650f
1 #ifndef _LINUX_RATELIMIT_H
2 #define _LINUX_RATELIMIT_H
4 #include <linux/param.h>
5 #include <linux/spinlock.h>
7 #define DEFAULT_RATELIMIT_INTERVAL (5 * HZ)
8 #define DEFAULT_RATELIMIT_BURST 10
10 struct ratelimit_state {
11 raw_spinlock_t lock; /* protect the state */
13 int interval;
14 int burst;
15 int printed;
16 int missed;
17 unsigned long begin;
20 #define RATELIMIT_STATE_INIT(name, interval_init, burst_init) { \
21 .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
22 .interval = interval_init, \
23 .burst = burst_init, \
26 #define RATELIMIT_STATE_INIT_DISABLED \
27 RATELIMIT_STATE_INIT(ratelimit_state, 0, DEFAULT_RATELIMIT_BURST)
29 #define DEFINE_RATELIMIT_STATE(name, interval_init, burst_init) \
31 struct ratelimit_state name = \
32 RATELIMIT_STATE_INIT(name, interval_init, burst_init) \
34 static inline void ratelimit_state_init(struct ratelimit_state *rs,
35 int interval, int burst)
37 raw_spin_lock_init(&rs->lock);
38 rs->interval = interval;
39 rs->burst = burst;
40 rs->printed = 0;
41 rs->missed = 0;
42 rs->begin = 0;
45 extern struct ratelimit_state printk_ratelimit_state;
47 extern int ___ratelimit(struct ratelimit_state *rs, const char *func);
48 #define __ratelimit(state) ___ratelimit(state, __func__)
50 #ifdef CONFIG_PRINTK
52 #define WARN_ON_RATELIMIT(condition, state) \
53 WARN_ON((condition) && __ratelimit(state))
55 #define WARN_RATELIMIT(condition, format, ...) \
56 ({ \
57 static DEFINE_RATELIMIT_STATE(_rs, \
58 DEFAULT_RATELIMIT_INTERVAL, \
59 DEFAULT_RATELIMIT_BURST); \
60 int rtn = !!(condition); \
62 if (unlikely(rtn && __ratelimit(&_rs))) \
63 WARN(rtn, format, ##__VA_ARGS__); \
65 rtn; \
68 #else
70 #define WARN_ON_RATELIMIT(condition, state) \
71 WARN_ON(condition)
73 #define WARN_RATELIMIT(condition, format, ...) \
74 ({ \
75 int rtn = WARN(condition, format, ##__VA_ARGS__); \
76 rtn; \
79 #endif
81 #endif /* _LINUX_RATELIMIT_H */