设为首页 收藏本站
查看: 3174|回复: 0

[经验分享] redis源码笔记

[复制链接]

尚未签到

发表于 2015-7-21 05:16:44 | 显示全部楼层 |阅读模式
  slowlog是redis提供的进行query分析的工具。它将执行时间长的命令统一以list形式保存在内存之中,使用者可以通过slowlog命令查看这些慢query,从而分析系统瓶颈。
  最好的分析笔记是作者的注释,除此之外,会做简要记录。
  slowlog.h



1 /* This structure defines an entry inside the slow log list */
2 typedef struct slowlogEntry {
3     robj **argv;                      //记录query参数
4     int argc;
5     long long id;       /* Unique entry identifier. */
6     long long duration; /* Time spent by the query, in nanoseconds. */
7     time_t time;        /* Unix time at which the query was executed. */
8 } slowlogEntry;
9
10 /* Exported API */
11 void slowlogInit(void);               //在redis.c的initServer函数中被调用,初始化一个list结构,只在系统启动时调用一次
12 void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration);
13
14 /* Exported commands */
15 void slowlogCommand(redisClient *c);  //slowlog Command将会触发此函数被调用
  slowlog.c



  1 #include "redis.h"
  2 #include "slowlog.h"
  3
  4 /* Slowlog implements a system that is able to remember the latest N
  5  * queries that took more than M microseconds to execute.
  6  *
  7  * The execution time to reach to be logged in the slow log is set
  8  * using the 'slowlog-log-slower-than' config directive, that is also
  9  * readable and writable using the CONFIG SET/GET command.
10  *
11  * The slow queries log is actually not "logged" in the Redis log file     //只在内存中保存
12  * but is accessible thanks to the SLOWLOG command. */
13
14 /* Create a new slowlog entry.
15  * Incrementing the ref count of all the objects retained is up to
16  * this function. */
17 slowlogEntry *slowlogCreateEntry(robj **argv, int argc, long long duration) {
18     slowlogEntry *se = zmalloc(sizeof(*se));
19     int j;
20
21     se->argc = argc;
22     se->argv = zmalloc(sizeof(robj*)*argc);
23     for (j = 0; j < argc; j++) {
24         se->argv[j] = argv[j];
25         incrRefCount(argv[j]);
26     }
27     se->time = time(NULL);
28     se->duration = duration;
29     se->id = server.slowlog_entry_id++;
30     return se;
31 }
32
33 /* Free a slow log entry. The argument is void so that the prototype of this
34  * function matches the one of the 'free' method of adlist.c.
35  *
36  * This function will take care to release all the retained object. */
37 void slowlogFreeEntry(void *septr) {
38     slowlogEntry *se = septr;
39     int j;
40
41     for (j = 0; j < se->argc; j++)
42         decrRefCount(se->argv[j]);
43     zfree(se->argv);
44     zfree(se);
45 }
46
47 /* Initialize the slow log. This function should be called a single time
48  * at server startup. */
49 void slowlogInit(void) {
50     server.slowlog = listCreate();
51     server.slowlog_entry_id = 0;
52     listSetFreeMethod(server.slowlog,slowlogFreeEntry);
53 }
54
55 /* Push a new entry into the slow log.
56  * This function will make sure to trim the slow log accordingly to the
57  * configured max length. */
58 void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration) {
59     if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */
60     if (duration >= server.slowlog_log_slower_than)
61         listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));
62
63     /* Remove old entries if needed. */
64     while (listLength(server.slowlog) > server.slowlog_max_len)
65         listDelNode(server.slowlog,listLast(server.slowlog));
66 }  //该函数在每次命令执行时均被调用,对于非慢速的命令,只有一个分支调用的开销;
67
68 /* Remove all the entries from the current slow log. */
69 void slowlogReset(void) {
70     while (listLength(server.slowlog) > 0)
71         listDelNode(server.slowlog,listLast(server.slowlog));
72 }
73
74 /* The SLOWLOG command. Implements all the subcommands needed to handle the
75  * Redis slow log. */ slow long get (count) || slowlog reset || slowlog len
76 void slowlogCommand(redisClient *c) {
77     if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"reset")) {
78         slowlogReset();
79         addReply(c,shared.ok);
80     } else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"len")) {
81         addReplyLongLong(c,listLength(server.slowlog));
82     } else if ((c->argc == 2 || c->argc == 3) &&
83                !strcasecmp(c->argv[1]->ptr,"get"))
84     {
85         long count = 10, sent = 0;
86         listIter li;
87         void *totentries;
88         listNode *ln;
89         slowlogEntry *se;
90
91         if (c->argc == 3 &&
92             getLongFromObjectOrReply(c,c->argv[2],&count,NULL) != REDIS_OK)
93             return;
94
95         listRewind(server.slowlog,&li);
96         totentries = addDeferredMultiBulkLength(c);
97         while(count-- && (ln = listNext(&li))) {
98             int j;
99
100             se = ln->value;
101             addReplyMultiBulkLen(c,4);
102             addReplyLongLong(c,se->id);
103             addReplyLongLong(c,se->time);
104             addReplyLongLong(c,se->duration);
105             addReplyMultiBulkLen(c,se->argc);
106             for (j = 0; j < se->argc; j++)
107                 addReplyBulk(c,se->argv[j]);     //返回的消息是一个类似嵌套的结构
108             sent++;
109         }
110         setDeferredMultiBulkLength(c,totentries,sent);
111     } else {
112         addReplyError(c,
113             "Unknown SLOWLOG subcommand or wrong # of args. Try GET, RESET, LEN.");
114     }
115 }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88795-1-1.html 上篇帖子: redis源码笔记-dict.c 下篇帖子: Redis简单本机测试
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表