Redis Source Analysis (33)---REDIS-CLI.C implementation of client command line interface (2)

Source: Internet
Author: User
Tags auth configuration settings eval redis sprintf stdin time interval server port

Today after learning the end of the command line client, the overall feeling is around 2 things turn, config and mode. Why do I say that, please continue to look down, the configuration structure in the client and the configuration structure that we have learned before, not the same concept, the structure in the CLI except the basic Ip,port port number, and the configuration of various mode.

/* REDIS Configuration Structure Body *
/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 Cluster_reissue_command;
    int slave_mode;
    int pipe_mode;
    int pipe_timeout;
    int getrdb_mode;
    int stat_mode;
    int scan_mode;
    int intrinsic_latency_mode;
    int intrinsic_latency_duration;
    char *pattern;
    char *rdb_filename;
    int Bigkeys;
    int stdinarg; /* Get last arg from stdin. (x option) */
    char *auth;
    int output; /* Output mode, output_* defines * * *
    SDS Mb_delim;
    Char prompt[128];
    char *eval;
    int last_cmd_type;
} Config
There are 10 mode modes in it. Let's go backwards and look at the process that the main program of the CLI runs, which is the execution step of the main function:

/*main function Main program operation */int main (int argc, char **argv) {int firstarg;
    First initialize the client configuration action Config.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_cmd_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) According to the user input parameters;
    ARGC-= Firstarg;

	argv + = Firstarg; Configuration settings are complete, depending on the mode settings in the configuration, call the corresponding mode method/* Latency mode/if (Config.latency_mode) {if (cliconnect (0) = = Redis_e
        RR) exit (1);
    Latencymode ();
        }/* Slave mode */if (Config.slave_mode) {if (cliconnect (0) = = redis_err) exit (1);
    Slavemode ();
 }
	....
The following code is the same as this, so it is omitted, the simple step is to set the configuration, according to the configuration to start the corresponding mode, the following said, the main models inside

1.statMode:

/* Statmode main output Some of the information read 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++%) = 0) {printf ("-------data---------------------------Load--------------------
        -Child-\n "" Keys MEM clients blocked requests connections);
        }/* Keys */aux = 0;

            for (j = 0; J <; J + +) {long k;
            sprintf (buf, "Db%d:keys", j);
            K = Getlonginfofield (REPLY-&GT;STR,BUF);
            if (k = = long_min) continue;
        Aux = k;
        } sprintf (buf, "%ld", aux);

        printf ("%-11s", buf);
        /* Used Memory * * aux = Getlonginfofield (reply->str, "used_memory"); ByteStohuman (Buf,aux);

        printf ("%-8s", buf);
        /* Clients * * aux = Getlonginfofield (reply->str, "connected_clients");
        sprintf (buf, "%ld", aux);

        printf ("%-8s", buf);
        /* Blocked (blpopping) Clients * * aux = Getlonginfofield (reply->str, "blocked_clients");
        sprintf (buf, "%ld", aux);
		printf ("%-8s", buf); ....
The current data statistics for the client.

The method used in 2.latencyMode to compute the performance of the test hardware:

/* This is just some computation the compiler can ' t optimize out.
 * Should run in less than 100-200 microseconds even using very
 * slow hardware. Runs in less than microseconds in modern HW. * *
* Common calculation operation, testing the speed of hardware calculation
/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 for the help command is output from the following function:

/* 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 the command, there will be 2 concepts, 1 General Command commands and one is the concept of the Commandgroup command group, for example, such as list,set and other frequently used commands, the following can be followed by a variety of parameters of the command, Attribute Command Group command, General config get, this very single command we'll call him a regular order, dump,exist. These commands are ordinary orders, commandgroup commands are not a lot on the following few:

/* All command groups */
static char *commandgroups[] = {
    "generic",
    "string",
    "list",
    "set",
    "sorted_ Set ",
    " hash ",
    " PubSub ",
    " transactions ",
    " Connection ",
    " Server ",
    " scripting ",
    " Hyperloglog "
};
It is also the most commonly used command in Redis systems.

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.