限制printk打印频率函数printk_ratelimit

来源:互联网 发布:天下3最帅捏脸数据 编辑:程序博客网 时间:2024/05/29 17:04

    在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__);     \                                                              })
0 0
原创粉丝点击