转自:http://blog.csdn.net/lkkey80/article/details/45190095
版权声明:博文地址 http://blog.csdn.net/lkkey80?viewmode=list 转载请注明出处
在Linux内核代码里当需要限制printk打印频率时会用到__ratelimit或printk_ratelimit(封装了__ratelimit)。
- /*
- * printk rate limiting, lifted from the networking subsystem.
- *
- * This enforces a rate limit: not more than 10 kernel messages
- * every 5s to make a denial-of-service attack impossible.
- */
- DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
- int printk_ratelimit(void)
- {
- return __ratelimit(&printk_ratelimit_state);
- }
- EXPORT_SYMBOL(printk_ratelimit);
/var/log/messages里一个可能的输出如下:
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: end_request: I/O error, dev sdr, sector 2104
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: end_request: I/O error, dev sdr, sector 2048
kernel: __ratelimit: 125 callbacks suppressed
kernel: sd 21:0:0:0: [sdr] Unhandled error code
kernel: sd 21:0:0:0: [sdr] Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK
kernel: sd 21:0:0:0: [sdr] CDB: Read(10): 28 00 00 00 00 00 00 00 20 00
kernel: __ratelimit: 125 callbacks suppressed
kernel: __ratelimit: 128 callbacks suppressed
kernel: Buffer I/O error on device sdr, logical block 0
kernel: Buffer I/O error on device sdr, logical block 1
kernel: Buffer I/O error on device sdr, logical block 2
kernel: Buffer I/O error on device sdr, logical block 3
printk_ratelimit默认允许在5s内最多打印10条消息出来。可通过 /proc/sys/kernel/printk_ratelimit (多长时间)和 /proc/sys/kernel/printk_ratelimit_burst (在printk_ratelimit时间段内最多允许的消息数量)。当然如果内核代码里没有调用printk_ratelimit就不受这两个值的限制。
内核块设备代码中的一个例子如下:
- bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
- {
- int total_bytes, bio_nbytes, next_idx = 0;
- struct bio *bio;
- if (!req->bio)
- return false;
- trace_block_rq_complete(req->q, req);
- /*
- * For fs requests, rq is just carrier of independent bio's
- * and each partial completion should be handled separately.
- * Reset per-request error on each partial completion.
- *
- * TODO: tj: This is too subtle. It would be better to let
- * low level drivers do what they see fit.
- */
- if (req->cmd_type == REQ_TYPE_FS)
- req->errors = 0;
- if (error && req->cmd_type == REQ_TYPE_FS &&
- !(req->cmd_flags & REQ_QUIET)) {
- char *error_type;
- switch (error) {
- case -ENOLINK:
- error_type = "recoverable transport";
- break;
- case -EREMOTEIO:
- error_type = "critical target";
- break;
- case -EBADE:
- error_type = "critical nexus";
- break;
- case -EIO:
- default:
- error_type = "I/O";
- break;
- }
- printk_ratelimited(KERN_ERR "end_request: %s error, dev %s, "
- "sector %llu\n", error_type, req->rq_disk ?
- req->rq_disk->disk_name : "?",
- (unsigned long long)blk_rq_pos(req));
- }
- blk_account_io_completion(req, nr_bytes);
- ...
- }
printk_ratelimited是一个宏,并直接调用了 __ratelimit
-
- #define printk_ratelimited(fmt, ...) ({ \
- static struct ratelimit_state _rs = { \
- .interval = DEFAULT_RATELIMIT_INTERVAL, \
- .burst = DEFAULT_RATELIMIT_BURST, \
- }; \
- \
- if (!__ratelimit(&_rs)) \
- printk(fmt, ##__VA_ARGS__); \
- })