Redis Source Code Analysis (33) --- implementation of redis-cli.c client command line interface (2)

Source: Internet
Author: User
Tags server port

After finishing the subsequent content of the command line client today, the overall feeling is that there are two things to convert, config and mode. Why do I say this? Let's continue to look at it. The configuration struct in the client and the configuration struct we learned earlier do not refer to the same concept. The struct in the cli except the basic ip address, port number, and various mode configurations.

/* Redis configuration struct */static struct config {char * hostip; int hostport; char * hostsocket; long repeat; long interval; int dbnum; int interactive; int shutdown; int monitor_mode; int pubsub_mode; int latency_mode; int latency_history; int cluster_mode; int short; int slave_mode; int pipe_mode; int pipe_timeout; int getrdb_mode; int stat_mode; int scan_mode; int short; char * pattern; char * rdb_filename; int bigkeys; int stdinarg;/* get last arg from stdin. (-x option) */char * auth; int output;/* output mode, see OUTPUT _ * defines */sds mb_delim; char prompt [128]; char * eval; int last_cmd_type;} config;
There are also 10 modes. Let's look at the main program running process of cli, that is, the execution steps of the main function:

/* Main function main program operation */int main (int argc, char ** argv) {int firstarg; // initialize the client configuration operation config first. hostip = sdsnew ("127.0.0.1"); config. hostport = 6379; config. hostsocket = NULL; config. repeat = 1; config. interval = 0; config. dbnum = 0; config. interactive = 0; config. shutdown = 0; config. monitor_mode = 0; config. pubsub_mode = 0; config. latency_mode = 0; config. latency_history = 0; config. cluster_mode = 0; config. Slave_mode = 0; config. getrdb_mode = 0; config. stat_mode = 0; config. scan_mode = 0; config. intrinsic_latency_mode = 0; config. pattern = NULL; config. rdb_filename = NULL; config. pipe_mode = 0; config. pipe_timeout = REDIS_CLI_DEFAULT_PIPE_TIMEOUT; config. bigkeys = 0; config. stdinarg = 0; config. auth = NULL; config. eval = NULL; config. last_1__type =-1; if (! Isatty (fileno (stdout) & (getenv ("FAKETTY") = NULL) config. output = OUTPUT_RAW; else config. output = OUTPUT_STANDARD; config. mb_delim = sdsnew ("\ n"); cliInitHelp (); // Configure config firstarg = parseOptions (argc, argv) based on user input parameters; argc-= firstarg; argv + = firstarg; // after the configuration is complete, call the corresponding mode method/* Latency mode */if (config. latency_mode) {if (cliConnect (0) = REDIS_ERR) exit (1); latencyMode () ;}/ * Slave mode */if (config. slave_mode) {if (cliConnect (0) = REDIS_ERR) exit (1); slaveMode ();}....
The code below is the same, so it is omitted. In simple terms, it is to set the configuration and start the corresponding mode according to the configuration. The following describes the main modes in it.

1. statMode:

/* StatMode mainly outputs some information for reading data Statistics */static void statMode (void) {redisReply * reply; long aux, requests = 0; int I = 0; while (1) {char buf [64]; int j; reply = reconnectingInfo (); if (reply-> type = REDIS_REPLY_ERROR) {printf ("ERROR: % s \ n ", reply-> str); exit (1) ;}if (I ++ % 20) = 0) {printf ("------- data ------ --------------------- load ---------------------child-\ n" "keys mem clients blocked requests connections \ n");}/* Keys */aux = 0; for (j = 0; j <20; j ++) {long k; sprintf (buf, "db % d: keys", j ); k = getLongInfoField (reply-> str, buf); if (k = LONG_MIN) continue; aux + = k;} sprintf (buf, "% ld", aux ); printf ("%-11 s", buf);/* Used memory */aux = getLongInfoField (reply-> str, "used_memory"); bytesToHuman (buf, aux ); printf ("%-8 s", buf);/* Clients */aux = getLongInfoField (reply-> str, "connected_clients"); sprintf (buf, "% ld ", aux); printf ("%-8 s", buf);/* Blocked (BLPOPPING) Clients */aux = getLongInfoField (reply-> str, "blocked_clients "); sprintf (buf, "% ld", aux); printf ("%-8 s", buf );....
The current statistics of the client.

2. How to test the hardware computing performance in latencyMode:

/* This is just some computation the compiler can't optimize out. * shocould run in less than 100-200 microseconds even using very * slow hardware. runs in less than 10 microseconds in modern HW. * // * common computing operations to test the speed of hardware computing */unsigned long compute_something_fast (void) {unsigned char s [256], I, j, t; int count = 1000, k; unsigned long output = 0; for (k = 0; k <256; k ++) s [k] = k; I = 0; j = 0; while (count --) {I ++; j = j + s [I]; t = s [I]; s [I] = s [j]; s [j] = t; output + = s [(s [I] + s [j]) & 255];} return output ;}
The output document of help commands is output by the following functions:

/* HELP command output document */static void usage (void) {sds version = cliVersion (); fprintf (stderr, "redis-cli % s \ n" "\ n" "Usage: redis-cli [OPTIONS] [cmd [arg [arg...] \ n ""-h In a Command, two concepts are involved. One is a general Command and the other is a CommandGroup Command group. For example, list, set and other frequently used commands, which can be followed by multiple parameter commands, attribute command group commands, generally config get, such a single command is called a common command, Dump, Exist, and so on. There are not many CommandGroup commands as follows:

/* All command groups */static char * commandGroups [] = {"generic", "string", "list", "set", "sorted_set", "hash ", "pubsub", "transactions", "connection", "server", "scripting", "hyperloglog "};
It is also the most common command in the redis system.

Redis Source Code Analysis (33) --- implementation of redis-cli.c client command line interface (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.