/* Slowlog implements a system that is able to remember the latest N
* queries that took more than M microseconds to execute.
*
* The execution time to reach to be logged in the slow log is set
* using the 'slowlog-log-slower-than' config directive, that is also
* readable and writable using the CONFIG SET/GET command.
*
* The slow queries log is actually not "logged" in the Redis log file
* but is accessible thanks to the SLOWLOG command.
*
*大致意思就是SlowLog记录的是系统最近N个超过一定时间的查询,就是比较耗时的查询
* ----------------------------------------------------------------------------
里面定义了一个slowLog entry的结构体:
/* This structure defines an entry inside the slow log list */
/* 慢日志结构体,将会插入到slowLogList,慢日志列表中 */
typedef struct slowlogEntry {
robj **argv;
int argc;
//自身的id标识
long long id; /* Unique entry identifier. */
//query操作所消耗的时间,单位为nanoseconds
long long duration; /* Time spent by the query, in nanoseconds. */
//查询发发生的时间
time_t time; /* Unix time at which the query was executed. */
} slowlogEntry;
/* Exported API */
void slowlogInit(void); /* slowlog初始化操作 */
void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration); /* slowLogEntry压入列表操作 */
/* Initialize the slow log. This function should be called a single time
* at server startup. */
/* slowLog的初始化操作 */
void slowlogInit(void) {
//创建slowLog的List
server.slowlog = listCreate();
//第一个entry_id声明为0
server.slowlog_entry_id = 0;
listSetFreeMethod(server.slowlog,slowlogFreeEntry);
}
插入列表的方法:
/* Push a new entry into the slow log.
* This function will make sure to trim the slow log accordingly to the
* configured max length. */
/* 插入一个entry到slowLog列表中,如果时间超出给定的时间范围时 */
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)
//如果entry的duration时间超出slowlog_log_slower_than时间,则添加
listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));
/* Remove old entries if needed. */
while (listLength(server.slowlog) > server.slowlog_max_len)
//如果列表长度已经超出slowLog的最大值,移除最后一个slowLogEntry
listDelNode(server.slowlog,listLast(server.slowlog));
}