]> git.saurik.com Git - redis.git/blob - src/slowlog.c
hiredis max multi bulk nesting level raised to 7
[redis.git] / src / 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 return se;
30 }
31
32 /* Free a slow log entry. The argument is void so that the prototype of this
33 * function matches the one of the 'free' method of adlist.c.
34 *
35 * This function will take care to release all the retained object. */
36 void slowlogFreeEntry(void *septr) {
37 slowlogEntry *se = septr;
38 int j;
39
40 for (j = 0; j < se->argc; j++)
41 decrRefCount(se->argv[j]);
42 zfree(se->argv);
43 zfree(se);
44 }
45
46 /* Initialize the slow log. This function should be called a single time
47 * at server startup. */
48 void slowlogInit(void) {
49 server.slowlog = listCreate();
50 listSetFreeMethod(server.slowlog,slowlogFreeEntry);
51 }
52
53 /* Push a new entry into the slow log.
54 * This function will make sure to trim the slow log accordingly to the
55 * configured max length. */
56 void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration) {
57 if (duration > server.slowlog_log_slower_than)
58 listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));
59
60 /* Remove old entries if needed. */
61 while (listLength(server.slowlog) > server.slowlog_max_len)
62 listDelNode(server.slowlog,listLast(server.slowlog));
63 }
64
65 /* Remove all the entries from the current slow log. */
66 void slowlogReset(void) {
67 while (listLength(server.slowlog) > 0)
68 listDelNode(server.slowlog,listLast(server.slowlog));
69 }
70
71 /* The SLOWLOG command. Implements all the subcommands needed to handle the
72 * Redis slow log. */
73 void slowlogCommand(redisClient *c) {
74 if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"reset")) {
75 slowlogReset();
76 addReply(c,shared.ok);
77 } else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"len")) {
78 addReplyLongLong(c,listLength(server.slowlog));
79 } else if ((c->argc == 2 || c->argc == 3) &&
80 !strcasecmp(c->argv[1]->ptr,"get"))
81 {
82 long count = 10, sent = 0;
83 listIter li;
84 void *totentries;
85 listNode *ln;
86 slowlogEntry *se;
87
88 if (c->argc == 3 &&
89 getLongFromObjectOrReply(c,c->argv[2],&count,NULL) != REDIS_OK)
90 return;
91
92 listRewind(server.slowlog,&li);
93 totentries = addDeferredMultiBulkLength(c);
94 while(count-- && (ln = listNext(&li))) {
95 int j;
96
97 se = ln->value;
98 addReplyMultiBulkLen(c,3);
99 addReplyLongLong(c,se->time);
100 addReplyLongLong(c,se->duration);
101 addReplyMultiBulkLen(c,se->argc);
102 for (j = 0; j < se->argc; j++)
103 addReplyBulk(c,se->argv[j]);
104 sent++;
105 }
106 setDeferredMultiBulkLength(c,totentries,sent);
107 } else {
108 addReplyError(c,
109 "Unknown SLOWLOG subcommand or wrong # of args. Try GET, RESET, LEN.");
110 }
111 }