您的位置:首页 > 数据库 > Redis

redis的慢查询日志

2016-07-12 17:10 288 查看
慢查询日志记录最新的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 < 0) return; /* Slowlog disabled */ // 如果执行时间超过服务器设置的上限,那么将命令添加到慢查询日志 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));}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: