redis-cli.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300
  1. /* Redis CLI (command line interface)
  2. *
  3. * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fmacros.h"
  31. #include "version.h"
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <signal.h>
  36. #include <unistd.h>
  37. #include <time.h>
  38. #include <ctype.h>
  39. #include <errno.h>
  40. #include <sys/stat.h>
  41. #include <sys/time.h>
  42. #include <assert.h>
  43. #include <fcntl.h>
  44. #include <limits.h>
  45. #include <math.h>
  46. #include "hiredis.h"
  47. #include "sds.h"
  48. #include "zmalloc.h"
  49. #include "linenoise.h"
  50. #include "help.h"
  51. #include "anet.h"
  52. #include "ae.h"
  53. #define REDIS_NOTUSED(V) ((void) V)
  54. #define OUTPUT_STANDARD 0
  55. #define OUTPUT_RAW 1
  56. #define OUTPUT_CSV 2
  57. #define REDIS_CLI_KEEPALIVE_INTERVAL 15 /* seconds */
  58. #define REDIS_CLI_DEFAULT_PIPE_TIMEOUT 30 /* seconds */
  59. #define REDIS_CLI_HISTFILE_ENV "REDISCLI_HISTFILE"
  60. #define REDIS_CLI_HISTFILE_DEFAULT ".rediscli_history"
  61. /* --latency-dist palettes. */
  62. int spectrum_palette_color_size = 19;
  63. int spectrum_palette_color[] = {0,233,234,235,237,239,241,243,245,247,144,143,142,184,226,214,208,202,196};
  64. int spectrum_palette_mono_size = 13;
  65. int spectrum_palette_mono[] = {0,233,234,235,237,239,241,243,245,247,249,251,253};
  66. /* The actual palette in use. */
  67. int *spectrum_palette;
  68. int spectrum_palette_size;
  69. static redisContext *context;
  70. static struct config {
  71. char *hostip;
  72. int hostport;
  73. char *hostsocket;
  74. long repeat;
  75. long interval;
  76. int dbnum;
  77. int interactive;
  78. int shutdown;
  79. int monitor_mode;
  80. int pubsub_mode;
  81. int latency_mode;
  82. int latency_dist_mode;
  83. int latency_history;
  84. int lru_test_mode;
  85. long long lru_test_sample_size;
  86. int cluster_mode;
  87. int cluster_reissue_command;
  88. int slave_mode;
  89. int pipe_mode;
  90. int pipe_timeout;
  91. int getrdb_mode;
  92. int stat_mode;
  93. int scan_mode;
  94. int intrinsic_latency_mode;
  95. int intrinsic_latency_duration;
  96. char *pattern;
  97. char *rdb_filename;
  98. int bigkeys;
  99. int stdinarg; /* get last arg from stdin. (-x option) */
  100. char *auth;
  101. int output; /* output mode, see OUTPUT_* defines */
  102. sds mb_delim;
  103. char prompt[128];
  104. char *eval;
  105. int last_cmd_type;
  106. } config;
  107. static volatile sig_atomic_t force_cancel_loop = 0;
  108. static void usage(void);
  109. static void slaveMode(void);
  110. char *redisGitSHA1(void);
  111. char *redisGitDirty(void);
  112. /*------------------------------------------------------------------------------
  113. * Utility functions
  114. *--------------------------------------------------------------------------- */
  115. static long long ustime(void) {
  116. struct timeval tv;
  117. long long ust;
  118. gettimeofday(&tv, NULL);
  119. ust = ((long long)tv.tv_sec)*1000000;
  120. ust += tv.tv_usec;
  121. return ust;
  122. }
  123. static long long mstime(void) {
  124. return ustime()/1000;
  125. }
  126. static void cliRefreshPrompt(void) {
  127. int len;
  128. if (config.hostsocket != NULL)
  129. len = snprintf(config.prompt,sizeof(config.prompt),"redis %s",
  130. config.hostsocket);
  131. else
  132. len = anetFormatAddr(config.prompt, sizeof(config.prompt),
  133. config.hostip, config.hostport);
  134. /* Add [dbnum] if needed */
  135. if (config.dbnum != 0 && config.last_cmd_type != REDIS_REPLY_ERROR)
  136. len += snprintf(config.prompt+len,sizeof(config.prompt)-len,"[%d]",
  137. config.dbnum);
  138. snprintf(config.prompt+len,sizeof(config.prompt)-len,"> ");
  139. }
  140. static sds getHistoryPath() {
  141. char *path = NULL;
  142. sds historyPath = NULL;
  143. /* check the env for a histfile override */
  144. path = getenv(REDIS_CLI_HISTFILE_ENV);
  145. if (path != NULL && *path != '\0') {
  146. if (!strcmp("/dev/null", path)) {
  147. return NULL;
  148. }
  149. /* if the env is set, return it */
  150. historyPath = sdscatprintf(sdsempty(), "%s", path);
  151. } else {
  152. char *home = getenv("HOME");
  153. if (home != NULL && *home != '\0') {
  154. /* otherwise, return the default */
  155. historyPath = sdscatprintf(sdsempty(), "%s/%s", home, REDIS_CLI_HISTFILE_DEFAULT);
  156. }
  157. }
  158. return historyPath;
  159. }
  160. /*------------------------------------------------------------------------------
  161. * Help functions
  162. *--------------------------------------------------------------------------- */
  163. #define CLI_HELP_COMMAND 1
  164. #define CLI_HELP_GROUP 2
  165. typedef struct {
  166. int type;
  167. int argc;
  168. sds *argv;
  169. sds full;
  170. /* Only used for help on commands */
  171. struct commandHelp *org;
  172. } helpEntry;
  173. static helpEntry *helpEntries;
  174. static int helpEntriesLen;
  175. static sds cliVersion(void) {
  176. sds version;
  177. version = sdscatprintf(sdsempty(), "%s", REDIS_VERSION);
  178. /* Add git commit and working tree status when available */
  179. if (strtoll(redisGitSHA1(),NULL,16)) {
  180. version = sdscatprintf(version, " (git:%s", redisGitSHA1());
  181. if (strtoll(redisGitDirty(),NULL,10))
  182. version = sdscatprintf(version, "-dirty");
  183. version = sdscat(version, ")");
  184. }
  185. return version;
  186. }
  187. static void cliInitHelp(void) {
  188. int commandslen = sizeof(commandHelp)/sizeof(struct commandHelp);
  189. int groupslen = sizeof(commandGroups)/sizeof(char*);
  190. int i, len, pos = 0;
  191. helpEntry tmp;
  192. helpEntriesLen = len = commandslen+groupslen;
  193. helpEntries = malloc(sizeof(helpEntry)*len);
  194. for (i = 0; i < groupslen; i++) {
  195. tmp.argc = 1;
  196. tmp.argv = malloc(sizeof(sds));
  197. tmp.argv[0] = sdscatprintf(sdsempty(),"@%s",commandGroups[i]);
  198. tmp.full = tmp.argv[0];
  199. tmp.type = CLI_HELP_GROUP;
  200. tmp.org = NULL;
  201. helpEntries[pos++] = tmp;
  202. }
  203. for (i = 0; i < commandslen; i++) {
  204. tmp.argv = sdssplitargs(commandHelp[i].name,&tmp.argc);
  205. tmp.full = sdsnew(commandHelp[i].name);
  206. tmp.type = CLI_HELP_COMMAND;
  207. tmp.org = &commandHelp[i];
  208. helpEntries[pos++] = tmp;
  209. }
  210. }
  211. /* Output command help to stdout. */
  212. static void cliOutputCommandHelp(struct commandHelp *help, int group) {
  213. printf("\r\n \x1b[1m%s\x1b[0m \x1b[90m%s\x1b[0m\r\n", help->name, help->params);
  214. printf(" \x1b[33msummary:\x1b[0m %s\r\n", help->summary);
  215. printf(" \x1b[33msince:\x1b[0m %s\r\n", help->since);
  216. if (group) {
  217. printf(" \x1b[33mgroup:\x1b[0m %s\r\n", commandGroups[help->group]);
  218. }
  219. }
  220. /* Print generic help. */
  221. static void cliOutputGenericHelp(void) {
  222. sds version = cliVersion();
  223. printf(
  224. "redis-cli %s\r\n"
  225. "Type: \"help @<group>\" to get a list of commands in <group>\r\n"
  226. " \"help <command>\" for help on <command>\r\n"
  227. " \"help <tab>\" to get a list of possible help topics\r\n"
  228. " \"quit\" to exit\r\n",
  229. version
  230. );
  231. sdsfree(version);
  232. }
  233. /* Output all command help, filtering by group or command name. */
  234. static void cliOutputHelp(int argc, char **argv) {
  235. int i, j, len;
  236. int group = -1;
  237. helpEntry *entry;
  238. struct commandHelp *help;
  239. if (argc == 0) {
  240. cliOutputGenericHelp();
  241. return;
  242. } else if (argc > 0 && argv[0][0] == '@') {
  243. len = sizeof(commandGroups)/sizeof(char*);
  244. for (i = 0; i < len; i++) {
  245. if (strcasecmp(argv[0]+1,commandGroups[i]) == 0) {
  246. group = i;
  247. break;
  248. }
  249. }
  250. }
  251. assert(argc > 0);
  252. for (i = 0; i < helpEntriesLen; i++) {
  253. entry = &helpEntries[i];
  254. if (entry->type != CLI_HELP_COMMAND) continue;
  255. help = entry->org;
  256. if (group == -1) {
  257. /* Compare all arguments */
  258. if (argc == entry->argc) {
  259. for (j = 0; j < argc; j++) {
  260. if (strcasecmp(argv[j],entry->argv[j]) != 0) break;
  261. }
  262. if (j == argc) {
  263. cliOutputCommandHelp(help,1);
  264. }
  265. }
  266. } else {
  267. if (group == help->group) {
  268. cliOutputCommandHelp(help,0);
  269. }
  270. }
  271. }
  272. printf("\r\n");
  273. }
  274. static void completionCallback(const char *buf, linenoiseCompletions *lc) {
  275. size_t startpos = 0;
  276. int mask;
  277. int i;
  278. size_t matchlen;
  279. sds tmp;
  280. if (strncasecmp(buf,"help ",5) == 0) {
  281. startpos = 5;
  282. while (isspace(buf[startpos])) startpos++;
  283. mask = CLI_HELP_COMMAND | CLI_HELP_GROUP;
  284. } else {
  285. mask = CLI_HELP_COMMAND;
  286. }
  287. for (i = 0; i < helpEntriesLen; i++) {
  288. if (!(helpEntries[i].type & mask)) continue;
  289. matchlen = strlen(buf+startpos);
  290. if (strncasecmp(buf+startpos,helpEntries[i].full,matchlen) == 0) {
  291. tmp = sdsnewlen(buf,startpos);
  292. tmp = sdscat(tmp,helpEntries[i].full);
  293. linenoiseAddCompletion(lc,tmp);
  294. sdsfree(tmp);
  295. }
  296. }
  297. }
  298. /*------------------------------------------------------------------------------
  299. * Networking / parsing
  300. *--------------------------------------------------------------------------- */
  301. /* Send AUTH command to the server */
  302. static int cliAuth(void) {
  303. redisReply *reply;
  304. if (config.auth == NULL) return REDIS_OK;
  305. reply = redisCommand(context,"AUTH %s",config.auth);
  306. if (reply != NULL) {
  307. freeReplyObject(reply);
  308. return REDIS_OK;
  309. }
  310. return REDIS_ERR;
  311. }
  312. /* Send SELECT dbnum to the server */
  313. static int cliSelect(void) {
  314. redisReply *reply;
  315. if (config.dbnum == 0) return REDIS_OK;
  316. reply = redisCommand(context,"SELECT %d",config.dbnum);
  317. if (reply != NULL) {
  318. int result = REDIS_OK;
  319. if (reply->type == REDIS_REPLY_ERROR) result = REDIS_ERR;
  320. freeReplyObject(reply);
  321. return result;
  322. }
  323. return REDIS_ERR;
  324. }
  325. /* Connect to the server. If force is not zero the connection is performed
  326. * even if there is already a connected socket. */
  327. static int cliConnect(int force) {
  328. if (context == NULL || force) {
  329. if (context != NULL)
  330. redisFree(context);
  331. if (config.hostsocket == NULL) {
  332. context = redisConnect(config.hostip,config.hostport);
  333. } else {
  334. context = redisConnectUnix(config.hostsocket);
  335. }
  336. if (context->err) {
  337. fprintf(stderr,"Could not connect to Redis at ");
  338. if (config.hostsocket == NULL)
  339. fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,context->errstr);
  340. else
  341. fprintf(stderr,"%s: %s\n",config.hostsocket,context->errstr);
  342. redisFree(context);
  343. context = NULL;
  344. return REDIS_ERR;
  345. }
  346. /* Set aggressive KEEP_ALIVE socket option in the Redis context socket
  347. * in order to prevent timeouts caused by the execution of long
  348. * commands. At the same time this improves the detection of real
  349. * errors. */
  350. anetKeepAlive(NULL, context->fd, REDIS_CLI_KEEPALIVE_INTERVAL);
  351. /* Do AUTH and select the right DB. */
  352. if (cliAuth() != REDIS_OK)
  353. return REDIS_ERR;
  354. if (cliSelect() != REDIS_OK)
  355. return REDIS_ERR;
  356. }
  357. return REDIS_OK;
  358. }
  359. static void cliPrintContextError(void) {
  360. if (context == NULL) return;
  361. fprintf(stderr,"Error: %s\n",context->errstr);
  362. }
  363. static sds cliFormatReplyTTY(redisReply *r, char *prefix) {
  364. sds out = sdsempty();
  365. switch (r->type) {
  366. case REDIS_REPLY_ERROR:
  367. out = sdscatprintf(out,"(error) %s\n", r->str);
  368. break;
  369. case REDIS_REPLY_STATUS:
  370. out = sdscat(out,r->str);
  371. out = sdscat(out,"\n");
  372. break;
  373. case REDIS_REPLY_INTEGER:
  374. out = sdscatprintf(out,"(integer) %lld\n",r->integer);
  375. break;
  376. case REDIS_REPLY_STRING:
  377. /* If you are producing output for the standard output we want
  378. * a more interesting output with quoted characters and so forth */
  379. out = sdscatrepr(out,r->str,r->len);
  380. out = sdscat(out,"\n");
  381. break;
  382. case REDIS_REPLY_NIL:
  383. out = sdscat(out,"(nil)\n");
  384. break;
  385. case REDIS_REPLY_ARRAY:
  386. if (r->elements == 0) {
  387. out = sdscat(out,"(empty list or set)\n");
  388. } else {
  389. unsigned int i, idxlen = 0;
  390. char _prefixlen[16];
  391. char _prefixfmt[16];
  392. sds _prefix;
  393. sds tmp;
  394. /* Calculate chars needed to represent the largest index */
  395. i = r->elements;
  396. do {
  397. idxlen++;
  398. i /= 10;
  399. } while(i);
  400. /* Prefix for nested multi bulks should grow with idxlen+2 spaces */
  401. memset(_prefixlen,' ',idxlen+2);
  402. _prefixlen[idxlen+2] = '\0';
  403. _prefix = sdscat(sdsnew(prefix),_prefixlen);
  404. /* Setup prefix format for every entry */
  405. snprintf(_prefixfmt,sizeof(_prefixfmt),"%%s%%%dd) ",idxlen);
  406. for (i = 0; i < r->elements; i++) {
  407. /* Don't use the prefix for the first element, as the parent
  408. * caller already prepended the index number. */
  409. out = sdscatprintf(out,_prefixfmt,i == 0 ? "" : prefix,i+1);
  410. /* Format the multi bulk entry */
  411. tmp = cliFormatReplyTTY(r->element[i],_prefix);
  412. out = sdscatlen(out,tmp,sdslen(tmp));
  413. sdsfree(tmp);
  414. }
  415. sdsfree(_prefix);
  416. }
  417. break;
  418. default:
  419. fprintf(stderr,"Unknown reply type: %d\n", r->type);
  420. exit(1);
  421. }
  422. return out;
  423. }
  424. static sds cliFormatReplyRaw(redisReply *r) {
  425. sds out = sdsempty(), tmp;
  426. size_t i;
  427. switch (r->type) {
  428. case REDIS_REPLY_NIL:
  429. /* Nothing... */
  430. break;
  431. case REDIS_REPLY_ERROR:
  432. out = sdscatlen(out,r->str,r->len);
  433. out = sdscatlen(out,"\n",1);
  434. break;
  435. case REDIS_REPLY_STATUS:
  436. case REDIS_REPLY_STRING:
  437. out = sdscatlen(out,r->str,r->len);
  438. break;
  439. case REDIS_REPLY_INTEGER:
  440. out = sdscatprintf(out,"%lld",r->integer);
  441. break;
  442. case REDIS_REPLY_ARRAY:
  443. for (i = 0; i < r->elements; i++) {
  444. if (i > 0) out = sdscat(out,config.mb_delim);
  445. tmp = cliFormatReplyRaw(r->element[i]);
  446. out = sdscatlen(out,tmp,sdslen(tmp));
  447. sdsfree(tmp);
  448. }
  449. break;
  450. default:
  451. fprintf(stderr,"Unknown reply type: %d\n", r->type);
  452. exit(1);
  453. }
  454. return out;
  455. }
  456. static sds cliFormatReplyCSV(redisReply *r) {
  457. unsigned int i;
  458. sds out = sdsempty();
  459. switch (r->type) {
  460. case REDIS_REPLY_ERROR:
  461. out = sdscat(out,"ERROR,");
  462. out = sdscatrepr(out,r->str,strlen(r->str));
  463. break;
  464. case REDIS_REPLY_STATUS:
  465. out = sdscatrepr(out,r->str,r->len);
  466. break;
  467. case REDIS_REPLY_INTEGER:
  468. out = sdscatprintf(out,"%lld",r->integer);
  469. break;
  470. case REDIS_REPLY_STRING:
  471. out = sdscatrepr(out,r->str,r->len);
  472. break;
  473. case REDIS_REPLY_NIL:
  474. out = sdscat(out,"NIL");
  475. break;
  476. case REDIS_REPLY_ARRAY:
  477. for (i = 0; i < r->elements; i++) {
  478. sds tmp = cliFormatReplyCSV(r->element[i]);
  479. out = sdscatlen(out,tmp,sdslen(tmp));
  480. if (i != r->elements-1) out = sdscat(out,",");
  481. sdsfree(tmp);
  482. }
  483. break;
  484. default:
  485. fprintf(stderr,"Unknown reply type: %d\n", r->type);
  486. exit(1);
  487. }
  488. return out;
  489. }
  490. static int cliReadReply(int output_raw_strings) {
  491. void *_reply;
  492. redisReply *reply;
  493. sds out = NULL;
  494. int output = 1;
  495. if (redisGetReply(context,&_reply) != REDIS_OK) {
  496. if (config.shutdown) {
  497. redisFree(context);
  498. context = NULL;
  499. return REDIS_OK;
  500. }
  501. if (config.interactive) {
  502. /* Filter cases where we should reconnect */
  503. if (context->err == REDIS_ERR_IO &&
  504. (errno == ECONNRESET || errno == EPIPE))
  505. return REDIS_ERR;
  506. if (context->err == REDIS_ERR_EOF)
  507. return REDIS_ERR;
  508. }
  509. cliPrintContextError();
  510. exit(1);
  511. return REDIS_ERR; /* avoid compiler warning */
  512. }
  513. reply = (redisReply*)_reply;
  514. config.last_cmd_type = reply->type;
  515. /* Check if we need to connect to a different node and reissue the
  516. * request. */
  517. if (config.cluster_mode && reply->type == REDIS_REPLY_ERROR &&
  518. (!strncmp(reply->str,"MOVED",5) || !strcmp(reply->str,"ASK")))
  519. {
  520. char *p = reply->str, *s;
  521. int slot;
  522. output = 0;
  523. /* Comments show the position of the pointer as:
  524. *
  525. * [S] for pointer 's'
  526. * [P] for pointer 'p'
  527. */
  528. s = strchr(p,' '); /* MOVED[S]3999 127.0.0.1:6381 */
  529. p = strchr(s+1,' '); /* MOVED[S]3999[P]127.0.0.1:6381 */
  530. *p = '\0';
  531. slot = atoi(s+1);
  532. s = strchr(p+1,':'); /* MOVED 3999[P]127.0.0.1[S]6381 */
  533. *s = '\0';
  534. sdsfree(config.hostip);
  535. config.hostip = sdsnew(p+1);
  536. config.hostport = atoi(s+1);
  537. if (config.interactive)
  538. printf("-> Redirected to slot [%d] located at %s:%d\n",
  539. slot, config.hostip, config.hostport);
  540. config.cluster_reissue_command = 1;
  541. cliRefreshPrompt();
  542. }
  543. if (output) {
  544. if (output_raw_strings) {
  545. out = cliFormatReplyRaw(reply);
  546. } else {
  547. if (config.output == OUTPUT_RAW) {
  548. out = cliFormatReplyRaw(reply);
  549. out = sdscat(out,"\n");
  550. } else if (config.output == OUTPUT_STANDARD) {
  551. out = cliFormatReplyTTY(reply,"");
  552. } else if (config.output == OUTPUT_CSV) {
  553. out = cliFormatReplyCSV(reply);
  554. out = sdscat(out,"\n");
  555. }
  556. }
  557. fwrite(out,sdslen(out),1,stdout);
  558. sdsfree(out);
  559. }
  560. freeReplyObject(reply);
  561. return REDIS_OK;
  562. }
  563. static int cliSendCommand(int argc, char **argv, int repeat) {
  564. char *command = argv[0];
  565. size_t *argvlen;
  566. int j, output_raw;
  567. if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
  568. cliOutputHelp(--argc, ++argv);
  569. return REDIS_OK;
  570. }
  571. if (context == NULL) return REDIS_ERR;
  572. output_raw = 0;
  573. if (!strcasecmp(command,"info") ||
  574. (argc >= 2 && !strcasecmp(command,"debug") &&
  575. (!strcasecmp(argv[1],"jemalloc") ||
  576. !strcasecmp(argv[1],"htstats"))) ||
  577. (argc == 2 && !strcasecmp(command,"cluster") &&
  578. (!strcasecmp(argv[1],"nodes") ||
  579. !strcasecmp(argv[1],"info"))) ||
  580. (argc == 2 && !strcasecmp(command,"client") &&
  581. !strcasecmp(argv[1],"list")) ||
  582. (argc == 3 && !strcasecmp(command,"latency") &&
  583. !strcasecmp(argv[1],"graph")) ||
  584. (argc == 2 && !strcasecmp(command,"latency") &&
  585. !strcasecmp(argv[1],"doctor")))
  586. {
  587. output_raw = 1;
  588. }
  589. if (!strcasecmp(command,"shutdown")) config.shutdown = 1;
  590. if (!strcasecmp(command,"monitor")) config.monitor_mode = 1;
  591. if (!strcasecmp(command,"subscribe") ||
  592. !strcasecmp(command,"psubscribe")) config.pubsub_mode = 1;
  593. if (!strcasecmp(command,"sync") ||
  594. !strcasecmp(command,"psync")) config.slave_mode = 1;
  595. /* Setup argument length */
  596. argvlen = malloc(argc*sizeof(size_t));
  597. for (j = 0; j < argc; j++)
  598. argvlen[j] = sdslen(argv[j]);
  599. while(repeat--) {
  600. redisAppendCommandArgv(context,argc,(const char**)argv,argvlen);
  601. while (config.monitor_mode) {
  602. if (cliReadReply(output_raw) != REDIS_OK) exit(1);
  603. fflush(stdout);
  604. }
  605. if (config.pubsub_mode) {
  606. if (config.output != OUTPUT_RAW)
  607. printf("Reading messages... (press Ctrl-C to quit)\n");
  608. while (1) {
  609. if (cliReadReply(output_raw) != REDIS_OK) exit(1);
  610. }
  611. }
  612. if (config.slave_mode) {
  613. printf("Entering slave output mode... (press Ctrl-C to quit)\n");
  614. slaveMode();
  615. config.slave_mode = 0;
  616. free(argvlen);
  617. return REDIS_ERR; /* Error = slaveMode lost connection to master */
  618. }
  619. if (cliReadReply(output_raw) != REDIS_OK) {
  620. free(argvlen);
  621. return REDIS_ERR;
  622. } else {
  623. /* Store database number when SELECT was successfully executed. */
  624. if (!strcasecmp(command,"select") && argc == 2) {
  625. config.dbnum = atoi(argv[1]);
  626. cliRefreshPrompt();
  627. } else if (!strcasecmp(command,"auth") && argc == 2) {
  628. cliSelect();
  629. }
  630. }
  631. if (config.interval) usleep(config.interval);
  632. fflush(stdout); /* Make it grep friendly */
  633. }
  634. free(argvlen);
  635. return REDIS_OK;
  636. }
  637. /* Send a command reconnecting the link if needed. */
  638. static redisReply *reconnectingRedisCommand(redisContext *c, const char *fmt, ...) {
  639. redisReply *reply = NULL;
  640. int tries = 0;
  641. va_list ap;
  642. assert(!c->err);
  643. while(reply == NULL) {
  644. while (c->err & (REDIS_ERR_IO | REDIS_ERR_EOF)) {
  645. printf("\r\x1b[0K"); /* Cursor to left edge + clear line. */
  646. printf("Reconnecting... %d\r", ++tries);
  647. fflush(stdout);
  648. redisFree(c);
  649. c = redisConnect(config.hostip,config.hostport);
  650. usleep(1000000);
  651. }
  652. va_start(ap,fmt);
  653. reply = redisvCommand(c,fmt,ap);
  654. va_end(ap);
  655. if (c->err && !(c->err & (REDIS_ERR_IO | REDIS_ERR_EOF))) {
  656. fprintf(stderr, "Error: %s\n", c->errstr);
  657. exit(1);
  658. } else if (tries > 0) {
  659. printf("\r\x1b[0K"); /* Cursor to left edge + clear line. */
  660. }
  661. }
  662. context = c;
  663. return reply;
  664. }
  665. /*------------------------------------------------------------------------------
  666. * User interface
  667. *--------------------------------------------------------------------------- */
  668. static int parseOptions(int argc, char **argv) {
  669. int i;
  670. for (i = 1; i < argc; i++) {
  671. int lastarg = i==argc-1;
  672. if (!strcmp(argv[i],"-h") && !lastarg) {
  673. sdsfree(config.hostip);
  674. config.hostip = sdsnew(argv[++i]);
  675. } else if (!strcmp(argv[i],"-h") && lastarg) {
  676. usage();
  677. } else if (!strcmp(argv[i],"--help")) {
  678. usage();
  679. } else if (!strcmp(argv[i],"-x")) {
  680. config.stdinarg = 1;
  681. } else if (!strcmp(argv[i],"-p") && !lastarg) {
  682. config.hostport = atoi(argv[++i]);
  683. } else if (!strcmp(argv[i],"-s") && !lastarg) {
  684. config.hostsocket = argv[++i];
  685. } else if (!strcmp(argv[i],"-r") && !lastarg) {
  686. config.repeat = strtoll(argv[++i],NULL,10);
  687. } else if (!strcmp(argv[i],"-i") && !lastarg) {
  688. double seconds = atof(argv[++i]);
  689. config.interval = seconds*1000000;
  690. } else if (!strcmp(argv[i],"-n") && !lastarg) {
  691. config.dbnum = atoi(argv[++i]);
  692. } else if (!strcmp(argv[i],"-a") && !lastarg) {
  693. config.auth = argv[++i];
  694. } else if (!strcmp(argv[i],"--raw")) {
  695. config.output = OUTPUT_RAW;
  696. } else if (!strcmp(argv[i],"--no-raw")) {
  697. config.output = OUTPUT_STANDARD;
  698. } else if (!strcmp(argv[i],"--csv")) {
  699. config.output = OUTPUT_CSV;
  700. } else if (!strcmp(argv[i],"--latency")) {
  701. config.latency_mode = 1;
  702. } else if (!strcmp(argv[i],"--latency-dist")) {
  703. config.latency_dist_mode = 1;
  704. } else if (!strcmp(argv[i],"--mono")) {
  705. spectrum_palette = spectrum_palette_mono;
  706. spectrum_palette_size = spectrum_palette_mono_size;
  707. } else if (!strcmp(argv[i],"--latency-history")) {
  708. config.latency_mode = 1;
  709. config.latency_history = 1;
  710. } else if (!strcmp(argv[i],"--lru-test") && !lastarg) {
  711. config.lru_test_mode = 1;
  712. config.lru_test_sample_size = strtoll(argv[++i],NULL,10);
  713. } else if (!strcmp(argv[i],"--slave")) {
  714. config.slave_mode = 1;
  715. } else if (!strcmp(argv[i],"--stat")) {
  716. config.stat_mode = 1;
  717. } else if (!strcmp(argv[i],"--scan")) {
  718. config.scan_mode = 1;
  719. } else if (!strcmp(argv[i],"--pattern") && !lastarg) {
  720. config.pattern = argv[++i];
  721. } else if (!strcmp(argv[i],"--intrinsic-latency") && !lastarg) {
  722. config.intrinsic_latency_mode = 1;
  723. config.intrinsic_latency_duration = atoi(argv[++i]);
  724. } else if (!strcmp(argv[i],"--rdb") && !lastarg) {
  725. config.getrdb_mode = 1;
  726. config.rdb_filename = argv[++i];
  727. } else if (!strcmp(argv[i],"--pipe")) {
  728. config.pipe_mode = 1;
  729. } else if (!strcmp(argv[i],"--pipe-timeout") && !lastarg) {
  730. config.pipe_timeout = atoi(argv[++i]);
  731. } else if (!strcmp(argv[i],"--bigkeys")) {
  732. config.bigkeys = 1;
  733. } else if (!strcmp(argv[i],"--eval") && !lastarg) {
  734. config.eval = argv[++i];
  735. } else if (!strcmp(argv[i],"-c")) {
  736. config.cluster_mode = 1;
  737. } else if (!strcmp(argv[i],"-d") && !lastarg) {
  738. sdsfree(config.mb_delim);
  739. config.mb_delim = sdsnew(argv[++i]);
  740. } else if (!strcmp(argv[i],"-v") || !strcmp(argv[i], "--version")) {
  741. sds version = cliVersion();
  742. printf("redis-cli %s\n", version);
  743. sdsfree(version);
  744. exit(0);
  745. } else {
  746. if (argv[i][0] == '-') {
  747. fprintf(stderr,
  748. "Unrecognized option or bad number of args for: '%s'\n",
  749. argv[i]);
  750. exit(1);
  751. } else {
  752. /* Likely the command name, stop here. */
  753. break;
  754. }
  755. }
  756. }
  757. return i;
  758. }
  759. static sds readArgFromStdin(void) {
  760. char buf[1024];
  761. sds arg = sdsempty();
  762. while(1) {
  763. int nread = read(fileno(stdin),buf,1024);
  764. if (nread == 0) break;
  765. else if (nread == -1) {
  766. perror("Reading from standard input");
  767. exit(1);
  768. }
  769. arg = sdscatlen(arg,buf,nread);
  770. }
  771. return arg;
  772. }
  773. static void usage(void) {
  774. sds version = cliVersion();
  775. fprintf(stderr,
  776. "redis-cli %s\n"
  777. "\n"
  778. "Usage: redis-cli [OPTIONS] [cmd [arg [arg ...]]]\n"
  779. " -h <hostname> Server hostname (default: 127.0.0.1).\n"
  780. " -p <port> Server port (default: 6379).\n"
  781. " -s <socket> Server socket (overrides hostname and port).\n"
  782. " -a <password> Password to use when connecting to the server.\n"
  783. " -r <repeat> Execute specified command N times.\n"
  784. " -i <interval> When -r is used, waits <interval> seconds per command.\n"
  785. " It is possible to specify sub-second times like -i 0.1.\n"
  786. " -n <db> Database number.\n"
  787. " -x Read last argument from STDIN.\n"
  788. " -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n).\n"
  789. " -c Enable cluster mode (follow -ASK and -MOVED redirections).\n"
  790. " --raw Use raw formatting for replies (default when STDOUT is\n"
  791. " not a tty).\n"
  792. " --no-raw Force formatted output even when STDOUT is not a tty.\n"
  793. " --csv Output in CSV format.\n"
  794. " --stat Print rolling stats about server: mem, clients, ...\n"
  795. " --latency Enter a special mode continuously sampling latency.\n"
  796. " --latency-history Like --latency but tracking latency changes over time.\n"
  797. " Default time interval is 15 sec. Change it using -i.\n"
  798. " --latency-dist Shows latency as a spectrum, requires xterm 256 colors.\n"
  799. " Default time interval is 1 sec. Change it using -i.\n"
  800. " --lru-test <keys> Simulate a cache workload with an 80-20 distribution.\n"
  801. " --slave Simulate a slave showing commands received from the master.\n"
  802. " --rdb <filename> Transfer an RDB dump from remote server to local file.\n"
  803. " --pipe Transfer raw Redis protocol from stdin to server.\n"
  804. " --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.\n"
  805. " no reply is received within <n> seconds.\n"
  806. " Default timeout: %d. Use 0 to wait forever.\n"
  807. " --bigkeys Sample Redis keys looking for big keys.\n"
  808. " --scan List all keys using the SCAN command.\n"
  809. " --pattern <pat> Useful with --scan to specify a SCAN pattern.\n"
  810. " --intrinsic-latency <sec> Run a test to measure intrinsic system latency.\n"
  811. " The test will run for the specified amount of seconds.\n"
  812. " --eval <file> Send an EVAL command using the Lua script at <file>.\n"
  813. " --help Output this help and exit.\n"
  814. " --version Output version and exit.\n"
  815. "\n"
  816. "Examples:\n"
  817. " cat /etc/passwd | redis-cli -x set mypasswd\n"
  818. " redis-cli get mypasswd\n"
  819. " redis-cli -r 100 lpush mylist x\n"
  820. " redis-cli -r 100 -i 1 info | grep used_memory_human:\n"
  821. " redis-cli --eval myscript.lua key1 key2 , arg1 arg2 arg3\n"
  822. " redis-cli --scan --pattern '*:12345*'\n"
  823. "\n"
  824. " (Note: when using --eval the comma separates KEYS[] from ARGV[] items)\n"
  825. "\n"
  826. "When no command is given, redis-cli starts in interactive mode.\n"
  827. "Type \"help\" in interactive mode for information on available commands.\n"
  828. "\n",
  829. version, REDIS_CLI_DEFAULT_PIPE_TIMEOUT);
  830. sdsfree(version);
  831. exit(1);
  832. }
  833. /* Turn the plain C strings into Sds strings */
  834. static char **convertToSds(int count, char** args) {
  835. int j;
  836. char **sds = zmalloc(sizeof(char*)*count);
  837. for(j = 0; j < count; j++)
  838. sds[j] = sdsnew(args[j]);
  839. return sds;
  840. }
  841. static int issueCommandRepeat(int argc, char **argv, long repeat) {
  842. while (1) {
  843. config.cluster_reissue_command = 0;
  844. if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
  845. cliConnect(1);
  846. /* If we still cannot send the command print error.
  847. * We'll try to reconnect the next time. */
  848. if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
  849. cliPrintContextError();
  850. return REDIS_ERR;
  851. }
  852. }
  853. /* Issue the command again if we got redirected in cluster mode */
  854. if (config.cluster_mode && config.cluster_reissue_command) {
  855. cliConnect(1);
  856. } else {
  857. break;
  858. }
  859. }
  860. return REDIS_OK;
  861. }
  862. static int issueCommand(int argc, char **argv) {
  863. return issueCommandRepeat(argc, argv, config.repeat);
  864. }
  865. static void repl(void) {
  866. sds historyfile = NULL;
  867. int history = 0;
  868. char *line;
  869. int argc;
  870. sds *argv;
  871. config.interactive = 1;
  872. linenoiseSetMultiLine(1);
  873. linenoiseSetCompletionCallback(completionCallback);
  874. /* Only use history when stdin is a tty. */
  875. if (isatty(fileno(stdin))) {
  876. historyfile = getHistoryPath();
  877. if (historyfile != NULL) {
  878. history = 1;
  879. linenoiseHistoryLoad(historyfile);
  880. }
  881. }
  882. cliRefreshPrompt();
  883. while((line = linenoise(context ? config.prompt : "not connected> ")) != NULL) {
  884. if (line[0] != '\0') {
  885. argv = sdssplitargs(line,&argc);
  886. if (history) linenoiseHistoryAdd(line);
  887. if (historyfile) linenoiseHistorySave(historyfile);
  888. if (argv == NULL) {
  889. printf("Invalid argument(s)\n");
  890. free(line);
  891. continue;
  892. } else if (argc > 0) {
  893. if (strcasecmp(argv[0],"quit") == 0 ||
  894. strcasecmp(argv[0],"exit") == 0)
  895. {
  896. exit(0);
  897. } else if (argc == 3 && !strcasecmp(argv[0],"connect")) {
  898. sdsfree(config.hostip);
  899. config.hostip = sdsnew(argv[1]);
  900. config.hostport = atoi(argv[2]);
  901. cliRefreshPrompt();
  902. cliConnect(1);
  903. } else if (argc == 1 && !strcasecmp(argv[0],"clear")) {
  904. linenoiseClearScreen();
  905. } else {
  906. long long start_time = mstime(), elapsed;
  907. int repeat, skipargs = 0;
  908. repeat = atoi(argv[0]);
  909. if (argc > 1 && repeat) {
  910. skipargs = 1;
  911. } else {
  912. repeat = 1;
  913. }
  914. issueCommandRepeat(argc-skipargs, argv+skipargs, repeat);
  915. elapsed = mstime()-start_time;
  916. if (elapsed >= 500) {
  917. printf("(%.2fs)\n",(double)elapsed/1000);
  918. }
  919. }
  920. }
  921. /* Free the argument vector */
  922. sdsfreesplitres(argv,argc);
  923. }
  924. /* linenoise() returns malloc-ed lines like readline() */
  925. free(line);
  926. }
  927. exit(0);
  928. }
  929. static int noninteractive(int argc, char **argv) {
  930. int retval = 0;
  931. if (config.stdinarg) {
  932. argv = zrealloc(argv, (argc+1)*sizeof(char*));
  933. argv[argc] = readArgFromStdin();
  934. retval = issueCommand(argc+1, argv);
  935. } else {
  936. retval = issueCommand(argc, argv);
  937. }
  938. return retval;
  939. }
  940. /*------------------------------------------------------------------------------
  941. * Eval mode
  942. *--------------------------------------------------------------------------- */
  943. static int evalMode(int argc, char **argv) {
  944. sds script = sdsempty();
  945. FILE *fp;
  946. char buf[1024];
  947. size_t nread;
  948. char **argv2;
  949. int j, got_comma = 0, keys = 0;
  950. /* Load the script from the file, as an sds string. */
  951. fp = fopen(config.eval,"r");
  952. if (!fp) {
  953. fprintf(stderr,
  954. "Can't open file '%s': %s\n", config.eval, strerror(errno));
  955. exit(1);
  956. }
  957. while((nread = fread(buf,1,sizeof(buf),fp)) != 0) {
  958. script = sdscatlen(script,buf,nread);
  959. }
  960. fclose(fp);
  961. /* Create our argument vector */
  962. argv2 = zmalloc(sizeof(sds)*(argc+3));
  963. argv2[0] = sdsnew("EVAL");
  964. argv2[1] = script;
  965. for (j = 0; j < argc; j++) {
  966. if (!got_comma && argv[j][0] == ',' && argv[j][1] == 0) {
  967. got_comma = 1;
  968. continue;
  969. }
  970. argv2[j+3-got_comma] = sdsnew(argv[j]);
  971. if (!got_comma) keys++;
  972. }
  973. argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
  974. /* Call it */
  975. return issueCommand(argc+3-got_comma, argv2);
  976. }
  977. /*------------------------------------------------------------------------------
  978. * Latency and latency history modes
  979. *--------------------------------------------------------------------------- */
  980. #define LATENCY_SAMPLE_RATE 10 /* milliseconds. */
  981. #define LATENCY_HISTORY_DEFAULT_INTERVAL 15000 /* milliseconds. */
  982. static void latencyMode(void) {
  983. redisReply *reply;
  984. long long start, latency, min = 0, max = 0, tot = 0, count = 0;
  985. long long history_interval =
  986. config.interval ? config.interval/1000 :
  987. LATENCY_HISTORY_DEFAULT_INTERVAL;
  988. double avg;
  989. long long history_start = mstime();
  990. if (!context) exit(1);
  991. while(1) {
  992. start = mstime();
  993. reply = reconnectingRedisCommand(context,"PING");
  994. if (reply == NULL) {
  995. fprintf(stderr,"\nI/O error\n");
  996. exit(1);
  997. }
  998. latency = mstime()-start;
  999. freeReplyObject(reply);
  1000. count++;
  1001. if (count == 1) {
  1002. min = max = tot = latency;
  1003. avg = (double) latency;
  1004. } else {
  1005. if (latency < min) min = latency;
  1006. if (latency > max) max = latency;
  1007. tot += latency;
  1008. avg = (double) tot/count;
  1009. }
  1010. printf("\x1b[0G\x1b[2Kmin: %lld, max: %lld, avg: %.2f (%lld samples)",
  1011. min, max, avg, count);
  1012. fflush(stdout);
  1013. if (config.latency_history && mstime()-history_start > history_interval)
  1014. {
  1015. printf(" -- %.2f seconds range\n", (float)(mstime()-history_start)/1000);
  1016. history_start = mstime();
  1017. min = max = tot = count = 0;
  1018. }
  1019. usleep(LATENCY_SAMPLE_RATE * 1000);
  1020. }
  1021. }
  1022. /*------------------------------------------------------------------------------
  1023. * Latency distribution mode -- requires 256 colors xterm
  1024. *--------------------------------------------------------------------------- */
  1025. #define LATENCY_DIST_DEFAULT_INTERVAL 1000 /* milliseconds. */
  1026. /* Structure to store samples distribution. */
  1027. struct distsamples {
  1028. long long max; /* Max latency to fit into this interval (usec). */
  1029. long long count; /* Number of samples in this interval. */
  1030. int character; /* Associated character in visualization. */
  1031. };
  1032. /* Helper function for latencyDistMode(). Performs the spectrum visualization
  1033. * of the collected samples targeting an xterm 256 terminal.
  1034. *
  1035. * Takes an array of distsamples structures, ordered from smaller to bigger
  1036. * 'max' value. Last sample max must be 0, to mean that it olds all the
  1037. * samples greater than the previous one, and is also the stop sentinel.
  1038. *
  1039. * "tot' is the total number of samples in the different buckets, so it
  1040. * is the SUM(samples[i].conut) for i to 0 up to the max sample.
  1041. *
  1042. * As a side effect the function sets all the buckets count to 0. */
  1043. void showLatencyDistSamples(struct distsamples *samples, long long tot) {
  1044. int j;
  1045. /* We convert samples into a index inside the palette
  1046. * proportional to the percentage a given bucket represents.
  1047. * This way intensity of the different parts of the spectrum
  1048. * don't change relative to the number of requests, which avoids to
  1049. * pollute the visualization with non-latency related info. */
  1050. printf("\033[38;5;0m"); /* Set foreground color to black. */
  1051. for (j = 0; ; j++) {
  1052. int coloridx =
  1053. ceil((float) samples[j].count / tot * (spectrum_palette_size-1));
  1054. int color = spectrum_palette[coloridx];
  1055. printf("\033[48;5;%dm%c", (int)color, samples[j].character);
  1056. samples[j].count = 0;
  1057. if (samples[j].max == 0) break; /* Last sample. */
  1058. }
  1059. printf("\033[0m\n");
  1060. fflush(stdout);
  1061. }
  1062. /* Show the legend: different buckets values and colors meaning, so
  1063. * that the spectrum is more easily readable. */
  1064. void showLatencyDistLegend(void) {
  1065. int j;
  1066. printf("---------------------------------------------\n");
  1067. printf(". - * # .01 .125 .25 .5 milliseconds\n");
  1068. printf("1,2,3,...,9 from 1 to 9 milliseconds\n");
  1069. printf("A,B,C,D,E 10,20,30,40,50 milliseconds\n");
  1070. printf("F,G,H,I,J .1,.2,.3,.4,.5 seconds\n");
  1071. printf("K,L,M,N,O,P,Q,? 1,2,4,8,16,30,60,>60 seconds\n");
  1072. printf("From 0 to 100%%: ");
  1073. for (j = 0; j < spectrum_palette_size; j++) {
  1074. printf("\033[48;5;%dm ", spectrum_palette[j]);
  1075. }
  1076. printf("\033[0m\n");
  1077. printf("---------------------------------------------\n");
  1078. }
  1079. static void latencyDistMode(void) {
  1080. redisReply *reply;
  1081. long long start, latency, count = 0;
  1082. long long history_interval =
  1083. config.interval ? config.interval/1000 :
  1084. LATENCY_DIST_DEFAULT_INTERVAL;
  1085. long long history_start = ustime();
  1086. int j, outputs = 0;
  1087. struct distsamples samples[] = {
  1088. /* We use a mostly logarithmic scale, with certain linear intervals
  1089. * which are more interesting than others, like 1-10 milliseconds
  1090. * range. */
  1091. {10,0,'.'}, /* 0.01 ms */
  1092. {125,0,'-'}, /* 0.125 ms */
  1093. {250,0,'*'}, /* 0.25 ms */
  1094. {500,0,'#'}, /* 0.5 ms */
  1095. {1000,0,'1'}, /* 1 ms */
  1096. {2000,0,'2'}, /* 2 ms */
  1097. {3000,0,'3'}, /* 3 ms */
  1098. {4000,0,'4'}, /* 4 ms */
  1099. {5000,0,'5'}, /* 5 ms */
  1100. {6000,0,'6'}, /* 6 ms */
  1101. {7000,0,'7'}, /* 7 ms */
  1102. {8000,0,'8'}, /* 8 ms */
  1103. {9000,0,'9'}, /* 9 ms */
  1104. {10000,0,'A'}, /* 10 ms */
  1105. {20000,0,'B'}, /* 20 ms */
  1106. {30000,0,'C'}, /* 30 ms */
  1107. {40000,0,'D'}, /* 40 ms */
  1108. {50000,0,'E'}, /* 50 ms */
  1109. {100000,0,'F'}, /* 0.1 s */
  1110. {200000,0,'G'}, /* 0.2 s */
  1111. {300000,0,'H'}, /* 0.3 s */
  1112. {400000,0,'I'}, /* 0.4 s */
  1113. {500000,0,'J'}, /* 0.5 s */
  1114. {1000000,0,'K'}, /* 1 s */
  1115. {2000000,0,'L'}, /* 2 s */
  1116. {4000000,0,'M'}, /* 4 s */
  1117. {8000000,0,'N'}, /* 8 s */
  1118. {16000000,0,'O'}, /* 16 s */
  1119. {30000000,0,'P'}, /* 30 s */
  1120. {60000000,0,'Q'}, /* 1 minute */
  1121. {0,0,'?'}, /* > 1 minute */
  1122. };
  1123. if (!context) exit(1);
  1124. while(1) {
  1125. start = ustime();
  1126. reply = reconnectingRedisCommand(context,"PING");
  1127. if (reply == NULL) {
  1128. fprintf(stderr,"\nI/O error\n");
  1129. exit(1);
  1130. }
  1131. latency = ustime()-start;
  1132. freeReplyObject(reply);
  1133. count++;
  1134. /* Populate the relevant bucket. */
  1135. for (j = 0; ; j++) {
  1136. if (samples[j].max == 0 || latency <= samples[j].max) {
  1137. samples[j].count++;
  1138. break;
  1139. }
  1140. }
  1141. /* From time to time show the spectrum. */
  1142. if (count && (ustime()-history_start)/1000 > history_interval) {
  1143. if ((outputs++ % 20) == 0)
  1144. showLatencyDistLegend();
  1145. showLatencyDistSamples(samples,count);
  1146. history_start = ustime();
  1147. count = 0;
  1148. }
  1149. usleep(LATENCY_SAMPLE_RATE * 1000);
  1150. }
  1151. }
  1152. /*------------------------------------------------------------------------------
  1153. * Slave mode
  1154. *--------------------------------------------------------------------------- */
  1155. /* Sends SYNC and reads the number of bytes in the payload. Used both by
  1156. * slaveMode() and getRDB(). */
  1157. unsigned long long sendSync(int fd) {
  1158. /* To start we need to send the SYNC command and return the payload.
  1159. * The hiredis client lib does not understand this part of the protocol
  1160. * and we don't want to mess with its buffers, so everything is performed
  1161. * using direct low-level I/O. */
  1162. char buf[4096], *p;
  1163. ssize_t nread;
  1164. /* Send the SYNC command. */
  1165. if (write(fd,"SYNC\r\n",6) != 6) {
  1166. fprintf(stderr,"Error writing to master\n");
  1167. exit(1);
  1168. }
  1169. /* Read $<payload>\r\n, making sure to read just up to "\n" */
  1170. p = buf;
  1171. while(1) {
  1172. nread = read(fd,p,1);
  1173. if (nread <= 0) {
  1174. fprintf(stderr,"Error reading bulk length while SYNCing\n");
  1175. exit(1);
  1176. }
  1177. if (*p == '\n' && p != buf) break;
  1178. if (*p != '\n') p++;
  1179. }
  1180. *p = '\0';
  1181. if (buf[0] == '-') {
  1182. printf("SYNC with master failed: %s\n", buf);
  1183. exit(1);
  1184. }
  1185. return strtoull(buf+1,NULL,10);
  1186. }
  1187. static void slaveMode(void) {
  1188. int fd = context->fd;
  1189. unsigned long long payload = sendSync(fd);
  1190. char buf[1024];
  1191. int original_output = config.output;
  1192. fprintf(stderr,"SYNC with master, discarding %llu "
  1193. "bytes of bulk transfer...\n", payload);
  1194. /* Discard the payload. */
  1195. while(payload) {
  1196. ssize_t nread;
  1197. nread = read(fd,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
  1198. if (nread <= 0) {
  1199. fprintf(stderr,"Error reading RDB payload while SYNCing\n");
  1200. exit(1);
  1201. }
  1202. payload -= nread;
  1203. }
  1204. fprintf(stderr,"SYNC done. Logging commands from master.\n");
  1205. /* Now we can use hiredis to read the incoming protocol. */
  1206. config.output = OUTPUT_CSV;
  1207. while (cliReadReply(0) == REDIS_OK);
  1208. config.output = original_output;
  1209. }
  1210. /*------------------------------------------------------------------------------
  1211. * RDB transfer mode
  1212. *--------------------------------------------------------------------------- */
  1213. /* This function implements --rdb, so it uses the replication protocol in order
  1214. * to fetch the RDB file from a remote server. */
  1215. static void getRDB(void) {
  1216. int s = context->fd;
  1217. int fd;
  1218. unsigned long long payload = sendSync(s);
  1219. char buf[4096];
  1220. fprintf(stderr,"SYNC sent to master, writing %llu bytes to '%s'\n",
  1221. payload, config.rdb_filename);
  1222. /* Write to file. */
  1223. if (!strcmp(config.rdb_filename,"-")) {
  1224. fd = STDOUT_FILENO;
  1225. } else {
  1226. fd = open(config.rdb_filename, O_CREAT|O_WRONLY, 0644);
  1227. if (fd == -1) {
  1228. fprintf(stderr, "Error opening '%s': %s\n", config.rdb_filename,
  1229. strerror(errno));
  1230. exit(1);
  1231. }
  1232. }
  1233. while(payload) {
  1234. ssize_t nread, nwritten;
  1235. nread = read(s,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload);
  1236. if (nread <= 0) {
  1237. fprintf(stderr,"I/O Error reading RDB payload from socket\n");
  1238. exit(1);
  1239. }
  1240. nwritten = write(fd, buf, nread);
  1241. if (nwritten != nread) {
  1242. fprintf(stderr,"Error writing data to file: %s\n",
  1243. strerror(errno));
  1244. exit(1);
  1245. }
  1246. payload -= nread;
  1247. }
  1248. close(s); /* Close the file descriptor ASAP as fsync() may take time. */
  1249. fsync(fd);
  1250. fprintf(stderr,"Transfer finished with success.\n");
  1251. exit(0);
  1252. }
  1253. /*------------------------------------------------------------------------------
  1254. * Bulk import (pipe) mode
  1255. *--------------------------------------------------------------------------- */
  1256. static void pipeMode(void) {
  1257. int fd = context->fd;
  1258. long long errors = 0, replies = 0, obuf_len = 0, obuf_pos = 0;
  1259. char ibuf[1024*16], obuf[1024*16]; /* Input and output buffers */
  1260. char aneterr[ANET_ERR_LEN];
  1261. redisReader *reader = redisReaderCreate();
  1262. redisReply *reply;
  1263. int eof = 0; /* True once we consumed all the standard input. */
  1264. int done = 0;
  1265. char magic[20]; /* Special reply we recognize. */
  1266. time_t last_read_time = time(NULL);
  1267. srand(time(NULL));
  1268. /* Use non blocking I/O. */
  1269. if (anetNonBlock(aneterr,fd) == ANET_ERR) {
  1270. fprintf(stderr, "Can't set the socket in non blocking mode: %s\n",
  1271. aneterr);
  1272. exit(1);
  1273. }
  1274. /* Transfer raw protocol and read replies from the server at the same
  1275. * time. */
  1276. while(!done) {
  1277. int mask = AE_READABLE;
  1278. if (!eof || obuf_len != 0) mask |= AE_WRITABLE;
  1279. mask = aeWait(fd,mask,1000);
  1280. /* Handle the readable state: we can read replies from the server. */
  1281. if (mask & AE_READABLE) {
  1282. ssize_t nread;
  1283. /* Read from socket and feed the hiredis reader. */
  1284. do {
  1285. nread = read(fd,ibuf,sizeof(ibuf));
  1286. if (nread == -1 && errno != EAGAIN && errno != EINTR) {
  1287. fprintf(stderr, "Error reading from the server: %s\n",
  1288. strerror(errno));
  1289. exit(1);
  1290. }
  1291. if (nread > 0) {
  1292. redisReaderFeed(reader,ibuf,nread);
  1293. last_read_time = time(NULL);
  1294. }
  1295. } while(nread > 0);
  1296. /* Consume replies. */
  1297. do {
  1298. if (redisReaderGetReply(reader,(void**)&reply) == REDIS_ERR) {
  1299. fprintf(stderr, "Error reading replies from server\n");
  1300. exit(1);
  1301. }
  1302. if (reply) {
  1303. if (reply->type == REDIS_REPLY_ERROR) {
  1304. fprintf(stderr,"%s\n", reply->str);
  1305. errors++;
  1306. } else if (eof && reply->type == REDIS_REPLY_STRING &&
  1307. reply->len == 20) {
  1308. /* Check if this is the reply to our final ECHO
  1309. * command. If so everything was received
  1310. * from the server. */
  1311. if (memcmp(reply->str,magic,20) == 0) {
  1312. printf("Last reply received from server.\n");
  1313. done = 1;
  1314. replies--;
  1315. }
  1316. }
  1317. replies++;
  1318. freeReplyObject(reply);
  1319. }
  1320. } while(reply);
  1321. }
  1322. /* Handle the writable state: we can send protocol to the server. */
  1323. if (mask & AE_WRITABLE) {
  1324. while(1) {
  1325. /* Transfer current buffer to server. */
  1326. if (obuf_len != 0) {
  1327. ssize_t nwritten = write(fd,obuf+obuf_pos,obuf_len);
  1328. if (nwritten == -1) {
  1329. if (errno != EAGAIN && errno != EINTR) {
  1330. fprintf(stderr, "Error writing to the server: %s\n",
  1331. strerror(errno));
  1332. exit(1);
  1333. } else {
  1334. nwritten = 0;
  1335. }
  1336. }
  1337. obuf_len -= nwritten;
  1338. obuf_pos += nwritten;
  1339. if (obuf_len != 0) break; /* Can't accept more data. */
  1340. }
  1341. /* If buffer is empty, load from stdin. */
  1342. if (obuf_len == 0 && !eof) {
  1343. ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf));
  1344. if (nread == 0) {
  1345. /* The ECHO sequence starts with a "\r\n" so that if there
  1346. * is garbage in the protocol we read from stdin, the ECHO
  1347. * will likely still be properly formatted.
  1348. * CRLF is ignored by Redis, so it has no effects. */
  1349. char echo[] =
  1350. "\r\n*2\r\n$4\r\nECHO\r\n$20\r\n01234567890123456789\r\n";
  1351. int j;
  1352. eof = 1;
  1353. /* Everything transferred, so we queue a special
  1354. * ECHO command that we can match in the replies
  1355. * to make sure everything was read from the server. */
  1356. for (j = 0; j < 20; j++)
  1357. magic[j] = rand() & 0xff;
  1358. memcpy(echo+21,magic,20);
  1359. memcpy(obuf,echo,sizeof(echo)-1);
  1360. obuf_len = sizeof(echo)-1;
  1361. obuf_pos = 0;
  1362. printf("All data transferred. Waiting for the last reply...\n");
  1363. } else if (nread == -1) {
  1364. fprintf(stderr, "Error reading from stdin: %s\n",
  1365. strerror(errno));
  1366. exit(1);
  1367. } else {
  1368. obuf_len = nread;
  1369. obuf_pos = 0;
  1370. }
  1371. }
  1372. if (obuf_len == 0 && eof) break;
  1373. }
  1374. }
  1375. /* Handle timeout, that is, we reached EOF, and we are not getting
  1376. * replies from the server for a few seconds, nor the final ECHO is
  1377. * received. */
  1378. if (eof && config.pipe_timeout > 0 &&
  1379. time(NULL)-last_read_time > config.pipe_timeout)
  1380. {
  1381. fprintf(stderr,"No replies for %d seconds: exiting.\n",
  1382. config.pipe_timeout);
  1383. errors++;
  1384. break;
  1385. }
  1386. }
  1387. redisReaderFree(reader);
  1388. printf("errors: %lld, replies: %lld\n", errors, replies);
  1389. if (errors)
  1390. exit(1);
  1391. else
  1392. exit(0);
  1393. }
  1394. /*------------------------------------------------------------------------------
  1395. * Find big keys
  1396. *--------------------------------------------------------------------------- */
  1397. #define TYPE_STRING 0
  1398. #define TYPE_LIST 1
  1399. #define TYPE_SET 2
  1400. #define TYPE_HASH 3
  1401. #define TYPE_ZSET 4
  1402. #define TYPE_NONE 5
  1403. static redisReply *sendScan(unsigned long long *it) {
  1404. redisReply *reply = redisCommand(context, "SCAN %llu", *it);
  1405. /* Handle any error conditions */
  1406. if(reply == NULL) {
  1407. fprintf(stderr, "\nI/O error\n");
  1408. exit(1);
  1409. } else if(reply->type == REDIS_REPLY_ERROR) {
  1410. fprintf(stderr, "SCAN error: %s\n", reply->str);
  1411. exit(1);
  1412. } else if(reply->type != REDIS_REPLY_ARRAY) {
  1413. fprintf(stderr, "Non ARRAY response from SCAN!\n");
  1414. exit(1);
  1415. } else if(reply->elements != 2) {
  1416. fprintf(stderr, "Invalid element count from SCAN!\n");
  1417. exit(1);
  1418. }
  1419. /* Validate our types are correct */
  1420. assert(reply->element[0]->type == REDIS_REPLY_STRING);
  1421. assert(reply->element[1]->type == REDIS_REPLY_ARRAY);
  1422. /* Update iterator */
  1423. *it = atoi(reply->element[0]->str);
  1424. return reply;
  1425. }
  1426. static int getDbSize(void) {
  1427. redisReply *reply;
  1428. int size;
  1429. reply = redisCommand(context, "DBSIZE");
  1430. if(reply == NULL || reply->type != REDIS_REPLY_INTEGER) {
  1431. fprintf(stderr, "Couldn't determine DBSIZE!\n");
  1432. exit(1);
  1433. }
  1434. /* Grab the number of keys and free our reply */
  1435. size = reply->integer;
  1436. freeReplyObject(reply);
  1437. return size;
  1438. }
  1439. static int toIntType(char *key, char *type) {
  1440. if(!strcmp(type, "string")) {
  1441. return TYPE_STRING;
  1442. } else if(!strcmp(type, "list")) {
  1443. return TYPE_LIST;
  1444. } else if(!strcmp(type, "set")) {
  1445. return TYPE_SET;
  1446. } else if(!strcmp(type, "hash")) {
  1447. return TYPE_HASH;
  1448. } else if(!strcmp(type, "zset")) {
  1449. return TYPE_ZSET;
  1450. } else if(!strcmp(type, "none")) {
  1451. return TYPE_NONE;
  1452. } else {
  1453. fprintf(stderr, "Unknown type '%s' for key '%s'\n", type, key);
  1454. exit(1);
  1455. }
  1456. }
  1457. static void getKeyTypes(redisReply *keys, int *types) {
  1458. redisReply *reply;
  1459. unsigned int i;
  1460. /* Pipeline TYPE commands */
  1461. for(i=0;i<keys->elements;i++) {
  1462. redisAppendCommand(context, "TYPE %s", keys->element[i]->str);
  1463. }
  1464. /* Retrieve types */
  1465. for(i=0;i<keys->elements;i++) {
  1466. if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
  1467. fprintf(stderr, "Error getting type for key '%s' (%d: %s)\n",
  1468. keys->element[i]->str, context->err, context->errstr);
  1469. exit(1);
  1470. } else if(reply->type != REDIS_REPLY_STATUS) {
  1471. fprintf(stderr, "Invalid reply type (%d) for TYPE on key '%s'!\n",
  1472. reply->type, keys->element[i]->str);
  1473. exit(1);
  1474. }
  1475. types[i] = toIntType(keys->element[i]->str, reply->str);
  1476. freeReplyObject(reply);
  1477. }
  1478. }
  1479. static void getKeySizes(redisReply *keys, int *types,
  1480. unsigned long long *sizes)
  1481. {
  1482. redisReply *reply;
  1483. char *sizecmds[] = {"STRLEN","LLEN","SCARD","HLEN","ZCARD"};
  1484. unsigned int i;
  1485. /* Pipeline size commands */
  1486. for(i=0;i<keys->elements;i++) {
  1487. /* Skip keys that were deleted */
  1488. if(types[i]==TYPE_NONE)
  1489. continue;
  1490. redisAppendCommand(context, "%s %s", sizecmds[types[i]],
  1491. keys->element[i]->str);
  1492. }
  1493. /* Retreive sizes */
  1494. for(i=0;i<keys->elements;i++) {
  1495. /* Skip keys that dissapeared between SCAN and TYPE */
  1496. if(types[i] == TYPE_NONE) {
  1497. sizes[i] = 0;
  1498. continue;
  1499. }
  1500. /* Retreive size */
  1501. if(redisGetReply(context, (void**)&reply)!=REDIS_OK) {
  1502. fprintf(stderr, "Error getting size for key '%s' (%d: %s)\n",
  1503. keys->element[i]->str, context->err, context->errstr);
  1504. exit(1);
  1505. } else if(reply->type != REDIS_REPLY_INTEGER) {
  1506. /* Theoretically the key could have been removed and
  1507. * added as a different type between TYPE and SIZE */
  1508. fprintf(stderr,
  1509. "Warning: %s on '%s' failed (may have changed type)\n",
  1510. sizecmds[types[i]], keys->element[i]->str);
  1511. sizes[i] = 0;
  1512. } else {
  1513. sizes[i] = reply->integer;
  1514. }
  1515. freeReplyObject(reply);
  1516. }
  1517. }
  1518. static void findBigKeys(void) {
  1519. unsigned long long biggest[5] = {0}, counts[5] = {0}, totalsize[5] = {0};
  1520. unsigned long long sampled = 0, total_keys, totlen=0, *sizes=NULL, it=0;
  1521. sds maxkeys[5] = {0};
  1522. char *typename[] = {"string","list","set","hash","zset"};
  1523. char *typeunit[] = {"bytes","items","members","fields","members"};
  1524. redisReply *reply, *keys;
  1525. unsigned int arrsize=0, i;
  1526. int type, *types=NULL;
  1527. double pct;
  1528. /* Total keys pre scanning */
  1529. total_keys = getDbSize();
  1530. /* Status message */
  1531. printf("\n# Scanning the entire keyspace to find biggest keys as well as\n");
  1532. printf("# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec\n");
  1533. printf("# per 100 SCAN commands (not usually needed).\n\n");
  1534. /* New up sds strings to keep track of overall biggest per type */
  1535. for(i=0;i<TYPE_NONE; i++) {
  1536. maxkeys[i] = sdsempty();
  1537. if(!maxkeys[i]) {
  1538. fprintf(stderr, "Failed to allocate memory for largest key names!\n");
  1539. exit(1);
  1540. }
  1541. }
  1542. /* SCAN loop */
  1543. do {
  1544. /* Calculate approximate percentage completion */
  1545. pct = 100 * (double)sampled/total_keys;
  1546. /* Grab some keys and point to the keys array */
  1547. reply = sendScan(&it);
  1548. keys = reply->element[1];
  1549. /* Reallocate our type and size array if we need to */
  1550. if(keys->elements > arrsize) {
  1551. types = zrealloc(types, sizeof(int)*keys->elements);
  1552. sizes = zrealloc(sizes, sizeof(unsigned long long)*keys->elements);
  1553. if(!types || !sizes) {
  1554. fprintf(stderr, "Failed to allocate storage for keys!\n");
  1555. exit(1);
  1556. }
  1557. arrsize = keys->elements;
  1558. }
  1559. /* Retreive types and then sizes */
  1560. getKeyTypes(keys, types);
  1561. getKeySizes(keys, types, sizes);
  1562. /* Now update our stats */
  1563. for(i=0;i<keys->elements;i++) {
  1564. if((type = types[i]) == TYPE_NONE)
  1565. continue;
  1566. totalsize[type] += sizes[i];
  1567. counts[type]++;
  1568. totlen += keys->element[i]->len;
  1569. sampled++;
  1570. if(biggest[type]<sizes[i]) {
  1571. printf(
  1572. "[%05.2f%%] Biggest %-6s found so far '%s' with %llu %s\n",
  1573. pct, typename[type], keys->element[i]->str, sizes[i],
  1574. typeunit[type]);
  1575. /* Keep track of biggest key name for this type */
  1576. maxkeys[type] = sdscpy(maxkeys[type], keys->element[i]->str);
  1577. if(!maxkeys[type]) {
  1578. fprintf(stderr, "Failed to allocate memory for key!\n");
  1579. exit(1);
  1580. }
  1581. /* Keep track of the biggest size for this type */
  1582. biggest[type] = sizes[i];
  1583. }
  1584. /* Update overall progress */
  1585. if(sampled % 1000000 == 0) {
  1586. printf("[%05.2f%%] Sampled %llu keys so far\n", pct, sampled);
  1587. }
  1588. }
  1589. /* Sleep if we've been directed to do so */
  1590. if(sampled && (sampled %100) == 0 && config.interval) {
  1591. usleep(config.interval);
  1592. }
  1593. freeReplyObject(reply);
  1594. } while(it != 0);
  1595. if(types) zfree(types);
  1596. if(sizes) zfree(sizes);
  1597. /* We're done */
  1598. printf("\n-------- summary -------\n\n");
  1599. printf("Sampled %llu keys in the keyspace!\n", sampled);
  1600. printf("Total key length in bytes is %llu (avg len %.2f)\n\n",
  1601. totlen, totlen ? (double)totlen/sampled : 0);
  1602. /* Output the biggest keys we found, for types we did find */
  1603. for(i=0;i<TYPE_NONE;i++) {
  1604. if(sdslen(maxkeys[i])>0) {
  1605. printf("Biggest %6s found '%s' has %llu %s\n", typename[i], maxkeys[i],
  1606. biggest[i], typeunit[i]);
  1607. }
  1608. }
  1609. printf("\n");
  1610. for(i=0;i<TYPE_NONE;i++) {
  1611. printf("%llu %ss with %llu %s (%05.2f%% of keys, avg size %.2f)\n",
  1612. counts[i], typename[i], totalsize[i], typeunit[i],
  1613. sampled ? 100 * (double)counts[i]/sampled : 0,
  1614. counts[i] ? (double)totalsize[i]/counts[i] : 0);
  1615. }
  1616. /* Free sds strings containing max keys */
  1617. for(i=0;i<TYPE_NONE;i++) {
  1618. sdsfree(maxkeys[i]);
  1619. }
  1620. /* Success! */
  1621. exit(0);
  1622. }
  1623. /*------------------------------------------------------------------------------
  1624. * Stats mode
  1625. *--------------------------------------------------------------------------- */
  1626. /* Return the specified INFO field from the INFO command output "info".
  1627. * A new buffer is allocated for the result, that needs to be free'd.
  1628. * If the field is not found NULL is returned. */
  1629. static char *getInfoField(char *info, char *field) {
  1630. char *p = strstr(info,field);
  1631. char *n1, *n2;
  1632. char *result;
  1633. if (!p) return NULL;
  1634. p += strlen(field)+1;
  1635. n1 = strchr(p,'\r');
  1636. n2 = strchr(p,',');
  1637. if (n2 && n2 < n1) n1 = n2;
  1638. result = malloc(sizeof(char)*(n1-p)+1);
  1639. memcpy(result,p,(n1-p));
  1640. result[n1-p] = '\0';
  1641. return result;
  1642. }
  1643. /* Like the above function but automatically convert the result into
  1644. * a long. On error (missing field) LONG_MIN is returned. */
  1645. static long getLongInfoField(char *info, char *field) {
  1646. char *value = getInfoField(info,field);
  1647. long l;
  1648. if (!value) return LONG_MIN;
  1649. l = strtol(value,NULL,10);
  1650. free(value);
  1651. return l;
  1652. }
  1653. /* Convert number of bytes into a human readable string of the form:
  1654. * 100B, 2G, 100M, 4K, and so forth. */
  1655. void bytesToHuman(char *s, long long n) {
  1656. double d;
  1657. if (n < 0) {
  1658. *s = '-';
  1659. s++;
  1660. n = -n;
  1661. }
  1662. if (n < 1024) {
  1663. /* Bytes */
  1664. sprintf(s,"%lluB",n);
  1665. return;
  1666. } else if (n < (1024*1024)) {
  1667. d = (double)n/(1024);
  1668. sprintf(s,"%.2fK",d);
  1669. } else if (n < (1024LL*1024*1024)) {
  1670. d = (double)n/(1024*1024);
  1671. sprintf(s,"%.2fM",d);
  1672. } else if (n < (1024LL*1024*1024*1024)) {
  1673. d = (double)n/(1024LL*1024*1024);
  1674. sprintf(s,"%.2fG",d);
  1675. }
  1676. }
  1677. static void statMode(void) {
  1678. redisReply *reply;
  1679. long aux, requests = 0;
  1680. int i = 0;
  1681. while(1) {
  1682. char buf[64];
  1683. int j;
  1684. reply = reconnectingRedisCommand(context,"INFO");
  1685. if (reply->type == REDIS_REPLY_ERROR) {
  1686. printf("ERROR: %s\n", reply->str);
  1687. exit(1);
  1688. }
  1689. if ((i++ % 20) == 0) {
  1690. printf(
  1691. "------- data ------ --------------------- load -------------------- - child -\n"
  1692. "keys mem clients blocked requests connections \n");
  1693. }
  1694. /* Keys */
  1695. aux = 0;
  1696. for (j = 0; j < 20; j++) {
  1697. long k;
  1698. sprintf(buf,"db%d:keys",j);
  1699. k = getLongInfoField(reply->str,buf);
  1700. if (k == LONG_MIN) continue;
  1701. aux += k;
  1702. }
  1703. sprintf(buf,"%ld",aux);
  1704. printf("%-11s",buf);
  1705. /* Used memory */
  1706. aux = getLongInfoField(reply->str,"used_memory");
  1707. bytesToHuman(buf,aux);
  1708. printf("%-8s",buf);
  1709. /* Clients */
  1710. aux = getLongInfoField(reply->str,"connected_clients");
  1711. sprintf(buf,"%ld",aux);
  1712. printf(" %-8s",buf);
  1713. /* Blocked (BLPOPPING) Clients */
  1714. aux = getLongInfoField(reply->str,"blocked_clients");
  1715. sprintf(buf,"%ld",aux);
  1716. printf("%-8s",buf);
  1717. /* Requets */
  1718. aux = getLongInfoField(reply->str,"total_commands_processed");
  1719. sprintf(buf,"%ld (+%ld)",aux,requests == 0 ? 0 : aux-requests);
  1720. printf("%-19s",buf);
  1721. requests = aux;
  1722. /* Connections */
  1723. aux = getLongInfoField(reply->str,"total_connections_received");
  1724. sprintf(buf,"%ld",aux);
  1725. printf(" %-12s",buf);
  1726. /* Children */
  1727. aux = getLongInfoField(reply->str,"bgsave_in_progress");
  1728. aux |= getLongInfoField(reply->str,"aof_rewrite_in_progress") << 1;
  1729. aux |= getLongInfoField(reply->str,"loading") << 2;
  1730. switch(aux) {
  1731. case 0: break;
  1732. case 1:
  1733. printf("SAVE");
  1734. break;
  1735. case 2:
  1736. printf("AOF");
  1737. break;
  1738. case 3:
  1739. printf("SAVE+AOF");
  1740. break;
  1741. case 4:
  1742. printf("LOAD");
  1743. break;
  1744. }
  1745. printf("\n");
  1746. freeReplyObject(reply);
  1747. usleep(config.interval);
  1748. }
  1749. }
  1750. /*------------------------------------------------------------------------------
  1751. * Scan mode
  1752. *--------------------------------------------------------------------------- */
  1753. static void scanMode(void) {
  1754. redisReply *reply;
  1755. unsigned long long cur = 0;
  1756. do {
  1757. if (config.pattern)
  1758. reply = redisCommand(context,"SCAN %llu MATCH %s",
  1759. cur,config.pattern);
  1760. else
  1761. reply = redisCommand(context,"SCAN %llu",cur);
  1762. if (reply == NULL) {
  1763. printf("I/O error\n");
  1764. exit(1);
  1765. } else if (reply->type == REDIS_REPLY_ERROR) {
  1766. printf("ERROR: %s\n", reply->str);
  1767. exit(1);
  1768. } else {
  1769. unsigned int j;
  1770. cur = strtoull(reply->element[0]->str,NULL,10);
  1771. for (j = 0; j < reply->element[1]->elements; j++)
  1772. printf("%s\n", reply->element[1]->element[j]->str);
  1773. }
  1774. freeReplyObject(reply);
  1775. } while(cur != 0);
  1776. exit(0);
  1777. }
  1778. /*------------------------------------------------------------------------------
  1779. * LRU test mode
  1780. *--------------------------------------------------------------------------- */
  1781. /* Return an integer from min to max (both inclusive) using a power-law
  1782. * distribution, depending on the value of alpha: the greater the alpha
  1783. * the more bias towards lower values.
  1784. *
  1785. * With alpha = 6.2 the output follows the 80-20 rule where 20% of
  1786. * the returned numbers will account for 80% of the frequency. */
  1787. long long powerLawRand(long long min, long long max, double alpha) {
  1788. double pl, r;
  1789. max += 1;
  1790. r = ((double)rand()) / RAND_MAX;
  1791. pl = pow(
  1792. ((pow(max,alpha+1) - pow(min,alpha+1))*r + pow(min,alpha+1)),
  1793. (1.0/(alpha+1)));
  1794. return (max-1-(long long)pl)+min;
  1795. }
  1796. /* Generates a key name among a set of lru_test_sample_size keys, using
  1797. * an 80-20 distribution. */
  1798. void LRUTestGenKey(char *buf, size_t buflen) {
  1799. snprintf(buf, buflen, "lru:%lld\n",
  1800. powerLawRand(1, config.lru_test_sample_size, 6.2));
  1801. }
  1802. #define LRU_CYCLE_PERIOD 1000 /* 1000 milliseconds. */
  1803. #define LRU_CYCLE_PIPELINE_SIZE 250
  1804. static void LRUTestMode(void) {
  1805. redisReply *reply;
  1806. char key[128];
  1807. long long start_cycle;
  1808. int j;
  1809. srand(time(NULL)^getpid());
  1810. while(1) {
  1811. /* Perform cycles of 1 second with 50% writes and 50% reads.
  1812. * We use pipelining batching writes / reads N times per cycle in order
  1813. * to fill the target instance easily. */
  1814. start_cycle = mstime();
  1815. long long hits = 0, misses = 0;
  1816. while(mstime() - start_cycle < 1000) {
  1817. /* Write cycle. */
  1818. for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {
  1819. LRUTestGenKey(key,sizeof(key));
  1820. redisAppendCommand(context, "SET %s val",key);
  1821. }
  1822. for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++)
  1823. redisGetReply(context, (void**)&reply);
  1824. /* Read cycle. */
  1825. for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {
  1826. LRUTestGenKey(key,sizeof(key));
  1827. redisAppendCommand(context, "GET %s",key);
  1828. }
  1829. for (j = 0; j < LRU_CYCLE_PIPELINE_SIZE; j++) {
  1830. if (redisGetReply(context, (void**)&reply) == REDIS_OK) {
  1831. switch(reply->type) {
  1832. case REDIS_REPLY_ERROR:
  1833. printf("%s\n", reply->str);
  1834. break;
  1835. case REDIS_REPLY_NIL:
  1836. misses++;
  1837. break;
  1838. default:
  1839. hits++;
  1840. break;
  1841. }
  1842. }
  1843. }
  1844. if (context->err) {
  1845. fprintf(stderr,"I/O error during LRU test\n");
  1846. exit(1);
  1847. }
  1848. }
  1849. /* Print stats. */
  1850. printf(
  1851. "%lld Gets/sec | Hits: %lld (%.2f%%) | Misses: %lld (%.2f%%)\n",
  1852. hits+misses,
  1853. hits, (double)hits/(hits+misses)*100,
  1854. misses, (double)misses/(hits+misses)*100);
  1855. }
  1856. exit(0);
  1857. }
  1858. /*------------------------------------------------------------------------------
  1859. * Intrisic latency mode.
  1860. *
  1861. * Measure max latency of a running process that does not result from
  1862. * syscalls. Basically this software should provide an hint about how much
  1863. * time the kernel leaves the process without a chance to run.
  1864. *--------------------------------------------------------------------------- */
  1865. /* This is just some computation the compiler can't optimize out.
  1866. * Should run in less than 100-200 microseconds even using very
  1867. * slow hardware. Runs in less than 10 microseconds in modern HW. */
  1868. unsigned long compute_something_fast(void) {
  1869. unsigned char s[256], i, j, t;
  1870. int count = 1000, k;
  1871. unsigned long output = 0;
  1872. for (k = 0; k < 256; k++) s[k] = k;
  1873. i = 0;
  1874. j = 0;
  1875. while(count--) {
  1876. i++;
  1877. j = j + s[i];
  1878. t = s[i];
  1879. s[i] = s[j];
  1880. s[j] = t;
  1881. output += s[(s[i]+s[j])&255];
  1882. }
  1883. return output;
  1884. }
  1885. static void intrinsicLatencyModeStop(int s) {
  1886. REDIS_NOTUSED(s);
  1887. force_cancel_loop = 1;
  1888. }
  1889. static void intrinsicLatencyMode(void) {
  1890. long long test_end, run_time, max_latency = 0, runs = 0;
  1891. run_time = config.intrinsic_latency_duration*1000000;
  1892. test_end = ustime() + run_time;
  1893. signal(SIGINT, intrinsicLatencyModeStop);
  1894. while(1) {
  1895. long long start, end, latency;
  1896. start = ustime();
  1897. compute_something_fast();
  1898. end = ustime();
  1899. latency = end-start;
  1900. runs++;
  1901. if (latency <= 0) continue;
  1902. /* Reporting */
  1903. if (latency > max_latency) {
  1904. max_latency = latency;
  1905. printf("Max latency so far: %lld microseconds.\n", max_latency);
  1906. }
  1907. double avg_us = (double)run_time/runs;
  1908. double avg_ns = avg_us * 10e3;
  1909. if (force_cancel_loop || end > test_end) {
  1910. printf("\n%lld total runs "
  1911. "(avg latency: "
  1912. "%.4f microseconds / %.2f nanoseconds per run).\n",
  1913. runs, avg_us, avg_ns);
  1914. printf("Worst run took %.0fx longer than the average latency.\n",
  1915. max_latency / avg_us);
  1916. exit(0);
  1917. }
  1918. }
  1919. }
  1920. /*------------------------------------------------------------------------------
  1921. * Program main()
  1922. *--------------------------------------------------------------------------- */
  1923. int main(int argc, char **argv) {
  1924. int firstarg;
  1925. config.hostip = sdsnew("127.0.0.1");
  1926. config.hostport = 6379;
  1927. config.hostsocket = NULL;
  1928. config.repeat = 1;
  1929. config.interval = 0;
  1930. config.dbnum = 0;
  1931. config.interactive = 0;
  1932. config.shutdown = 0;
  1933. config.monitor_mode = 0;
  1934. config.pubsub_mode = 0;
  1935. config.latency_mode = 0;
  1936. config.latency_dist_mode = 0;
  1937. config.latency_history = 0;
  1938. config.lru_test_mode = 0;
  1939. config.lru_test_sample_size = 0;
  1940. config.cluster_mode = 0;
  1941. config.slave_mode = 0;
  1942. config.getrdb_mode = 0;
  1943. config.stat_mode = 0;
  1944. config.scan_mode = 0;
  1945. config.intrinsic_latency_mode = 0;
  1946. config.pattern = NULL;
  1947. config.rdb_filename = NULL;
  1948. config.pipe_mode = 0;
  1949. config.pipe_timeout = REDIS_CLI_DEFAULT_PIPE_TIMEOUT;
  1950. config.bigkeys = 0;
  1951. config.stdinarg = 0;
  1952. config.auth = NULL;
  1953. config.eval = NULL;
  1954. config.last_cmd_type = -1;
  1955. spectrum_palette = spectrum_palette_color;
  1956. spectrum_palette_size = spectrum_palette_color_size;
  1957. if (!isatty(fileno(stdout)) && (getenv("FAKETTY") == NULL))
  1958. config.output = OUTPUT_RAW;
  1959. else
  1960. config.output = OUTPUT_STANDARD;
  1961. config.mb_delim = sdsnew("\n");
  1962. cliInitHelp();
  1963. firstarg = parseOptions(argc,argv);
  1964. argc -= firstarg;
  1965. argv += firstarg;
  1966. /* Latency mode */
  1967. if (config.latency_mode) {
  1968. if (cliConnect(0) == REDIS_ERR) exit(1);
  1969. latencyMode();
  1970. }
  1971. /* Latency distribution mode */
  1972. if (config.latency_dist_mode) {
  1973. if (cliConnect(0) == REDIS_ERR) exit(1);
  1974. latencyDistMode();
  1975. }
  1976. /* Slave mode */
  1977. if (config.slave_mode) {
  1978. if (cliConnect(0) == REDIS_ERR) exit(1);
  1979. slaveMode();
  1980. }
  1981. /* Get RDB mode. */
  1982. if (config.getrdb_mode) {
  1983. if (cliConnect(0) == REDIS_ERR) exit(1);
  1984. getRDB();
  1985. }
  1986. /* Pipe mode */
  1987. if (config.pipe_mode) {
  1988. if (cliConnect(0) == REDIS_ERR) exit(1);
  1989. pipeMode();
  1990. }
  1991. /* Find big keys */
  1992. if (config.bigkeys) {
  1993. if (cliConnect(0) == REDIS_ERR) exit(1);
  1994. findBigKeys();
  1995. }
  1996. /* Stat mode */
  1997. if (config.stat_mode) {
  1998. if (cliConnect(0) == REDIS_ERR) exit(1);
  1999. if (config.interval == 0) config.interval = 1000000;
  2000. statMode();
  2001. }
  2002. /* Scan mode */
  2003. if (config.scan_mode) {
  2004. if (cliConnect(0) == REDIS_ERR) exit(1);
  2005. scanMode();
  2006. }
  2007. /* LRU test mode */
  2008. if (config.lru_test_mode) {
  2009. if (cliConnect(0) == REDIS_ERR) exit(1);
  2010. LRUTestMode();
  2011. }
  2012. /* Intrinsic latency mode */
  2013. if (config.intrinsic_latency_mode) intrinsicLatencyMode();
  2014. /* Start interactive mode when no command is provided */
  2015. if (argc == 0 && !config.eval) {
  2016. /* Ignore SIGPIPE in interactive mode to force a reconnect */
  2017. signal(SIGPIPE, SIG_IGN);
  2018. /* Note that in repl mode we don't abort on connection error.
  2019. * A new attempt will be performed for every command send. */
  2020. cliConnect(0);
  2021. repl();
  2022. }
  2023. /* Otherwise, we have some arguments to execute */
  2024. if (cliConnect(0) != REDIS_OK) exit(1);
  2025. if (config.eval) {
  2026. return evalMode(argc,argv);
  2027. } else {
  2028. return noninteractive(argc,convertToSds(argc,argv));
  2029. }
  2030. }