linux 查看redis 日志文件,Redis的慢查询日志

慢查询日志

记录最新的N条执行时间超过M毫秒的命令。慢查询日志保存在内存中,而不是文件中,这保证了慢查询日志的效率。

慢查询日志的条目定义

/* This structure defines an entry inside the slow log list */

/*

* 慢查询日志

*/

typedef struct slowlogEntry {

// 命令与命令参数

robj **argv;

// 命令与命令参数的数量

int argc;

// 唯一标识符

long long id;       /* Unique entry identifier. */

// 执行命令消耗的时间,以微秒为单位

// 注释里说的 nanoseconds 是错误的

long long duration; /* Time spent by the query, in nanoseconds. */

// 命令执行时的时间,格式为 UNIX 时间戳

time_t time;        /* Unix time at which the query was executed. */

} slowlogEntry;

服务器和慢查询有关的定义

/* slowlog */

// 保存了所有慢查询日志的链表

list *slowlog;                  /* SLOWLOG list of commands */

// 下一条慢查询日志的 ID

long long slowlog_entry_id;     /* SLOWLOG current entry ID */

// 服务器配置 slowlog-log-slower-than 选项的值

long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */

// 服务器配置 slowlog-max-len 选项的值

unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */

服务器的慢查询存储在一个list中,list中的每一项都是一条慢查询日志,较新的日志总是保存在队首。慢查询日志中保存命令的执行参数和执行时间,如果超出系统限制,参数和日志可能被截断。

慢查询支持的客户端操作

GET:获取某条或者全部慢查询日志

RESET:清空慢查询日志

LEN:慢查询日志的数量

慢查询日志的应用

Redis每执行一条命令,就会记录命令的开始时间和结束时间,由此计算命令的执行时间。并发命令以及命令的执行时间传递给slowlogPushEntryIfNeeded,由slowlogPushEntryIfNeeded决定是否生成慢查询日志。

/* Call() is the core of Redis execution of a command */

// 调用命令的实现函数,执行命令

void call(redisClient *c, int flags)

{

//获取命令的执行时间

/* Log the command into the Slow log if needed, and populate the

* per-command statistics that we show in INFO commandstats. */

// 如果有需要,将命令放到 SLOWLOG 里面

if (flags & REDIS_CALL_SLOWLOG && c->cmd->proc != execCommand)

slowlogPushEntryIfNeeded(c->argv,c->argc,duration);

}

slowlogPushEntryIfNeeded的实现

判断系统标志位,并把慢查询日志加入到服务器的慢查询链表中

/* Push a new entry into the slow log.

*

* 如果参数 duration 超过服务器设置的上限时间,

* 那么将一个新条目以 FIFO 顺序推入到慢查询日志中。

*

* This function will make sure to trim the slow log accordingly to the

* configured max length.

*

* 根据服务器设置的最大日志长度,可能会对日志进行截断(trim)

*/

void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration) {

// 慢查询功能未开启,直接返回

if (server.slowlog_log_slower_than 

// 如果执行时间超过服务器设置的上限,那么将命令添加到慢查询日志

if (duration >= server.slowlog_log_slower_than)

// 新日志添加到链表表头

listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));

/* Remove old entries if needed. */

// 如果日志数量过多,那么进行删除

while (listLength(server.slowlog) > server.slowlog_max_len)

listDelNode(server.slowlog,listLast(server.slowlog));

}

下面关于Redis的文章您也可能喜欢,不妨参考下:

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值