• 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 < 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));
    }
  • 相关阅读:
    第一讲 jQuery入门
    Log4j日志记录
    第四讲 Hibernate 缓存管理
    第一讲 Hibernate 简介
    第三讲 Spring 持久层封装、事务控制
    对 PInvoke 函数的调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标
    善用 C# 3.0 Extensions 方法
    .net 下如何将文档文件(Word, Pdf等) 中的文本提取出来(转)
    vs2010常用快捷键 (转)
    Silverlight 视频学习札记(一)
  • 原文地址:https://www.cnblogs.com/learn-my-life/p/5664205.html
Copyright © 2020-2023  润新知