redis-慢查询日志

时间:2017-07-29 14:41:29   收藏:0   阅读:296
慢查询日志是 Redis 提供的一个用于观察系统性能的功能
 
相关数据结构
每条慢查询日志都以一个 slowlog.h/slowlogEntry 结构定义:
 
typedef struct slowlogEntry {
     // 命令参数
     robj **argv;
    
     // 命令参数数量
     int argc;
    
     // 唯一标识符
     long long id; /* Unique entry identifier. */
    
     // 执行命令消耗的时间,以纳秒( 1 / 1,000,000,000 秒)为单位
     long long duration; /* Time spent by the query, in nanoseconds. */
    
     // 命令执行时的时间
     time_t time; /* Unix time at which the query was executed. */
} slowlogEntry;
 
记录服务器状态的 redis.h/redisServer 结构里保存了几个和慢查询有关的属性:
struct redisServer {
     // ... other fields
 
     // 保存慢查询日志的链表
     list *slowlog; /* SLOWLOG list of commands */
 
     // 慢查询日志的当前 id 值
     long long slowlog_entry_id; /* SLOWLOG current entry ID */
 
     // 慢查询时间限制
     long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
 
     // 慢查询日志的最大条目数量
     unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */
 
     // ... other fields
};
 
 技术分享

技术分享

 
 
慢查询日志的记录
伪代码
def execute_redis_command_with_slowlog():
     # 命令执行前的时间
     start = ustime()
    
     # 执行命令
     execute_command(argv, argc)
 
     # 计算命令执行所耗费的时间
     duration = ustime() - start
 
     if slowlog_is_enabled:
          slowlogPushEntryIfNeed(argv, argc, duration)
 
def slowlogPushEntryIfNeed(argv, argc, duration)
     # 如果执行命令耗费的时间超过服务器设置命令执行时间上限
     # 那么创建一条新的 slowlog
     if duration > server.slowlog_log_slower_than:
 
          # 创建新 slowlog
          log = new slowlogEntry()
 
          # 设置各个域
          log.argv = argv
          log.argc = argc
          log.duration = duration
          log.id = server.slowlog_entry_id
          log.time = now()
 
          # 将新 slowlog 追加到日志链表末尾
          server.slowlog.append(log)
     
          # 更新服务器 slowlog
          server.slowlog_entry_id += 1
 

 

慢查询日志的操作
 
小结
 

原文:http://www.cnblogs.com/Aiapple/p/7255625.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!