]>
git.saurik.com Git - redis.git/blob - src/slowlog.c
4 /* Slowlog implements a system that is able to remember the latest N
5 * queries that took more than M microseconds to execute.
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.
11 * The slow queries log is actually not "logged" in the Redis log file
12 * but is accessible thanks to the SLOWLOG command. */
14 /* Create a new slowlog entry.
15 * Incrementing the ref count of all the objects retained is up to
17 slowlogEntry
*slowlogCreateEntry(robj
**argv
, int argc
, long long duration
) {
18 slowlogEntry
*se
= zmalloc(sizeof(*se
));
22 se
->argv
= zmalloc(sizeof(robj
*)*argc
);
23 for (j
= 0; j
< argc
; j
++) {
24 se
->argv
[j
] = argv
[j
];
25 incrRefCount(argv
[j
]);
27 se
->time
= time(NULL
);
28 se
->duration
= duration
;
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.
35 * This function will take care to release all the retained object. */
36 void slowlogFreeEntry(void *septr
) {
37 slowlogEntry
*se
= septr
;
40 for (j
= 0; j
< se
->argc
; j
++)
41 decrRefCount(se
->argv
[j
]);
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
);
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
));
60 /* Remove old entries if needed. */
61 while (listLength(server
.slowlog
) > server
.slowlog_max_len
)
62 listDelNode(server
.slowlog
,listLast(server
.slowlog
));
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
));
71 /* The SLOWLOG command. Implements all the subcommands needed to handle the
73 void slowlogCommand(redisClient
*c
) {
74 if (c
->argc
== 2 && !strcasecmp(c
->argv
[1]->ptr
,"reset")) {
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"))
82 long count
= 10, sent
= 0;
89 getLongFromObjectOrReply(c
,c
->argv
[2],&count
,NULL
) != REDIS_OK
)
92 listRewind(server
.slowlog
,&li
);
93 totentries
= addDeferredMultiBulkLength(c
);
94 while(count
-- && (ln
= listNext(&li
))) {
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
]);
106 setDeferredMultiBulkLength(c
,totentries
,sent
);
109 "Unknown SLOWLOG subcommand or wrong # of args. Try GET, RESET, LEN.");